WordPress.org

Make WordPress Core

Ticket #36819: 36819.3.diff

File 36819.3.diff, 5.8 KB (added by danielbachhuber, 4 years ago)
  • src/wp-includes/plugin.php

    diff --git src/wp-includes/plugin.php src/wp-includes/plugin.php
    index dd8798a..f4353ea 100644
    function _wp_filter_build_unique_id($tag, $function, $priority) { 
    946946                return $function[0] . '::' . $function[1];
    947947        }
    948948}
     949
     950/**
     951 * Back up global variables used for actions and filters.
     952 *
     953 * Prevents redefinition of these globals in advanced-cache.php from accidentally
     954 * destroying existing data.
     955 *
     956 * @since 4.6.0
     957 * @access private
     958 *
     959 * @global array $wp_filter         Stores all filters and actions.
     960 * @global array $wp_actions        Stores the amount of times an action was triggered.
     961 * @global array $merged_filters    Merges the filter hooks using this function.
     962 * @global array $wp_current_filter Stores the list of current filters with the current one last.
     963 * @staticvar array $backup_globals Backed up globals.
     964 */
     965function _backup_plugin_globals(){
     966        global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
     967        static $backup_globals = array();
     968        if ( empty( $backup_globals ) ) {
     969                $backup_globals = array(
     970                        'backup_wp_filter'         => $wp_filter,
     971                        'backup_wp_actions'        => $wp_actions,
     972                        'backup_merged_filters'    => $merged_filters,
     973                        'backup_wp_current_filter' => $wp_current_filter,
     974                );
     975        };
     976        return $backup_globals;
     977}
     978
     979/**
     980 * Safely restore backed up global variables used for actions and filters.
     981 *
     982 * @since 4.6.0
     983 * @access private
     984 *
     985 * @global array $wp_filter         Stores all filters and actions.
     986 * @global array $wp_actions        Stores the amount of times an action was triggered.
     987 * @global array $merged_filters    Merges the filter hooks using this function.
     988 * @global array $wp_current_filter Stores the list of current filters with the current one last.
     989 * @staticvar array $backup_globals Backed up globals.
     990 */
     991function _restore_plugin_globals(){
     992        global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
     993        $backup_globals = _backup_plugin_globals();
     994        if ( $wp_filter !== $backup_globals['backup_wp_filter'] ){
     995                $wp_filter = array_merge_recursive( $wp_filter, $backup_globals['backup_wp_filter'] );
     996        }
     997
     998        if ( $wp_actions !== $backup_globals['backup_wp_actions'] ){
     999                $wp_actions = array_merge_recursive( $wp_actions, $backup_globals['backup_wp_actions'] );
     1000        }
     1001
     1002        if ( $merged_filters !== $backup_globals['backup_merged_filters'] ){
     1003                $merged_filters = array_merge_recursive( $merged_filters, $backup_globals['backup_merged_filters'] );
     1004        }
     1005
     1006        if ( $wp_current_filter !== $backup_globals['backup_wp_current_filter'] ){
     1007                $wp_current_filter = array_merge_recursive( $wp_current_filter, $backup_globals['backup_wp_current_filter'] );
     1008        }
     1009}
  • src/wp-settings.php

    diff --git src/wp-settings.php src/wp-settings.php
    index 9be6b9d..4d81e1d 100644
    define( 'WPINC', 'wp-includes' ); 
    2020// Include files required for initialization.
    2121require( ABSPATH . WPINC . '/load.php' );
    2222require( ABSPATH . WPINC . '/default-constants.php' );
     23require( ABSPATH . WPINC . '/plugin.php' );
    2324
    2425/*
    2526 * These can't be directly globalized in version.php. When updating,
    timer_start(); 
    7071wp_debug_mode();
    7172
    7273// For an advanced caching plugin to use. Uses a static drop-in because you would only want one.
    73 if ( WP_CACHE )
     74if ( WP_CACHE ) {
     75        _backup_plugin_globals();
    7476        WP_DEBUG ? include( WP_CONTENT_DIR . '/advanced-cache.php' ) : @include( WP_CONTENT_DIR . '/advanced-cache.php' );
     77        _restore_plugin_globals();
     78}
    7579
    7680// Define WP_LANG_DIR if not set.
    7781wp_set_lang_dir();
    require( ABSPATH . WPINC . '/compat.php' ); 
    8185require( ABSPATH . WPINC . '/functions.php' );
    8286require( ABSPATH . WPINC . '/class-wp.php' );
    8387require( ABSPATH . WPINC . '/class-wp-error.php' );
    84 require( ABSPATH . WPINC . '/plugin.php' );
    8588require( ABSPATH . WPINC . '/pomo/mo.php' );
    8689
    8790// Include the wpdb class and, if present, a db.php database drop-in.
  • tests/phpunit/tests/actions.php

    diff --git tests/phpunit/tests/actions.php tests/phpunit/tests/actions.php
    index 583c8ce..ad53ec0 100644
    class Tests_Actions extends WP_UnitTestCase { 
    328328                $this->assertFalse( doing_filter( 'testing' ) ); // No longer doing this filter
    329329        }
    330330
     331        /**
     332         * @ticket 36819
     333         */
     334        function test_backup_plugin_globals_returns_filters() {
     335                $backup = _backup_plugin_globals();
     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();
     347
     348                $a = new MockAction();
     349                $tag = rand_str();
     350
     351                add_action($tag, array(&$a, 'action'));
     352
     353                $new_backup = _backup_plugin_globals();
     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();
     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();
     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
    331394        function apply_testing_filter() {
    332395                $this->apply_testing_filter = true;
    333396