YahnisElsts / plugin-update-checker

A custom update checker for WordPress plugins. Useful if you don't want to host your project in the official WP repository, but would still like it to support automatic updates. Despite the name, it also works with themes.
MIT License
2.22k stars 403 forks source link

Feature request: wp_unschedule_hook() #430

Closed StonehengeCreations closed 3 years ago

StonehengeCreations commented 3 years ago

May I suggest an additional line to the removeUpdaterCron() method in Schedular.php, please?

Currently if a user deactivates the theme or plugin, the cron job will be cleared using wp_clear_scheduled_hook(), but as the name already suggests, that only clears the actions assigned to that cron job - the cron job itself is still there. So WordPress is still executing a cron job (do_action), without running any methods.

Image of an empty cron job.

Could you add a line in the next update to, after clearing the scheduled hook, also remove the cron job, please? wp_unschedule_hook($this->cronHook);

Many thanks in advance, Patrick.

YahnisElsts commented 3 years ago

If I'm reading the source code of wp_clear_scheduled_hook() correctly, it calls wp_unschedule_event() which already removes cron jobs that don't have any associated hooks:

$crons = _get_cron_array();
$key   = md5( serialize( $args ) );
unset( $crons[ $timestamp ][ $hook ][ $key ] );
if ( empty( $crons[ $timestamp ][ $hook ] ) ) {
    unset( $crons[ $timestamp ][ $hook ] ); // <===
}
if ( empty( $crons[ $timestamp ] ) ) {
    unset( $crons[ $timestamp ] ); // <===
}

wp_unschedule_hook() has a very similar section:

unset( $crons[ $timestamp ][ $hook ] );

if ( empty( $crons[ $timestamp ] ) ) {
    unset( $crons[ $timestamp ] );
}

As far as I can tell, the job should be removed if wp_clear_scheduled_hook() is called. Maybe it didn't get called for some reason?

StonehengeCreations commented 3 years ago

Obviously you are right about the practically identical code. Maybe something is going wrong at my end then... I will do some more testing why my crons keep hanging around.