wp-media / wp-rocket

Performance optimization plugin for WordPress
https://wp-rocket.me
GNU General Public License v2.0
701 stars 218 forks source link

Calling update_rocket_option produces a fatal error if Cloudflare add-on connection fails: "add_settings_error is not defined" #6932

Open alfonso100 opened 2 months ago

alfonso100 commented 2 months ago

Before submitting an issue please check that you’ve completed the following steps: Yes - Made sure you’re on the latest version Yes - Used the search feature to ensure that the bug hasn’t been reported before

Describe the bug When calling rocket_update_option programmatically, if our Cloudflare addon is active, but the connection fails (for example if fields are empty) we produce a fatal error when calling WordPress core function add_settings_error because is not defined: https://github.com/wp-media/wp-rocket/blob/7999ed2618d11d7fa9b63fc78a7810789072d4b9/inc/Addon/Cloudflare/Subscriber.php#L632

The fatal error:

Fatal error: Uncaught Error: Call to undefined function WP_Rocket\Addon\Cloudflare\add_settings_error() in /wp-content/plugins/wp-rocket/inc/Addon/Cloudflare/Subscriber.php:633 Stack trace: #0 /wp-includes/class-wp-hook.php(326): WP_Rocket\Addon\Cloudflare\Subscriber->display_settings_notice(Array, Array) #1 /wp-includes/plugin.php(205): WP_Hook->apply_filters(Array, Array) #2 /wp-includes/option.php(881): apply_filters('pre_update_opti...', Array, Array, 'wp_rocket_setti...') #3 /wp-content/plugins/wp-rocket/inc/classes/admin/class-options.php(79): update_option('wp_rocket_setti...', Array) #4 /wp-content/plugins/wp-rocket/inc/functions/options.php(52): WP_Rocket\Admin\Options->set('settings', Array) #5 /clean_prod_cats.php(11): update_rocket_option('lazyload', 1) #6 {main} thrown in /wp-content/plugins/wp-rocket/inc/Addon/Cloudflare/Subscriber.php on line 633

To Reproduce Steps to reproduce the behavior:

  1. Enable the Cloudflare add-on, and leave the fields empty
  2. Create a PHP standalone snippet to update an option, for example:
    <?php 
    // Load WordPress.
    require( 'wp-load.php' );
    update_rocket_option( 'lazyload', 1 );
    echo "lazyload enabled";
  3. Trigger/visit the snippet
  4. See error

Expected behavior This scenario is kinda edgy, but we could check if add_settings_error is defined here to prevent the error:

        if ( is_wp_error( $connection ) ) {
            if(function_exists("add_settings_error")) {
                add_settings_error( 'general', 'cloudflare_api_key_invalid', __( 'WP Rocket: ', 'rocket' ) . '</strong>' . $connection->get_error_message() . '<strong>', 'error' );
            }
        } 

Additional context Ticket: https://secure.helpscout.net/conversation/2685627480/508599/ Slack Thread: https://wp-media.slack.com/archives/C08N8J6VC/p1724406001657579

Acceptance Criteria (for WP Media team use only) Clear instructions for developers, to be added before the grooming

alfonso100 commented 1 day ago

another case here: https://secure.helpscout.net/conversation/2753091866/521181?folderId=2683093