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 );
107 * Handle previewing the setting.
111 public function preview() {
112 switch( $this->type ) {
114 add_filter( 'theme_mod_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
117 if ( empty( $this->id_data[ 'keys' ] ) )
118 add_filter( 'pre_option_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
120 add_filter( 'option_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
121 add_filter( 'default_option_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
127 * Fires when the {@see WP_Customize_Setting::preview()} method is called for settings
128 * not handled as theme_mods or options.
130 * The dynamic portion of the hook name, `$this->id`, refers to the setting ID.
134 * @param WP_Customize_Setting $this {@see WP_Customize_Setting} instance.
136 do_action( "customize_preview_{$this->id}", $this );
139 * Fires when the {@see WP_Customize_Setting::preview()} method is called for settings
140 * not handled as theme_mods or options.
142 * The dynamic portion of the hook name, `$this->type`, refers to the setting type.
146 * @param WP_Customize_Setting $this {@see WP_Customize_Setting} instance.
148 do_action( "customize_preview_{$this->type}", $this );
153 * Callback function to filter the theme mods and options.
156 * @uses WP_Customize_Setting::multidimensional_replace()
158 * @param mixed $original Old value.
159 * @return mixed New or old value.
161 public function _preview_filter( $original ) {
162 return $this->multidimensional_replace( $original, $this->id_data[ 'keys' ], $this->post_value() );
166 * Check user capabilities and theme supports, and then save
167 * the value of the setting.
171 * @return false|null False if cap check fails or value isn't set.
173 public final function save() {
174 $value = $this->post_value();
176 if ( ! $this->check_capabilities() || ! isset( $value ) )
180 * Fires when the WP_Customize_Setting::save() method is called.
182 * The dynamic portion of the hook name, `$this->id_data['base']` refers to
183 * the base slug of the setting name.
187 * @param WP_Customize_Setting $this {@see WP_Customize_Setting} instance.
189 do_action( 'customize_save_' . $this->id_data[ 'base' ], $this );
191 $this->update( $value );
195 * Fetch and sanitize the $_POST value for the setting.
199 * @param mixed $default A default value which is used as a fallback. Default is null.
200 * @return mixed The default value on failure, otherwise the sanitized value.
202 public final function post_value( $default = null ) {
203 // Check for a cached value
204 if ( isset( $this->_post_value ) )
205 return $this->_post_value;
207 // Call the manager for the post value
208 $result = $this->manager->post_value( $this );
210 if ( isset( $result ) )
211 return $this->_post_value = $result;
221 * @param mixed $value The value to sanitize.
222 * @return mixed Null if an input isn't valid, otherwise the sanitized value.
224 public function sanitize( $value ) {
225 $value = wp_unslash( $value );
228 * Filter a Customize setting value in un-slashed form.
232 * @param mixed $value Value of the setting.
233 * @param WP_Customize_Setting $this WP_Customize_Setting instance.
235 return apply_filters( "customize_sanitize_{$this->id}", $value, $this );
239 * Save the value of the setting, using the related API.
243 * @param mixed $value The value to update.
244 * @return mixed The result of saving the value.
246 protected function update( $value ) {
247 switch( $this->type ) {
249 return $this->_update_theme_mod( $value );
252 return $this->_update_option( $value );
257 * Fires when the {@see WP_Customize_Setting::update()} method is called for settings
258 * not handled as theme_mods or options.
260 * The dynamic portion of the hook name, `$this->type`, refers to the type of setting.
264 * @param mixed $value Value of the setting.
265 * @param WP_Customize_Setting $this WP_Customize_Setting instance.
267 return do_action( 'customize_update_' . $this->type, $value, $this );
272 * Update the theme mod from the value of the parameter.
276 * @param mixed $value The value to update.
277 * @return mixed The result of saving the value.
279 protected function _update_theme_mod( $value ) {
280 // Handle non-array theme mod.
281 if ( empty( $this->id_data[ 'keys' ] ) )
282 return set_theme_mod( $this->id_data[ 'base' ], $value );
284 // Handle array-based theme mod.
285 $mods = get_theme_mod( $this->id_data[ 'base' ] );
286 $mods = $this->multidimensional_replace( $mods, $this->id_data[ 'keys' ], $value );
287 if ( isset( $mods ) )
288 return set_theme_mod( $this->id_data[ 'base' ], $mods );
292 * Update the option from the value of the setting.
296 * @param mixed $value The value to update.
297 * @return bool|null The result of saving the value.
299 protected function _update_option( $value ) {
300 // Handle non-array option.
301 if ( empty( $this->id_data[ 'keys' ] ) )
302 return update_option( $this->id_data[ 'base' ], $value );
304 // Handle array-based options.
305 $options = get_option( $this->id_data[ 'base' ] );
306 $options = $this->multidimensional_replace( $options, $this->id_data[ 'keys' ], $value );
307 if ( isset( $options ) )
308 return update_option( $this->id_data[ 'base' ], $options );
312 * Fetch the value of the setting.
316 * @return mixed The value.
318 public function value() {
319 // Get the callback that corresponds to the setting type.
320 switch( $this->type ) {
322 $function = 'get_theme_mod';
325 $function = 'get_option';
330 * Filter a Customize setting value not handled as a theme_mod or option.
332 * The dynamic portion of the hook name, `$this->id_date['base']`, refers to
333 * the base slug of the setting name.
335 * For settings handled as theme_mods or options, see those corresponding
336 * functions for available hooks.
340 * @param mixed $default The setting default value. Default empty.
342 return apply_filters( 'customize_value_' . $this->id_data[ 'base' ], $this->default );
345 // Handle non-array value
346 if ( empty( $this->id_data[ 'keys' ] ) )
347 return $function( $this->id_data[ 'base' ], $this->default );
349 // Handle array-based value
350 $values = $function( $this->id_data[ 'base' ] );
351 return $this->multidimensional_get( $values, $this->id_data[ 'keys' ], $this->default );
355 * Sanitize the setting's value for use in JavaScript.
359 * @return mixed The requested escaped value.
361 public function js_value() {
364 * Filter a Customize setting value for use in JavaScript.
366 * The dynamic portion of the hook name, `$this->id`, refers to the setting ID.
370 * @param mixed $value The setting value.
371 * @param WP_Customize_Setting $this {@see WP_Customize_Setting} instance.
373 $value = apply_filters( "customize_sanitize_js_{$this->id}", $this->value(), $this );
375 if ( is_string( $value ) )
376 return html_entity_decode( $value, ENT_QUOTES, 'UTF-8');
382 * Validate user capabilities whether the theme supports the setting.
386 * @return bool False if theme doesn't support the setting or user can't change setting, otherwise true.
388 public final function check_capabilities() {
389 if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) )
392 if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) )
399 * Multidimensional helper function.
405 * @param bool $create Default is false.
406 * @return null|array Keys are 'root', 'node', and 'key'.
408 final protected function multidimensional( &$root, $keys, $create = false ) {
409 if ( $create && empty( $root ) )
412 if ( ! isset( $root ) || empty( $keys ) )
415 $last = array_pop( $keys );
418 foreach ( $keys as $key ) {
419 if ( $create && ! isset( $node[ $key ] ) )
420 $node[ $key ] = array();
422 if ( ! is_array( $node ) || ! isset( $node[ $key ] ) )
425 $node = &$node[ $key ];
428 if ( $create && ! isset( $node[ $last ] ) )
429 $node[ $last ] = array();
431 if ( ! isset( $node[ $last ] ) )
442 * Will attempt to replace a specific value in a multidimensional array.
448 * @param mixed $value The value to update.
451 final protected function multidimensional_replace( $root, $keys, $value ) {
452 if ( ! isset( $value ) )
454 elseif ( empty( $keys ) ) // If there are no keys, we're replacing the root.
457 $result = $this->multidimensional( $root, $keys, true );
459 if ( isset( $result ) )
460 $result['node'][ $result['key'] ] = $value;
466 * Will attempt to fetch a specific value from a multidimensional array.
472 * @param mixed $default A default value which is used as a fallback. Default is null.
473 * @return mixed The requested value or the default value.
475 final protected function multidimensional_get( $root, $keys, $default = null ) {
476 if ( empty( $keys ) ) // If there are no keys, test the root.
477 return isset( $root ) ? $root : $default;
479 $result = $this->multidimensional( $root, $keys );
480 return isset( $result ) ? $result['node'][ $result['key'] ] : $default;
484 * Will attempt to check if a specific value in a multidimensional array is set.
490 * @return bool True if value is set, false if not.
492 final protected function multidimensional_isset( $root, $keys ) {
493 $result = $this->multidimensional_get( $root, $keys );
494 return isset( $result );
499 * A setting that is used to filter a value, but will not save the results.
501 * Results should be properly handled using another setting or callback.
504 * @subpackage Customize
507 class WP_Customize_Filter_Setting extends WP_Customize_Setting {
512 public function update( $value ) {}
516 * A setting that is used to filter a value, but will not save the results.
518 * Results should be properly handled using another setting or callback.
521 * @subpackage Customize
524 final class WP_Customize_Header_Image_Setting extends WP_Customize_Setting {
525 public $id = 'header_image_data';
532 public function update( $value ) {
533 global $custom_image_header;
535 // If the value doesn't exist (removed or random),
536 // use the header_image value.
538 $value = $this->manager->get_setting('header_image')->post_value();
540 if ( is_array( $value ) && isset( $value['choice'] ) )
541 $custom_image_header->set_header_image( $value['choice'] );
543 $custom_image_header->set_header_image( $value );
548 * Class WP_Customize_Background_Image_Setting
551 * @subpackage Customize
554 final class WP_Customize_Background_Image_Setting extends WP_Customize_Setting {
555 public $id = 'background_image_thumb';
562 public function update( $value ) {
563 remove_theme_mod( 'background_image_thumb' );