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

Fix fatal error for Post_Meta hook return type #39

Open
wants to merge 2 commits into
base: v2.0
Choose a base branch
from

Conversation

r-a-y
Copy link

@r-a-y r-a-y commented Oct 9, 2020

This PR switches the return type for Post_Meta\remove_schedule() and Post_Meta\update_schedule() from bool to void because those functions:

  • Are hooked to an action, not a filter; and
  • Returns Unpublish\unschedule_unpublish(), which can be an integer, not a bool:

* @return mixed On success an integer indicating number of events unscheduled
* (0 indicates no events were registered with the hook and
* arguments combination), false if unscheduling one or more events fail.
*/
function unschedule_unpublish( int $post_id ) {
return wp_clear_scheduled_hook( CRON_KEY, [ $post_id ] );
}

This can throw the following fatal error when deleting or updating a post that has an unpublish timestamp:

PHP Fatal error: Uncaught TypeError: Return value of HM\Unpublish\Post_Meta\remove_schedule() must be of the type bool, int returned in /wp-content/plugins/Unpublish/inc/post-meta.php:71

Stack trace:
#0 /wp-includes/class-wp-hook.php(289): HM\Unpublish\Post_Meta\remove_schedule(Array, 354, 'unpublish_times...')
#1 /wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array)
#2 /wp-includes/plugin.php(478): WP_Hook->do_action(Array)
#3 /wp-includes/meta.php(965): do_action('deleted_post_me...', Array, 354, 'unpublish_times...', '1602519250')
#4 /wp-includes/post.php(3066): delete_metadata_by_mid('post', 1046)
#5 /wp-admin/post.php(318): wp_delete_post(354, true)
#6 {main} thrown in /wp-content/plugins/Unpublish/inc/post-meta.php on line 71

This commit switches the return type from bool to void, since
remove_schedule() is hooked to an action, not a filter. Also,
Unpublish\unschedule_unpublish() can return an integer and not a bool,
which can throw a fatal error.
Same as remove_schedule() fix, but for update_schedule().
@r-a-y r-a-y changed the title Fix fatal error for Post_Meta\remove_schedule() return type Fix fatal error for Post_Meta hook return type Mar 9, 2021
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.

1 participant