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 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 WP_Customize_Setting instance.
136 do_action( 'customize_preview_' . $this->id, $this );
141 * Callback function to filter the theme mods and options.
144 * @uses WP_Customize_Setting::multidimensional_replace()
146 * @param mixed $original Old value.
147 * @return mixed New or old value.
149 public function _preview_filter( $original ) {
150 return $this->multidimensional_replace( $original, $this->id_data[ 'keys' ], $this->post_value() );
154 * Check user capabilities and theme supports, and then save
155 * the value of the setting.
159 * @return bool False if cap check fails or value isn't set.
161 public final function save() {
162 $value = $this->post_value();
164 if ( ! $this->check_capabilities() || ! isset( $value ) )
168 * Fires when the WP_Customize_Setting::save() method is called.
170 * The dynamic portion of the hook name, $this->id_data['base'] refers to
171 * the base slug of the setting name.
175 * @param WP_Customize_Setting $this WP_Customize_Setting instance.
177 do_action( 'customize_save_' . $this->id_data[ 'base' ], $this );
179 $this->update( $value );
183 * Fetch and sanitize the $_POST value for the setting.
187 * @param mixed $default A default value which is used as a fallback. Default is null.
188 * @return mixed The default value on failure, otherwise the sanitized value.
190 public final function post_value( $default = null ) {
191 // Check for a cached value
192 if ( isset( $this->_post_value ) )
193 return $this->_post_value;
195 // Call the manager for the post value
196 $result = $this->manager->post_value( $this );
198 if ( isset( $result ) )
199 return $this->_post_value = $result;
209 * @param mixed $value The value to sanitize.
210 * @return mixed Null if an input isn't valid, otherwise the sanitized value.
212 public function sanitize( $value ) {
213 $value = wp_unslash( $value );
216 * Filter a Customize setting value in un-slashed form.
220 * @param mixed $value Value of the setting.
221 * @param WP_Customize_Setting $this WP_Customize_Setting instance.
223 return apply_filters( "customize_sanitize_{$this->id}", $value, $this );
227 * Save the value of the setting, using the related API.
231 * @param mixed $value The value to update.
232 * @return mixed The result of saving the value.
234 protected function update( $value ) {
235 switch( $this->type ) {
237 return $this->_update_theme_mod( $value );
240 return $this->_update_option( $value );
245 * Fires when the WP_Customize_Setting::update() method is called for settings
246 * not handled as theme_mods or options.
248 * The dynamic portion of the hook name, $this->type, refers to the type of setting.
252 * @param mixed $value Value of the setting.
253 * @param WP_Customize_Setting $this WP_Customize_Setting instance.
255 return do_action( 'customize_update_' . $this->type, $value, $this );
260 * Update the theme mod from the value of the parameter.
264 * @param mixed $value The value to update.
265 * @return mixed The result of saving the value.
267 protected function _update_theme_mod( $value ) {
268 // Handle non-array theme mod.
269 if ( empty( $this->id_data[ 'keys' ] ) )
270 return set_theme_mod( $this->id_data[ 'base' ], $value );
272 // Handle array-based theme mod.
273 $mods = get_theme_mod( $this->id_data[ 'base' ] );
274 $mods = $this->multidimensional_replace( $mods, $this->id_data[ 'keys' ], $value );
275 if ( isset( $mods ) )
276 return set_theme_mod( $this->id_data[ 'base' ], $mods );
280 * Update the option from the value of the setting.
284 * @param mixed $value The value to update.
285 * @return mixed The result of saving the value.
287 protected function _update_option( $value ) {
288 // Handle non-array option.
289 if ( empty( $this->id_data[ 'keys' ] ) )
290 return update_option( $this->id_data[ 'base' ], $value );
292 // Handle array-based options.
293 $options = get_option( $this->id_data[ 'base' ] );
294 $options = $this->multidimensional_replace( $options, $this->id_data[ 'keys' ], $value );
295 if ( isset( $options ) )
296 return update_option( $this->id_data[ 'base' ], $options );
300 * Fetch the value of the setting.
304 * @return mixed The value.
306 public function value() {
307 // Get the callback that corresponds to the setting type.
308 switch( $this->type ) {
310 $function = 'get_theme_mod';
313 $function = 'get_option';
318 * Filter a Customize setting value not handled as a theme_mod or option.
320 * The dynamic portion of the hook name, $this->id_date['base'], refers to
321 * the base slug of the setting name.
323 * For settings handled as theme_mods or options, see those corresponding
324 * functions for available hooks.
328 * @param mixed $default The setting default value. Default empty.
330 return apply_filters( 'customize_value_' . $this->id_data[ 'base' ], $this->default );
333 // Handle non-array value
334 if ( empty( $this->id_data[ 'keys' ] ) )
335 return $function( $this->id_data[ 'base' ], $this->default );
337 // Handle array-based value
338 $values = $function( $this->id_data[ 'base' ] );
339 return $this->multidimensional_get( $values, $this->id_data[ 'keys' ], $this->default );
343 * Sanitize the setting's value for use in JavaScript.
347 * @return mixed The requested escaped value.
349 public function js_value() {
352 * Filter a Customize setting value for use in JavaScript.
354 * The dynamic portion of the hook name, $this->id, refers to the setting ID.
358 * @param mixed $value The setting value.
359 * @param WP_Customize_Setting $this WP_Customize_Setting instance.
361 $value = apply_filters( "customize_sanitize_js_{$this->id}", $this->value(), $this );
363 if ( is_string( $value ) )
364 return html_entity_decode( $value, ENT_QUOTES, 'UTF-8');
370 * Validate user capabilities whether the theme supports the setting.
374 * @return bool False if theme doesn't support the setting or user can't change setting, otherwise true.
376 public final function check_capabilities() {
377 if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) )
380 if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) )
387 * Multidimensional helper function.
393 * @param bool $create Default is false.
394 * @return null|array Keys are 'root', 'node', and 'key'.
396 final protected function multidimensional( &$root, $keys, $create = false ) {
397 if ( $create && empty( $root ) )
400 if ( ! isset( $root ) || empty( $keys ) )
403 $last = array_pop( $keys );
406 foreach ( $keys as $key ) {
407 if ( $create && ! isset( $node[ $key ] ) )
408 $node[ $key ] = array();
410 if ( ! is_array( $node ) || ! isset( $node[ $key ] ) )
413 $node = &$node[ $key ];
416 if ( $create && ! isset( $node[ $last ] ) )
417 $node[ $last ] = array();
419 if ( ! isset( $node[ $last ] ) )
430 * Will attempt to replace a specific value in a multidimensional array.
436 * @param mixed $value The value to update.
439 final protected function multidimensional_replace( $root, $keys, $value ) {
440 if ( ! isset( $value ) )
442 elseif ( empty( $keys ) ) // If there are no keys, we're replacing the root.
445 $result = $this->multidimensional( $root, $keys, true );
447 if ( isset( $result ) )
448 $result['node'][ $result['key'] ] = $value;
454 * Will attempt to fetch a specific value from a multidimensional array.
460 * @param $default A default value which is used as a fallback. Default is null.
461 * @return mixed The requested value or the default value.
463 final protected function multidimensional_get( $root, $keys, $default = null ) {
464 if ( empty( $keys ) ) // If there are no keys, test the root.
465 return isset( $root ) ? $root : $default;
467 $result = $this->multidimensional( $root, $keys );
468 return isset( $result ) ? $result['node'][ $result['key'] ] : $default;
472 * Will attempt to check if a specific value in a multidimensional array is set.
478 * @return bool True if value is set, false if not.
480 final protected function multidimensional_isset( $root, $keys ) {
481 $result = $this->multidimensional_get( $root, $keys );
482 return isset( $result );
487 * A setting that is used to filter a value, but will not save the results.
489 * Results should be properly handled using another setting or callback.
492 * @subpackage Customize
495 class WP_Customize_Filter_Setting extends WP_Customize_Setting {
500 public function update( $value ) {}
504 * A setting that is used to filter a value, but will not save the results.
506 * Results should be properly handled using another setting or callback.
509 * @subpackage Customize
512 final class WP_Customize_Header_Image_Setting extends WP_Customize_Setting {
513 public $id = 'header_image_data';
520 public function update( $value ) {
521 global $custom_image_header;
523 // If the value doesn't exist (removed or random),
524 // use the header_image value.
526 $value = $this->manager->get_setting('header_image')->post_value();
528 if ( is_array( $value ) && isset( $value['choice'] ) )
529 $custom_image_header->set_header_image( $value['choice'] );
531 $custom_image_header->set_header_image( $value );
536 * Class WP_Customize_Background_Image_Setting
539 * @subpackage Customize
542 final class WP_Customize_Background_Image_Setting extends WP_Customize_Setting {
543 public $id = 'background_image_thumb';
547 * @uses remove_theme_mod()
551 public function update( $value ) {
552 remove_theme_mod( 'background_image_thumb' );