Make WordPress Core

Changeset 56448


Ignore:
Timestamp:
08/24/2023 03:55:27 PM (9 months ago)
Author:
joemcgill
Message:

Rewrite Rules: Prevent stampedes when flush_rewrite_rules() is called

This ensures that the rewrite_rules option is not emptied until the new value has been recalculated and the option is updated. The logic for refreshing the option value is moved to a new private method named WP_Rewrite::refresh_rewrite_rules which is used by both the flush_rules and refresh_rewrite_rules methods.

Props iCaleb, joemcgill, flixos90, mukesh27.
Fixes #58998.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-rewrite.php

    r56210 r56448  
    14911491        $this->rules = get_option( 'rewrite_rules' );
    14921492        if ( empty( $this->rules ) ) {
    1493             $this->matches = 'matches';
    1494             $this->rewrite_rules();
    1495             if ( ! did_action( 'wp_loaded' ) ) {
    1496                 add_action( 'wp_loaded', array( $this, 'flush_rules' ) );
    1497                 return $this->rules;
    1498             }
     1493            $this->refresh_rewrite_rules();
     1494        }
     1495
     1496        return $this->rules;
     1497    }
     1498
     1499    /**
     1500     * Refreshes the rewrite rules, saving the fresh value to the database.
     1501     * If the `wp_loaded` action has not occurred yet, will postpone saving to the database.
     1502     *
     1503     * @since 6.4.0
     1504     */
     1505    private function refresh_rewrite_rules() {
     1506        $this->rules   = '';
     1507        $this->matches = 'matches';
     1508
     1509        $this->rewrite_rules();
     1510
     1511        if ( ! did_action( 'wp_loaded' ) ) {
     1512            /*
     1513             * Is not safe to save the results right now, as the rules may be partial.
     1514             * Need to give all rules the chance to register.
     1515             */
     1516            add_action( 'wp_loaded', array( $this, 'flush_rules' ) );
     1517        } else {
    14991518            update_option( 'rewrite_rules', $this->rules );
    15001519        }
    1501 
    1502         return $this->rules;
    15031520    }
    15041521
     
    18651882        }
    18661883
    1867         update_option( 'rewrite_rules', '' );
    1868         $this->wp_rewrite_rules();
     1884        $this->refresh_rewrite_rules();
    18691885
    18701886        /**
Note: See TracChangeset for help on using the changeset viewer.