WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 7 years ago

#8899 closed defect (bug) (invalid)

has_action no reporting existing action hook

Reported by: firebird75 Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Plugins Keywords:
Focuses: Cc:

Description

I am trying to write a plugin which uses the has_action function. Unfortunately, it doesn't seem to be working under specific circunstances. It reports that the action isn't existing while it is thus allowing the action scheduling to be duplicated...

So he is what I am doing :
1/ First, I am registering and deregistering the plugin :
register_activation_hook(FILE, 'myplugin_activation');
register_deactivation_hook(FILE, 'myplugin_deactivation');

2/ Then we have both activation and deactivation functions :
function myplugin_activation()
{

if (!has_action('myplugin_monthly_cron_hook','myplugin_monthly_cron'))
{

add_action('myplugin_monthly_cron_hook','myplugin_monthly_cron');
wp_schedule_event(time(), 'hourly', 'myplugin_monthly_cron_hook');

}

}

function myplugin_deactivation()
{

wp_clear_scheduled_hook('myplugin_monthly_cron_hook');
remove_action('myplugin_monthly_cron_hook','myplugin_monthly_cron');

}

So in the activation function, I am registering a hook for a cron task and then I am scheduling it and in the deactivation function, I am removing it.

Here is the scenario of the problem :
1/ Activate the plugin => the action hook is created and the associated cron action as well;
2/ Modify the plugin file (either by uploading a new one or by modifying it within the inline editor) without deactivating the plugin;
3/ This triggers the register_activation_hook function again (as the plugin file has been modified);
4/ Wordpress goes through myplugin_activation() and the (!has_action('myplugin_monthly_cron_hook','myplugin_monthly_cron')) test is entered while the action hasn't been removed (since the plugin wasn't deactivated manually). This is where the bug is, I cannot figure out why the has_action is returning false here while the action is indeed there...

I understand that this scenario will happen only when people updates the files without deactivating the plugin first. This isn't the right way to upgrade plugins but this can happen and so cause duplicate scheduling action registrations...

Change History (1)

comment:1 in reply to: ↑ description @filosofo7 years ago

  • Milestone 2.8 deleted
  • Resolution set to invalid
  • Status changed from new to closed
  • Version 2.7 deleted

Replying to firebird75:

4/ Wordpress goes through myplugin_activation() and the (!has_action('myplugin_monthly_cron_hook','myplugin_monthly_cron')) test is entered while the action hasn't been removed (since the plugin wasn't deactivated manually). This is where the bug is, I cannot figure out why the has_action is returning false here while the action is indeed there...

I don't think you're understanding how the action hook API works. An action hook isn't something that can be registered just once when a plugin is activated (registered action hooks are not stored in the database). Rather, it must be done for every script load.

Note: See TracTickets for help on using tickets.