Make WordPress Core


Ignore:
Timestamp:
07/15/2026 07:22:36 AM (7 weeks ago)
Author:
westonruter
Message:

General: Allow configuration of speculative loading defaults via env variables and constants.

Introduce two overrides that let a site or hosting provider change the default speculative loading configuration that the auto value resolves to, without having to ship an mu-plugin:

  • WP_SPECULATIVE_LOADING_DEFAULT_MODE (prefetch or prerender)
  • WP_SPECULATIVE_LOADING_DEFAULT_EAGERNESS (conservative, moderate, or eager).

Each may be supplied as an environment variable, read via getenv(), or as a constant of the same name that takes precedence over it, mirroring how wp_get_environment_type() resolves WP_ENVIRONMENT_TYPE. An unrecognized value falls back to the core default.

These overrides only change what auto resolves to, so an explicit mode or eagerness supplied through the wp_speculation_rules_configuration filter still wins. An eagerness of immediate is rejected because WordPress does not permit it for the document-level rules it generates; accepting it would cause WP_Speculation_Rules::add_rule() to reject the rule and leave the page with no speculation rules at all.

Also relax WP_Speculation_Rules::is_valid_mode() and WP_Speculation_Rules::is_valid_eagerness() to accept mixed, so an arbitrary value from the filter is validated and rejected rather than raising a TypeError.

Fix PHPStan errors in speculative loading functions resulting from insufficient typing.

Developed in https://github.com/WordPress/wordpress-develop/pull/12514.
Follow-up to r59837.

Props westonruter, mukesh27, adamsilverstein, swissspidy.
See #64066, #62503, #64896, #64898.
Fixes #65624.

File:
1 edited

Legend:

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

    r61280 r62752  
    260260         *
    261261         * @since 6.8.0
    262          *
    263          * @param string $mode Speculation rules mode.
     262         * @since 7.1.0 The $mode param now allows mixed and not just string.
     263         *
     264         * @param mixed $mode Speculation rules mode.
    264265         * @return bool True if valid, false otherwise.
    265          */
    266         public static function is_valid_mode( string $mode ): bool {
    267                 return isset( self::$mode_allowlist[ $mode ] );
     266         *
     267         * @phpstan-assert-if-true 'prefetch'|'prerender' $mode
     268         */
     269        public static function is_valid_mode( $mode ): bool {
     270                return is_string( $mode ) && isset( self::$mode_allowlist[ $mode ] );
    268271        }
    269272
     
    272275         *
    273276         * @since 6.8.0
    274          *
    275          * @param string $eagerness Speculation rules eagerness.
     277         * @since 7.1.0 The $eagerness param now allows mixed and not just string.
     278         *
     279         * @param mixed $eagerness Speculation rules eagerness.
    276280         * @return bool True if valid, false otherwise.
    277          */
    278         public static function is_valid_eagerness( string $eagerness ): bool {
    279                 return isset( self::$eagerness_allowlist[ $eagerness ] );
     281         *
     282         * @phpstan-assert-if-true 'conservative'|'moderate'|'eager'|'immediate' $eagerness
     283         */
     284        public static function is_valid_eagerness( $eagerness ): bool {
     285                return is_string( $eagerness ) && isset( self::$eagerness_allowlist[ $eagerness ] );
    280286        }
    281287
Note: See TracChangeset for help on using the changeset viewer.