Skip to content

Commit

Permalink
~ Added filtering of JS scripts registered by the legacy Custom JS pl…
Browse files Browse the repository at this point in the history
…ugin if they exist.
  • Loading branch information
veryspry committed Feb 2, 2024
1 parent 7b6d176 commit fa1f8d6
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion class-gwiz-gf-custom-code.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ public function init() {

add_filter( 'gform_register_init_scripts', array( $this, 'register_init_scripts' ), 99, 1 );
add_filter( 'gform_register_init_scripts', array( $this, 'maybe_register_custom_js_scripts_first' ), 100, 1 );
add_filter( 'gform_register_init_scripts', array( $this, 'maybe_remove_legacy_custom_js_scripts' ), 100, 1 );
add_filter( 'gform_get_form_filter', array( $this, 'add_custom_css' ), 10, 2 );
}

Expand Down Expand Up @@ -341,6 +342,33 @@ public function maybe_register_custom_js_scripts_first( $form ) {
GFFormDisplay::$init_scripts[ $form['id'] ] = $filtered;
}

public function maybe_remove_legacy_custom_js_scripts( $form ) {
/**
* Whether or not to remove legacy Custom JS plugin scripts from the
* registration list.
*
* @param bool $should_remove_legacy_custom_js_scripts Whether to remove legacy Custom JS plugin scripts from the registration list.
* @param array $form The form object.
*/
if ( ! apply_filters( 'should_remove_legacy_custom_js_scripts', true, $form ) ) {
return;
}

$scripts = rgar( GFFormDisplay::$init_scripts, $form['id'] );
if ( empty( $scripts ) ) {
return;
}

$filtered = array();
foreach ( $scripts as $slug => $script ) {
if ( strpos( $slug, 'gf_custom_js' ) === false ) {
$filtered[ $slug ] = $script;
}
}

GFFormDisplay::$init_scripts[ $form['id'] ] = $filtered;
}

public function add_custom_css( $form_string, $form ) {
$custom_css = $this->get_custom_css( $form );
$custom_css = html_entity_decode( $custom_css );
Expand Down Expand Up @@ -575,7 +603,7 @@ public function maybe_display_custom_js_warning() {
echo '<div class="notice notice-warning is-dismissible">';
/* translators: %s: <b> opening HTML tag, %s </b> closing HTML tag */
echo '<p>' . __( sprintf(

Check failure on line 605 in class-gwiz-gf-custom-code.php

View workflow job for this annotation

GitHub Actions / PHPCS

The $text arg must be a single string literal, not "sprintf( 'Warning: %sGravity Forms Custom Javascript%s is currently active.', '<b>', '</b>' )".
'Warning: %sGravity Forms Custom Javascript%s is currently active.', '<b>', '</b>' ),
'Warning: %sGravity Forms Custom Javascript%s is currently active.', '<b>', '</b>' ),

Check failure on line 606 in class-gwiz-gf-custom-code.php

View workflow job for this annotation

GitHub Actions / PHPCS

Multi-line function call not indented correctly; expected 16 spaces but found 12
'gf-custom-code'
) . '</p>';
echo '<p>' . __(
Expand Down

0 comments on commit fa1f8d6

Please sign in to comment.