WordPress.org

Make WordPress Core

Ticket #2927: pre.post.upgrade-hooks.complete.patch

File pre.post.upgrade-hooks.complete.patch, 6.8 KB (added by darkdragon, 8 years ago)

FInishes the enhancement for pre upgrade and post upgrade hooks

  • wp-admin/includes/upgrade.php

     
    144144        if ( $wp_db_version == $wp_current_db_version ) 
    145145                return; 
    146146 
     147        $plugin_results = wp_call_pre_upgrade_hook(); 
    147148        wp_check_mysql_version(); 
    148149        wp_cache_flush(); 
    149150        make_db_current_silent(); 
    150151        upgrade_all(); 
    151152        wp_cache_flush(); 
     153        wp_call_post_upgrade_hook($plugin_results); 
    152154} 
    153155endif; 
    154156 
     
    12431245        } 
    12441246} 
    12451247 
     1248/** 
     1249 * plugin_disable_self() - When called will disable the current plugin 
     1250 * 
     1251 * Allows plugins a simple way to disable their plugin during the 
     1252 * pre-upgrade process. The plugin just has to call the function and it 
     1253 * will do all the work. 
     1254 * 
     1255 * Also a safer way, in case the way plugins are enabled changes. 
     1256 * 
     1257 * @uses $current_plugin_processed Disables current plugin 
     1258 */ 
     1259function plugin_disable_self() { 
     1260        global $current_plugin_processed; 
     1261 
     1262        $plugins = __get_option( 'active_plugins' ); 
     1263 
     1264        foreach ( (array) $plugins as $plugin ) { 
     1265                if ( $current_plugin_processed == $plugin ) { 
     1266                        array_splice( $plugins, array_search( $plugin, $plugins ), 1 ); 
     1267                        update_option( 'active_plugins', $plugins ); 
     1268                        break; 
     1269                } 
     1270        } 
     1271} 
     1272 
     1273/** 
     1274 * plugin_enable_self() - When called will enable the current plugin 
     1275 * 
     1276 * Companion of plugin_disable_self(), will enable the plugin if it disabled 
     1277 * itself. Real simple way of enabling a plugin that disabled itself and is 
     1278 * future proof, in case the plugin activation is changed. 
     1279 * 
     1280 * @uses $current_plugin_processed Enables the current plugin 
     1281 */ 
     1282function plugin_enable_self() { 
     1283        global $current_plugin_processed; 
     1284 
     1285        $plugins = __get_option( 'active_plugins' ); 
     1286 
     1287        // Prevent duplicate plugin entries if the plugin did not disable itself. 
     1288        if( false === in_array($current_plugin_processed, $plugins) ) 
     1289                $plugins[] = $current_plugin_processed; 
     1290 
     1291        sort($plugins); 
     1292        update_option( 'active_plugins', $plugins ); 
     1293} 
     1294 
     1295/** 
     1296 * Called before upgrade for plugins to clean up. 
     1297 * 
     1298 * Plugins must not die during their call. Plugins will be able to enable themselves 
     1299 * after the upgrade has been made and the result of the pre-upgrade hook will be passed 
     1300 * to the post upgrade plugin hook. 
     1301 * 
     1302 * @uses apply_filters() Calls pre_upgrade_PLUGIN PLUGIN is the path from PLUGINDIR. 
     1303 * @global string $current_plugin_processed Sets the current plugin processed value 
     1304 * @since 2.4 
     1305 * 
     1306 * @returns array The results of the plugins upgrade hook 
     1307 */ 
     1308function wp_call_pre_upgrade_hook() 
     1309{ 
     1310        global $current_plugin_processed; 
     1311 
     1312        $plugin_pre_upgrade = array(); 
     1313        $current_plugins = __get_option('active_plugins'); 
     1314        if ( is_array($current_plugins) ) { 
     1315                foreach ($current_plugins as $plugin) { 
     1316                        if ('' != $plugin && file_exists(ABSPATH . PLUGINDIR . '/' . $plugin)) { 
     1317                                // Use '@' to prevent errors from halting the rest of the upgrade process 
     1318                                @include_once(ABSPATH . PLUGINDIR . '/' . $plugin); 
     1319 
     1320                                $current_plugin_processed = $plugin; 
     1321                                $plugin_pre_upgrade[ $plugin ] = apply_filters('pre_upgrade_'.$plugin, null); 
     1322                        } 
     1323                } 
     1324        } 
     1325 
     1326        return $plugin_pre_upgrade; 
     1327} 
     1328 
     1329/** 
     1330 * Called for plugins after the WordPress upgrade process has been made. 
     1331 * 
     1332 * The result of the pre-upgrade process for the plugin is passed to the plugin 
     1333 * from the pre upgrade process. The plugin can use this as they will. The plugin 
     1334 * may also  output their status for the user to see on the upgrade page to provide 
     1335 * feedback. 
     1336 * 
     1337 * @uses do_action() Calls post_upgrade_PLUGIN where PLUGIN is the path of the plugin from PLUGINDIR 
     1338 * @since 2.4 
     1339 * @param array $pre_upgrade Passed the array from the pre upgrade 
     1340 */ 
     1341function wp_call_post_upgrade_hook($pre_upgrade) 
     1342{ 
     1343        global $current_plugin_processed; 
     1344 
     1345        if ( is_array($pre_upgrade) ) { 
     1346                foreach ($pre_upgrade as $plugin => $result) { 
     1347                        $current_plugin_processed = $plugin; 
     1348                        do_action( 'post_upgrade_'.$plugin, $result ); 
     1349                } 
     1350        } 
     1351} 
     1352 
    12461353function wp_check_mysql_version() { 
    12471354        global $wpdb; 
    12481355        $result = $wpdb->check_database_version(); 
  • wp-includes/plugin.php

     
    504504        add_action('deactivate_' . $file, $function); 
    505505} 
    506506 
     507/** 
     508 * register_pre_upgrade_hook() - Hook a plugin function into the pre upgrade process 
     509 * 
     510 * The pre-upgrade process is called before the upgrade is started to allow plugins 
     511 * to shut themselves down or do any cleanup that may cause the upgrade to fail. 
     512 * 
     513 * The filter expects the called plugin function to return true, if the function 
     514 * succeeded in doing what it was supposed to do and false if it failed to complete 
     515 * what it was supposed to do. These values aren't currently used by WordPress 
     516 * upgrade process, but they may in the future. 
     517 * 
     518 * The plugins should not make calls to any WordPress function without checking that 
     519 * it exists first. The upgrade process halts the inclusion of the entire WordPress 
     520 * library, therefore access to many of the WordPress API will not be available. 
     521 * However, the administration API will be available. 
     522 * 
     523 * The script should also take care to not run anything outside of functions in the 
     524 * plugin file. On plugin error, the script must not harm the upgrade process in any 
     525 * way and must not try to halt the upgrade process. If the script wishes to halt 
     526 * reading of the script, the script can return within the script body and outside of 
     527 * any functions. 
     528 * 
     529 * @param string $file The plugin file path that has the plugin info 
     530 * @param callback $function The function or method to call when hook is called. 
     531 */ 
     532function register_pre_upgrade_hook($file, $function) { 
     533        add_filter('pre_upgrade_'.plugin_basename($file), $function); 
     534} 
     535 
     536/** 
     537 * register_post_upgrade_hook() - Hook a plugin function into the post upgrade process 
     538 * 
     539 * The post upgrade hook is called after the upgrade has finished. This allows plugins 
     540 * which previously disabled themselves to enable themselves again. The plugin can run 
     541 * their install process again, if they previously uninstalled themselves. 
     542 * 
     543 * The plugin function will be passed the value of the pre-upgrade hook in an attempt 
     544 * for the post to handle any errors or problems that weren't handled in the pre upgrade 
     545 * hook. 
     546 * 
     547 * The function again must not cause WordPress to die any errors must fail silently. 
     548 * 
     549 * @param string $file The plugin file path that has the plugin info 
     550 * @param callback $function The function or method to call when hook is called. 
     551 */ 
     552function register_post_upgrade_hook($file, $function) { 
     553        add_action('post_upgrade_'.plugin_basename($file), $function); 
     554} 
     555 
    507556/**  
    508557 * _wp_call_all_hook() - Calls the 'all' hook, which will process the functions hooked into it. 
    509558 *