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

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

Open
alfonso100 opened this issue Aug 29, 2024 · 0 comments

Comments

@alfonso100
Copy link
Contributor

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:

add_settings_error( 'general', 'cloudflare_api_key_invalid', __( 'WP Rocket: ', 'rocket' ) . '</strong>' . $connection->get_error_message() . '<strong>', 'error' );

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";
  1. Trigger/visit the snippet
  2. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant