Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement DJE Safe Mode Feature #7058

Open
Miraeld opened this issue Oct 25, 2024 · 3 comments · May be fixed by #7081
Open

Implement DJE Safe Mode Feature #7058

Miraeld opened this issue Oct 25, 2024 · 3 comments · May be fixed by #7081
Assignees
Labels
effort: [M] 3-5 days of estimated development time epics 🔥 For large tasks or features, broken into smaller issues.
Milestone

Comments

@Miraeld
Copy link
Contributor

Miraeld commented Oct 25, 2024

  • Frontend Changes:

Remove the placeholder from the textarea.
Add a checkbox labeled "Delay JavaScript Execution safe mode" before the textarea.
Display a warning message when the checkbox is checked, detailing potential performance impacts and suggesting users contact support.

  • Backend Changes:

Implement logic to handle the checkbox state.
Update the exclusions list based on the checkbox state.
Ensure the system correctly applies these changes when the settings are saved.

@Miraeld Miraeld added this to the 3.18 milestone Oct 25, 2024
@Miraeld Miraeld added the epics 🔥 For large tasks or features, broken into smaller issues. label Oct 25, 2024
@Miraeld Miraeld self-assigned this Oct 25, 2024
@Miraeld
Copy link
Contributor Author

Miraeld commented Oct 25, 2024

Scope a solution

Frontend changes

  • Remove the placeholder from the textarea and text under it.
    Delete these lines:

    $delay_js_list_helper = esc_html__( 'If you have problems after activating this option, copy and paste the default exclusions to quickly resolve issues:', 'rocket' );
    $delay_js_list_helper .= sprintf( '<br><pre><code>%1$s</code></pre><br>', implode( '<br>', DelayJSSettings::get_delay_js_default_exclusions() ) );
    and empty the placeholder here:
    'placeholder' => '/wp-includes/js/jquery/jquery.min.js',

  • Add a checkbox labeled "Delay JavaScript Execution safe mode" before the textarea.
    Add this after delay_js_exclusions_selected item here

Click to expand code
'delay_js_execution_safe_mode' => [  
  'type' => 'checkbox',  
    'label' => __( 'Delay JavaScript Execution safe mode', 'rocket' ),  
    'container_class' => [  
  'wpr-field--children',  
       'wpr-isParent',  
    ],  
    'description' => $invalid_license ? __( 'Delay JavaScript Execution safe mode temporarily resolves issues with Delay JavaScript execution but may reduce your PageSpeed Scores and performance. Contact support for help excluding problematic scripts to use this feature fully.', 'rocket' ) : '',  
    'section' => 'js',  
    'parent' => 'delay_js',  
    'page' => 'file_optimization',  
    'default' => 0,  
    'sanitize_callback' => 'sanitize_checkbox',  
    'input_attr' => [  
  'disabled' => get_rocket_option( 'delay_js_execution_safe_mode' ) ? 1 : 0,  
    ],  
    'helper' => '',  
],  
  
'delay_js_execution_safe_mode_method' => [  
  'type' => 'radio_buttons',  
    'label' => __( 'Delay JavaScript Execution safe mode', 'rocket' ),  
    'container_class' => [  
  'wpr-field--children',  
       'wpr-field--js-execution-safe-mode-delivery',  
       'wpr-field--optimize-css-delivery',  
    ],  
    'buttons_container_class' => '',  
    'parent' => 'delay_js_execution_safe_mode',  
    'section' => 'js',  
    'page' => 'file_optimization',  
    'default' => 'dje_safe_mode',  
    'sanitize_callback' => 'sanitize_checkbox',  
    'options' => [  
  'dje_safe_mode' => [  
  'label' => __( 'Delay JavaScript Execution safe mode', 'rocket' ),  
          'disabled' => $invalid_license ? 'disabled' : false,  
          'description' => '',  
          // translators: %1$s = opening <a> tag, %2$s = closing </a> tag.  
  'warning' => $invalid_license ? [] : [  
  'title' => __( 'This will decrease the effect of Delay JavaScript Execution', 'rocket' ),  
             'description' => __( 'This mode temporarily resolves issues with Delay JavaScript execution but may reduce your PageSpeed Scores and performance. Contact support for help excluding problematic scripts to use this feature fully.', 'rocket' ),  
             'button_label' => __( 'Activate Safe Mode', 'rocket' ),  
          ],  
          'sub_fields' => $invalid_license ? [] : [  
  'delay_js_exclusions' => [  
  'type' => 'textarea',  
                'label' => __( 'Excluded JavaScript Files', 'rocket' ),  
                'description' => __( 'Specify URLs or keywords that can identify inline or JavaScript files to be excluded from delaying execution (one per line).', 'rocket' ),  
                'parent' => '',  
                'section' => 'js',  
                'page' => 'file_optimization',  
                'default' => [],  
                'sanitize_callback' => 'sanitize_textarea',  
                'input_attr' => [  
  'disabled' => get_rocket_option( 'delay_js_execution_safe_mode' ) ? 0 : 1,  
                ],  
                'helper' => DelayJSSettings::exclusion_list_has_default() ? $delay_js_found_list_helper : $delay_js_list_helper,  
                'placeholder' => '',  
             ],  
          ],  
       ],  
    ],
  • Display a warning message when the checkbox is checked, detailing potential performance impacts and suggesting users contact support.

Probably need to modify wpr-admin.js to display the notice on click of the checkbox.

Backend changes

  • Step 1: Update the default exclusions list:

    $exclusions = [
    '\/jquery(-migrate)?-?([0-9.]+)?(.min|.slim|.slim.min)?.js(\?(.*))?( |\'|"|>)',
    'js-(before|after)',
    ];
    Here, add (?:/wp-content/|/wp-includes/)(.*) to the default list.

  • Step 2: Update the settings save logic
    Modify

    public function sanitize_options( $input, $settings ): array {
    $input['delay_js'] = $settings->sanitize_checkbox( $input, 'delay_js' );
    $input['delay_js_exclusions'] =
    ! empty( $input['delay_js_exclusions'] )
    ?
    rocket_sanitize_textarea_field( 'delay_js_exclusions', $input['delay_js_exclusions'] )
    :
    [];
    return $input;
    }
    to take care of the newly created checkbox (delay_js_safe_mode) by adding : $input['delay_js_safe_mode'] = $settings->sanitize_checkbox( $input, 'delay_js_safe_mode' );
    And delete default exclusion from the textarea if there are some.

  • On upgrade, we need to keep the exclusions if there are some existing, for this we'll need to take care of it here:

    public function set_option_on_update( $old_version ) {

  • When safe mode is enabled, add by default these regex to exclusions in this function

    public function delay_js( $html ): string {

		\/jquery(-migrate)?-?([0-9.]+)?(.min|.slim|.slim.min)?.js(\?(.*))?( |'|"|>)
		js-(before|after)
		(?:/wp-content/|/wp-includes/)(.*)

@mostafa-hisham
Copy link
Contributor

@Miraeld I think the JS function that shows the warning is here.
not sure if you need to edit the function but you can check

function wprShowRadioWarning($elm) {

@jeawhanlee
Copy link
Contributor

LGTM

@Miraeld Miraeld removed their assignment Oct 28, 2024
@Miraeld Miraeld added the effort: [M] 3-5 days of estimated development time label Oct 29, 2024
@Miraeld Miraeld assigned Miraeld and unassigned Miraeld Oct 29, 2024
@Miraeld Miraeld linked a pull request Oct 31, 2024 that will close this issue
8 tasks
@remyperona remyperona linked a pull request Nov 5, 2024 that will close this issue
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort: [M] 3-5 days of estimated development time epics 🔥 For large tasks or features, broken into smaller issues.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants