wppaste
WordPress

WP_Customize_Setting::preview(): bool

Since
3.4.0, 4.4.0
Source
wp-includes/class-wp-customize-setting.php:319
Add filters to supply the setting's value when accessed.

Description

If the setting already has a pre-existing value and there is no incoming post value for the setting, then this method will short-circuit since there is no change to preview.

Compatibility

WordPress
since 4.4.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Return value

bool
False when preview short-circuits due no change needing to be previewed.

Performance profile

How much work a call to WP_Customize_Setting::preview() 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
5–81

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

Plugin surface
2 hooks

Third-party callbacks on 'customize_preview_{$this->id}', 'customize_preview_{$this->type}' run inside this call, and their cost is not bounded by anything here.

Called by
0

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

What it touches

  • hookthird-party callbacksdo_action()called directly

What one call costs · 26 distinct outcomes

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

WhenInstructionsCalls it makes
isset($value)5none
!isset($value)9get_current_blog_id()
isset($value) && $undefined !== false37–48->post_value()
!isset($value) && $undefined !== false41–52get_current_blog_id(), ->post_value()
isset($value) && $undefined !== false41–46->post_value(), do_action(), do_action()
isset($value) && $undefined === false44–60->post_value(), ->value()
!isset($value) && $undefined !== false45–50get_current_blog_id(), ->post_value(), do_action(), do_action()
!isset($value) && $undefined === false48–64get_current_blog_id(), ->post_value(), ->value()
isset($value) && $undefined !== false && !$value && empty($value)49–51->post_value(), add_filter()
isset($value) && $undefined === false51–67->post_value(), ->multidimensional_get()
!isset($value) && $undefined !== false && !$value && empty($value)53–55get_current_blog_id(), ->post_value(), add_filter()
isset($value) && $undefined === false53–58->post_value(), ->value(), do_action(), do_action()
14 further outcomes, up to 81 instructions
isset($value) && $undefined !== false && !$value && empty($value)54–58->post_value(), add_filter(), add_filter()
!isset($value) && $undefined === false55–71get_current_blog_id(), ->post_value(), ->multidimensional_get()
!isset($value) && $undefined === false57–62get_current_blog_id(), ->post_value(), ->value(), do_action(), do_action()
!isset($value) && $undefined !== false && !$value && empty($value)58–62get_current_blog_id(), ->post_value(), add_filter(), add_filter()
isset($value) && $undefined === false60–65->post_value(), ->multidimensional_get(), do_action(), do_action()
isset($value) && $undefined === false && !$value && empty($value)61–63->post_value(), ->value(), add_filter()
!isset($value) && $undefined === false64–69get_current_blog_id(), ->post_value(), ->multidimensional_get(), do_action(), do_action()
!isset($value) && $undefined === false && !$value && empty($value)65–67get_current_blog_id(), ->post_value(), ->value(), add_filter()
isset($value) && $undefined === false && !$value && empty($value)66–70->post_value(), ->value(), add_filter(), add_filter()
isset($value) && $undefined === false && !$value && empty($value)68–70->post_value(), ->multidimensional_get(), add_filter()
!isset($value) && $undefined === false && !$value && empty($value)70–74get_current_blog_id(), ->post_value(), ->value(), add_filter(), add_filter()
!isset($value) && $undefined === false && !$value && empty($value)72–74get_current_blog_id(), ->post_value(), ->multidimensional_get(), add_filter()
isset($value) && $undefined === false && !$value && empty($value)73–77->post_value(), ->multidimensional_get(), add_filter(), add_filter()
!isset($value) && $undefined === false && !$value && empty($value)77–81get_current_blog_id(), ->post_value(), ->multidimensional_get(), add_filter(), add_filter()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1655–8113
8.51655–8113
8.41655–8113
8.31655–8113
8.21655–81131 more instruction than PHP 8.1
8.11645–8113
7.41645–8113

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.

Hooks and filters fired · 2

2 hooks fire while WP_Customize_Setting::preview() runs, in this order:

  1. do_action( customize_preview_{$this->id} )actionline 400 (+81 into the body)

    Fires when the WP_Customize_Setting::preview() method is called for settings not handled as theme_mods or options.

  2. do_action( customize_preview_{$this->type} )actionline 412 (+93 into the body)

    Fires when the WP_Customize_Setting::preview() method is called for settings not handled as theme_mods or options.

Uses · 9

Source code

	public function preview() {		if ( ! isset( $this->_previewed_blog_id ) ) {			$this->_previewed_blog_id = get_current_blog_id();		} 		// Prevent re-previewing an already-previewed setting.		if ( $this->is_previewed ) {			return true;		} 		$id_base                 = $this->id_data['base'];		$is_multidimensional     = ! empty( $this->id_data['keys'] );		$multidimensional_filter = array( $this, '_multidimensional_preview_filter' ); 		/*		 * Check if the setting has a pre-existing value (an isset check),		 * and if doesn't have any incoming post value. If both checks are true,		 * then the preview short-circuits because there is nothing that needs		 * to be previewed.		 */		$undefined     = new stdClass();		$needs_preview = ( $undefined !== $this->post_value( $undefined ) );		$value         = null; 		// Since no post value was defined, check if we have an initial value set.		if ( ! $needs_preview ) {			if ( $this->is_multidimensional_aggregated ) {				$root  = self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'];				$value = $this->multidimensional_get( $root, $this->id_data['keys'], $undefined );			} else {				$default       = $this->default;				$this->default = $undefined; // Temporarily set default to undefined so we can detect if existing value is set.				$value         = $this->value();				$this->default = $default;			}			$needs_preview = ( $undefined === $value ); // Because the default needs to be supplied.		} 		// If the setting does not need previewing now, defer to when it has a value to preview.		if ( ! $needs_preview ) {			if ( ! has_action( "customize_post_value_set_{$this->id}", array( $this, 'preview' ) ) ) {				add_action( "customize_post_value_set_{$this->id}", array( $this, 'preview' ) );			}			return false;		} 		switch ( $this->type ) {			case 'theme_mod':				if ( ! $is_multidimensional ) {					add_filter( "theme_mod_{$id_base}", array( $this, '_preview_filter' ) );				} else {					if ( empty( self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'] ) ) {						// Only add this filter once for this ID base.						add_filter( "theme_mod_{$id_base}", $multidimensional_filter );					}					self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'][ $this->id ] = $this;				}				break;			case 'option':				if ( ! $is_multidimensional ) {					add_filter( "pre_option_{$id_base}", array( $this, '_preview_filter' ) );				} else {					if ( empty( self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'] ) ) {						// Only add these filters once for this ID base.						add_filter( "option_{$id_base}", $multidimensional_filter );						add_filter( "default_option_{$id_base}", $multidimensional_filter );					}					self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'][ $this->id ] = $this;				}				break;			default:				/**				 * Fires when the WP_Customize_Setting::preview() method is called for settings				 * not handled as theme_mods or options.				 *				 * The dynamic portion of the hook name, `$this->id`, refers to the setting ID.				 *				 * @since 3.4.0				 *				 * @param WP_Customize_Setting $setting WP_Customize_Setting instance.

Changelog

Introduced in 3.4.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

4.4.0
Added boolean return value.from the docblock
3.4.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/class-wp-customize-setting.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.