3 * Customize Setting Class.
6 * @subpackage Customize
9 class WP_Customize_Setting {
13 public $type = 'theme_mod';
14 public $capability = 'edit_theme_options';
15 public $theme_supports = '';
17 public $transport = 'refresh';
19 public $sanitize_callback = '';
20 public $sanitize_js_callback = '';
22 protected $id_data = array();
23 private $_post_value; // Cached, sanitized $_POST value.
30 * @param WP_Customize_Manager $manager
31 * @param string $id An specific ID of the setting. Can be a
32 * theme mod or option name.
33 * @param array $args Setting arguments.
34 * @return WP_Customize_Setting
36 function __construct( $manager, $id, $args = array() ) {
37 $keys = array_keys( get_class_vars( __CLASS__ ) );
38 foreach ( $keys as $key ) {
39 if ( isset( $args[ $key ] ) )
40 $this->$key = $args[ $key ];
43 $this->manager = $manager;
46 // Parse the ID for array keys.
47 $this->id_data[ 'keys' ] = preg_split( '/\[/', str_replace( ']', '', $this->id ) );
48 $this->id_data[ 'base' ] = array_shift( $this->id_data[ 'keys' ] );
51 $this->id = $this->id_data[ 'base' ];
52 if ( ! empty( $this->id_data[ 'keys' ] ) )
53 $this->id .= '[' . implode( '][', $this->id_data[ 'keys' ] ) . ']';
55 if ( $this->sanitize_callback )
56 add_filter( "customize_sanitize_{$this->id}", $this->sanitize_callback, 10, 2 );
58 if ( $this->sanitize_js_callback )
59 add_filter( "customize_sanitize_js_{$this->id}", $this->sanitize_js_callback, 10, 2 );
65 * Handle previewing the setting.
69 public function preview() {
70 switch( $this->type ) {
72 add_filter( 'theme_mod_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
75 if ( empty( $this->id_data[ 'keys' ] ) )
76 add_filter( 'pre_option_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
78 add_filter( 'option_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
79 add_filter( 'default_option_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
83 do_action( 'customize_preview_' . $this->id );
88 * Callback function to filter the theme mods and options.
91 * @uses WP_Customize_Setting::multidimensional_replace()
93 * @param mixed $original Old value.
94 * @return mixed New or old value.
96 public function _preview_filter( $original ) {
97 return $this->multidimensional_replace( $original, $this->id_data[ 'keys' ], $this->post_value() );
101 * Set the value of the parameter for a specific theme.
105 * @return bool False if cap check fails or value isn't set.
107 public final function save() {
108 $value = $this->post_value();
110 if ( ! $this->check_capabilities() || ! isset( $value ) )
113 do_action( 'customize_save_' . $this->id_data[ 'base' ] );
115 $this->update( $value );
119 * Fetches, validates, and sanitizes the $_POST value.
123 * @param mixed $default A default value which is used as a fallback. Default is null.
124 * @return mixed The default value on failure, otherwise the sanitized value.
126 public final function post_value( $default = null ) {
127 if ( isset( $this->_post_value ) )
128 return $this->_post_value;
130 $result = $this->manager->post_value( $this );
132 if ( isset( $result ) )
133 return $this->_post_value = $result;
143 * @param mixed $value The value to sanitize.
144 * @return mixed Null if an input isn't valid, otherwise the sanitized value.
146 public function sanitize( $value ) {
147 $value = wp_unslash( $value );
148 return apply_filters( "customize_sanitize_{$this->id}", $value, $this );
152 * Set the value of the parameter for a specific theme.
156 * @param mixed $value The value to update.
157 * @return mixed The result of saving the value.
159 protected function update( $value ) {
160 switch( $this->type ) {
162 return $this->_update_theme_mod( $value );
165 return $this->_update_option( $value );
168 return do_action( 'customize_update_' . $this->type, $value );
173 * Update the theme mod from the value of the parameter.
177 * @param mixed $value The value to update.
178 * @return mixed The result of saving the value.
180 protected function _update_theme_mod( $value ) {
181 // Handle non-array theme mod.
182 if ( empty( $this->id_data[ 'keys' ] ) )
183 return set_theme_mod( $this->id_data[ 'base' ], $value );
185 // Handle array-based theme mod.
186 $mods = get_theme_mod( $this->id_data[ 'base' ] );
187 $mods = $this->multidimensional_replace( $mods, $this->id_data[ 'keys' ], $value );
188 if ( isset( $mods ) )
189 return set_theme_mod( $this->id_data[ 'base' ], $mods );
193 * Update the theme mod from the value of the parameter.
197 * @param mixed $value The value to update.
198 * @return mixed The result of saving the value.
200 protected function _update_option( $value ) {
201 // Handle non-array option.
202 if ( empty( $this->id_data[ 'keys' ] ) )
203 return update_option( $this->id_data[ 'base' ], $value );
205 // Handle array-based options.
206 $options = get_option( $this->id_data[ 'base' ] );
207 $options = $this->multidimensional_replace( $options, $this->id_data[ 'keys' ], $value );
208 if ( isset( $options ) )
209 return update_option( $this->id_data[ 'base' ], $options );
213 * Fetch the value of the parameter for a specific theme.
217 * @return mixed The requested value.
219 public function value() {
220 switch( $this->type ) {
222 $function = 'get_theme_mod';
225 $function = 'get_option';
228 return apply_filters( 'customize_value_' . $this->id_data[ 'base' ], $this->default );
231 // Handle non-array value
232 if ( empty( $this->id_data[ 'keys' ] ) )
233 return $function( $this->id_data[ 'base' ], $this->default );
235 // Handle array-based value
236 $values = $function( $this->id_data[ 'base' ] );
237 return $this->multidimensional_get( $values, $this->id_data[ 'keys' ], $this->default );
241 * Escape the parameter's value for use in JavaScript.
245 * @return mixed The requested escaped value.
247 public function js_value() {
248 $value = apply_filters( "customize_sanitize_js_{$this->id}", $this->value(), $this );
250 if ( is_string( $value ) )
251 return html_entity_decode( $value, ENT_QUOTES, 'UTF-8');
257 * Check if the theme supports the setting and check user capabilities.
261 * @return bool False if theme doesn't support the setting or user can't change setting, otherwise true.
263 public final function check_capabilities() {
264 if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) )
267 if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) )
274 * Multidimensional helper function.
280 * @param bool $create Default is false.
281 * @return null|array Keys are 'root', 'node', and 'key'.
283 final protected function multidimensional( &$root, $keys, $create = false ) {
284 if ( $create && empty( $root ) )
287 if ( ! isset( $root ) || empty( $keys ) )
290 $last = array_pop( $keys );
293 foreach ( $keys as $key ) {
294 if ( $create && ! isset( $node[ $key ] ) )
295 $node[ $key ] = array();
297 if ( ! is_array( $node ) || ! isset( $node[ $key ] ) )
300 $node = &$node[ $key ];
303 if ( $create && ! isset( $node[ $last ] ) )
304 $node[ $last ] = array();
306 if ( ! isset( $node[ $last ] ) )
317 * Will attempt to replace a specific value in a multidimensional array.
323 * @param mixed $value The value to update.
326 final protected function multidimensional_replace( $root, $keys, $value ) {
327 if ( ! isset( $value ) )
329 elseif ( empty( $keys ) ) // If there are no keys, we're replacing the root.
332 $result = $this->multidimensional( $root, $keys, true );
334 if ( isset( $result ) )
335 $result['node'][ $result['key'] ] = $value;
341 * Will attempt to fetch a specific value from a multidimensional array.
347 * @param $default A default value which is used as a fallback. Default is null.
348 * @return mixed The requested value or the default value.
350 final protected function multidimensional_get( $root, $keys, $default = null ) {
351 if ( empty( $keys ) ) // If there are no keys, test the root.
352 return isset( $root ) ? $root : $default;
354 $result = $this->multidimensional( $root, $keys );
355 return isset( $result ) ? $result['node'][ $result['key'] ] : $default;
359 * Will attempt to check if a specific value in a multidimensional array is set.
365 * @return bool True if value is set, false if not.
367 final protected function multidimensional_isset( $root, $keys ) {
368 $result = $this->multidimensional_get( $root, $keys );
369 return isset( $result );
374 * A setting that is used to filter a value, but will not save the results.
376 * Results should be properly handled using another setting or callback.
379 * @subpackage Customize
382 class WP_Customize_Filter_Setting extends WP_Customize_Setting {
387 public function update( $value ) {}
391 * A setting that is used to filter a value, but will not save the results.
393 * Results should be properly handled using another setting or callback.
396 * @subpackage Customize
399 final class WP_Customize_Header_Image_Setting extends WP_Customize_Setting {
400 public $id = 'header_image_data';
407 public function update( $value ) {
408 global $custom_image_header;
410 // If the value doesn't exist (removed or random),
411 // use the header_image value.
413 $value = $this->manager->get_setting('header_image')->post_value();
415 if ( is_array( $value ) && isset( $value['choice'] ) )
416 $custom_image_header->set_header_image( $value['choice'] );
418 $custom_image_header->set_header_image( $value );
424 * @subpackage Customize
427 final class WP_Customize_Background_Image_Setting extends WP_Customize_Setting {
428 public $id = 'background_image_thumb';
432 * @uses remove_theme_mod()
436 public function update( $value ) {
437 remove_theme_mod( 'background_image_thumb' );