Make WordPress Core

Changeset 38252


Ignore:
Timestamp:
08/13/2016 04:01:09 PM (8 years ago)
Author:
jorbin
Message:

Bootstrap/Load: Revert Plugin Global restoration around advance-cache.php.

Merges [38251] to the 4.6 branch.

First added in [37588] and later modified in [38224], the idea was to ensure that filters/actions added before advance-cache.php would not disappear if advance-cache.php overloaded the filters/actions with code such as $wp_filter = array(). This is an edge case and one that there is no documented case of existing.

This restores the behavior from WordPress 4.5 and before. It is strongly encouraged that developers using advance-cache.php to use the Plugins API that is available before the loading of advance-cache.php rather than directly interacting with any of the globals.

Props azaozz, jorbin, dd32 for review, pento for review, westi for investigation, ipstenu for research.

See #36819.

Location:
branches/4.6
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/4.6

  • branches/4.6/src/wp-includes/plugin.php

    r38224 r38252  
    10021002    }
    10031003}
    1004 
    1005 /**
    1006  * Backs up global variables used for actions and filters.
    1007  *
    1008  * Prevents redefinition of these globals in advanced-cache.php from accidentally
    1009  * destroying existing data.
    1010  *
    1011  * @since 4.6.0
    1012  * @access private
    1013  *
    1014  * @global array $wp_filter         Stores all filters and actions.
    1015  * @global array $wp_actions        Stores the amount of times an action was triggered.
    1016  * @global array $merged_filters    Merges the filter hooks using this function.
    1017  * @global array $wp_current_filter Stores the list of current filters with the current one last.
    1018  * @staticvar array $backup_globals Backed up globals.
    1019  *
    1020  * @return array the staticvar from the first time it is set.
    1021  */
    1022 function _backup_plugin_globals( $backup = true ) {
    1023     global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
    1024 
    1025     static $backup_globals = array();
    1026 
    1027     if ( $backup ) {
    1028         $backup_globals = array(
    1029             'backup_wp_filter'         => $wp_filter,
    1030             'backup_wp_actions'        => $wp_actions,
    1031             'backup_merged_filters'    => $merged_filters,
    1032             'backup_wp_current_filter' => $wp_current_filter,
    1033         );
    1034 
    1035         $wp_filter = $wp_actions = array();
    1036     }
    1037     return $backup_globals;
    1038 }
    1039 
    1040 /**
    1041  * Safely restores backed up global variables used for actions and filters.
    1042  *
    1043  * @since 4.6.0
    1044  * @access private
    1045  *
    1046  * @global array $wp_filter         Stores all filters and actions.
    1047  * @global array $wp_actions        Stores the amount of times an action was triggered.
    1048  * @global array $merged_filters    Merges the filter hooks using this function.
    1049  * @global array $wp_current_filter Stores the list of current filters with the current one last.
    1050  * @staticvar array $backup_globals Backed up globals.
    1051  */
    1052 function _restore_plugin_globals() {
    1053     global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
    1054 
    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'] ) {
    1091         $merged_filters = array_merge_recursive( $merged_filters, $backup_globals['backup_merged_filters'] );
    1092     }
    1093 
    1094     if ( $wp_current_filter !== $backup_globals['backup_wp_current_filter'] ) {
    1095         $wp_current_filter = array_merge_recursive( $wp_current_filter, $backup_globals['backup_wp_current_filter'] );
    1096     }
    1097 }
  • branches/4.6/src/wp-settings.php

    r38224 r38252  
    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( true );
    8786    WP_DEBUG ? include( WP_CONTENT_DIR . '/advanced-cache.php' ) : @include( WP_CONTENT_DIR . '/advanced-cache.php' );
    88     _restore_plugin_globals();
    8987}
    9088
  • branches/4.6/tests/phpunit/tests/actions.php

    r38224 r38252  
    329329    }
    330330
    331     /**
    332      * @ticket 36819
    333      */
    334     function test_backup_plugin_globals_returns_filters() {
    335         $backup = _backup_plugin_globals( true );
    336         $this->assertArrayHasKey( 'backup_wp_filter',         $backup );
    337         $this->assertArrayHasKey( 'backup_wp_actions',        $backup );
    338         $this->assertArrayHasKey( 'backup_wp_current_filter', $backup );
    339         $this->assertArrayHasKey( 'backup_merged_filters', $backup );
    340     }
    341 
    342     /**
    343      * @ticket 36819
    344      */
    345     function test_backup_plugin_globals_returns_filters_from_first_time_called() {
    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 );
    354         $this->assertEquals( $backup, $new_backup );
    355     }
    356 
    357     /**
    358      * @ticket 36819
    359      */
    360     function test_restore_plugin_globals_from_stomp() {
    361         global $wp_actions;
    362         $original_actions = $wp_actions;
    363 
    364         _backup_plugin_globals( true );
    365 
    366         $wp_actions = array();
    367 
    368         $this->assertEmpty( $wp_actions );
    369         _restore_plugin_globals();
    370 
    371         $this->assertEquals( $GLOBALS['wp_actions'], $original_actions );
    372     }
    373 
    374     /**
    375      * @ticket 36819
    376      */
    377     function test_restore_plugin_globals_includes_additions() {
    378         global $wp_filter;
    379         $original_filter = $wp_filter;
    380 
    381         $backup = _backup_plugin_globals( true );
    382 
    383         $a = new MockAction();
    384         $tag = rand_str();
    385         add_action($tag, array(&$a, 'action'));
    386 
    387         $this->assertNotEquals( $GLOBALS['wp_filter'], $original_filter );
    388 
    389         _restore_plugin_globals();
    390 
    391         $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 ] );
    412     }
    413 
    414331    function apply_testing_filter() {
    415332        $this->apply_testing_filter = true;
Note: See TracChangeset for help on using the changeset viewer.