web/wp-includes/class-wp-customize-setting.php
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
     1 <?php
     1 <?php
     2 /**
     2 /**
     3  * Customize Setting Class
     3  * Customize Setting Class.
     4  *
     4  *
     5  * @package WordPress
     5  * @package WordPress
     6  * @subpackage Customize
     6  * @subpackage Customize
     7  * @since 3.4.0
     7  * @since 3.4.0
     8  */
     8  */
     9 
       
    10 class WP_Customize_Setting {
     9 class WP_Customize_Setting {
    11 	public $manager;
    10 	public $manager;
    12 	public $id;
    11 	public $id;
    13 
    12 
    14 	public $type            = 'theme_mod';
    13 	public $type            = 'theme_mod';
    26 	/**
    25 	/**
    27 	 * Constructor.
    26 	 * Constructor.
    28 	 *
    27 	 *
    29 	 * @since 3.4.0
    28 	 * @since 3.4.0
    30 	 *
    29 	 *
       
    30 	 * @param WP_Customize_Manager $manager
    31 	 * @param string $id An specific ID of the setting. Can be a
    31 	 * @param string $id An specific ID of the setting. Can be a
    32 	 *                   theme mod or option name.
    32 	 *                   theme mod or option name.
    33 	 * @param array $args Setting arguments.
    33 	 * @param array $args Setting arguments.
       
    34 	 * @return WP_Customize_Setting
    34 	 */
    35 	 */
    35 	function __construct( $manager, $id, $args = array() ) {
    36 	function __construct( $manager, $id, $args = array() ) {
    36 		$keys = array_keys( get_class_vars( __CLASS__ ) );
    37 		$keys = array_keys( get_class_vars( __CLASS__ ) );
    37 		foreach ( $keys as $key ) {
    38 		foreach ( $keys as $key ) {
    38 			if ( isset( $args[ $key ] ) )
    39 			if ( isset( $args[ $key ] ) )
    85 
    86 
    86 	/**
    87 	/**
    87 	 * Callback function to filter the theme mods and options.
    88 	 * Callback function to filter the theme mods and options.
    88 	 *
    89 	 *
    89 	 * @since 3.4.0
    90 	 * @since 3.4.0
    90 	 *
    91 	 * @uses WP_Customize_Setting::multidimensional_replace()
    91 	 * @param mixed Old value.
    92 	 *
       
    93 	 * @param mixed $original Old value.
    92 	 * @return mixed New or old value.
    94 	 * @return mixed New or old value.
    93 	 */
    95 	 */
    94 	public function _preview_filter( $original ) {
    96 	public function _preview_filter( $original ) {
    95 		return $this->multidimensional_replace( $original, $this->id_data[ 'keys' ], $this->post_value() );
    97 		return $this->multidimensional_replace( $original, $this->id_data[ 'keys' ], $this->post_value() );
    96 	}
    98 	}
   116 	/**
   118 	/**
   117 	 * Fetches, validates, and sanitizes the $_POST value.
   119 	 * Fetches, validates, and sanitizes the $_POST value.
   118 	 *
   120 	 *
   119 	 * @since 3.4.0
   121 	 * @since 3.4.0
   120 	 *
   122 	 *
   121 	 * @param $default mixed A default value which is used as a fallback. Default is null.
   123 	 * @param mixed $default A default value which is used as a fallback. Default is null.
   122 	 * @return mixed Either the default value on failure or sanitized value.
   124 	 * @return mixed The default value on failure, otherwise the sanitized value.
   123 	 */
   125 	 */
   124 	public final function post_value( $default = null ) {
   126 	public final function post_value( $default = null ) {
   125 		if ( isset( $this->_post_value ) )
   127 		if ( isset( $this->_post_value ) )
   126 			return $this->_post_value;
   128 			return $this->_post_value;
   127 
   129 
   136 	/**
   138 	/**
   137 	 * Sanitize an input.
   139 	 * Sanitize an input.
   138 	 *
   140 	 *
   139 	 * @since 3.4.0
   141 	 * @since 3.4.0
   140 	 *
   142 	 *
   141 	 * @param $value mixed The value to sanitize.
   143 	 * @param mixed $value The value to sanitize.
   142 	 * @return mixed Null if an input isn't valid, otherwise the sanitized value.
   144 	 * @return mixed Null if an input isn't valid, otherwise the sanitized value.
   143 	 */
   145 	 */
   144 	public function sanitize( $value ) {
   146 	public function sanitize( $value ) {
   145 		$value = stripslashes_deep( $value );
   147 		$value = stripslashes_deep( $value );
   146 		return apply_filters( "customize_sanitize_{$this->id}", $value, $this );
   148 		return apply_filters( "customize_sanitize_{$this->id}", $value, $this );
   149 	/**
   151 	/**
   150 	 * Set the value of the parameter for a specific theme.
   152 	 * Set the value of the parameter for a specific theme.
   151 	 *
   153 	 *
   152 	 * @since 3.4.0
   154 	 * @since 3.4.0
   153 	 *
   155 	 *
   154 	 * @param $value mixed The value to update.
   156 	 * @param mixed $value The value to update.
   155 	 * @return mixed The result of saving the value.
   157 	 * @return mixed The result of saving the value.
   156 	 */
   158 	 */
   157 	protected function update( $value ) {
   159 	protected function update( $value ) {
   158 		switch( $this->type ) {
   160 		switch( $this->type ) {
   159 			case 'theme_mod' :
   161 			case 'theme_mod' :
   170 	/**
   172 	/**
   171 	 * Update the theme mod from the value of the parameter.
   173 	 * Update the theme mod from the value of the parameter.
   172 	 *
   174 	 *
   173 	 * @since 3.4.0
   175 	 * @since 3.4.0
   174 	 *
   176 	 *
   175 	 * @param $value mixed The value to update.
   177 	 * @param mixed $value The value to update.
   176 	 * @return mixed The result of saving the value.
   178 	 * @return mixed The result of saving the value.
   177 	 */
   179 	 */
   178 	protected function _update_theme_mod( $value ) {
   180 	protected function _update_theme_mod( $value ) {
   179 		// Handle non-array theme mod.
   181 		// Handle non-array theme mod.
   180 		if ( empty( $this->id_data[ 'keys' ] ) )
   182 		if ( empty( $this->id_data[ 'keys' ] ) )
   190 	/**
   192 	/**
   191 	 * Update the theme mod from the value of the parameter.
   193 	 * Update the theme mod from the value of the parameter.
   192 	 *
   194 	 *
   193 	 * @since 3.4.0
   195 	 * @since 3.4.0
   194 	 *
   196 	 *
   195 	 * @param $value mixed The value to update.
   197 	 * @param mixed $value The value to update.
   196 	 * @return mixed The result of saving the value.
   198 	 * @return mixed The result of saving the value.
   197 	 */
   199 	 */
   198 	protected function _update_option( $value ) {
   200 	protected function _update_option( $value ) {
   199 		// Handle non-array option.
   201 		// Handle non-array option.
   200 		if ( empty( $this->id_data[ 'keys' ] ) )
   202 		if ( empty( $this->id_data[ 'keys' ] ) )
   274 	 * @since 3.4.0
   276 	 * @since 3.4.0
   275 	 *
   277 	 *
   276 	 * @param $root
   278 	 * @param $root
   277 	 * @param $keys
   279 	 * @param $keys
   278 	 * @param bool $create Default is false.
   280 	 * @param bool $create Default is false.
   279 	 * @return null|array
   281 	 * @return null|array Keys are 'root', 'node', and 'key'.
   280 	 */
   282 	 */
   281 	final protected function multidimensional( &$root, $keys, $create = false ) {
   283 	final protected function multidimensional( &$root, $keys, $create = false ) {
   282 		if ( $create && empty( $root ) )
   284 		if ( $create && empty( $root ) )
   283 			$root = array();
   285 			$root = array();
   284 
   286 
   370 
   372 
   371 /**
   373 /**
   372  * A setting that is used to filter a value, but will not save the results.
   374  * A setting that is used to filter a value, but will not save the results.
   373  *
   375  *
   374  * Results should be properly handled using another setting or callback.
   376  * Results should be properly handled using another setting or callback.
       
   377  *
       
   378  * @package WordPress
       
   379  * @subpackage Customize
       
   380  * @since 3.4.0
   375  */
   381  */
   376 class WP_Customize_Filter_Setting extends WP_Customize_Setting {
   382 class WP_Customize_Filter_Setting extends WP_Customize_Setting {
   377 	public function update() {}
   383 
       
   384 	/**
       
   385 	 * @since 3.4.0
       
   386 	 */
       
   387 	public function update( $value ) {}
   378 }
   388 }
   379 
   389 
   380 /**
   390 /**
   381  * A setting that is used to filter a value, but will not save the results.
   391  * A setting that is used to filter a value, but will not save the results.
   382  *
   392  *
   383  * Results should be properly handled using another setting or callback.
   393  * Results should be properly handled using another setting or callback.
       
   394  *
       
   395  * @package WordPress
       
   396  * @subpackage Customize
       
   397  * @since 3.4.0
   384  */
   398  */
   385 final class WP_Customize_Header_Image_Setting extends WP_Customize_Setting {
   399 final class WP_Customize_Header_Image_Setting extends WP_Customize_Setting {
   386 	public $id = 'header_image_data';
   400 	public $id = 'header_image_data';
   387 
   401 
       
   402 	/**
       
   403 	 * @since 3.4.0
       
   404 	 *
       
   405 	 * @param $value
       
   406 	 */
   388 	public function update( $value ) {
   407 	public function update( $value ) {
   389 		global $custom_image_header;
   408 		global $custom_image_header;
   390 
   409 
   391 		// If the value doesn't exist (removed or random),
   410 		// If the value doesn't exist (removed or random),
   392 		// use the header_image value.
   411 		// use the header_image value.
   398 		else
   417 		else
   399 			$custom_image_header->set_header_image( $value );
   418 			$custom_image_header->set_header_image( $value );
   400 	}
   419 	}
   401 }
   420 }
   402 
   421 
       
   422 /**
       
   423  * @package WordPress
       
   424  * @subpackage Customize
       
   425  * @since 3.4.0
       
   426  */
   403 final class WP_Customize_Background_Image_Setting extends WP_Customize_Setting {
   427 final class WP_Customize_Background_Image_Setting extends WP_Customize_Setting {
   404 	public $id = 'background_image_thumb';
   428 	public $id = 'background_image_thumb';
   405 
   429 
       
   430 	/**
       
   431 	 * @since 3.4.0
       
   432 	 * @uses remove_theme_mod()
       
   433 	 *
       
   434 	 * @param $value
       
   435 	 */
   406 	public function update( $value ) {
   436 	public function update( $value ) {
   407 		remove_theme_mod( 'background_image_thumb' );
   437 		remove_theme_mod( 'background_image_thumb' );
   408 	}
   438 	}
   409 }
   439 }