wp/wp-includes/class-wp-customize-setting.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
   266 	 * This is intended only for use by unit tests.
   266 	 * This is intended only for use by unit tests.
   267 	 *
   267 	 *
   268 	 * @since 4.5.0
   268 	 * @since 4.5.0
   269 	 * @ignore
   269 	 * @ignore
   270 	 */
   270 	 */
   271 	static public function reset_aggregated_multidimensionals() {
   271 	public static function reset_aggregated_multidimensionals() {
   272 		self::$aggregated_multidimensionals = array();
   272 		self::$aggregated_multidimensionals = array();
   273 	}
   273 	}
   274 
   274 
   275 	/**
   275 	/**
   276 	 * The ID for the current site when the preview() method was called.
   276 	 * The ID for the current site when the preview() method was called.
   392 				 *
   392 				 *
   393 				 * The dynamic portion of the hook name, `$this->id`, refers to the setting ID.
   393 				 * The dynamic portion of the hook name, `$this->id`, refers to the setting ID.
   394 				 *
   394 				 *
   395 				 * @since 3.4.0
   395 				 * @since 3.4.0
   396 				 *
   396 				 *
   397 				 * @param WP_Customize_Setting $this WP_Customize_Setting instance.
   397 				 * @param WP_Customize_Setting $setting WP_Customize_Setting instance.
   398 				 */
   398 				 */
   399 				do_action( "customize_preview_{$this->id}", $this );
   399 				do_action( "customize_preview_{$this->id}", $this );
   400 
   400 
   401 				/**
   401 				/**
   402 				 * Fires when the WP_Customize_Setting::preview() method is called for settings
   402 				 * Fires when the WP_Customize_Setting::preview() method is called for settings
   404 				 *
   404 				 *
   405 				 * The dynamic portion of the hook name, `$this->type`, refers to the setting type.
   405 				 * The dynamic portion of the hook name, `$this->type`, refers to the setting type.
   406 				 *
   406 				 *
   407 				 * @since 4.1.0
   407 				 * @since 4.1.0
   408 				 *
   408 				 *
   409 				 * @param WP_Customize_Setting $this WP_Customize_Setting instance.
   409 				 * @param WP_Customize_Setting $setting WP_Customize_Setting instance.
   410 				 */
   410 				 */
   411 				do_action( "customize_preview_{$this->type}", $this );
   411 				do_action( "customize_preview_{$this->type}", $this );
   412 		}
   412 		}
   413 
   413 
   414 		$this->is_previewed = true;
   414 		$this->is_previewed = true;
   531 		 * The dynamic portion of the hook name, `$id_base` refers to
   531 		 * The dynamic portion of the hook name, `$id_base` refers to
   532 		 * the base slug of the setting name.
   532 		 * the base slug of the setting name.
   533 		 *
   533 		 *
   534 		 * @since 3.4.0
   534 		 * @since 3.4.0
   535 		 *
   535 		 *
   536 		 * @param WP_Customize_Setting $this WP_Customize_Setting instance.
   536 		 * @param WP_Customize_Setting $setting WP_Customize_Setting instance.
   537 		 */
   537 		 */
   538 		do_action( "customize_save_{$id_base}", $this );
   538 		do_action( "customize_save_{$id_base}", $this );
   539 
   539 
   540 		$this->update( $value );
   540 		$this->update( $value );
   541 	}
   541 	}
   545 	 *
   545 	 *
   546 	 * During a save request prior to save, post_value() provides the new value while value() does not.
   546 	 * During a save request prior to save, post_value() provides the new value while value() does not.
   547 	 *
   547 	 *
   548 	 * @since 3.4.0
   548 	 * @since 3.4.0
   549 	 *
   549 	 *
   550 	 * @param mixed $default A default value which is used as a fallback. Default null.
   550 	 * @param mixed $default_value A default value which is used as a fallback. Default null.
   551 	 * @return mixed The default value on failure, otherwise the sanitized and validated value.
   551 	 * @return mixed The default value on failure, otherwise the sanitized and validated value.
   552 	 */
   552 	 */
   553 	final public function post_value( $default = null ) {
   553 	final public function post_value( $default_value = null ) {
   554 		return $this->manager->post_value( $this, $default );
   554 		return $this->manager->post_value( $this, $default_value );
   555 	}
   555 	}
   556 
   556 
   557 	/**
   557 	/**
   558 	 * Sanitize an input.
   558 	 * Sanitize an input.
   559 	 *
   559 	 *
   560 	 * @since 3.4.0
   560 	 * @since 3.4.0
   561 	 *
   561 	 *
   562 	 * @param string|array $value    The value to sanitize.
   562 	 * @param string|array $value The value to sanitize.
   563 	 * @return string|array|null|WP_Error Sanitized value, or `null`/`WP_Error` if invalid.
   563 	 * @return string|array|null|WP_Error Sanitized value, or `null`/`WP_Error` if invalid.
   564 	 */
   564 	 */
   565 	public function sanitize( $value ) {
   565 	public function sanitize( $value ) {
   566 
   566 
   567 		/**
   567 		/**
   568 		 * Filters a Customize setting value in un-slashed form.
   568 		 * Filters a Customize setting value in un-slashed form.
   569 		 *
   569 		 *
   570 		 * @since 3.4.0
   570 		 * @since 3.4.0
   571 		 *
   571 		 *
   572 		 * @param mixed                $value Value of the setting.
   572 		 * @param mixed                $value   Value of the setting.
   573 		 * @param WP_Customize_Setting $this  WP_Customize_Setting instance.
   573 		 * @param WP_Customize_Setting $setting WP_Customize_Setting instance.
   574 		 */
   574 		 */
   575 		return apply_filters( "customize_sanitize_{$this->id}", $value, $this );
   575 		return apply_filters( "customize_sanitize_{$this->id}", $value, $this );
   576 	}
   576 	}
   577 
   577 
   578 	/**
   578 	/**
   619 	/**
   619 	/**
   620 	 * Get the root value for a setting, especially for multidimensional ones.
   620 	 * Get the root value for a setting, especially for multidimensional ones.
   621 	 *
   621 	 *
   622 	 * @since 4.4.0
   622 	 * @since 4.4.0
   623 	 *
   623 	 *
   624 	 * @param mixed $default Value to return if root does not exist.
   624 	 * @param mixed $default_value Value to return if root does not exist.
   625 	 * @return mixed
   625 	 * @return mixed
   626 	 */
   626 	 */
   627 	protected function get_root_value( $default = null ) {
   627 	protected function get_root_value( $default_value = null ) {
   628 		$id_base = $this->id_data['base'];
   628 		$id_base = $this->id_data['base'];
   629 		if ( 'option' === $this->type ) {
   629 		if ( 'option' === $this->type ) {
   630 			return get_option( $id_base, $default );
   630 			return get_option( $id_base, $default_value );
   631 		} elseif ( 'theme_mod' === $this->type ) {
   631 		} elseif ( 'theme_mod' === $this->type ) {
   632 			return get_theme_mod( $id_base, $default );
   632 			return get_theme_mod( $id_base, $default_value );
   633 		} else {
   633 		} else {
   634 			/*
   634 			/*
   635 			 * Any WP_Customize_Setting subclass implementing aggregate multidimensional
   635 			 * Any WP_Customize_Setting subclass implementing aggregate multidimensional
   636 			 * will need to override this method to obtain the data from the appropriate
   636 			 * will need to override this method to obtain the data from the appropriate
   637 			 * location.
   637 			 * location.
   638 			 */
   638 			 */
   639 			return $default;
   639 			return $default_value;
   640 		}
   640 		}
   641 	}
   641 	}
   642 
   642 
   643 	/**
   643 	/**
   644 	 * Set the root value for a setting, especially for multidimensional ones.
   644 	 * Set the root value for a setting, especially for multidimensional ones.
   695 			 *
   695 			 *
   696 			 * The dynamic portion of the hook name, `$this->type`, refers to the type of setting.
   696 			 * The dynamic portion of the hook name, `$this->type`, refers to the type of setting.
   697 			 *
   697 			 *
   698 			 * @since 3.4.0
   698 			 * @since 3.4.0
   699 			 *
   699 			 *
   700 			 * @param mixed                $value Value of the setting.
   700 			 * @param mixed                $value   Value of the setting.
   701 			 * @param WP_Customize_Setting $this  WP_Customize_Setting instance.
   701 			 * @param WP_Customize_Setting $setting WP_Customize_Setting instance.
   702 			 */
   702 			 */
   703 			do_action( "customize_update_{$this->type}", $value, $this );
   703 			do_action( "customize_update_{$this->type}", $value, $this );
   704 
   704 
   705 			return has_action( "customize_update_{$this->type}" );
   705 			return has_action( "customize_update_{$this->type}" );
   706 		}
   706 		}
   759 			 * functions for available hooks.
   759 			 * functions for available hooks.
   760 			 *
   760 			 *
   761 			 * @since 3.4.0
   761 			 * @since 3.4.0
   762 			 * @since 4.6.0 Added the `$this` setting instance as the second parameter.
   762 			 * @since 4.6.0 Added the `$this` setting instance as the second parameter.
   763 			 *
   763 			 *
   764 			 * @param mixed                $default The setting default value. Default empty.
   764 			 * @param mixed                $default_value The setting default value. Default empty.
   765 			 * @param WP_Customize_Setting $setting The setting instance.
   765 			 * @param WP_Customize_Setting $setting       The setting instance.
   766 			 */
   766 			 */
   767 			$value = apply_filters( "customize_value_{$id_base}", $value, $this );
   767 			$value = apply_filters( "customize_value_{$id_base}", $value, $this );
   768 		} elseif ( $this->is_multidimensional_aggregated ) {
   768 		} elseif ( $this->is_multidimensional_aggregated ) {
   769 			$root_value = self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'];
   769 			$root_value = self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'];
   770 			$value      = $this->multidimensional_get( $root_value, $this->id_data['keys'], $this->default );
   770 			$value      = $this->multidimensional_get( $root_value, $this->id_data['keys'], $this->default );
   928 	 *
   928 	 *
   929 	 * @since 3.4.0
   929 	 * @since 3.4.0
   930 	 *
   930 	 *
   931 	 * @param array $root
   931 	 * @param array $root
   932 	 * @param array $keys
   932 	 * @param array $keys
   933 	 * @param mixed $default A default value which is used as a fallback. Default null.
   933 	 * @param mixed $default_value A default value which is used as a fallback. Default null.
   934 	 * @return mixed The requested value or the default value.
   934 	 * @return mixed The requested value or the default value.
   935 	 */
   935 	 */
   936 	final protected function multidimensional_get( $root, $keys, $default = null ) {
   936 	final protected function multidimensional_get( $root, $keys, $default_value = null ) {
   937 		if ( empty( $keys ) ) { // If there are no keys, test the root.
   937 		if ( empty( $keys ) ) { // If there are no keys, test the root.
   938 			return isset( $root ) ? $root : $default;
   938 			return isset( $root ) ? $root : $default_value;
   939 		}
   939 		}
   940 
   940 
   941 		$result = $this->multidimensional( $root, $keys );
   941 		$result = $this->multidimensional( $root, $keys );
   942 		return isset( $result ) ? $result['node'][ $result['key'] ] : $default;
   942 		return isset( $result ) ? $result['node'][ $result['key'] ] : $default_value;
   943 	}
   943 	}
   944 
   944 
   945 	/**
   945 	/**
   946 	 * Will attempt to check if a specific value in a multidimensional array is set.
   946 	 * Will attempt to check if a specific value in a multidimensional array is set.
   947 	 *
   947 	 *