wppaste
WordPress

WP_Speculation_Rules::add_rule( string $mode, string $id, array $rule ): bool

Since
6.8.0
Source
wp-includes/class-wp-speculation-rules.php:73
Adds a speculation rule to the speculation rules to consider.

Compatibility

WordPress
since 6.8.0
PHP
7.4–8.6-dev
  • 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, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$modestring
Speculative loading mode. Either 'prefetch' or 'prerender'.
$idstring
Unique string identifier for the speculation rule.
$rulearray

Return value

bool
True on success, false if invalid parameters are provided.

Performance profile

How much work a call to WP_Speculation_Rules::add_rule() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
23–68

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 205.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksdo_action()one call below WP_Speculation_Rules::add_rule()

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 14 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Speculation_Rules::add_rule() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!::is_valid_mode()23::is_valid_mode(), __(), esc_html(), sprintf(), _doing_it_wrong()
::is_valid_mode() && !->is_valid_id()27::is_valid_mode(), ->is_valid_id(), __(), esc_html(), sprintf(), _doing_it_wrong()
::is_valid_mode() && ->is_valid_id() && !->has_rule()32–39::is_valid_mode(), ->is_valid_id(), ->has_rule()
::is_valid_mode() && ->is_valid_id() && ->has_rule()32::is_valid_mode(), ->is_valid_id(), ->has_rule(), __(), esc_html(), sprintf(), _doing_it_wrong()
::is_valid_mode() && ->is_valid_id() && !->has_rule()34–38::is_valid_mode(), ->is_valid_id(), ->has_rule(), __(), sprintf(), _doing_it_wrong()
::is_valid_mode() && ->is_valid_id() && !->has_rule() && ::is_valid_eagerness()40–50::is_valid_mode(), ->is_valid_id(), ->has_rule(), ::is_valid_eagerness()
::is_valid_mode() && ->is_valid_id() && !->has_rule() && ::is_valid_source()44–55::is_valid_mode(), ->is_valid_id(), ->has_rule(), ::is_valid_source()
::is_valid_mode() && ->is_valid_id() && !->has_rule() && !::is_valid_source()46–50::is_valid_mode(), ->is_valid_id(), ->has_rule(), ::is_valid_source(), __(), esc_html(), sprintf(), _doing_it_wrong()
::is_valid_mode() && ->is_valid_id() && !->has_rule() && ::is_valid_source()47–56::is_valid_mode(), ->is_valid_id(), ->has_rule(), ::is_valid_source(), __(), sprintf(), _doing_it_wrong()
::is_valid_mode() && ->is_valid_id() && !->has_rule() && ::is_valid_eagerness()48–52::is_valid_mode(), ->is_valid_id(), ->has_rule(), ::is_valid_eagerness(), __(), sprintf(), _doing_it_wrong()
::is_valid_mode() && ->is_valid_id() && !->has_rule() && !::is_valid_eagerness()48–52::is_valid_mode(), ->is_valid_id(), ->has_rule(), ::is_valid_eagerness(), __(), esc_html(), sprintf(), _doing_it_wrong()
::is_valid_mode() && ->is_valid_id() && !->has_rule() && ::is_valid_source() && ::is_valid_eagerness()52–66::is_valid_mode(), ->is_valid_id(), ->has_rule(), ::is_valid_source(), ::is_valid_eagerness()
2 further outcomes, up to 68 instructions
::is_valid_mode() && ->is_valid_id() && !->has_rule() && ::is_valid_source() && ::is_valid_eagerness()60–68::is_valid_mode(), ->is_valid_id(), ->has_rule(), ::is_valid_source(), ::is_valid_eagerness(), __(), sprintf(), _doing_it_wrong()
::is_valid_mode() && ->is_valid_id() && !->has_rule() && ::is_valid_source() && !::is_valid_eagerness()60–68::is_valid_mode(), ->is_valid_id(), ->has_rule(), ::is_valid_source(), ::is_valid_eagerness(), __(), esc_html(), sprintf(), _doing_it_wrong()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 205 instructions, 23–68 executed per call, 18 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 8

Source code

	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'			);			return false;		} 		if ( $this->has_rule( $mode, $id ) ) {			_doing_it_wrong(				__METHOD__,				sprintf(					/* translators: %s: invalid ID value */					__( 'A speculation rule with ID "%s" already exists.' ),					esc_html( $id )				),				'6.8.0'			);			return false;		} 		/*		 * Perform some basic speculation rule validation.		 * Every rule must have either a 'where' key or a 'urls' key, but not both.		 * The presence of a 'where' key implies a 'source' of 'document', while the presence of a 'urls' key implies		 * a 'source' of 'list'.		 */		if (			( ! isset( $rule['where'] ) && ! isset( $rule['urls'] ) ) ||			( isset( $rule['where'] ) && isset( $rule['urls'] ) )		) {			_doing_it_wrong(				__METHOD__,				sprintf(					/* translators: 1: allowed key, 2: alternative allowed key */					__( 'A speculation rule must include either a "%1$s" key or a "%2$s" key, but not both.' ),					'where',					'urls'				),				'6.8.0'			);			return false;		}		if ( isset( $rule['source'] ) ) {			if ( ! self::is_valid_source( $rule['source'] ) ) {				_doing_it_wrong(					__METHOD__,					sprintf(						/* translators: %s: invalid source value */						__( 'The value "%s" is not a valid source for a speculation rule.' ),						esc_html( $rule['source'] )					),					'6.8.0'				);				return false;			} 			if ( 'list' === $rule['source'] && isset( $rule['where'] ) ) {				_doing_it_wrong(					__METHOD__,					sprintf(

Changelog

Introduced in 6.8.0. Unchanged from 6.8.8 through 7.1.0.

  1. 6.8.8
  2. 6.9.7
  3. 7.0.4
  4. 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.