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

Use _doing_it_wrong in initialization check #977

Merged
merged 1 commit into from
Sep 6, 2023

Conversation

coreymckrill
Copy link
Contributor

Replaces the error_log call with WP Core's _doing_it_wrong function. The latter uses trigger_error, which is more versatile than error_log because its behavior can be customized with an error handler. _doing_it_wrong also only triggers an error if WP_DEBUG is set to true, so it won't add noise to logs in production environments.

To test

  1. In your testing environment, set WP_DEBUG to true. Also, either install and activate Query Monitor (for viewing PHP errors) or set WP_DEBUG_LOG to true.
  2. Add the following snippet to a file in your mu-plugins directory:
    add_action(
    	'plugins_loaded',
    	function() {
    		as_schedule_single_action( time() + HOUR_IN_SECONDS, 'not_a_real_hook' );
    	}
    );
    
  3. In WP Admin, navigate to Tools > Scheduled Actions. There should not be a scheduled event for not_a_real_hook, because as_schedule_single_action was called before Action Scheduler was fully initialized.
  4. Now check your PHP errors, either in the Query Monitor console or in your error log file. You should see the following error:

    Function as_schedule_single_action was called incorrectly. as_schedule_single_action() was called before the Action Scheduler data store was initialized Please see Debugging in WordPress for more information. (This message was added in version 3.1.6.)

  5. Now set WP_DEBUG to false and refresh the Scheduled Actions screen. You should still not see a scheduled event for not_a_real_hook. Additionally, though, there should not be a PHP error message this time, because debug has been disabled.

Replaces the `error_log` call with WP Core's `_doing_it_wrong`
function. The latter uses `trigger_error`, which is more versatile
than `error_log` because its behavior can be customized with an error
handler. `_doing_it_wrong` also only triggers an error if WP_DEBUG is
set to true, so it won't add noise to logs in production environments.
@coreymckrill coreymckrill self-assigned this Aug 31, 2023
@barryhughes barryhughes added this to the 3.6.3 milestone Sep 6, 2023
Copy link
Member

@barryhughes barryhughes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

  • Tests as per description.
  • Seems like a better way to surface this problem.
  • Was initially a little unsure if 3.1.6 was appropriate as the introduced version number, but on reflection I think it makes sense.

Thanks, @coreymckrill!

@barryhughes barryhughes merged commit 5b17c9b into trunk Sep 6, 2023
36 of 38 checks passed
@barryhughes barryhughes deleted the update/as-initialization-check branch September 6, 2023 04:35
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

Successfully merging this pull request may close these issues.

2 participants