WordPress.org

Make WordPress Core

Changeset 38224


Ignore:
Timestamp:
08/09/2016 01:50:21 AM (3 years ago)
Author:
pento
Message:

Boostrap/Load: Improve forward compatiblity of plugin global backup methods.

[37588] added methods to backup the plugin globals, for ensuring that advanced cache drop-ins don't overwrite hooks that've been added earlier in the load process.

The method for restoring the plugin globals wasn't compatible with the implementation of WP_Hook in #17817. WP_Hook implements ArrayAccess, so _restore_plugin_globals() was treating it as an array, and inadvertantly overwriting the WP_Hook object with a plain array.

To avoid having to re-write this code as part of #17817, we now use add_filter() to restore any hooks that were added by cache drop-ins, which WP_Hook correctly supports.

Merge of [38223] with the 4.6 branch.

Props pento, jorbin.
Fixes #36819.

Location:
branches/4.6
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.6/src/wp-includes/plugin.php

    r38125 r38224  
    10201020 * @return array the staticvar from the first time it is set.
    10211021 */
    1022 function _backup_plugin_globals(){
     1022function _backup_plugin_globals( $backup = true ) {
    10231023    global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
    10241024
    10251025    static $backup_globals = array();
    10261026
    1027     if ( empty( $backup_globals ) ) {
     1027    if ( $backup ) {
    10281028        $backup_globals = array(
    10291029            'backup_wp_filter'         => $wp_filter,
     
    10321032            'backup_wp_current_filter' => $wp_current_filter,
    10331033        );
    1034     };
     1034
     1035        $wp_filter = $wp_actions = array();
     1036    }
    10351037    return $backup_globals;
    10361038}
     
    10481050 * @staticvar array $backup_globals Backed up globals.
    10491051 */
    1050 function _restore_plugin_globals(){
     1052function _restore_plugin_globals() {
    10511053    global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
    10521054
    1053     $backup_globals = _backup_plugin_globals();
    1054 
    1055     if ( $wp_filter !== $backup_globals['backup_wp_filter'] ){
    1056         $wp_filter = array_merge_recursive( $wp_filter, $backup_globals['backup_wp_filter'] );
    1057     }
    1058 
    1059     if ( $wp_actions !== $backup_globals['backup_wp_actions'] ){
    1060         $wp_actions = array_merge_recursive( $wp_actions, $backup_globals['backup_wp_actions'] );
    1061     }
    1062 
    1063     if ( $merged_filters !== $backup_globals['backup_merged_filters'] ){
     1055    $backup_globals = _backup_plugin_globals( false );
     1056
     1057    if ( empty( $wp_filter ) ) {
     1058        $wp_filter = $backup_globals['backup_wp_filter'];
     1059    } else {
     1060        $added_filters = $wp_filter;
     1061        $wp_filter = $backup_globals['backup_wp_filter'];
     1062
     1063        foreach ( $added_filters as $tag => $callback_groups ) {
     1064            // Loop through callback groups.
     1065            foreach ( $callback_groups as $priority => $callbacks ) {
     1066
     1067                // Loop through callbacks.
     1068                foreach ( $callbacks as $cb ) {
     1069                    add_filter( $tag, $cb['function'], $priority, $cb['accepted_args'] );
     1070                }
     1071            }
     1072        }
     1073    }
     1074
     1075    if ( empty ( $wp_actions ) ) {
     1076        $wp_actions = $backup_globals['backup_wp_actions'];
     1077    } else {
     1078        $run_actions = $wp_actions;
     1079        $wp_actions = $backup_globals['backup_wp_actions'];
     1080
     1081        foreach( $run_actions as $action => $count ) {
     1082            if ( ! isset( $wp_actions[ $action ] ) ) {
     1083                $wp_actions[ $action ] = 0;
     1084            }
     1085
     1086            $wp_actions[ $action ] += $count;
     1087        }
     1088    }
     1089
     1090    if ( $merged_filters !== $backup_globals['backup_merged_filters'] ) {
    10641091        $merged_filters = array_merge_recursive( $merged_filters, $backup_globals['backup_merged_filters'] );
    10651092    }
    10661093
    1067     if ( $wp_current_filter !== $backup_globals['backup_wp_current_filter'] ){
     1094    if ( $wp_current_filter !== $backup_globals['backup_wp_current_filter'] ) {
    10681095        $wp_current_filter = array_merge_recursive( $wp_current_filter, $backup_globals['backup_wp_current_filter'] );
    10691096    }
  • branches/4.6/src/wp-settings.php

    r38028 r38224  
    8484if ( WP_CACHE && apply_filters( 'enable_loading_advanced_cache_dropin', true ) ) {
    8585// For an advanced caching plugin to use. Uses a static drop-in because you would only want one.
    86     _backup_plugin_globals();
     86    _backup_plugin_globals( true );
    8787    WP_DEBUG ? include( WP_CONTENT_DIR . '/advanced-cache.php' ) : @include( WP_CONTENT_DIR . '/advanced-cache.php' );
    8888    _restore_plugin_globals();
  • branches/4.6/tests/phpunit/tests/actions.php

    r37909 r38224  
    333333     */
    334334    function test_backup_plugin_globals_returns_filters() {
    335         $backup = _backup_plugin_globals();
     335        $backup = _backup_plugin_globals( true );
    336336        $this->assertArrayHasKey( 'backup_wp_filter',         $backup );
    337337        $this->assertArrayHasKey( 'backup_wp_actions',        $backup );
     
    344344     */
    345345    function test_backup_plugin_globals_returns_filters_from_first_time_called() {
    346         $backup = _backup_plugin_globals();
    347 
    348         $a = new MockAction();
    349         $tag = rand_str();
    350 
    351         add_action($tag, array(&$a, 'action'));
    352 
    353         $new_backup = _backup_plugin_globals();
     346        $backup = _backup_plugin_globals( true );
     347
     348        $a = new MockAction();
     349        $tag = rand_str();
     350
     351        add_action($tag, array(&$a, 'action'));
     352
     353        $new_backup = _backup_plugin_globals( false );
    354354        $this->assertEquals( $backup, $new_backup );
    355355    }
     
    362362        $original_actions = $wp_actions;
    363363
    364         _backup_plugin_globals();
     364        _backup_plugin_globals( true );
    365365
    366366        $wp_actions = array();
     
    379379        $original_filter = $wp_filter;
    380380
    381         $backup = _backup_plugin_globals();
     381        $backup = _backup_plugin_globals( true );
    382382
    383383        $a = new MockAction();
     
    390390
    391391        $this->assertNotEquals( $GLOBALS['wp_filter'], $original_filter );
     392    }
     393
     394    /**
     395     * @ticket 36819
     396     */
     397    function test_applied_actions_are_counted_after_restore() {
     398        global $wp_actions;
     399
     400        $action_name = 'this_is_a_fake_action_name';
     401        $this->assertArrayNotHasKey( $action_name, $wp_actions );
     402
     403        do_action( $action_name );
     404
     405        $this->assertEquals( 1, $wp_actions[ $action_name ] );
     406
     407        _backup_plugin_globals( true );
     408        do_action( $action_name );
     409        _restore_plugin_globals();
     410
     411        $this->assertEquals( 2, $wp_actions[ $action_name ] );
    392412    }
    393413
Note: See TracChangeset for help on using the changeset viewer.