WordPress.org

Make WordPress Core

Opened 3 months ago

Last modified 3 weeks ago

#49915 new defect (bug)

Function registered on `upgrader_process_complete` does not run on first update.

Reported by: apedog Owned by:
Milestone: Awaiting Review 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 (3)

#1 @SergeyBiryukov
3 months ago

  • Component changed from Cron API to Upgrade/Install

#2 follow-up: @apedog
3 weeks ago

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 return 1.1 but get_option('my_plugin_version_hardcoded') will return 1.0

@pbiron
You said in slack that you might take a look at this. Did you get a chance?

#3 in reply to: ↑ 2 @pbiron
3 weeks 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.

Note: See TracTickets for help on using tickets.