From 17afb9c70438b198359327ee7c195315d9bc70a3 Mon Sep 17 00:00:00 2001 From: Aaron Jorbin Date: Sat, 13 Aug 2016 15:59:03 +0000 Subject: [PATCH] Bootstrap/Load: Revert Plugin Global restoration around `advance-cache.php`. 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. git-svn-id: https://develop.svn.wordpress.org/trunk@38251 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/plugin.php | 94 --------------------------------- src/wp-settings.php | 2 - tests/phpunit/tests/actions.php | 83 ----------------------------- 3 files changed, 179 deletions(-) diff --git a/src/wp-includes/plugin.php b/src/wp-includes/plugin.php index 6e3a21e0a3..ca4985f1f0 100644 --- a/src/wp-includes/plugin.php +++ b/src/wp-includes/plugin.php @@ -1001,97 +1001,3 @@ function _wp_filter_build_unique_id($tag, $function, $priority) { return $function[0] . '::' . $function[1]; } } - -/** - * Backs up global variables used for actions and filters. - * - * Prevents redefinition of these globals in advanced-cache.php from accidentally - * destroying existing data. - * - * @since 4.6.0 - * @access private - * - * @global array $wp_filter Stores all filters and actions. - * @global array $wp_actions Stores the amount of times an action was triggered. - * @global array $merged_filters Merges the filter hooks using this function. - * @global array $wp_current_filter Stores the list of current filters with the current one last. - * @staticvar array $backup_globals Backed up globals. - * - * @return array the staticvar from the first time it is set. - */ -function _backup_plugin_globals( $backup = true ) { - global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter; - - static $backup_globals = array(); - - if ( $backup ) { - $backup_globals = array( - 'backup_wp_filter' => $wp_filter, - 'backup_wp_actions' => $wp_actions, - 'backup_merged_filters' => $merged_filters, - 'backup_wp_current_filter' => $wp_current_filter, - ); - - $wp_filter = $wp_actions = array(); - } - return $backup_globals; -} - -/** - * Safely restores backed up global variables used for actions and filters. - * - * @since 4.6.0 - * @access private - * - * @global array $wp_filter Stores all filters and actions. - * @global array $wp_actions Stores the amount of times an action was triggered. - * @global array $merged_filters Merges the filter hooks using this function. - * @global array $wp_current_filter Stores the list of current filters with the current one last. - * @staticvar array $backup_globals Backed up globals. - */ -function _restore_plugin_globals() { - global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter; - - $backup_globals = _backup_plugin_globals( false ); - - if ( empty( $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 ( empty ( $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'] ) { - $merged_filters = array_merge_recursive( $merged_filters, $backup_globals['backup_merged_filters'] ); - } - - 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'] ); - } -} diff --git a/src/wp-settings.php b/src/wp-settings.php index 1a241fac05..dc3b25a7cb 100644 --- a/src/wp-settings.php +++ b/src/wp-settings.php @@ -83,9 +83,7 @@ wp_debug_mode(); */ 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. - _backup_plugin_globals( true ); WP_DEBUG ? include( WP_CONTENT_DIR . '/advanced-cache.php' ) : @include( WP_CONTENT_DIR . '/advanced-cache.php' ); - _restore_plugin_globals(); } // Define WP_LANG_DIR if not set. diff --git a/tests/phpunit/tests/actions.php b/tests/phpunit/tests/actions.php index fd45ffd6af..ebfd6dad5a 100644 --- a/tests/phpunit/tests/actions.php +++ b/tests/phpunit/tests/actions.php @@ -328,89 +328,6 @@ class Tests_Actions extends WP_UnitTestCase { $this->assertFalse( doing_filter( 'testing' ) ); // No longer doing this filter } - /** - * @ticket 36819 - */ - function test_backup_plugin_globals_returns_filters() { - $backup = _backup_plugin_globals( true ); - $this->assertArrayHasKey( 'backup_wp_filter', $backup ); - $this->assertArrayHasKey( 'backup_wp_actions', $backup ); - $this->assertArrayHasKey( 'backup_wp_current_filter', $backup ); - $this->assertArrayHasKey( 'backup_merged_filters', $backup ); - } - - /** - * @ticket 36819 - */ - function test_backup_plugin_globals_returns_filters_from_first_time_called() { - $backup = _backup_plugin_globals( true ); - - $a = new MockAction(); - $tag = rand_str(); - - add_action($tag, array(&$a, 'action')); - - $new_backup = _backup_plugin_globals( false ); - $this->assertEquals( $backup, $new_backup ); - } - - /** - * @ticket 36819 - */ - function test_restore_plugin_globals_from_stomp() { - global $wp_actions; - $original_actions = $wp_actions; - - _backup_plugin_globals( true ); - - $wp_actions = array(); - - $this->assertEmpty( $wp_actions ); - _restore_plugin_globals(); - - $this->assertEquals( $GLOBALS['wp_actions'], $original_actions ); - } - - /** - * @ticket 36819 - */ - function test_restore_plugin_globals_includes_additions() { - global $wp_filter; - $original_filter = $wp_filter; - - $backup = _backup_plugin_globals( true ); - - $a = new MockAction(); - $tag = rand_str(); - add_action($tag, array(&$a, 'action')); - - $this->assertNotEquals( $GLOBALS['wp_filter'], $original_filter ); - - _restore_plugin_globals(); - - $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() { $this->apply_testing_filter = true;