3 * WordPress Customize Setting classes
6 * @subpackage Customize
11 * Customize Setting class.
13 * Handles saving and sanitizing of settings.
17 * @see WP_Customize_Manager
19 class WP_Customize_Setting {
22 * @var WP_Customize_Manager
27 * Unique string identifier for the setting.
38 public $type = 'theme_mod';
41 * Capability required to edit this setting.
45 public $capability = 'edit_theme_options';
48 * Feature a theme is required to support to enable this setting.
53 public $theme_supports = '';
55 public $transport = 'refresh';
58 * Server-side sanitization callback for the setting's value.
62 public $sanitize_callback = '';
63 public $sanitize_js_callback = '';
66 * Whether or not the setting is initially dirty when created.
68 * This is used to ensure that a setting will be sent from the pane to the
69 * preview when loading the Customizer. Normally a setting only is synced to
70 * the preview if it has been changed. This allows the setting to be sent
77 public $dirty = false;
82 protected $id_data = array();
85 * Whether or not preview() was called.
91 protected $is_previewed = false;
94 * Cache of multidimensional values to improve performance.
101 protected static $aggregated_multidimensionals = array();
104 * Whether the multidimensional setting is aggregated.
110 protected $is_multidimensional_aggregated = false;
115 * Any supplied $args override class property defaults.
119 * @param WP_Customize_Manager $manager
120 * @param string $id An specific ID of the setting. Can be a
121 * theme mod or option name.
122 * @param array $args Setting arguments.
124 public function __construct( $manager, $id, $args = array() ) {
125 $keys = array_keys( get_object_vars( $this ) );
126 foreach ( $keys as $key ) {
127 if ( isset( $args[ $key ] ) ) {
128 $this->$key = $args[ $key ];
132 $this->manager = $manager;
135 // Parse the ID for array keys.
136 $this->id_data['keys'] = preg_split( '/\[/', str_replace( ']', '', $this->id ) );
137 $this->id_data['base'] = array_shift( $this->id_data['keys'] );
140 $this->id = $this->id_data[ 'base' ];
141 if ( ! empty( $this->id_data[ 'keys' ] ) ) {
142 $this->id .= '[' . implode( '][', $this->id_data['keys'] ) . ']';
145 if ( $this->sanitize_callback ) {
146 add_filter( "customize_sanitize_{$this->id}", $this->sanitize_callback, 10, 2 );
148 if ( $this->sanitize_js_callback ) {
149 add_filter( "customize_sanitize_js_{$this->id}", $this->sanitize_js_callback, 10, 2 );
152 if ( 'option' === $this->type || 'theme_mod' === $this->type ) {
153 // Other setting types can opt-in to aggregate multidimensional explicitly.
154 $this->aggregate_multidimensional();
156 // Allow option settings to indicate whether they should be autoloaded.
157 if ( 'option' === $this->type && isset( $args['autoload'] ) ) {
158 self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['autoload'] = $args['autoload'];
164 * Get parsed ID data for multidimensional setting.
170 * ID data for multidimensional setting.
172 * @type string $base ID base
173 * @type array $keys Keys for multidimensional array.
176 final public function id_data() {
177 return $this->id_data;
181 * Set up the setting for aggregated multidimensional values.
183 * When a multidimensional setting gets aggregated, all of its preview and update
184 * calls get combined into one call, greatly improving performance.
189 protected function aggregate_multidimensional() {
190 $id_base = $this->id_data['base'];
191 if ( ! isset( self::$aggregated_multidimensionals[ $this->type ] ) ) {
192 self::$aggregated_multidimensionals[ $this->type ] = array();
194 if ( ! isset( self::$aggregated_multidimensionals[ $this->type ][ $id_base ] ) ) {
195 self::$aggregated_multidimensionals[ $this->type ][ $id_base ] = array(
196 'previewed_instances' => array(), // Calling preview() will add the $setting to the array.
197 'preview_applied_instances' => array(), // Flags for which settings have had their values applied.
198 'root_value' => $this->get_root_value( array() ), // Root value for initial state, manipulated by preview and update calls.
202 if ( ! empty( $this->id_data['keys'] ) ) {
203 // Note the preview-applied flag is cleared at priority 9 to ensure it is cleared before a deferred-preview runs.
204 add_action( "customize_post_value_set_{$this->id}", array( $this, '_clear_aggregated_multidimensional_preview_applied_flag' ), 9 );
205 $this->is_multidimensional_aggregated = true;
210 * The ID for the current blog when the preview() method was called.
216 protected $_previewed_blog_id;
219 * Return true if the current blog is not the same as the previewed blog.
224 * @return bool If preview() has been called.
226 public function is_current_blog_previewed() {
227 if ( ! isset( $this->_previewed_blog_id ) ) {
230 return ( get_current_blog_id() === $this->_previewed_blog_id );
234 * Original non-previewed value stored by the preview method.
236 * @see WP_Customize_Setting::preview()
240 protected $_original_value;
243 * Add filters to supply the setting's value when accessed.
245 * If the setting already has a pre-existing value and there is no incoming
246 * post value for the setting, then this method will short-circuit since
247 * there is no change to preview.
250 * @since 4.4.0 Added boolean return value.
253 * @return bool False when preview short-circuits due no change needing to be previewed.
255 public function preview() {
256 if ( ! isset( $this->_previewed_blog_id ) ) {
257 $this->_previewed_blog_id = get_current_blog_id();
260 // Prevent re-previewing an already-previewed setting.
261 if ( $this->is_previewed ) {
265 $id_base = $this->id_data['base'];
266 $is_multidimensional = ! empty( $this->id_data['keys'] );
267 $multidimensional_filter = array( $this, '_multidimensional_preview_filter' );
270 * Check if the setting has a pre-existing value (an isset check),
271 * and if doesn't have any incoming post value. If both checks are true,
272 * then the preview short-circuits because there is nothing that needs
275 $undefined = new stdClass();
276 $needs_preview = ( $undefined !== $this->post_value( $undefined ) );
279 // Since no post value was defined, check if we have an initial value set.
280 if ( ! $needs_preview ) {
281 if ( $this->is_multidimensional_aggregated ) {
282 $root = self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'];
283 $value = $this->multidimensional_get( $root, $this->id_data['keys'], $undefined );
285 $default = $this->default;
286 $this->default = $undefined; // Temporarily set default to undefined so we can detect if existing value is set.
287 $value = $this->value();
288 $this->default = $default;
290 $needs_preview = ( $undefined === $value ); // Because the default needs to be supplied.
293 // If the setting does not need previewing now, defer to when it has a value to preview.
294 if ( ! $needs_preview ) {
295 if ( ! has_action( "customize_post_value_set_{$this->id}", array( $this, 'preview' ) ) ) {
296 add_action( "customize_post_value_set_{$this->id}", array( $this, 'preview' ) );
301 switch ( $this->type ) {
303 if ( ! $is_multidimensional ) {
304 add_filter( "theme_mod_{$id_base}", array( $this, '_preview_filter' ) );
306 if ( empty( self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'] ) ) {
307 // Only add this filter once for this ID base.
308 add_filter( "theme_mod_{$id_base}", $multidimensional_filter );
310 self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'][ $this->id ] = $this;
314 if ( ! $is_multidimensional ) {
315 add_filter( "pre_option_{$id_base}", array( $this, '_preview_filter' ) );
317 if ( empty( self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'] ) ) {
318 // Only add these filters once for this ID base.
319 add_filter( "option_{$id_base}", $multidimensional_filter );
320 add_filter( "default_option_{$id_base}", $multidimensional_filter );
322 self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'][ $this->id ] = $this;
328 * Fires when the {@see WP_Customize_Setting::preview()} method is called for settings
329 * not handled as theme_mods or options.
331 * The dynamic portion of the hook name, `$this->id`, refers to the setting ID.
335 * @param WP_Customize_Setting $this {@see WP_Customize_Setting} instance.
337 do_action( "customize_preview_{$this->id}", $this );
340 * Fires when the {@see WP_Customize_Setting::preview()} method is called for settings
341 * not handled as theme_mods or options.
343 * The dynamic portion of the hook name, `$this->type`, refers to the setting type.
347 * @param WP_Customize_Setting $this {@see WP_Customize_Setting} instance.
349 do_action( "customize_preview_{$this->type}", $this );
352 $this->is_previewed = true;
358 * Clear out the previewed-applied flag for a multidimensional-aggregated value whenever its post value is updated.
360 * This ensures that the new value will get sanitized and used the next time
361 * that <code>WP_Customize_Setting::_multidimensional_preview_filter()</code>
362 * is called for this setting.
366 * @see WP_Customize_Manager::set_post_value()
367 * @see WP_Customize_Setting::_multidimensional_preview_filter()
369 final public function _clear_aggregated_multidimensional_preview_applied_flag() {
370 unset( self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['preview_applied_instances'][ $this->id ] );
374 * Callback function to filter non-multidimensional theme mods and options.
376 * If switch_to_blog() was called after the preview() method, and the current
377 * blog is now not the same blog, then this method does a no-op and returns
378 * the original value.
382 * @param mixed $original Old value.
383 * @return mixed New or old value.
385 public function _preview_filter( $original ) {
386 if ( ! $this->is_current_blog_previewed() ) {
390 $undefined = new stdClass(); // Symbol hack.
391 $post_value = $this->post_value( $undefined );
392 if ( $undefined !== $post_value ) {
393 $value = $post_value;
396 * Note that we don't use $original here because preview() will
397 * not add the filter in the first place if it has an initial value
398 * and there is no post value.
400 $value = $this->default;
406 * Callback function to filter multidimensional theme mods and options.
408 * For all multidimensional settings of a given type, the preview filter for
409 * the first setting previewed will be used to apply the values for the others.
414 * @see WP_Customize_Setting::$aggregated_multidimensionals
415 * @param mixed $original Original root value.
416 * @return mixed New or old value.
418 final public function _multidimensional_preview_filter( $original ) {
419 if ( ! $this->is_current_blog_previewed() ) {
423 $id_base = $this->id_data['base'];
425 // If no settings have been previewed yet (which should not be the case, since $this is), just pass through the original value.
426 if ( empty( self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'] ) ) {
430 foreach ( self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'] as $previewed_setting ) {
431 // Skip applying previewed value for any settings that have already been applied.
432 if ( ! empty( self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['preview_applied_instances'][ $previewed_setting->id ] ) ) {
436 // Do the replacements of the posted/default sub value into the root value.
437 $value = $previewed_setting->post_value( $previewed_setting->default );
438 $root = self::$aggregated_multidimensionals[ $previewed_setting->type ][ $id_base ]['root_value'];
439 $root = $previewed_setting->multidimensional_replace( $root, $previewed_setting->id_data['keys'], $value );
440 self::$aggregated_multidimensionals[ $previewed_setting->type ][ $id_base ]['root_value'] = $root;
442 // Mark this setting having been applied so that it will be skipped when the filter is called again.
443 self::$aggregated_multidimensionals[ $previewed_setting->type ][ $id_base ]['preview_applied_instances'][ $previewed_setting->id ] = true;
446 return self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'];
450 * Check user capabilities and theme supports, and then save
451 * the value of the setting.
455 * @return false|void False if cap check fails or value isn't set.
457 final public function save() {
458 $value = $this->post_value();
460 if ( ! $this->check_capabilities() || ! isset( $value ) )
464 * Fires when the WP_Customize_Setting::save() method is called.
466 * The dynamic portion of the hook name, `$this->id_data['base']` refers to
467 * the base slug of the setting name.
471 * @param WP_Customize_Setting $this {@see WP_Customize_Setting} instance.
473 do_action( 'customize_save_' . $this->id_data[ 'base' ], $this );
475 $this->update( $value );
479 * Fetch and sanitize the $_POST value for the setting.
483 * @param mixed $default A default value which is used as a fallback. Default is null.
484 * @return mixed The default value on failure, otherwise the sanitized value.
486 final public function post_value( $default = null ) {
487 return $this->manager->post_value( $this, $default );
495 * @param string|array $value The value to sanitize.
496 * @return string|array|null Null if an input isn't valid, otherwise the sanitized value.
498 public function sanitize( $value ) {
499 $value = wp_unslash( $value );
502 * Filter a Customize setting value in un-slashed form.
506 * @param mixed $value Value of the setting.
507 * @param WP_Customize_Setting $this WP_Customize_Setting instance.
509 return apply_filters( "customize_sanitize_{$this->id}", $value, $this );
513 * Get the root value for a setting, especially for multidimensional ones.
518 * @param mixed $default Value to return if root does not exist.
521 protected function get_root_value( $default = null ) {
522 $id_base = $this->id_data['base'];
523 if ( 'option' === $this->type ) {
524 return get_option( $id_base, $default );
525 } else if ( 'theme_mod' ) {
526 return get_theme_mod( $id_base, $default );
529 * Any WP_Customize_Setting subclass implementing aggregate multidimensional
530 * will need to override this method to obtain the data from the appropriate
538 * Set the root value for a setting, especially for multidimensional ones.
543 * @param mixed $value Value to set as root of multidimensional setting.
544 * @return bool Whether the multidimensional root was updated successfully.
546 protected function set_root_value( $value ) {
547 $id_base = $this->id_data['base'];
548 if ( 'option' === $this->type ) {
550 if ( isset( self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['autoload'] ) ) {
551 $autoload = self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['autoload'];
553 return update_option( $id_base, $value, $autoload );
554 } else if ( 'theme_mod' ) {
555 set_theme_mod( $id_base, $value );
559 * Any WP_Customize_Setting subclass implementing aggregate multidimensional
560 * will need to override this method to obtain the data from the appropriate
568 * Save the value of the setting, using the related API.
572 * @param mixed $value The value to update.
573 * @return bool The result of saving the value.
575 protected function update( $value ) {
576 $id_base = $this->id_data['base'];
577 if ( 'option' === $this->type || 'theme_mod' === $this->type ) {
578 if ( ! $this->is_multidimensional_aggregated ) {
579 return $this->set_root_value( $value );
581 $root = self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'];
582 $root = $this->multidimensional_replace( $root, $this->id_data['keys'], $value );
583 self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'] = $root;
584 return $this->set_root_value( $root );
588 * Fires when the {@see WP_Customize_Setting::update()} method is called for settings
589 * not handled as theme_mods or options.
591 * The dynamic portion of the hook name, `$this->type`, refers to the type of setting.
595 * @param mixed $value Value of the setting.
596 * @param WP_Customize_Setting $this WP_Customize_Setting instance.
598 do_action( "customize_update_{$this->type}", $value, $this );
600 return has_action( "customize_update_{$this->type}" );
608 * @deprecated 4.4.0 Deprecated in favor of update() method.
610 protected function _update_theme_mod() {
611 _deprecated_function( __METHOD__, '4.4.0', __CLASS__ . '::update()' );
618 * @deprecated 4.4.0 Deprecated in favor of update() method.
620 protected function _update_option() {
621 _deprecated_function( __METHOD__, '4.4.0', __CLASS__ . '::update()' );
625 * Fetch the value of the setting.
629 * @return mixed The value.
631 public function value() {
632 $id_base = $this->id_data['base'];
633 $is_core_type = ( 'option' === $this->type || 'theme_mod' === $this->type );
635 if ( ! $is_core_type && ! $this->is_multidimensional_aggregated ) {
636 $value = $this->get_root_value( $this->default );
639 * Filter a Customize setting value not handled as a theme_mod or option.
641 * The dynamic portion of the hook name, `$this->id_date['base']`, refers to
642 * the base slug of the setting name.
644 * For settings handled as theme_mods or options, see those corresponding
645 * functions for available hooks.
649 * @param mixed $default The setting default value. Default empty.
651 $value = apply_filters( "customize_value_{$id_base}", $value );
652 } else if ( $this->is_multidimensional_aggregated ) {
653 $root_value = self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'];
654 $value = $this->multidimensional_get( $root_value, $this->id_data['keys'], $this->default );
656 $value = $this->get_root_value( $this->default );
662 * Sanitize the setting's value for use in JavaScript.
666 * @return mixed The requested escaped value.
668 public function js_value() {
671 * Filter a Customize setting value for use in JavaScript.
673 * The dynamic portion of the hook name, `$this->id`, refers to the setting ID.
677 * @param mixed $value The setting value.
678 * @param WP_Customize_Setting $this {@see WP_Customize_Setting} instance.
680 $value = apply_filters( "customize_sanitize_js_{$this->id}", $this->value(), $this );
682 if ( is_string( $value ) )
683 return html_entity_decode( $value, ENT_QUOTES, 'UTF-8');
689 * Validate user capabilities whether the theme supports the setting.
693 * @return bool False if theme doesn't support the setting or user can't change setting, otherwise true.
695 final public function check_capabilities() {
696 if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) )
699 if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) )
706 * Multidimensional helper function.
712 * @param bool $create Default is false.
713 * @return array|void Keys are 'root', 'node', and 'key'.
715 final protected function multidimensional( &$root, $keys, $create = false ) {
716 if ( $create && empty( $root ) )
719 if ( ! isset( $root ) || empty( $keys ) )
722 $last = array_pop( $keys );
725 foreach ( $keys as $key ) {
726 if ( $create && ! isset( $node[ $key ] ) )
727 $node[ $key ] = array();
729 if ( ! is_array( $node ) || ! isset( $node[ $key ] ) )
732 $node = &$node[ $key ];
736 if ( ! is_array( $node ) ) {
737 // account for an array overriding a string or object value
740 if ( ! isset( $node[ $last ] ) ) {
741 $node[ $last ] = array();
745 if ( ! isset( $node[ $last ] ) )
756 * Will attempt to replace a specific value in a multidimensional array.
762 * @param mixed $value The value to update.
765 final protected function multidimensional_replace( $root, $keys, $value ) {
766 if ( ! isset( $value ) )
768 elseif ( empty( $keys ) ) // If there are no keys, we're replacing the root.
771 $result = $this->multidimensional( $root, $keys, true );
773 if ( isset( $result ) )
774 $result['node'][ $result['key'] ] = $value;
780 * Will attempt to fetch a specific value from a multidimensional array.
786 * @param mixed $default A default value which is used as a fallback. Default is null.
787 * @return mixed The requested value or the default value.
789 final protected function multidimensional_get( $root, $keys, $default = null ) {
790 if ( empty( $keys ) ) // If there are no keys, test the root.
791 return isset( $root ) ? $root : $default;
793 $result = $this->multidimensional( $root, $keys );
794 return isset( $result ) ? $result['node'][ $result['key'] ] : $default;
798 * Will attempt to check if a specific value in a multidimensional array is set.
804 * @return bool True if value is set, false if not.
806 final protected function multidimensional_isset( $root, $keys ) {
807 $result = $this->multidimensional_get( $root, $keys );
808 return isset( $result );
812 /** WP_Customize_Filter_Setting class */
813 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-filter-setting.php' );
815 /** WP_Customize_Header_Image_Setting class */
816 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-header-image-setting.php' );
818 /** WP_Customize_Background_Image_Setting class */
819 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-background-image-setting.php' );
821 /** WP_Customize_Nav_Menu_Item_Setting class */
822 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-item-setting.php' );
824 /** WP_Customize_Nav_Menu_Setting class */
825 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-setting.php' );