3 * Customize Setting Class.
5 * Handles saving and sanitizing of settings.
8 * @subpackage Customize
11 class WP_Customize_Setting {
14 * @var WP_Customize_Manager
28 public $type = 'theme_mod';
31 * Capability required to edit this setting.
35 public $capability = 'edit_theme_options';
38 * Feature a theme is required to support to enable this setting.
43 public $theme_supports = '';
45 public $transport = 'refresh';
48 * Server-side sanitization callback for the setting's value.
52 public $sanitize_callback = '';
53 public $sanitize_js_callback = '';
55 protected $id_data = array();
58 * Cached and sanitized $_POST value for the setting.
68 * Any supplied $args override class property defaults.
72 * @param WP_Customize_Manager $manager
73 * @param string $id An specific ID of the setting. Can be a
74 * theme mod or option name.
75 * @param array $args Setting arguments.
76 * @return WP_Customize_Setting $setting
78 public function __construct( $manager, $id, $args = array() ) {
79 $keys = array_keys( get_object_vars( $this ) );
80 foreach ( $keys as $key ) {
81 if ( isset( $args[ $key ] ) )
82 $this->$key = $args[ $key ];
85 $this->manager = $manager;
88 // Parse the ID for array keys.
89 $this->id_data[ 'keys' ] = preg_split( '/\[/', str_replace( ']', '', $this->id ) );
90 $this->id_data[ 'base' ] = array_shift( $this->id_data[ 'keys' ] );
93 $this->id = $this->id_data[ 'base' ];
94 if ( ! empty( $this->id_data[ 'keys' ] ) )
95 $this->id .= '[' . implode( '][', $this->id_data[ 'keys' ] ) . ']';
97 if ( $this->sanitize_callback )
98 add_filter( "customize_sanitize_{$this->id}", $this->sanitize_callback, 10, 2 );
100 if ( $this->sanitize_js_callback )
101 add_filter( "customize_sanitize_js_{$this->id}", $this->sanitize_js_callback, 10, 2 );
106 protected $_original_value;
109 * Handle previewing the setting.
113 public function preview() {
114 if ( ! isset( $this->_original_value ) ) {
115 $this->_original_value = $this->value();
118 switch( $this->type ) {
120 add_filter( 'theme_mod_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
123 if ( empty( $this->id_data[ 'keys' ] ) )
124 add_filter( 'pre_option_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
126 add_filter( 'option_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
127 add_filter( 'default_option_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
133 * Fires when the {@see WP_Customize_Setting::preview()} method is called for settings
134 * not handled as theme_mods or options.
136 * The dynamic portion of the hook name, `$this->id`, refers to the setting ID.
140 * @param WP_Customize_Setting $this {@see WP_Customize_Setting} instance.
142 do_action( "customize_preview_{$this->id}", $this );
145 * Fires when the {@see WP_Customize_Setting::preview()} method is called for settings
146 * not handled as theme_mods or options.
148 * The dynamic portion of the hook name, `$this->type`, refers to the setting type.
152 * @param WP_Customize_Setting $this {@see WP_Customize_Setting} instance.
154 do_action( "customize_preview_{$this->type}", $this );
159 * Callback function to filter the theme mods and options.
162 * @uses WP_Customize_Setting::multidimensional_replace()
164 * @param mixed $original Old value.
165 * @return mixed New or old value.
167 public function _preview_filter( $original ) {
168 $undefined = new stdClass(); // symbol hack
169 $post_value = $this->manager->post_value( $this, $undefined );
170 if ( $undefined === $post_value ) {
171 $value = $this->_original_value;
173 $value = $post_value;
176 return $this->multidimensional_replace( $original, $this->id_data['keys'], $value );
180 * Check user capabilities and theme supports, and then save
181 * the value of the setting.
185 * @return false|null False if cap check fails or value isn't set.
187 public final function save() {
188 $value = $this->post_value();
190 if ( ! $this->check_capabilities() || ! isset( $value ) )
194 * Fires when the WP_Customize_Setting::save() method is called.
196 * The dynamic portion of the hook name, `$this->id_data['base']` refers to
197 * the base slug of the setting name.
201 * @param WP_Customize_Setting $this {@see WP_Customize_Setting} instance.
203 do_action( 'customize_save_' . $this->id_data[ 'base' ], $this );
205 $this->update( $value );
209 * Fetch and sanitize the $_POST value for the setting.
213 * @param mixed $default A default value which is used as a fallback. Default is null.
214 * @return mixed The default value on failure, otherwise the sanitized value.
216 public final function post_value( $default = null ) {
217 // Check for a cached value
218 if ( isset( $this->_post_value ) )
219 return $this->_post_value;
221 // Call the manager for the post value
222 $result = $this->manager->post_value( $this );
224 if ( isset( $result ) )
225 return $this->_post_value = $result;
235 * @param mixed $value The value to sanitize.
236 * @return mixed Null if an input isn't valid, otherwise the sanitized value.
238 public function sanitize( $value ) {
239 $value = wp_unslash( $value );
242 * Filter a Customize setting value in un-slashed form.
246 * @param mixed $value Value of the setting.
247 * @param WP_Customize_Setting $this WP_Customize_Setting instance.
249 return apply_filters( "customize_sanitize_{$this->id}", $value, $this );
253 * Save the value of the setting, using the related API.
257 * @param mixed $value The value to update.
258 * @return mixed The result of saving the value.
260 protected function update( $value ) {
261 switch( $this->type ) {
263 return $this->_update_theme_mod( $value );
266 return $this->_update_option( $value );
271 * Fires when the {@see WP_Customize_Setting::update()} method is called for settings
272 * not handled as theme_mods or options.
274 * The dynamic portion of the hook name, `$this->type`, refers to the type of setting.
278 * @param mixed $value Value of the setting.
279 * @param WP_Customize_Setting $this WP_Customize_Setting instance.
281 return do_action( 'customize_update_' . $this->type, $value, $this );
286 * Update the theme mod from the value of the parameter.
290 * @param mixed $value The value to update.
291 * @return mixed The result of saving the value.
293 protected function _update_theme_mod( $value ) {
294 // Handle non-array theme mod.
295 if ( empty( $this->id_data[ 'keys' ] ) )
296 return set_theme_mod( $this->id_data[ 'base' ], $value );
298 // Handle array-based theme mod.
299 $mods = get_theme_mod( $this->id_data[ 'base' ] );
300 $mods = $this->multidimensional_replace( $mods, $this->id_data[ 'keys' ], $value );
301 if ( isset( $mods ) )
302 return set_theme_mod( $this->id_data[ 'base' ], $mods );
306 * Update the option from the value of the setting.
310 * @param mixed $value The value to update.
311 * @return bool|null The result of saving the value.
313 protected function _update_option( $value ) {
314 // Handle non-array option.
315 if ( empty( $this->id_data[ 'keys' ] ) )
316 return update_option( $this->id_data[ 'base' ], $value );
318 // Handle array-based options.
319 $options = get_option( $this->id_data[ 'base' ] );
320 $options = $this->multidimensional_replace( $options, $this->id_data[ 'keys' ], $value );
321 if ( isset( $options ) )
322 return update_option( $this->id_data[ 'base' ], $options );
326 * Fetch the value of the setting.
330 * @return mixed The value.
332 public function value() {
333 // Get the callback that corresponds to the setting type.
334 switch( $this->type ) {
336 $function = 'get_theme_mod';
339 $function = 'get_option';
344 * Filter a Customize setting value not handled as a theme_mod or option.
346 * The dynamic portion of the hook name, `$this->id_date['base']`, refers to
347 * the base slug of the setting name.
349 * For settings handled as theme_mods or options, see those corresponding
350 * functions for available hooks.
354 * @param mixed $default The setting default value. Default empty.
356 return apply_filters( 'customize_value_' . $this->id_data[ 'base' ], $this->default );
359 // Handle non-array value
360 if ( empty( $this->id_data[ 'keys' ] ) )
361 return $function( $this->id_data[ 'base' ], $this->default );
363 // Handle array-based value
364 $values = $function( $this->id_data[ 'base' ] );
365 return $this->multidimensional_get( $values, $this->id_data[ 'keys' ], $this->default );
369 * Sanitize the setting's value for use in JavaScript.
373 * @return mixed The requested escaped value.
375 public function js_value() {
378 * Filter a Customize setting value for use in JavaScript.
380 * The dynamic portion of the hook name, `$this->id`, refers to the setting ID.
384 * @param mixed $value The setting value.
385 * @param WP_Customize_Setting $this {@see WP_Customize_Setting} instance.
387 $value = apply_filters( "customize_sanitize_js_{$this->id}", $this->value(), $this );
389 if ( is_string( $value ) )
390 return html_entity_decode( $value, ENT_QUOTES, 'UTF-8');
396 * Validate user capabilities whether the theme supports the setting.
400 * @return bool False if theme doesn't support the setting or user can't change setting, otherwise true.
402 public final function check_capabilities() {
403 if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) )
406 if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) )
413 * Multidimensional helper function.
419 * @param bool $create Default is false.
420 * @return null|array Keys are 'root', 'node', and 'key'.
422 final protected function multidimensional( &$root, $keys, $create = false ) {
423 if ( $create && empty( $root ) )
426 if ( ! isset( $root ) || empty( $keys ) )
429 $last = array_pop( $keys );
432 foreach ( $keys as $key ) {
433 if ( $create && ! isset( $node[ $key ] ) )
434 $node[ $key ] = array();
436 if ( ! is_array( $node ) || ! isset( $node[ $key ] ) )
439 $node = &$node[ $key ];
443 if ( ! is_array( $node ) ) {
444 // account for an array overriding a string or object value
447 if ( ! isset( $node[ $last ] ) ) {
448 $node[ $last ] = array();
452 if ( ! isset( $node[ $last ] ) )
463 * Will attempt to replace a specific value in a multidimensional array.
469 * @param mixed $value The value to update.
472 final protected function multidimensional_replace( $root, $keys, $value ) {
473 if ( ! isset( $value ) )
475 elseif ( empty( $keys ) ) // If there are no keys, we're replacing the root.
478 $result = $this->multidimensional( $root, $keys, true );
480 if ( isset( $result ) )
481 $result['node'][ $result['key'] ] = $value;
487 * Will attempt to fetch a specific value from a multidimensional array.
493 * @param mixed $default A default value which is used as a fallback. Default is null.
494 * @return mixed The requested value or the default value.
496 final protected function multidimensional_get( $root, $keys, $default = null ) {
497 if ( empty( $keys ) ) // If there are no keys, test the root.
498 return isset( $root ) ? $root : $default;
500 $result = $this->multidimensional( $root, $keys );
501 return isset( $result ) ? $result['node'][ $result['key'] ] : $default;
505 * Will attempt to check if a specific value in a multidimensional array is set.
511 * @return bool True if value is set, false if not.
513 final protected function multidimensional_isset( $root, $keys ) {
514 $result = $this->multidimensional_get( $root, $keys );
515 return isset( $result );
520 * A setting that is used to filter a value, but will not save the results.
522 * Results should be properly handled using another setting or callback.
525 * @subpackage Customize
528 class WP_Customize_Filter_Setting extends WP_Customize_Setting {
533 public function update( $value ) {}
537 * A setting that is used to filter a value, but will not save the results.
539 * Results should be properly handled using another setting or callback.
542 * @subpackage Customize
545 final class WP_Customize_Header_Image_Setting extends WP_Customize_Setting {
546 public $id = 'header_image_data';
553 public function update( $value ) {
554 global $custom_image_header;
556 // If the value doesn't exist (removed or random),
557 // use the header_image value.
559 $value = $this->manager->get_setting('header_image')->post_value();
561 if ( is_array( $value ) && isset( $value['choice'] ) )
562 $custom_image_header->set_header_image( $value['choice'] );
564 $custom_image_header->set_header_image( $value );
569 * Class WP_Customize_Background_Image_Setting
572 * @subpackage Customize
575 final class WP_Customize_Background_Image_Setting extends WP_Customize_Setting {
576 public $id = 'background_image_thumb';
583 public function update( $value ) {
584 remove_theme_mod( 'background_image_thumb' );