wp/wp-includes/class-wp-customize-setting.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    13  * Handles saving and sanitizing of settings.
    13  * Handles saving and sanitizing of settings.
    14  *
    14  *
    15  * @since 3.4.0
    15  * @since 3.4.0
    16  *
    16  *
    17  * @see WP_Customize_Manager
    17  * @see WP_Customize_Manager
       
    18  * @link https://developer.wordpress.org/themes/customize-api
    18  */
    19  */
    19 class WP_Customize_Setting {
    20 class WP_Customize_Setting {
    20 	/**
    21 	/**
    21 	 * Customizer bootstrap instance.
    22 	 * Customizer bootstrap instance.
    22 	 *
    23 	 *
    48 	 * @var string|array
    49 	 * @var string|array
    49 	 */
    50 	 */
    50 	public $capability = 'edit_theme_options';
    51 	public $capability = 'edit_theme_options';
    51 
    52 
    52 	/**
    53 	/**
    53 	 * Feature a theme is required to support to enable this setting.
    54 	 * Theme features required to support the setting.
    54 	 *
    55 	 *
    55 	 * @since 3.4.0
    56 	 * @since 3.4.0
    56 	 * @var string
    57 	 * @var string|string[]
    57 	 */
    58 	 */
    58 	public $theme_supports = '';
    59 	public $theme_supports = '';
    59 
    60 
    60 	/**
    61 	/**
    61 	 * The default value for the setting.
    62 	 * The default value for the setting.
    69 	 * Options for rendering the live preview of changes in Customizer.
    70 	 * Options for rendering the live preview of changes in Customizer.
    70 	 *
    71 	 *
    71 	 * Set this value to 'postMessage' to enable a custom JavaScript handler to render changes to this setting
    72 	 * Set this value to 'postMessage' to enable a custom JavaScript handler to render changes to this setting
    72 	 * as opposed to reloading the whole page.
    73 	 * as opposed to reloading the whole page.
    73 	 *
    74 	 *
    74 	 * @link https://developer.wordpress.org/themes/customize-api
       
    75 	 *
       
    76 	 * @since 3.4.0
    75 	 * @since 3.4.0
    77 	 * @var string
    76 	 * @var string
    78 	 */
    77 	 */
    79 	public $transport = 'refresh';
    78 	public $transport = 'refresh';
    80 
    79 
    96 
    95 
    97 	/**
    96 	/**
    98 	 * Callback to convert a Customize PHP setting value to a value that is JSON serializable.
    97 	 * Callback to convert a Customize PHP setting value to a value that is JSON serializable.
    99 	 *
    98 	 *
   100 	 * @since 3.4.0
    99 	 * @since 3.4.0
   101 	 * @var string
   100 	 * @var callable
   102 	 */
   101 	 */
   103 	public $sanitize_js_callback = '';
   102 	public $sanitize_js_callback = '';
   104 
   103 
   105 	/**
   104 	/**
   106 	 * Whether or not the setting is initially dirty when created.
   105 	 * Whether or not the setting is initially dirty when created.
   152 	 *
   151 	 *
   153 	 * Any supplied $args override class property defaults.
   152 	 * Any supplied $args override class property defaults.
   154 	 *
   153 	 *
   155 	 * @since 3.4.0
   154 	 * @since 3.4.0
   156 	 *
   155 	 *
   157 	 * @param WP_Customize_Manager $manager
   156 	 * @param WP_Customize_Manager $manager Customizer bootstrap instance.
   158 	 * @param string               $id      An specific ID of the setting. Can be a
   157 	 * @param string               $id      A specific ID of the setting.
   159 	 *                                      theme mod or option name.
   158 	 *                                      Can be a theme mod or option name.
   160 	 * @param array                $args    Setting arguments.
   159 	 * @param array                $args    {
       
   160 	 *     Optional. Array of properties for the new Setting object. Default empty array.
       
   161 	 *
       
   162 	 *     @type string          $type                 Type of the setting. Default 'theme_mod'.
       
   163 	 *     @type string          $capability           Capability required for the setting. Default 'edit_theme_options'
       
   164 	 *     @type string|string[] $theme_supports       Theme features required to support the panel. Default is none.
       
   165 	 *     @type string          $default              Default value for the setting. Default is empty string.
       
   166 	 *     @type string          $transport            Options for rendering the live preview of changes in Customizer.
       
   167 	 *                                                 Using 'refresh' makes the change visible by reloading the whole preview.
       
   168 	 *                                                 Using 'postMessage' allows a custom JavaScript to handle live changes.
       
   169 	 *                                                 Default is 'refresh'.
       
   170 	 *     @type callable        $validate_callback    Server-side validation callback for the setting's value.
       
   171 	 *     @type callable        $sanitize_callback    Callback to filter a Customize setting value in un-slashed form.
       
   172 	 *     @type callable        $sanitize_js_callback Callback to convert a Customize PHP setting value to a value that is
       
   173 	 *                                                 JSON serializable.
       
   174 	 *     @type bool            $dirty                Whether or not the setting is initially dirty when created.
       
   175 	 * }
   161 	 */
   176 	 */
   162 	public function __construct( $manager, $id, $args = array() ) {
   177 	public function __construct( $manager, $id, $args = array() ) {
   163 		$keys = array_keys( get_object_vars( $this ) );
   178 		$keys = array_keys( get_object_vars( $this ) );
   164 		foreach ( $keys as $key ) {
   179 		foreach ( $keys as $key ) {
   165 			if ( isset( $args[ $key ] ) ) {
   180 			if ( isset( $args[ $key ] ) ) {
   496 	 * Checks user capabilities and theme supports, and then saves
   511 	 * Checks user capabilities and theme supports, and then saves
   497 	 * the value of the setting.
   512 	 * the value of the setting.
   498 	 *
   513 	 *
   499 	 * @since 3.4.0
   514 	 * @since 3.4.0
   500 	 *
   515 	 *
   501 	 * @return false|void False if cap check fails or value isn't set or is invalid.
   516 	 * @return void|false False if cap check fails or value isn't set or is invalid.
   502 	 */
   517 	 */
   503 	final public function save() {
   518 	final public function save() {
   504 		$value = $this->post_value();
   519 		$value = $this->post_value();
   505 
   520 
   506 		if ( ! $this->check_capabilities() || ! isset( $value ) ) {
   521 		if ( ! $this->check_capabilities() || ! isset( $value ) ) {
   529 	 *
   544 	 *
   530 	 * During a save request prior to save, post_value() provides the new value while value() does not.
   545 	 * During a save request prior to save, post_value() provides the new value while value() does not.
   531 	 *
   546 	 *
   532 	 * @since 3.4.0
   547 	 * @since 3.4.0
   533 	 *
   548 	 *
   534 	 * @param mixed $default A default value which is used as a fallback. Default is null.
   549 	 * @param mixed $default A default value which is used as a fallback. Default null.
   535 	 * @return mixed The default value on failure, otherwise the sanitized and validated value.
   550 	 * @return mixed The default value on failure, otherwise the sanitized and validated value.
   536 	 */
   551 	 */
   537 	final public function post_value( $default = null ) {
   552 	final public function post_value( $default = null ) {
   538 		return $this->manager->post_value( $this, $default );
   553 		return $this->manager->post_value( $this, $default );
   539 	}
   554 	}
   813 	 * @since 3.4.0
   828 	 * @since 3.4.0
   814 	 *
   829 	 *
   815 	 * @return bool False if theme doesn't support the setting or user can't change setting, otherwise true.
   830 	 * @return bool False if theme doesn't support the setting or user can't change setting, otherwise true.
   816 	 */
   831 	 */
   817 	final public function check_capabilities() {
   832 	final public function check_capabilities() {
   818 		if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) {
   833 		if ( $this->capability && ! current_user_can( $this->capability ) ) {
   819 			return false;
   834 			return false;
   820 		}
   835 		}
   821 
   836 
   822 		if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) ) {
   837 		if ( $this->theme_supports && ! current_theme_supports( ... (array) $this->theme_supports ) ) {
   823 			return false;
   838 			return false;
   824 		}
   839 		}
   825 
   840 
   826 		return true;
   841 		return true;
   827 	}
   842 	}
   829 	/**
   844 	/**
   830 	 * Multidimensional helper function.
   845 	 * Multidimensional helper function.
   831 	 *
   846 	 *
   832 	 * @since 3.4.0
   847 	 * @since 3.4.0
   833 	 *
   848 	 *
   834 	 * @param $root
   849 	 * @param array $root
   835 	 * @param $keys
   850 	 * @param array $keys
   836 	 * @param bool $create Default is false.
   851 	 * @param bool  $create Default false.
   837 	 * @return array|void Keys are 'root', 'node', and 'key'.
   852 	 * @return array|void Keys are 'root', 'node', and 'key'.
   838 	 */
   853 	 */
   839 	final protected function multidimensional( &$root, $keys, $create = false ) {
   854 	final protected function multidimensional( &$root, $keys, $create = false ) {
   840 		if ( $create && empty( $root ) ) {
   855 		if ( $create && empty( $root ) ) {
   841 			$root = array();
   856 			$root = array();
   860 			$node = &$node[ $key ];
   875 			$node = &$node[ $key ];
   861 		}
   876 		}
   862 
   877 
   863 		if ( $create ) {
   878 		if ( $create ) {
   864 			if ( ! is_array( $node ) ) {
   879 			if ( ! is_array( $node ) ) {
   865 				// account for an array overriding a string or object value
   880 				// Account for an array overriding a string or object value.
   866 				$node = array();
   881 				$node = array();
   867 			}
   882 			}
   868 			if ( ! isset( $node[ $last ] ) ) {
   883 			if ( ! isset( $node[ $last ] ) ) {
   869 				$node[ $last ] = array();
   884 				$node[ $last ] = array();
   870 			}
   885 			}
   884 	/**
   899 	/**
   885 	 * Will attempt to replace a specific value in a multidimensional array.
   900 	 * Will attempt to replace a specific value in a multidimensional array.
   886 	 *
   901 	 *
   887 	 * @since 3.4.0
   902 	 * @since 3.4.0
   888 	 *
   903 	 *
   889 	 * @param $root
   904 	 * @param array $root
   890 	 * @param $keys
   905 	 * @param array $keys
   891 	 * @param mixed $value The value to update.
   906 	 * @param mixed $value The value to update.
   892 	 * @return mixed
   907 	 * @return mixed
   893 	 */
   908 	 */
   894 	final protected function multidimensional_replace( $root, $keys, $value ) {
   909 	final protected function multidimensional_replace( $root, $keys, $value ) {
   895 		if ( ! isset( $value ) ) {
   910 		if ( ! isset( $value ) ) {
   910 	/**
   925 	/**
   911 	 * Will attempt to fetch a specific value from a multidimensional array.
   926 	 * Will attempt to fetch a specific value from a multidimensional array.
   912 	 *
   927 	 *
   913 	 * @since 3.4.0
   928 	 * @since 3.4.0
   914 	 *
   929 	 *
   915 	 * @param $root
   930 	 * @param array $root
   916 	 * @param $keys
   931 	 * @param array $keys
   917 	 * @param mixed $default A default value which is used as a fallback. Default is null.
   932 	 * @param mixed $default A default value which is used as a fallback. Default null.
   918 	 * @return mixed The requested value or the default value.
   933 	 * @return mixed The requested value or the default value.
   919 	 */
   934 	 */
   920 	final protected function multidimensional_get( $root, $keys, $default = null ) {
   935 	final protected function multidimensional_get( $root, $keys, $default = null ) {
   921 		if ( empty( $keys ) ) { // If there are no keys, test the root.
   936 		if ( empty( $keys ) ) { // If there are no keys, test the root.
   922 			return isset( $root ) ? $root : $default;
   937 			return isset( $root ) ? $root : $default;
   929 	/**
   944 	/**
   930 	 * Will attempt to check if a specific value in a multidimensional array is set.
   945 	 * Will attempt to check if a specific value in a multidimensional array is set.
   931 	 *
   946 	 *
   932 	 * @since 3.4.0
   947 	 * @since 3.4.0
   933 	 *
   948 	 *
   934 	 * @param $root
   949 	 * @param array $root
   935 	 * @param $keys
   950 	 * @param array $keys
   936 	 * @return bool True if value is set, false if not.
   951 	 * @return bool True if value is set, false if not.
   937 	 */
   952 	 */
   938 	final protected function multidimensional_isset( $root, $keys ) {
   953 	final protected function multidimensional_isset( $root, $keys ) {
   939 		$result = $this->multidimensional_get( $root, $keys );
   954 		$result = $this->multidimensional_get( $root, $keys );
   940 		return isset( $result );
   955 		return isset( $result );
   942 }
   957 }
   943 
   958 
   944 /**
   959 /**
   945  * WP_Customize_Filter_Setting class.
   960  * WP_Customize_Filter_Setting class.
   946  */
   961  */
   947 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-filter-setting.php' );
   962 require_once ABSPATH . WPINC . '/customize/class-wp-customize-filter-setting.php';
   948 
   963 
   949 /**
   964 /**
   950  * WP_Customize_Header_Image_Setting class.
   965  * WP_Customize_Header_Image_Setting class.
   951  */
   966  */
   952 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-header-image-setting.php' );
   967 require_once ABSPATH . WPINC . '/customize/class-wp-customize-header-image-setting.php';
   953 
   968 
   954 /**
   969 /**
   955  * WP_Customize_Background_Image_Setting class.
   970  * WP_Customize_Background_Image_Setting class.
   956  */
   971  */
   957 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-background-image-setting.php' );
   972 require_once ABSPATH . WPINC . '/customize/class-wp-customize-background-image-setting.php';
   958 
   973 
   959 /**
   974 /**
   960  * WP_Customize_Nav_Menu_Item_Setting class.
   975  * WP_Customize_Nav_Menu_Item_Setting class.
   961  */
   976  */
   962 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-item-setting.php' );
   977 require_once ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-item-setting.php';
   963 
   978 
   964 /**
   979 /**
   965  * WP_Customize_Nav_Menu_Setting class.
   980  * WP_Customize_Nav_Menu_Setting class.
   966  */
   981  */
   967 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-setting.php' );
   982 require_once ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-setting.php';