WP_Customize_Setting::preview(): bool
- Since
- 3.4.0, 4.4.0
- Source
wp-includes/class-wp-customize-setting.php:324
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
- Scaling
- Constant
- Instructions
- 5–81
- Plugin surface
- 2 hooks
- Called by
- 0
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 165.
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.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
do_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.
| When | Instructions | Calls it makes |
|---|---|---|
isset($value) | 5 | none |
!isset($value) | 9 | get_current_blog_id() |
isset($value) && $undefined !== false | 37–48 | ->post_value() |
!isset($value) && $undefined !== false | 41–52 | get_current_blog_id(), ->post_value() |
isset($value) && $undefined !== false | 41–46 | ->post_value(), do_action(), do_action() |
isset($value) && $undefined === false | 44–60 | ->post_value(), ->value() |
!isset($value) && $undefined !== false | 45–50 | get_current_blog_id(), ->post_value(), do_action(), do_action() |
!isset($value) && $undefined === false | 48–64 | get_current_blog_id(), ->post_value(), ->value() |
isset($value) && $undefined !== false && !$value && empty($value) | 49–51 | ->post_value(), add_filter() |
isset($value) && $undefined === false | 51–67 | ->post_value(), ->multidimensional_get() |
!isset($value) && $undefined !== false && !$value && empty($value) | 53–55 | get_current_blog_id(), ->post_value(), add_filter() |
isset($value) && $undefined === false | 53–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 === false | 55–71 | get_current_blog_id(), ->post_value(), ->multidimensional_get() |
!isset($value) && $undefined === false | 57–62 | get_current_blog_id(), ->post_value(), ->value(), do_action(), do_action() |
!isset($value) && $undefined !== false && !$value && empty($value) | 58–62 | get_current_blog_id(), ->post_value(), add_filter(), add_filter() |
isset($value) && $undefined === false | 60–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 === false | 64–69 | get_current_blog_id(), ->post_value(), ->multidimensional_get(), do_action(), do_action() |
!isset($value) && $undefined === false && !$value && empty($value) | 65–67 | get_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–74 | get_current_blog_id(), ->post_value(), ->value(), add_filter(), add_filter() |
!isset($value) && $undefined === false && !$value && empty($value) | 72–74 | get_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–81 | get_current_blog_id(), ->post_value(), ->multidimensional_get(), add_filter(), add_filter() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 165 | 5–81 | 13 | |
| 8.5 | 165 | 5–81 | 13 | |
| 8.4 | 165 | 5–81 | 13 | |
| 8.3 | 165 | 5–81 | 13 | |
| 8.2 | 165 | 5–81 | 13 | 1 more instruction than PHP 8.1 |
| 8.1 | 164 | 5–81 | 13 | |
| 7.4 | 164 | 5–81 | 13 |
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:
- do_action( customize_preview_{$this->id} )actionline 405 (+81 into the body)
Fires when the WP_Customize_Setting::preview() method is called for settings not handled as theme_mods or options.
- do_action( customize_preview_{$this->type} )actionline 417 (+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
- get_current_blog_id()Retrieves the current site ID.
- has_action()Checks if any action has been registered for a hook.
- add_action()Adds a callback function to an action hook.
- add_filter()Adds a callback function to a filter hook.
- do_action()Calls the callback functions that have been added to an action hook.
- stdClass::__construct()
- WP_Customize_Setting::post_value()Fetch and sanitize the $_POST value for the setting.
- WP_Customize_Setting::multidimensional_get()Will attempt to fetch a specific value from a multidimensional array.
- WP_Customize_Setting::value()Fetch the value of the setting.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.