WP_Speculation_Rules
- Since
- 6.8.0
- Source
wp-includes/class-wp-speculation-rules.php:16
Class representing a set of speculation rules.
Compatibility
- WordPress
- since 6.8.0
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in 4 of the 5 tracked releases, added in 6.8.0.
Properties · 4
$rules_by_modearray<string,private- Stored rules, as a map of
$mode => $rulespairs. $mode_allowlistarray<string,private static- The allowed speculation rules modes as a map, used for validation.
$eagerness_allowlistarray<string,private static- The allowed speculation rules eagerness levels as a map, used for validation.
$source_allowlistarray<string,private static- The allowed speculation rules sources as a map, used for validation.
Methods · 7
- add_rule()Adds a speculation rule to the speculation rules to consider.
- has_rule()Checks whether a speculation rule for the given mode and ID already exists.
- jsonSerialize()Returns the speculation rules data ready to be JSON-encoded.
- is_valid_id()Checks whether the given ID is valid.
- is_valid_mode()Checks whether the given speculation rules mode is valid.
- is_valid_eagerness()Checks whether the given speculation rules eagerness is valid.
- is_valid_source()Checks whether the given speculation rules source is valid.
Source code
final class WP_Speculation_Rules implements JsonSerializable { /** * Stored rules, as a map of `$mode => $rules` pairs. * * Every `$rules` value is a map of `$id => $rule` pairs. * * @since 6.8.0 * @var array<string, array<string, array<string, mixed>>> */ private $rules_by_mode = array(); /** * The allowed speculation rules modes as a map, used for validation. * * @since 6.8.0 * @var array<string, bool> */ private static $mode_allowlist = array( 'prefetch' => true, 'prerender' => true, ); /** * The allowed speculation rules eagerness levels as a map, used for validation. * * @since 6.8.0 * @var array<string, bool> */ private static $eagerness_allowlist = array( 'immediate' => true, 'eager' => true, 'moderate' => true, 'conservative' => true, ); /** * The allowed speculation rules sources as a map, used for validation. * * @since 6.8.0 * @var array<string, bool> */ private static $source_allowlist = array( 'list' => true, 'document' => true, ); /** * Adds a speculation rule to the speculation rules to consider. * * @since 6.8.0 * * @param string $mode Speculative loading mode. Either 'prefetch' or 'prerender'. * @param string $id Unique string identifier for the speculation rule. * @param array<string, mixed> $rule Associative array of rule arguments. * @return bool True on success, false if invalid parameters are provided. */ public function add_rule( string $mode, string $id, array $rule ): bool { if ( ! self::is_valid_mode( $mode ) ) { _doing_it_wrong( __METHOD__, sprintf( /* translators: %s: invalid mode value */ __( 'The value "%s" is not a valid speculation rules mode.' ), esc_html( $mode ) ), '6.8.0' ); return false; } if ( ! $this->is_valid_id( $id ) ) { _doing_it_wrong( __METHOD__, sprintf( /* translators: %s: invalid ID value */ __( 'The value "%s" is not a valid ID for a speculation rule.' ), esc_html( $id ) ), '6.8.0'Changelog
Introduced in 6.8.0. Unchanged from 6.8.8 through 7.1.0.
Signature, return type and hooks compared across 4 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/class-wp-speculation-rules.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.