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 function __construct( $manager, $id, $args = array() ) {
79 $keys = array_keys( get_class_vars( __CLASS__ ) );
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 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 do_action( 'customize_preview_' . $this->id );
139 * Callback function to filter the theme mods and options.
142 * @uses WP_Customize_Setting::multidimensional_replace()
144 * @param mixed $original Old value.
145 * @return mixed New or old value.
147 public function _preview_filter( $original ) {
148 return $this->multidimensional_replace( $original, $this->id_data[ 'keys' ], $this->post_value() );
152 * Check user capabilities and theme supports, and then save
153 * the value of the setting.
157 * @return bool False if cap check fails or value isn't set.
159 public final function save() {
160 $value = $this->post_value();
162 if ( ! $this->check_capabilities() || ! isset( $value ) )
166 * Fires when the WP_Customize_Setting::save() method is called for settings
167 * not handled as theme_mods or options.
169 * The dynamic portion of the hook name, $this->id_data['base'] refers to
170 * the base slug of the setting name.
174 do_action( 'customize_save_' . $this->id_data[ 'base' ] );
176 $this->update( $value );
180 * Fetch and sanitize the $_POST value for the setting.
184 * @param mixed $default A default value which is used as a fallback. Default is null.
185 * @return mixed The default value on failure, otherwise the sanitized value.
187 public final function post_value( $default = null ) {
188 // Check for a cached value
189 if ( isset( $this->_post_value ) )
190 return $this->_post_value;
192 // Call the manager for the post value
193 $result = $this->manager->post_value( $this );
195 if ( isset( $result ) )
196 return $this->_post_value = $result;
206 * @param mixed $value The value to sanitize.
207 * @return mixed Null if an input isn't valid, otherwise the sanitized value.
209 public function sanitize( $value ) {
210 $value = wp_unslash( $value );
213 * Filter a Customize setting value in un-slashed form.
217 * @param mixed $value Value of the setting.
218 * @param WP_Customize_Setting $this WP_Customize_Setting instance.
220 return apply_filters( "customize_sanitize_{$this->id}", $value, $this );
224 * Save the value of the setting, using the related API.
228 * @param mixed $value The value to update.
229 * @return mixed The result of saving the value.
231 protected function update( $value ) {
232 switch( $this->type ) {
234 return $this->_update_theme_mod( $value );
237 return $this->_update_option( $value );
242 * Fires when the WP_Customize_Setting::update() method is called for settings
243 * not handled as theme_mods or options.
245 * The dynamic portion of the hook name, $this->type, refers to the type of setting.
249 * @param mixed $value Value of the setting.
251 return do_action( 'customize_update_' . $this->type, $value );
256 * Update the theme mod from the value of the parameter.
260 * @param mixed $value The value to update.
261 * @return mixed The result of saving the value.
263 protected function _update_theme_mod( $value ) {
264 // Handle non-array theme mod.
265 if ( empty( $this->id_data[ 'keys' ] ) )
266 return set_theme_mod( $this->id_data[ 'base' ], $value );
268 // Handle array-based theme mod.
269 $mods = get_theme_mod( $this->id_data[ 'base' ] );
270 $mods = $this->multidimensional_replace( $mods, $this->id_data[ 'keys' ], $value );
271 if ( isset( $mods ) )
272 return set_theme_mod( $this->id_data[ 'base' ], $mods );
276 * Update the option from the value of the setting.
280 * @param mixed $value The value to update.
281 * @return mixed The result of saving the value.
283 protected function _update_option( $value ) {
284 // Handle non-array option.
285 if ( empty( $this->id_data[ 'keys' ] ) )
286 return update_option( $this->id_data[ 'base' ], $value );
288 // Handle array-based options.
289 $options = get_option( $this->id_data[ 'base' ] );
290 $options = $this->multidimensional_replace( $options, $this->id_data[ 'keys' ], $value );
291 if ( isset( $options ) )
292 return update_option( $this->id_data[ 'base' ], $options );
296 * Fetch the value of the setting.
300 * @return mixed The value.
302 public function value() {
303 // Get the callback that corresponds to the setting type.
304 switch( $this->type ) {
306 $function = 'get_theme_mod';
309 $function = 'get_option';
314 * Filter a Customize setting value not handled as a theme_mod or option.
316 * The dynamic portion of the hook name, $this->id_date['base'], refers to
317 * the base slug of the setting name.
319 * For settings handled as theme_mods or options, see those corresponding
320 * functions for available hooks.
324 * @param mixed $default The setting default value. Default empty.
326 return apply_filters( 'customize_value_' . $this->id_data[ 'base' ], $this->default );
329 // Handle non-array value
330 if ( empty( $this->id_data[ 'keys' ] ) )
331 return $function( $this->id_data[ 'base' ], $this->default );
333 // Handle array-based value
334 $values = $function( $this->id_data[ 'base' ] );
335 return $this->multidimensional_get( $values, $this->id_data[ 'keys' ], $this->default );
339 * Sanitize the setting's value for use in JavaScript.
343 * @return mixed The requested escaped value.
345 public function js_value() {
348 * Filter a Customize setting value for use in JavaScript.
350 * The dynamic portion of the hook name, $this->id, refers to the setting ID.
354 * @param mixed $value The setting value.
355 * @param WP_Customize_Setting $this WP_Customize_Setting instance.
357 $value = apply_filters( "customize_sanitize_js_{$this->id}", $this->value(), $this );
359 if ( is_string( $value ) )
360 return html_entity_decode( $value, ENT_QUOTES, 'UTF-8');
366 * Validate user capabilities whether the theme supports the setting.
370 * @return bool False if theme doesn't support the setting or user can't change setting, otherwise true.
372 public final function check_capabilities() {
373 if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) )
376 if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) )
383 * Multidimensional helper function.
389 * @param bool $create Default is false.
390 * @return null|array Keys are 'root', 'node', and 'key'.
392 final protected function multidimensional( &$root, $keys, $create = false ) {
393 if ( $create && empty( $root ) )
396 if ( ! isset( $root ) || empty( $keys ) )
399 $last = array_pop( $keys );
402 foreach ( $keys as $key ) {
403 if ( $create && ! isset( $node[ $key ] ) )
404 $node[ $key ] = array();
406 if ( ! is_array( $node ) || ! isset( $node[ $key ] ) )
409 $node = &$node[ $key ];
412 if ( $create && ! isset( $node[ $last ] ) )
413 $node[ $last ] = array();
415 if ( ! isset( $node[ $last ] ) )
426 * Will attempt to replace a specific value in a multidimensional array.
432 * @param mixed $value The value to update.
435 final protected function multidimensional_replace( $root, $keys, $value ) {
436 if ( ! isset( $value ) )
438 elseif ( empty( $keys ) ) // If there are no keys, we're replacing the root.
441 $result = $this->multidimensional( $root, $keys, true );
443 if ( isset( $result ) )
444 $result['node'][ $result['key'] ] = $value;
450 * Will attempt to fetch a specific value from a multidimensional array.
456 * @param $default A default value which is used as a fallback. Default is null.
457 * @return mixed The requested value or the default value.
459 final protected function multidimensional_get( $root, $keys, $default = null ) {
460 if ( empty( $keys ) ) // If there are no keys, test the root.
461 return isset( $root ) ? $root : $default;
463 $result = $this->multidimensional( $root, $keys );
464 return isset( $result ) ? $result['node'][ $result['key'] ] : $default;
468 * Will attempt to check if a specific value in a multidimensional array is set.
474 * @return bool True if value is set, false if not.
476 final protected function multidimensional_isset( $root, $keys ) {
477 $result = $this->multidimensional_get( $root, $keys );
478 return isset( $result );
483 * A setting that is used to filter a value, but will not save the results.
485 * Results should be properly handled using another setting or callback.
488 * @subpackage Customize
491 class WP_Customize_Filter_Setting extends WP_Customize_Setting {
496 public function update( $value ) {}
500 * A setting that is used to filter a value, but will not save the results.
502 * Results should be properly handled using another setting or callback.
505 * @subpackage Customize
508 final class WP_Customize_Header_Image_Setting extends WP_Customize_Setting {
509 public $id = 'header_image_data';
516 public function update( $value ) {
517 global $custom_image_header;
519 // If the value doesn't exist (removed or random),
520 // use the header_image value.
522 $value = $this->manager->get_setting('header_image')->post_value();
524 if ( is_array( $value ) && isset( $value['choice'] ) )
525 $custom_image_header->set_header_image( $value['choice'] );
527 $custom_image_header->set_header_image( $value );
532 * Class WP_Customize_Background_Image_Setting
535 * @subpackage Customize
538 final class WP_Customize_Background_Image_Setting extends WP_Customize_Setting {
539 public $id = 'background_image_thumb';
543 * @uses remove_theme_mod()
547 public function update( $value ) {
548 remove_theme_mod( 'background_image_thumb' );