#49915 closed defect (bug) (invalid)
Function registered on `upgrader_process_complete` does not run on first update.
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | Upgrade/Install | Keywords: | |
Focuses: | Cc: |
Description
Adding the following line to a plugin will not run on first plugin update:
add_action( 'upgrader_process_complete', 'my_upgrade_routine', 10, 2 );
However on subsequent plugin upgrades it will run.
ie. my_upgrade_routine()
will only run if it had already been registered on a previous plugin update.
It will not run if it was added in current update.
I'm assuming this also happens if the function name is changed. I've not tested this.
I have a custom plugin that updates from a github repo using a third-party plugin updater (that is reliable and stable).
I am testing this by manually running wp_version_check
from WP-Crontrol plugin page.
Change History (5)
#3
in reply to:
↑ 2
@
3 years ago
Replying to apedog:
@pbiron
You said in slack that you might take a look at this. Did you get a chance?
No, I haven't had the time. Will try to in the next few days.
#4
@
3 years ago
- Resolution set to invalid
- Status changed from new to closed
After discussion on slack, it's been made clear that this is expected behaviour.
- The
upgrader_process_complete
is not meant to be used to run plugin update routines. Only cleanup/logging/setting transients etc. - No code from the updated plugin can be run during the upgrade process. Only the old version of the plugin is available during the request.
Follow-up:
I ran further tests on this. Basically what happens is the plugin runs the old pre-upgrade function and not the post-upgrade version of the function. Strangely enough, if
get_plugin_data()
is called from within the upgrade function it will reference the new post-upgrade header.So if the old plugin had this in version 1.0:
add_action( 'upgrader_process_complete', 'my_upgrade_routine', 10, 2 ); function my_upgrade_routine( $upgrader_object, $options ){ if( $options['action'] == 'update' && $options['type'] == 'plugin' && ( ( isset( $options['plugins'] ) && in_array( plugin_basename( __FILE__ ), $options['plugins']) ) || ( isset( $options['plugin'] ) && plugin_basename( __FILE__ ) == $options['plugin'] ) ) ){ $plugin_data = get_plugin_data( __FILE__, false); $header_version = $plugin_data['Version']; update_option( 'my_plugin_version_header', $header_version ); update_option( 'my_plugin_version_hardcoded', '1.0' ); } }
And the upgraded plugin has this in version 1.1:
$plugin_data = get_plugin_data( __FILE__, false); $header_version = $plugin_data['Version']; update_option( 'my_plugin_version_header', $header_version ); update_option( 'my_plugin_version_hardcoded', '1.1' );
After upgrading to 1.1,
get_option('my_plugin_version_header')
will return1.1
butget_option('my_plugin_version_hardcoded')
will return1.0
@pbiron
You said in slack that you might take a look at this. Did you get a chance?