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.

Props pento, jorbin.
See #36819.



git-svn-id: https://develop.svn.wordpress.org/trunk@38223 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast 2016-08-09 01:44:20 +00:00
parent 7044243aa2
commit 32b55e7e7a
3 changed files with 64 additions and 17 deletions

View File

@ -1019,19 +1019,21 @@ function _wp_filter_build_unique_id($tag, $function, $priority) {
* *
* @return array the staticvar from the first time it is set. * @return array the staticvar from the first time it is set.
*/ */
function _backup_plugin_globals(){ function _backup_plugin_globals( $backup = true ) {
global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter; global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
static $backup_globals = array(); static $backup_globals = array();
if ( empty( $backup_globals ) ) { if ( $backup ) {
$backup_globals = array( $backup_globals = array(
'backup_wp_filter' => $wp_filter, 'backup_wp_filter' => $wp_filter,
'backup_wp_actions' => $wp_actions, 'backup_wp_actions' => $wp_actions,
'backup_merged_filters' => $merged_filters, 'backup_merged_filters' => $merged_filters,
'backup_wp_current_filter' => $wp_current_filter, 'backup_wp_current_filter' => $wp_current_filter,
); );
};
$wp_filter = $wp_actions = array();
}
return $backup_globals; return $backup_globals;
} }
@ -1047,24 +1049,49 @@ function _backup_plugin_globals(){
* @global array $wp_current_filter Stores the list of current filters with the current one last. * @global array $wp_current_filter Stores the list of current filters with the current one last.
* @staticvar array $backup_globals Backed up globals. * @staticvar array $backup_globals Backed up globals.
*/ */
function _restore_plugin_globals(){ function _restore_plugin_globals() {
global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter; global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
$backup_globals = _backup_plugin_globals(); $backup_globals = _backup_plugin_globals( false );
if ( $wp_filter !== $backup_globals['backup_wp_filter'] ){ if ( empty( $wp_filter ) ) {
$wp_filter = array_merge_recursive( $wp_filter, $backup_globals['backup_wp_filter'] ); $wp_filter = $backup_globals['backup_wp_filter'];
} else {
$added_filters = $wp_filter;
$wp_filter = $backup_globals['backup_wp_filter'];
foreach ( $added_filters as $tag => $callback_groups ) {
// Loop through callback groups.
foreach ( $callback_groups as $priority => $callbacks ) {
// Loop through callbacks.
foreach ( $callbacks as $cb ) {
add_filter( $tag, $cb['function'], $priority, $cb['accepted_args'] );
}
}
}
} }
if ( $wp_actions !== $backup_globals['backup_wp_actions'] ){ if ( empty ( $wp_actions ) ) {
$wp_actions = array_merge_recursive( $wp_actions, $backup_globals['backup_wp_actions'] ); $wp_actions = $backup_globals['backup_wp_actions'];
} else {
$run_actions = $wp_actions;
$wp_actions = $backup_globals['backup_wp_actions'];
foreach( $run_actions as $action => $count ) {
if ( ! isset( $wp_actions[ $action ] ) ) {
$wp_actions[ $action ] = 0;
}
$wp_actions[ $action ] += $count;
}
} }
if ( $merged_filters !== $backup_globals['backup_merged_filters'] ){ if ( $merged_filters !== $backup_globals['backup_merged_filters'] ) {
$merged_filters = array_merge_recursive( $merged_filters, $backup_globals['backup_merged_filters'] ); $merged_filters = array_merge_recursive( $merged_filters, $backup_globals['backup_merged_filters'] );
} }
if ( $wp_current_filter !== $backup_globals['backup_wp_current_filter'] ){ if ( $wp_current_filter !== $backup_globals['backup_wp_current_filter'] ) {
$wp_current_filter = array_merge_recursive( $wp_current_filter, $backup_globals['backup_wp_current_filter'] ); $wp_current_filter = array_merge_recursive( $wp_current_filter, $backup_globals['backup_wp_current_filter'] );
} }
} }

View File

@ -83,7 +83,7 @@ wp_debug_mode();
*/ */
if ( WP_CACHE && apply_filters( 'enable_loading_advanced_cache_dropin', true ) ) { if ( WP_CACHE && apply_filters( 'enable_loading_advanced_cache_dropin', true ) ) {
// For an advanced caching plugin to use. Uses a static drop-in because you would only want one. // For an advanced caching plugin to use. Uses a static drop-in because you would only want one.
_backup_plugin_globals(); _backup_plugin_globals( true );
WP_DEBUG ? include( WP_CONTENT_DIR . '/advanced-cache.php' ) : @include( WP_CONTENT_DIR . '/advanced-cache.php' ); WP_DEBUG ? include( WP_CONTENT_DIR . '/advanced-cache.php' ) : @include( WP_CONTENT_DIR . '/advanced-cache.php' );
_restore_plugin_globals(); _restore_plugin_globals();
} }

View File

@ -332,7 +332,7 @@ class Tests_Actions extends WP_UnitTestCase {
* @ticket 36819 * @ticket 36819
*/ */
function test_backup_plugin_globals_returns_filters() { function test_backup_plugin_globals_returns_filters() {
$backup = _backup_plugin_globals(); $backup = _backup_plugin_globals( true );
$this->assertArrayHasKey( 'backup_wp_filter', $backup ); $this->assertArrayHasKey( 'backup_wp_filter', $backup );
$this->assertArrayHasKey( 'backup_wp_actions', $backup ); $this->assertArrayHasKey( 'backup_wp_actions', $backup );
$this->assertArrayHasKey( 'backup_wp_current_filter', $backup ); $this->assertArrayHasKey( 'backup_wp_current_filter', $backup );
@ -343,14 +343,14 @@ class Tests_Actions extends WP_UnitTestCase {
* @ticket 36819 * @ticket 36819
*/ */
function test_backup_plugin_globals_returns_filters_from_first_time_called() { function test_backup_plugin_globals_returns_filters_from_first_time_called() {
$backup = _backup_plugin_globals(); $backup = _backup_plugin_globals( true );
$a = new MockAction(); $a = new MockAction();
$tag = rand_str(); $tag = rand_str();
add_action($tag, array(&$a, 'action')); add_action($tag, array(&$a, 'action'));
$new_backup = _backup_plugin_globals(); $new_backup = _backup_plugin_globals( false );
$this->assertEquals( $backup, $new_backup ); $this->assertEquals( $backup, $new_backup );
} }
@ -361,7 +361,7 @@ class Tests_Actions extends WP_UnitTestCase {
global $wp_actions; global $wp_actions;
$original_actions = $wp_actions; $original_actions = $wp_actions;
_backup_plugin_globals(); _backup_plugin_globals( true );
$wp_actions = array(); $wp_actions = array();
@ -378,7 +378,7 @@ class Tests_Actions extends WP_UnitTestCase {
global $wp_filter; global $wp_filter;
$original_filter = $wp_filter; $original_filter = $wp_filter;
$backup = _backup_plugin_globals(); $backup = _backup_plugin_globals( true );
$a = new MockAction(); $a = new MockAction();
$tag = rand_str(); $tag = rand_str();
@ -391,6 +391,26 @@ class Tests_Actions extends WP_UnitTestCase {
$this->assertNotEquals( $GLOBALS['wp_filter'], $original_filter ); $this->assertNotEquals( $GLOBALS['wp_filter'], $original_filter );
} }
/**
* @ticket 36819
*/
function test_applied_actions_are_counted_after_restore() {
global $wp_actions;
$action_name = 'this_is_a_fake_action_name';
$this->assertArrayNotHasKey( $action_name, $wp_actions );
do_action( $action_name );
$this->assertEquals( 1, $wp_actions[ $action_name ] );
_backup_plugin_globals( true );
do_action( $action_name );
_restore_plugin_globals();
$this->assertEquals( 2, $wp_actions[ $action_name ] );
}
function apply_testing_filter() { function apply_testing_filter() {
$this->apply_testing_filter = true; $this->apply_testing_filter = true;