2012-02-24 23:12:43 -05:00
|
|
|
<?php
|
|
|
|
/**
|
2015-02-25 03:18:26 -05:00
|
|
|
* WordPress Customize Setting classes
|
2014-03-04 15:21:14 -05:00
|
|
|
*
|
2012-02-24 23:12:43 -05:00
|
|
|
* @package WordPress
|
|
|
|
* @subpackage Customize
|
|
|
|
* @since 3.4.0
|
|
|
|
*/
|
2015-02-25 03:18:26 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Customize Setting class.
|
|
|
|
*
|
|
|
|
* Handles saving and sanitizing of settings.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @see WP_Customize_Manager
|
|
|
|
*/
|
2012-02-24 23:12:43 -05:00
|
|
|
class WP_Customize_Setting {
|
2014-03-04 15:21:14 -05:00
|
|
|
/**
|
|
|
|
* @access public
|
|
|
|
* @var WP_Customize_Manager
|
|
|
|
*/
|
2012-03-21 18:55:43 -04:00
|
|
|
public $manager;
|
2014-03-04 15:21:14 -05:00
|
|
|
|
|
|
|
/**
|
2015-09-05 15:53:24 -04:00
|
|
|
* Unique string identifier for the setting.
|
|
|
|
*
|
2014-03-04 15:21:14 -05:00
|
|
|
* @access public
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-02-24 23:12:43 -05:00
|
|
|
public $id;
|
2012-03-28 00:14:09 -04:00
|
|
|
|
2014-03-04 15:21:14 -05:00
|
|
|
/**
|
|
|
|
* @access public
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $type = 'theme_mod';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Capability required to edit this setting.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $capability = 'edit_theme_options';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Feature a theme is required to support to enable this setting.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-05-26 14:44:31 -04:00
|
|
|
public $theme_supports = '';
|
|
|
|
public $default = '';
|
|
|
|
public $transport = 'refresh';
|
|
|
|
|
2014-03-04 15:21:14 -05:00
|
|
|
/**
|
|
|
|
* Server-side sanitization callback for the setting's value.
|
|
|
|
*
|
|
|
|
* @var callback
|
|
|
|
*/
|
Customize: Add setting validation model and control notifications to augment setting sanitization.
When a setting is invalid, not only will it be blocked from being saved but all other settings will be blocked as well. This ensures that Customizer saves aren't partial but are more transactional. User will be displayed the error in a notification so that they can fix and re-attempt saving.
PHP changes:
* Introduces `WP_Customize_Setting::validate()`, `WP_Customize_Setting::$validate_callback`, and the `customize_validate_{$setting_id}` filter.
* Introduces `WP_Customize_Manager::validate_setting_values()` to do validation (and sanitization) for the setting values supplied, returning a list of `WP_Error` instances for invalid settings.
* Attempting to save settings that are invalid will result in the save being blocked entirely, with the errors being sent in the `customize_save_response`. Modifies `WP_Customize_Manager::save()` to check all settings for validity issues prior to calling their `save` methods.
* Introduces `WP_Customize_Setting::json()` for parity with the other Customizer classes. This includes exporting of the `type`.
* Modifies `WP_Customize_Manager::post_value()` to apply `validate` after `sanitize`, and if validation fails, to return the `$default`.
* Introduces `customize_save_validation_before` action which fires right before the validation checks are made prior to saving.
JS changes:
* Introduces `wp.customize.Notification` in JS which to represent `WP_Error` instances returned from the server when setting validation fails.
* Introduces `wp.customize.Setting.prototype.notifications`.
* Introduces `wp.customize.Control.prototype.notifications`, which are synced with a control's settings' notifications.
* Introduces `wp.customize.Control.prototype.renderNotifications()` to re-render a control's notifications in its notification area. This is called automatically when the notifications collection changes.
* Introduces `wp.customize.settingConstructor`, allowing custom setting types to be used in the same way that custom controls, panels, and sections can be made.
* Injects a notification area into existing controls which is populated in response to the control's `notifications` collection changing. A custom control can customize the placement of the notification area by overriding the new `getNotificationsContainerElement` method.
* When a save fails due to setting invalidity, the invalidity errors will be added to the settings to then populate in the controls' notification areas, and the first such invalid control will be focused.
Props westonruter, celloexpressions, mrahmadawais.
See #35210.
See #30937.
Fixes #34893.
Built from https://develop.svn.wordpress.org/trunk@37476
git-svn-id: http://core.svn.wordpress.org/trunk@37444 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-05-20 17:10:27 -04:00
|
|
|
public $validate_callback = '';
|
2012-05-26 14:44:31 -04:00
|
|
|
public $sanitize_callback = '';
|
|
|
|
public $sanitize_js_callback = '';
|
2012-02-24 23:12:43 -05:00
|
|
|
|
2015-03-10 18:39:28 -04:00
|
|
|
/**
|
|
|
|
* Whether or not the setting is initially dirty when created.
|
|
|
|
*
|
|
|
|
* This is used to ensure that a setting will be sent from the pane to the
|
|
|
|
* preview when loading the Customizer. Normally a setting only is synced to
|
|
|
|
* the preview if it has been changed. This allows the setting to be sent
|
|
|
|
* from the start.
|
|
|
|
*
|
|
|
|
* @since 4.2.0
|
|
|
|
* @access public
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $dirty = false;
|
|
|
|
|
2015-09-05 15:53:24 -04:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2012-02-24 23:12:43 -05:00
|
|
|
protected $id_data = array();
|
2014-03-04 15:21:14 -05:00
|
|
|
|
2015-11-20 21:52:27 -05:00
|
|
|
/**
|
|
|
|
* Whether or not preview() was called.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
* @access protected
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $is_previewed = false;
|
|
|
|
|
2015-10-10 05:06:25 -04:00
|
|
|
/**
|
|
|
|
* Cache of multidimensional values to improve performance.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
* @access protected
|
|
|
|
* @var array
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
protected static $aggregated_multidimensionals = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the multidimensional setting is aggregated.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
* @access protected
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $is_multidimensional_aggregated = false;
|
|
|
|
|
2012-02-24 23:12:43 -05:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2014-03-04 15:21:14 -05:00
|
|
|
* Any supplied $args override class property defaults.
|
|
|
|
*
|
2012-02-24 23:12:43 -05:00
|
|
|
* @since 3.4.0
|
|
|
|
*
|
2012-07-26 17:45:33 -04:00
|
|
|
* @param WP_Customize_Manager $manager
|
2014-03-04 15:21:14 -05:00
|
|
|
* @param string $id An specific ID of the setting. Can be a
|
|
|
|
* theme mod or option name.
|
|
|
|
* @param array $args Setting arguments.
|
2012-02-24 23:12:43 -05:00
|
|
|
*/
|
2014-05-19 01:45:16 -04:00
|
|
|
public function __construct( $manager, $id, $args = array() ) {
|
2014-06-24 18:56:14 -04:00
|
|
|
$keys = array_keys( get_object_vars( $this ) );
|
2012-02-24 23:12:43 -05:00
|
|
|
foreach ( $keys as $key ) {
|
2015-10-10 05:06:25 -04:00
|
|
|
if ( isset( $args[ $key ] ) ) {
|
2012-02-24 23:12:43 -05:00
|
|
|
$this->$key = $args[ $key ];
|
2015-10-10 05:06:25 -04:00
|
|
|
}
|
2012-02-24 23:12:43 -05:00
|
|
|
}
|
|
|
|
|
2012-03-21 18:55:43 -04:00
|
|
|
$this->manager = $manager;
|
2012-02-24 23:12:43 -05:00
|
|
|
$this->id = $id;
|
|
|
|
|
|
|
|
// Parse the ID for array keys.
|
2015-10-10 05:06:25 -04:00
|
|
|
$this->id_data['keys'] = preg_split( '/\[/', str_replace( ']', '', $this->id ) );
|
|
|
|
$this->id_data['base'] = array_shift( $this->id_data['keys'] );
|
2012-02-24 23:12:43 -05:00
|
|
|
|
|
|
|
// Rebuild the ID.
|
|
|
|
$this->id = $this->id_data[ 'base' ];
|
2015-10-10 05:06:25 -04:00
|
|
|
if ( ! empty( $this->id_data[ 'keys' ] ) ) {
|
|
|
|
$this->id .= '[' . implode( '][', $this->id_data['keys'] ) . ']';
|
|
|
|
}
|
2012-02-24 23:12:43 -05:00
|
|
|
|
Customize: Add setting validation model and control notifications to augment setting sanitization.
When a setting is invalid, not only will it be blocked from being saved but all other settings will be blocked as well. This ensures that Customizer saves aren't partial but are more transactional. User will be displayed the error in a notification so that they can fix and re-attempt saving.
PHP changes:
* Introduces `WP_Customize_Setting::validate()`, `WP_Customize_Setting::$validate_callback`, and the `customize_validate_{$setting_id}` filter.
* Introduces `WP_Customize_Manager::validate_setting_values()` to do validation (and sanitization) for the setting values supplied, returning a list of `WP_Error` instances for invalid settings.
* Attempting to save settings that are invalid will result in the save being blocked entirely, with the errors being sent in the `customize_save_response`. Modifies `WP_Customize_Manager::save()` to check all settings for validity issues prior to calling their `save` methods.
* Introduces `WP_Customize_Setting::json()` for parity with the other Customizer classes. This includes exporting of the `type`.
* Modifies `WP_Customize_Manager::post_value()` to apply `validate` after `sanitize`, and if validation fails, to return the `$default`.
* Introduces `customize_save_validation_before` action which fires right before the validation checks are made prior to saving.
JS changes:
* Introduces `wp.customize.Notification` in JS which to represent `WP_Error` instances returned from the server when setting validation fails.
* Introduces `wp.customize.Setting.prototype.notifications`.
* Introduces `wp.customize.Control.prototype.notifications`, which are synced with a control's settings' notifications.
* Introduces `wp.customize.Control.prototype.renderNotifications()` to re-render a control's notifications in its notification area. This is called automatically when the notifications collection changes.
* Introduces `wp.customize.settingConstructor`, allowing custom setting types to be used in the same way that custom controls, panels, and sections can be made.
* Injects a notification area into existing controls which is populated in response to the control's `notifications` collection changing. A custom control can customize the placement of the notification area by overriding the new `getNotificationsContainerElement` method.
* When a save fails due to setting invalidity, the invalidity errors will be added to the settings to then populate in the controls' notification areas, and the first such invalid control will be focused.
Props westonruter, celloexpressions, mrahmadawais.
See #35210.
See #30937.
Fixes #34893.
Built from https://develop.svn.wordpress.org/trunk@37476
git-svn-id: http://core.svn.wordpress.org/trunk@37444 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-05-20 17:10:27 -04:00
|
|
|
if ( $this->validate_callback ) {
|
|
|
|
add_filter( "customize_validate_{$this->id}", $this->validate_callback, 10, 3 );
|
|
|
|
}
|
2015-10-10 05:06:25 -04:00
|
|
|
if ( $this->sanitize_callback ) {
|
2012-05-26 14:44:31 -04:00
|
|
|
add_filter( "customize_sanitize_{$this->id}", $this->sanitize_callback, 10, 2 );
|
2015-10-10 05:06:25 -04:00
|
|
|
}
|
|
|
|
if ( $this->sanitize_js_callback ) {
|
2012-05-26 14:44:31 -04:00
|
|
|
add_filter( "customize_sanitize_js_{$this->id}", $this->sanitize_js_callback, 10, 2 );
|
2015-10-10 05:06:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'option' === $this->type || 'theme_mod' === $this->type ) {
|
|
|
|
// Other setting types can opt-in to aggregate multidimensional explicitly.
|
|
|
|
$this->aggregate_multidimensional();
|
2015-10-20 17:19:25 -04:00
|
|
|
|
|
|
|
// Allow option settings to indicate whether they should be autoloaded.
|
|
|
|
if ( 'option' === $this->type && isset( $args['autoload'] ) ) {
|
|
|
|
self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['autoload'] = $args['autoload'];
|
|
|
|
}
|
2015-10-10 05:06:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get parsed ID data for multidimensional setting.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
* @access public
|
|
|
|
*
|
|
|
|
* @return array {
|
|
|
|
* ID data for multidimensional setting.
|
|
|
|
*
|
|
|
|
* @type string $base ID base
|
|
|
|
* @type array $keys Keys for multidimensional array.
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
final public function id_data() {
|
|
|
|
return $this->id_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up the setting for aggregated multidimensional values.
|
|
|
|
*
|
|
|
|
* When a multidimensional setting gets aggregated, all of its preview and update
|
|
|
|
* calls get combined into one call, greatly improving performance.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
protected function aggregate_multidimensional() {
|
|
|
|
$id_base = $this->id_data['base'];
|
|
|
|
if ( ! isset( self::$aggregated_multidimensionals[ $this->type ] ) ) {
|
|
|
|
self::$aggregated_multidimensionals[ $this->type ] = array();
|
|
|
|
}
|
|
|
|
if ( ! isset( self::$aggregated_multidimensionals[ $this->type ][ $id_base ] ) ) {
|
|
|
|
self::$aggregated_multidimensionals[ $this->type ][ $id_base ] = array(
|
|
|
|
'previewed_instances' => array(), // Calling preview() will add the $setting to the array.
|
|
|
|
'preview_applied_instances' => array(), // Flags for which settings have had their values applied.
|
|
|
|
'root_value' => $this->get_root_value( array() ), // Root value for initial state, manipulated by preview and update calls.
|
|
|
|
);
|
|
|
|
}
|
2015-10-20 17:19:25 -04:00
|
|
|
|
|
|
|
if ( ! empty( $this->id_data['keys'] ) ) {
|
2015-11-20 21:52:27 -05:00
|
|
|
// Note the preview-applied flag is cleared at priority 9 to ensure it is cleared before a deferred-preview runs.
|
|
|
|
add_action( "customize_post_value_set_{$this->id}", array( $this, '_clear_aggregated_multidimensional_preview_applied_flag' ), 9 );
|
2015-10-20 17:19:25 -04:00
|
|
|
$this->is_multidimensional_aggregated = true;
|
|
|
|
}
|
2012-02-24 23:12:43 -05:00
|
|
|
}
|
|
|
|
|
2016-04-07 16:59:29 -04:00
|
|
|
/**
|
|
|
|
* Reset `$aggregated_multidimensionals` static variable.
|
|
|
|
*
|
|
|
|
* This is intended only for use by unit tests.
|
|
|
|
*
|
|
|
|
* @since 4.5.0
|
|
|
|
* @access public
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
static public function reset_aggregated_multidimensionals() {
|
|
|
|
self::$aggregated_multidimensionals = array();
|
|
|
|
}
|
|
|
|
|
2015-03-10 19:05:27 -04:00
|
|
|
/**
|
2016-01-27 22:35:27 -05:00
|
|
|
* The ID for the current site when the preview() method was called.
|
2015-03-10 19:05:27 -04:00
|
|
|
*
|
|
|
|
* @since 4.2.0
|
2015-04-05 11:17:27 -04:00
|
|
|
* @access protected
|
2015-03-10 19:05:27 -04:00
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $_previewed_blog_id;
|
|
|
|
|
|
|
|
/**
|
2016-01-27 22:35:27 -05:00
|
|
|
* Return true if the current site is not the same as the previewed site.
|
2015-03-10 19:05:27 -04:00
|
|
|
*
|
|
|
|
* @since 4.2.0
|
2015-04-05 11:17:27 -04:00
|
|
|
* @access public
|
|
|
|
*
|
2015-06-14 14:56:26 -04:00
|
|
|
* @return bool If preview() has been called.
|
2015-03-10 19:05:27 -04:00
|
|
|
*/
|
|
|
|
public function is_current_blog_previewed() {
|
|
|
|
if ( ! isset( $this->_previewed_blog_id ) ) {
|
2015-06-14 14:56:26 -04:00
|
|
|
return false;
|
2015-03-10 19:05:27 -04:00
|
|
|
}
|
|
|
|
return ( get_current_blog_id() === $this->_previewed_blog_id );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Original non-previewed value stored by the preview method.
|
|
|
|
*
|
|
|
|
* @see WP_Customize_Setting::preview()
|
|
|
|
* @since 4.1.1
|
|
|
|
* @var mixed
|
|
|
|
*/
|
2015-02-03 05:15:21 -05:00
|
|
|
protected $_original_value;
|
|
|
|
|
2012-02-24 23:12:43 -05:00
|
|
|
/**
|
2015-10-10 05:06:25 -04:00
|
|
|
* Add filters to supply the setting's value when accessed.
|
|
|
|
*
|
|
|
|
* If the setting already has a pre-existing value and there is no incoming
|
|
|
|
* post value for the setting, then this method will short-circuit since
|
|
|
|
* there is no change to preview.
|
2012-02-24 23:12:43 -05:00
|
|
|
*
|
|
|
|
* @since 3.4.0
|
2015-10-10 05:06:25 -04:00
|
|
|
* @since 4.4.0 Added boolean return value.
|
|
|
|
* @access public
|
|
|
|
*
|
|
|
|
* @return bool False when preview short-circuits due no change needing to be previewed.
|
2012-02-24 23:12:43 -05:00
|
|
|
*/
|
|
|
|
public function preview() {
|
2015-03-10 19:05:27 -04:00
|
|
|
if ( ! isset( $this->_previewed_blog_id ) ) {
|
|
|
|
$this->_previewed_blog_id = get_current_blog_id();
|
|
|
|
}
|
2015-11-20 21:52:27 -05:00
|
|
|
|
|
|
|
// Prevent re-previewing an already-previewed setting.
|
|
|
|
if ( $this->is_previewed ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-10 05:06:25 -04:00
|
|
|
$id_base = $this->id_data['base'];
|
|
|
|
$is_multidimensional = ! empty( $this->id_data['keys'] );
|
|
|
|
$multidimensional_filter = array( $this, '_multidimensional_preview_filter' );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the setting has a pre-existing value (an isset check),
|
|
|
|
* and if doesn't have any incoming post value. If both checks are true,
|
|
|
|
* then the preview short-circuits because there is nothing that needs
|
|
|
|
* to be previewed.
|
|
|
|
*/
|
|
|
|
$undefined = new stdClass();
|
|
|
|
$needs_preview = ( $undefined !== $this->post_value( $undefined ) );
|
|
|
|
$value = null;
|
|
|
|
|
|
|
|
// Since no post value was defined, check if we have an initial value set.
|
|
|
|
if ( ! $needs_preview ) {
|
|
|
|
if ( $this->is_multidimensional_aggregated ) {
|
|
|
|
$root = self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'];
|
|
|
|
$value = $this->multidimensional_get( $root, $this->id_data['keys'], $undefined );
|
|
|
|
} else {
|
|
|
|
$default = $this->default;
|
|
|
|
$this->default = $undefined; // Temporarily set default to undefined so we can detect if existing value is set.
|
|
|
|
$value = $this->value();
|
|
|
|
$this->default = $default;
|
|
|
|
}
|
|
|
|
$needs_preview = ( $undefined === $value ); // Because the default needs to be supplied.
|
|
|
|
}
|
|
|
|
|
2015-11-20 21:52:27 -05:00
|
|
|
// If the setting does not need previewing now, defer to when it has a value to preview.
|
2015-10-10 05:06:25 -04:00
|
|
|
if ( ! $needs_preview ) {
|
2015-11-20 21:52:27 -05:00
|
|
|
if ( ! has_action( "customize_post_value_set_{$this->id}", array( $this, 'preview' ) ) ) {
|
|
|
|
add_action( "customize_post_value_set_{$this->id}", array( $this, 'preview' ) );
|
|
|
|
}
|
2015-10-10 05:06:25 -04:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-03 05:15:21 -05:00
|
|
|
|
2015-10-10 05:06:25 -04:00
|
|
|
switch ( $this->type ) {
|
2012-02-24 23:12:43 -05:00
|
|
|
case 'theme_mod' :
|
2015-10-10 05:06:25 -04:00
|
|
|
if ( ! $is_multidimensional ) {
|
|
|
|
add_filter( "theme_mod_{$id_base}", array( $this, '_preview_filter' ) );
|
|
|
|
} else {
|
|
|
|
if ( empty( self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'] ) ) {
|
|
|
|
// Only add this filter once for this ID base.
|
|
|
|
add_filter( "theme_mod_{$id_base}", $multidimensional_filter );
|
|
|
|
}
|
|
|
|
self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'][ $this->id ] = $this;
|
|
|
|
}
|
2012-02-24 23:12:43 -05:00
|
|
|
break;
|
|
|
|
case 'option' :
|
2015-10-10 05:06:25 -04:00
|
|
|
if ( ! $is_multidimensional ) {
|
|
|
|
add_filter( "pre_option_{$id_base}", array( $this, '_preview_filter' ) );
|
|
|
|
} else {
|
|
|
|
if ( empty( self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'] ) ) {
|
|
|
|
// Only add these filters once for this ID base.
|
|
|
|
add_filter( "option_{$id_base}", $multidimensional_filter );
|
|
|
|
add_filter( "default_option_{$id_base}", $multidimensional_filter );
|
|
|
|
}
|
|
|
|
self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'][ $this->id ] = $this;
|
2012-05-14 12:45:32 -04:00
|
|
|
}
|
2012-02-24 23:12:43 -05:00
|
|
|
break;
|
|
|
|
default :
|
2014-03-06 09:04:13 -05:00
|
|
|
|
|
|
|
/**
|
2016-05-02 00:00:28 -04:00
|
|
|
* Fires when the WP_Customize_Setting::preview() method is called for settings
|
2014-03-06 09:04:13 -05:00
|
|
|
* not handled as theme_mods or options.
|
|
|
|
*
|
2014-11-30 07:10:23 -05:00
|
|
|
* The dynamic portion of the hook name, `$this->id`, refers to the setting ID.
|
2014-03-06 09:04:13 -05:00
|
|
|
*
|
|
|
|
* @since 3.4.0
|
2014-06-17 19:35:15 -04:00
|
|
|
*
|
2016-05-02 00:00:28 -04:00
|
|
|
* @param WP_Customize_Setting $this WP_Customize_Setting instance.
|
2014-03-06 09:04:13 -05:00
|
|
|
*/
|
2014-10-17 17:21:19 -04:00
|
|
|
do_action( "customize_preview_{$this->id}", $this );
|
|
|
|
|
|
|
|
/**
|
2016-05-02 00:00:28 -04:00
|
|
|
* Fires when the WP_Customize_Setting::preview() method is called for settings
|
2014-10-17 17:21:19 -04:00
|
|
|
* not handled as theme_mods or options.
|
|
|
|
*
|
2014-11-28 06:13:24 -05:00
|
|
|
* The dynamic portion of the hook name, `$this->type`, refers to the setting type.
|
2014-10-17 17:21:19 -04:00
|
|
|
*
|
|
|
|
* @since 4.1.0
|
|
|
|
*
|
2016-05-02 00:00:28 -04:00
|
|
|
* @param WP_Customize_Setting $this WP_Customize_Setting instance.
|
2014-10-17 17:21:19 -04:00
|
|
|
*/
|
|
|
|
do_action( "customize_preview_{$this->type}", $this );
|
2012-02-24 23:12:43 -05:00
|
|
|
}
|
2015-11-20 21:52:27 -05:00
|
|
|
|
|
|
|
$this->is_previewed = true;
|
|
|
|
|
2015-10-10 05:06:25 -04:00
|
|
|
return true;
|
2012-02-24 23:12:43 -05:00
|
|
|
}
|
|
|
|
|
2015-11-20 21:52:27 -05:00
|
|
|
/**
|
|
|
|
* Clear out the previewed-applied flag for a multidimensional-aggregated value whenever its post value is updated.
|
|
|
|
*
|
|
|
|
* This ensures that the new value will get sanitized and used the next time
|
2016-02-23 14:18:27 -05:00
|
|
|
* that `WP_Customize_Setting::_multidimensional_preview_filter()`
|
2015-11-20 21:52:27 -05:00
|
|
|
* is called for this setting.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
* @access private
|
|
|
|
* @see WP_Customize_Manager::set_post_value()
|
|
|
|
* @see WP_Customize_Setting::_multidimensional_preview_filter()
|
|
|
|
*/
|
|
|
|
final public function _clear_aggregated_multidimensional_preview_applied_flag() {
|
|
|
|
unset( self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['preview_applied_instances'][ $this->id ] );
|
|
|
|
}
|
|
|
|
|
2012-02-24 23:12:43 -05:00
|
|
|
/**
|
2015-10-10 05:06:25 -04:00
|
|
|
* Callback function to filter non-multidimensional theme mods and options.
|
2012-02-24 23:12:43 -05:00
|
|
|
*
|
2015-03-10 19:05:27 -04:00
|
|
|
* If switch_to_blog() was called after the preview() method, and the current
|
2016-01-27 22:35:27 -05:00
|
|
|
* site is now not the same site, then this method does a no-op and returns
|
2015-03-10 19:05:27 -04:00
|
|
|
* the original value.
|
|
|
|
*
|
2012-02-24 23:12:43 -05:00
|
|
|
* @since 3.4.0
|
2012-02-27 14:46:52 -05:00
|
|
|
*
|
2012-07-26 17:45:33 -04:00
|
|
|
* @param mixed $original Old value.
|
2012-02-24 23:12:43 -05:00
|
|
|
* @return mixed New or old value.
|
|
|
|
*/
|
|
|
|
public function _preview_filter( $original ) {
|
2015-03-10 19:05:27 -04:00
|
|
|
if ( ! $this->is_current_blog_previewed() ) {
|
|
|
|
return $original;
|
|
|
|
}
|
|
|
|
|
2015-10-10 05:06:25 -04:00
|
|
|
$undefined = new stdClass(); // Symbol hack.
|
2015-02-08 18:11:25 -05:00
|
|
|
$post_value = $this->post_value( $undefined );
|
2015-10-10 05:06:25 -04:00
|
|
|
if ( $undefined !== $post_value ) {
|
2015-02-03 05:15:21 -05:00
|
|
|
$value = $post_value;
|
2015-10-10 05:06:25 -04:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Note that we don't use $original here because preview() will
|
|
|
|
* not add the filter in the first place if it has an initial value
|
|
|
|
* and there is no post value.
|
|
|
|
*/
|
|
|
|
$value = $this->default;
|
2015-02-03 05:15:21 -05:00
|
|
|
}
|
2015-10-10 05:06:25 -04:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback function to filter multidimensional theme mods and options.
|
|
|
|
*
|
|
|
|
* For all multidimensional settings of a given type, the preview filter for
|
|
|
|
* the first setting previewed will be used to apply the values for the others.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
2015-11-20 21:52:27 -05:00
|
|
|
* @access private
|
2015-10-10 05:06:25 -04:00
|
|
|
*
|
|
|
|
* @see WP_Customize_Setting::$aggregated_multidimensionals
|
|
|
|
* @param mixed $original Original root value.
|
|
|
|
* @return mixed New or old value.
|
|
|
|
*/
|
2015-11-20 21:52:27 -05:00
|
|
|
final public function _multidimensional_preview_filter( $original ) {
|
2015-10-10 05:06:25 -04:00
|
|
|
if ( ! $this->is_current_blog_previewed() ) {
|
|
|
|
return $original;
|
|
|
|
}
|
|
|
|
|
|
|
|
$id_base = $this->id_data['base'];
|
|
|
|
|
|
|
|
// If no settings have been previewed yet (which should not be the case, since $this is), just pass through the original value.
|
|
|
|
if ( empty( self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'] ) ) {
|
|
|
|
return $original;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'] as $previewed_setting ) {
|
|
|
|
// Skip applying previewed value for any settings that have already been applied.
|
|
|
|
if ( ! empty( self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['preview_applied_instances'][ $previewed_setting->id ] ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-02-03 05:15:21 -05:00
|
|
|
|
2015-10-10 05:06:25 -04:00
|
|
|
// Do the replacements of the posted/default sub value into the root value.
|
|
|
|
$value = $previewed_setting->post_value( $previewed_setting->default );
|
|
|
|
$root = self::$aggregated_multidimensionals[ $previewed_setting->type ][ $id_base ]['root_value'];
|
|
|
|
$root = $previewed_setting->multidimensional_replace( $root, $previewed_setting->id_data['keys'], $value );
|
|
|
|
self::$aggregated_multidimensionals[ $previewed_setting->type ][ $id_base ]['root_value'] = $root;
|
|
|
|
|
|
|
|
// Mark this setting having been applied so that it will be skipped when the filter is called again.
|
|
|
|
self::$aggregated_multidimensionals[ $previewed_setting->type ][ $id_base ]['preview_applied_instances'][ $previewed_setting->id ] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'];
|
2012-02-24 23:12:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-20 12:57:32 -04:00
|
|
|
* Checks user capabilities and theme supports, and then saves
|
2014-03-04 15:21:14 -05:00
|
|
|
* the value of the setting.
|
2012-02-24 23:12:43 -05:00
|
|
|
*
|
|
|
|
* @since 3.4.0
|
2016-08-04 18:54:31 -04:00
|
|
|
*
|
2016-07-20 12:57:32 -04:00
|
|
|
* @access public
|
2012-02-24 23:12:43 -05:00
|
|
|
*
|
Customize: Add setting validation model and control notifications to augment setting sanitization.
When a setting is invalid, not only will it be blocked from being saved but all other settings will be blocked as well. This ensures that Customizer saves aren't partial but are more transactional. User will be displayed the error in a notification so that they can fix and re-attempt saving.
PHP changes:
* Introduces `WP_Customize_Setting::validate()`, `WP_Customize_Setting::$validate_callback`, and the `customize_validate_{$setting_id}` filter.
* Introduces `WP_Customize_Manager::validate_setting_values()` to do validation (and sanitization) for the setting values supplied, returning a list of `WP_Error` instances for invalid settings.
* Attempting to save settings that are invalid will result in the save being blocked entirely, with the errors being sent in the `customize_save_response`. Modifies `WP_Customize_Manager::save()` to check all settings for validity issues prior to calling their `save` methods.
* Introduces `WP_Customize_Setting::json()` for parity with the other Customizer classes. This includes exporting of the `type`.
* Modifies `WP_Customize_Manager::post_value()` to apply `validate` after `sanitize`, and if validation fails, to return the `$default`.
* Introduces `customize_save_validation_before` action which fires right before the validation checks are made prior to saving.
JS changes:
* Introduces `wp.customize.Notification` in JS which to represent `WP_Error` instances returned from the server when setting validation fails.
* Introduces `wp.customize.Setting.prototype.notifications`.
* Introduces `wp.customize.Control.prototype.notifications`, which are synced with a control's settings' notifications.
* Introduces `wp.customize.Control.prototype.renderNotifications()` to re-render a control's notifications in its notification area. This is called automatically when the notifications collection changes.
* Introduces `wp.customize.settingConstructor`, allowing custom setting types to be used in the same way that custom controls, panels, and sections can be made.
* Injects a notification area into existing controls which is populated in response to the control's `notifications` collection changing. A custom control can customize the placement of the notification area by overriding the new `getNotificationsContainerElement` method.
* When a save fails due to setting invalidity, the invalidity errors will be added to the settings to then populate in the controls' notification areas, and the first such invalid control will be focused.
Props westonruter, celloexpressions, mrahmadawais.
See #35210.
See #30937.
Fixes #34893.
Built from https://develop.svn.wordpress.org/trunk@37476
git-svn-id: http://core.svn.wordpress.org/trunk@37444 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-05-20 17:10:27 -04:00
|
|
|
* @return false|void False if cap check fails or value isn't set or is invalid.
|
2012-02-24 23:12:43 -05:00
|
|
|
*/
|
2015-01-08 01:02:24 -05:00
|
|
|
final public function save() {
|
2012-02-24 23:12:43 -05:00
|
|
|
$value = $this->post_value();
|
|
|
|
|
Customize: Add setting validation model and control notifications to augment setting sanitization.
When a setting is invalid, not only will it be blocked from being saved but all other settings will be blocked as well. This ensures that Customizer saves aren't partial but are more transactional. User will be displayed the error in a notification so that they can fix and re-attempt saving.
PHP changes:
* Introduces `WP_Customize_Setting::validate()`, `WP_Customize_Setting::$validate_callback`, and the `customize_validate_{$setting_id}` filter.
* Introduces `WP_Customize_Manager::validate_setting_values()` to do validation (and sanitization) for the setting values supplied, returning a list of `WP_Error` instances for invalid settings.
* Attempting to save settings that are invalid will result in the save being blocked entirely, with the errors being sent in the `customize_save_response`. Modifies `WP_Customize_Manager::save()` to check all settings for validity issues prior to calling their `save` methods.
* Introduces `WP_Customize_Setting::json()` for parity with the other Customizer classes. This includes exporting of the `type`.
* Modifies `WP_Customize_Manager::post_value()` to apply `validate` after `sanitize`, and if validation fails, to return the `$default`.
* Introduces `customize_save_validation_before` action which fires right before the validation checks are made prior to saving.
JS changes:
* Introduces `wp.customize.Notification` in JS which to represent `WP_Error` instances returned from the server when setting validation fails.
* Introduces `wp.customize.Setting.prototype.notifications`.
* Introduces `wp.customize.Control.prototype.notifications`, which are synced with a control's settings' notifications.
* Introduces `wp.customize.Control.prototype.renderNotifications()` to re-render a control's notifications in its notification area. This is called automatically when the notifications collection changes.
* Introduces `wp.customize.settingConstructor`, allowing custom setting types to be used in the same way that custom controls, panels, and sections can be made.
* Injects a notification area into existing controls which is populated in response to the control's `notifications` collection changing. A custom control can customize the placement of the notification area by overriding the new `getNotificationsContainerElement` method.
* When a save fails due to setting invalidity, the invalidity errors will be added to the settings to then populate in the controls' notification areas, and the first such invalid control will be focused.
Props westonruter, celloexpressions, mrahmadawais.
See #35210.
See #30937.
Fixes #34893.
Built from https://develop.svn.wordpress.org/trunk@37476
git-svn-id: http://core.svn.wordpress.org/trunk@37444 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-05-20 17:10:27 -04:00
|
|
|
if ( ! $this->check_capabilities() || ! isset( $value ) ) {
|
2012-02-24 23:12:43 -05:00
|
|
|
return false;
|
Customize: Add setting validation model and control notifications to augment setting sanitization.
When a setting is invalid, not only will it be blocked from being saved but all other settings will be blocked as well. This ensures that Customizer saves aren't partial but are more transactional. User will be displayed the error in a notification so that they can fix and re-attempt saving.
PHP changes:
* Introduces `WP_Customize_Setting::validate()`, `WP_Customize_Setting::$validate_callback`, and the `customize_validate_{$setting_id}` filter.
* Introduces `WP_Customize_Manager::validate_setting_values()` to do validation (and sanitization) for the setting values supplied, returning a list of `WP_Error` instances for invalid settings.
* Attempting to save settings that are invalid will result in the save being blocked entirely, with the errors being sent in the `customize_save_response`. Modifies `WP_Customize_Manager::save()` to check all settings for validity issues prior to calling their `save` methods.
* Introduces `WP_Customize_Setting::json()` for parity with the other Customizer classes. This includes exporting of the `type`.
* Modifies `WP_Customize_Manager::post_value()` to apply `validate` after `sanitize`, and if validation fails, to return the `$default`.
* Introduces `customize_save_validation_before` action which fires right before the validation checks are made prior to saving.
JS changes:
* Introduces `wp.customize.Notification` in JS which to represent `WP_Error` instances returned from the server when setting validation fails.
* Introduces `wp.customize.Setting.prototype.notifications`.
* Introduces `wp.customize.Control.prototype.notifications`, which are synced with a control's settings' notifications.
* Introduces `wp.customize.Control.prototype.renderNotifications()` to re-render a control's notifications in its notification area. This is called automatically when the notifications collection changes.
* Introduces `wp.customize.settingConstructor`, allowing custom setting types to be used in the same way that custom controls, panels, and sections can be made.
* Injects a notification area into existing controls which is populated in response to the control's `notifications` collection changing. A custom control can customize the placement of the notification area by overriding the new `getNotificationsContainerElement` method.
* When a save fails due to setting invalidity, the invalidity errors will be added to the settings to then populate in the controls' notification areas, and the first such invalid control will be focused.
Props westonruter, celloexpressions, mrahmadawais.
See #35210.
See #30937.
Fixes #34893.
Built from https://develop.svn.wordpress.org/trunk@37476
git-svn-id: http://core.svn.wordpress.org/trunk@37444 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-05-20 17:10:27 -04:00
|
|
|
}
|
2012-02-24 23:12:43 -05:00
|
|
|
|
2014-03-06 09:04:13 -05:00
|
|
|
/**
|
2014-08-15 15:49:15 -04:00
|
|
|
* Fires when the WP_Customize_Setting::save() method is called.
|
2014-03-06 09:04:13 -05:00
|
|
|
*
|
2014-11-30 07:10:23 -05:00
|
|
|
* The dynamic portion of the hook name, `$this->id_data['base']` refers to
|
2014-03-06 09:04:13 -05:00
|
|
|
* the base slug of the setting name.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
2014-06-17 19:58:16 -04:00
|
|
|
*
|
2016-05-02 00:00:28 -04:00
|
|
|
* @param WP_Customize_Setting $this WP_Customize_Setting instance.
|
2014-03-06 09:04:13 -05:00
|
|
|
*/
|
Customize: Add setting validation model and control notifications to augment setting sanitization.
When a setting is invalid, not only will it be blocked from being saved but all other settings will be blocked as well. This ensures that Customizer saves aren't partial but are more transactional. User will be displayed the error in a notification so that they can fix and re-attempt saving.
PHP changes:
* Introduces `WP_Customize_Setting::validate()`, `WP_Customize_Setting::$validate_callback`, and the `customize_validate_{$setting_id}` filter.
* Introduces `WP_Customize_Manager::validate_setting_values()` to do validation (and sanitization) for the setting values supplied, returning a list of `WP_Error` instances for invalid settings.
* Attempting to save settings that are invalid will result in the save being blocked entirely, with the errors being sent in the `customize_save_response`. Modifies `WP_Customize_Manager::save()` to check all settings for validity issues prior to calling their `save` methods.
* Introduces `WP_Customize_Setting::json()` for parity with the other Customizer classes. This includes exporting of the `type`.
* Modifies `WP_Customize_Manager::post_value()` to apply `validate` after `sanitize`, and if validation fails, to return the `$default`.
* Introduces `customize_save_validation_before` action which fires right before the validation checks are made prior to saving.
JS changes:
* Introduces `wp.customize.Notification` in JS which to represent `WP_Error` instances returned from the server when setting validation fails.
* Introduces `wp.customize.Setting.prototype.notifications`.
* Introduces `wp.customize.Control.prototype.notifications`, which are synced with a control's settings' notifications.
* Introduces `wp.customize.Control.prototype.renderNotifications()` to re-render a control's notifications in its notification area. This is called automatically when the notifications collection changes.
* Introduces `wp.customize.settingConstructor`, allowing custom setting types to be used in the same way that custom controls, panels, and sections can be made.
* Injects a notification area into existing controls which is populated in response to the control's `notifications` collection changing. A custom control can customize the placement of the notification area by overriding the new `getNotificationsContainerElement` method.
* When a save fails due to setting invalidity, the invalidity errors will be added to the settings to then populate in the controls' notification areas, and the first such invalid control will be focused.
Props westonruter, celloexpressions, mrahmadawais.
See #35210.
See #30937.
Fixes #34893.
Built from https://develop.svn.wordpress.org/trunk@37476
git-svn-id: http://core.svn.wordpress.org/trunk@37444 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-05-20 17:10:27 -04:00
|
|
|
do_action( 'customize_save_' . $this->id_data['base'], $this );
|
2012-02-24 23:12:43 -05:00
|
|
|
|
|
|
|
$this->update( $value );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-04 15:21:14 -05:00
|
|
|
* Fetch and sanitize the $_POST value for the setting.
|
2012-02-24 23:12:43 -05:00
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
2012-07-26 17:45:33 -04:00
|
|
|
* @param mixed $default A default value which is used as a fallback. Default is null.
|
Customize: Add setting validation model and control notifications to augment setting sanitization.
When a setting is invalid, not only will it be blocked from being saved but all other settings will be blocked as well. This ensures that Customizer saves aren't partial but are more transactional. User will be displayed the error in a notification so that they can fix and re-attempt saving.
PHP changes:
* Introduces `WP_Customize_Setting::validate()`, `WP_Customize_Setting::$validate_callback`, and the `customize_validate_{$setting_id}` filter.
* Introduces `WP_Customize_Manager::validate_setting_values()` to do validation (and sanitization) for the setting values supplied, returning a list of `WP_Error` instances for invalid settings.
* Attempting to save settings that are invalid will result in the save being blocked entirely, with the errors being sent in the `customize_save_response`. Modifies `WP_Customize_Manager::save()` to check all settings for validity issues prior to calling their `save` methods.
* Introduces `WP_Customize_Setting::json()` for parity with the other Customizer classes. This includes exporting of the `type`.
* Modifies `WP_Customize_Manager::post_value()` to apply `validate` after `sanitize`, and if validation fails, to return the `$default`.
* Introduces `customize_save_validation_before` action which fires right before the validation checks are made prior to saving.
JS changes:
* Introduces `wp.customize.Notification` in JS which to represent `WP_Error` instances returned from the server when setting validation fails.
* Introduces `wp.customize.Setting.prototype.notifications`.
* Introduces `wp.customize.Control.prototype.notifications`, which are synced with a control's settings' notifications.
* Introduces `wp.customize.Control.prototype.renderNotifications()` to re-render a control's notifications in its notification area. This is called automatically when the notifications collection changes.
* Introduces `wp.customize.settingConstructor`, allowing custom setting types to be used in the same way that custom controls, panels, and sections can be made.
* Injects a notification area into existing controls which is populated in response to the control's `notifications` collection changing. A custom control can customize the placement of the notification area by overriding the new `getNotificationsContainerElement` method.
* When a save fails due to setting invalidity, the invalidity errors will be added to the settings to then populate in the controls' notification areas, and the first such invalid control will be focused.
Props westonruter, celloexpressions, mrahmadawais.
See #35210.
See #30937.
Fixes #34893.
Built from https://develop.svn.wordpress.org/trunk@37476
git-svn-id: http://core.svn.wordpress.org/trunk@37444 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-05-20 17:10:27 -04:00
|
|
|
* @return mixed The default value on failure, otherwise the sanitized and validated value.
|
2012-02-24 23:12:43 -05:00
|
|
|
*/
|
2015-01-08 01:02:24 -05:00
|
|
|
final public function post_value( $default = null ) {
|
2015-02-08 18:11:25 -05:00
|
|
|
return $this->manager->post_value( $this, $default );
|
2012-02-24 23:12:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitize an input.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
Customize: Add setting validation model and control notifications to augment setting sanitization.
When a setting is invalid, not only will it be blocked from being saved but all other settings will be blocked as well. This ensures that Customizer saves aren't partial but are more transactional. User will be displayed the error in a notification so that they can fix and re-attempt saving.
PHP changes:
* Introduces `WP_Customize_Setting::validate()`, `WP_Customize_Setting::$validate_callback`, and the `customize_validate_{$setting_id}` filter.
* Introduces `WP_Customize_Manager::validate_setting_values()` to do validation (and sanitization) for the setting values supplied, returning a list of `WP_Error` instances for invalid settings.
* Attempting to save settings that are invalid will result in the save being blocked entirely, with the errors being sent in the `customize_save_response`. Modifies `WP_Customize_Manager::save()` to check all settings for validity issues prior to calling their `save` methods.
* Introduces `WP_Customize_Setting::json()` for parity with the other Customizer classes. This includes exporting of the `type`.
* Modifies `WP_Customize_Manager::post_value()` to apply `validate` after `sanitize`, and if validation fails, to return the `$default`.
* Introduces `customize_save_validation_before` action which fires right before the validation checks are made prior to saving.
JS changes:
* Introduces `wp.customize.Notification` in JS which to represent `WP_Error` instances returned from the server when setting validation fails.
* Introduces `wp.customize.Setting.prototype.notifications`.
* Introduces `wp.customize.Control.prototype.notifications`, which are synced with a control's settings' notifications.
* Introduces `wp.customize.Control.prototype.renderNotifications()` to re-render a control's notifications in its notification area. This is called automatically when the notifications collection changes.
* Introduces `wp.customize.settingConstructor`, allowing custom setting types to be used in the same way that custom controls, panels, and sections can be made.
* Injects a notification area into existing controls which is populated in response to the control's `notifications` collection changing. A custom control can customize the placement of the notification area by overriding the new `getNotificationsContainerElement` method.
* When a save fails due to setting invalidity, the invalidity errors will be added to the settings to then populate in the controls' notification areas, and the first such invalid control will be focused.
Props westonruter, celloexpressions, mrahmadawais.
See #35210.
See #30937.
Fixes #34893.
Built from https://develop.svn.wordpress.org/trunk@37476
git-svn-id: http://core.svn.wordpress.org/trunk@37444 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-05-20 17:10:27 -04:00
|
|
|
* @param string|array $value The value to sanitize.
|
|
|
|
* @return string|array|null|WP_Error Sanitized value, or `null`/`WP_Error` if invalid.
|
2012-02-24 23:12:43 -05:00
|
|
|
*/
|
|
|
|
public function sanitize( $value ) {
|
2014-03-06 09:04:13 -05:00
|
|
|
|
|
|
|
/**
|
2016-05-22 14:10:29 -04:00
|
|
|
* Filters a Customize setting value in un-slashed form.
|
2014-03-06 09:04:13 -05:00
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @param mixed $value Value of the setting.
|
|
|
|
* @param WP_Customize_Setting $this WP_Customize_Setting instance.
|
|
|
|
*/
|
2012-05-26 14:44:31 -04:00
|
|
|
return apply_filters( "customize_sanitize_{$this->id}", $value, $this );
|
2012-02-24 23:12:43 -05:00
|
|
|
}
|
|
|
|
|
Customize: Add setting validation model and control notifications to augment setting sanitization.
When a setting is invalid, not only will it be blocked from being saved but all other settings will be blocked as well. This ensures that Customizer saves aren't partial but are more transactional. User will be displayed the error in a notification so that they can fix and re-attempt saving.
PHP changes:
* Introduces `WP_Customize_Setting::validate()`, `WP_Customize_Setting::$validate_callback`, and the `customize_validate_{$setting_id}` filter.
* Introduces `WP_Customize_Manager::validate_setting_values()` to do validation (and sanitization) for the setting values supplied, returning a list of `WP_Error` instances for invalid settings.
* Attempting to save settings that are invalid will result in the save being blocked entirely, with the errors being sent in the `customize_save_response`. Modifies `WP_Customize_Manager::save()` to check all settings for validity issues prior to calling their `save` methods.
* Introduces `WP_Customize_Setting::json()` for parity with the other Customizer classes. This includes exporting of the `type`.
* Modifies `WP_Customize_Manager::post_value()` to apply `validate` after `sanitize`, and if validation fails, to return the `$default`.
* Introduces `customize_save_validation_before` action which fires right before the validation checks are made prior to saving.
JS changes:
* Introduces `wp.customize.Notification` in JS which to represent `WP_Error` instances returned from the server when setting validation fails.
* Introduces `wp.customize.Setting.prototype.notifications`.
* Introduces `wp.customize.Control.prototype.notifications`, which are synced with a control's settings' notifications.
* Introduces `wp.customize.Control.prototype.renderNotifications()` to re-render a control's notifications in its notification area. This is called automatically when the notifications collection changes.
* Introduces `wp.customize.settingConstructor`, allowing custom setting types to be used in the same way that custom controls, panels, and sections can be made.
* Injects a notification area into existing controls which is populated in response to the control's `notifications` collection changing. A custom control can customize the placement of the notification area by overriding the new `getNotificationsContainerElement` method.
* When a save fails due to setting invalidity, the invalidity errors will be added to the settings to then populate in the controls' notification areas, and the first such invalid control will be focused.
Props westonruter, celloexpressions, mrahmadawais.
See #35210.
See #30937.
Fixes #34893.
Built from https://develop.svn.wordpress.org/trunk@37476
git-svn-id: http://core.svn.wordpress.org/trunk@37444 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-05-20 17:10:27 -04:00
|
|
|
/**
|
2016-07-20 12:57:32 -04:00
|
|
|
* Validates an input.
|
Customize: Add setting validation model and control notifications to augment setting sanitization.
When a setting is invalid, not only will it be blocked from being saved but all other settings will be blocked as well. This ensures that Customizer saves aren't partial but are more transactional. User will be displayed the error in a notification so that they can fix and re-attempt saving.
PHP changes:
* Introduces `WP_Customize_Setting::validate()`, `WP_Customize_Setting::$validate_callback`, and the `customize_validate_{$setting_id}` filter.
* Introduces `WP_Customize_Manager::validate_setting_values()` to do validation (and sanitization) for the setting values supplied, returning a list of `WP_Error` instances for invalid settings.
* Attempting to save settings that are invalid will result in the save being blocked entirely, with the errors being sent in the `customize_save_response`. Modifies `WP_Customize_Manager::save()` to check all settings for validity issues prior to calling their `save` methods.
* Introduces `WP_Customize_Setting::json()` for parity with the other Customizer classes. This includes exporting of the `type`.
* Modifies `WP_Customize_Manager::post_value()` to apply `validate` after `sanitize`, and if validation fails, to return the `$default`.
* Introduces `customize_save_validation_before` action which fires right before the validation checks are made prior to saving.
JS changes:
* Introduces `wp.customize.Notification` in JS which to represent `WP_Error` instances returned from the server when setting validation fails.
* Introduces `wp.customize.Setting.prototype.notifications`.
* Introduces `wp.customize.Control.prototype.notifications`, which are synced with a control's settings' notifications.
* Introduces `wp.customize.Control.prototype.renderNotifications()` to re-render a control's notifications in its notification area. This is called automatically when the notifications collection changes.
* Introduces `wp.customize.settingConstructor`, allowing custom setting types to be used in the same way that custom controls, panels, and sections can be made.
* Injects a notification area into existing controls which is populated in response to the control's `notifications` collection changing. A custom control can customize the placement of the notification area by overriding the new `getNotificationsContainerElement` method.
* When a save fails due to setting invalidity, the invalidity errors will be added to the settings to then populate in the controls' notification areas, and the first such invalid control will be focused.
Props westonruter, celloexpressions, mrahmadawais.
See #35210.
See #30937.
Fixes #34893.
Built from https://develop.svn.wordpress.org/trunk@37476
git-svn-id: http://core.svn.wordpress.org/trunk@37444 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-05-20 17:10:27 -04:00
|
|
|
*
|
|
|
|
* @since 4.6.0
|
|
|
|
* @access public
|
2016-07-20 12:57:32 -04:00
|
|
|
*
|
Customize: Add setting validation model and control notifications to augment setting sanitization.
When a setting is invalid, not only will it be blocked from being saved but all other settings will be blocked as well. This ensures that Customizer saves aren't partial but are more transactional. User will be displayed the error in a notification so that they can fix and re-attempt saving.
PHP changes:
* Introduces `WP_Customize_Setting::validate()`, `WP_Customize_Setting::$validate_callback`, and the `customize_validate_{$setting_id}` filter.
* Introduces `WP_Customize_Manager::validate_setting_values()` to do validation (and sanitization) for the setting values supplied, returning a list of `WP_Error` instances for invalid settings.
* Attempting to save settings that are invalid will result in the save being blocked entirely, with the errors being sent in the `customize_save_response`. Modifies `WP_Customize_Manager::save()` to check all settings for validity issues prior to calling their `save` methods.
* Introduces `WP_Customize_Setting::json()` for parity with the other Customizer classes. This includes exporting of the `type`.
* Modifies `WP_Customize_Manager::post_value()` to apply `validate` after `sanitize`, and if validation fails, to return the `$default`.
* Introduces `customize_save_validation_before` action which fires right before the validation checks are made prior to saving.
JS changes:
* Introduces `wp.customize.Notification` in JS which to represent `WP_Error` instances returned from the server when setting validation fails.
* Introduces `wp.customize.Setting.prototype.notifications`.
* Introduces `wp.customize.Control.prototype.notifications`, which are synced with a control's settings' notifications.
* Introduces `wp.customize.Control.prototype.renderNotifications()` to re-render a control's notifications in its notification area. This is called automatically when the notifications collection changes.
* Introduces `wp.customize.settingConstructor`, allowing custom setting types to be used in the same way that custom controls, panels, and sections can be made.
* Injects a notification area into existing controls which is populated in response to the control's `notifications` collection changing. A custom control can customize the placement of the notification area by overriding the new `getNotificationsContainerElement` method.
* When a save fails due to setting invalidity, the invalidity errors will be added to the settings to then populate in the controls' notification areas, and the first such invalid control will be focused.
Props westonruter, celloexpressions, mrahmadawais.
See #35210.
See #30937.
Fixes #34893.
Built from https://develop.svn.wordpress.org/trunk@37476
git-svn-id: http://core.svn.wordpress.org/trunk@37444 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-05-20 17:10:27 -04:00
|
|
|
* @see WP_REST_Request::has_valid_params()
|
|
|
|
*
|
|
|
|
* @param mixed $value Value to validate.
|
2016-07-20 12:57:32 -04:00
|
|
|
* @return true|WP_Error True if the input was validated, otherwise WP_Error.
|
Customize: Add setting validation model and control notifications to augment setting sanitization.
When a setting is invalid, not only will it be blocked from being saved but all other settings will be blocked as well. This ensures that Customizer saves aren't partial but are more transactional. User will be displayed the error in a notification so that they can fix and re-attempt saving.
PHP changes:
* Introduces `WP_Customize_Setting::validate()`, `WP_Customize_Setting::$validate_callback`, and the `customize_validate_{$setting_id}` filter.
* Introduces `WP_Customize_Manager::validate_setting_values()` to do validation (and sanitization) for the setting values supplied, returning a list of `WP_Error` instances for invalid settings.
* Attempting to save settings that are invalid will result in the save being blocked entirely, with the errors being sent in the `customize_save_response`. Modifies `WP_Customize_Manager::save()` to check all settings for validity issues prior to calling their `save` methods.
* Introduces `WP_Customize_Setting::json()` for parity with the other Customizer classes. This includes exporting of the `type`.
* Modifies `WP_Customize_Manager::post_value()` to apply `validate` after `sanitize`, and if validation fails, to return the `$default`.
* Introduces `customize_save_validation_before` action which fires right before the validation checks are made prior to saving.
JS changes:
* Introduces `wp.customize.Notification` in JS which to represent `WP_Error` instances returned from the server when setting validation fails.
* Introduces `wp.customize.Setting.prototype.notifications`.
* Introduces `wp.customize.Control.prototype.notifications`, which are synced with a control's settings' notifications.
* Introduces `wp.customize.Control.prototype.renderNotifications()` to re-render a control's notifications in its notification area. This is called automatically when the notifications collection changes.
* Introduces `wp.customize.settingConstructor`, allowing custom setting types to be used in the same way that custom controls, panels, and sections can be made.
* Injects a notification area into existing controls which is populated in response to the control's `notifications` collection changing. A custom control can customize the placement of the notification area by overriding the new `getNotificationsContainerElement` method.
* When a save fails due to setting invalidity, the invalidity errors will be added to the settings to then populate in the controls' notification areas, and the first such invalid control will be focused.
Props westonruter, celloexpressions, mrahmadawais.
See #35210.
See #30937.
Fixes #34893.
Built from https://develop.svn.wordpress.org/trunk@37476
git-svn-id: http://core.svn.wordpress.org/trunk@37444 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-05-20 17:10:27 -04:00
|
|
|
*/
|
|
|
|
public function validate( $value ) {
|
|
|
|
if ( is_wp_error( $value ) ) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
if ( is_null( $value ) ) {
|
|
|
|
return new WP_Error( 'invalid_value', __( 'Invalid value.' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
$validity = new WP_Error();
|
|
|
|
|
|
|
|
/**
|
2016-07-20 12:57:32 -04:00
|
|
|
* Validates a Customize setting value.
|
Customize: Add setting validation model and control notifications to augment setting sanitization.
When a setting is invalid, not only will it be blocked from being saved but all other settings will be blocked as well. This ensures that Customizer saves aren't partial but are more transactional. User will be displayed the error in a notification so that they can fix and re-attempt saving.
PHP changes:
* Introduces `WP_Customize_Setting::validate()`, `WP_Customize_Setting::$validate_callback`, and the `customize_validate_{$setting_id}` filter.
* Introduces `WP_Customize_Manager::validate_setting_values()` to do validation (and sanitization) for the setting values supplied, returning a list of `WP_Error` instances for invalid settings.
* Attempting to save settings that are invalid will result in the save being blocked entirely, with the errors being sent in the `customize_save_response`. Modifies `WP_Customize_Manager::save()` to check all settings for validity issues prior to calling their `save` methods.
* Introduces `WP_Customize_Setting::json()` for parity with the other Customizer classes. This includes exporting of the `type`.
* Modifies `WP_Customize_Manager::post_value()` to apply `validate` after `sanitize`, and if validation fails, to return the `$default`.
* Introduces `customize_save_validation_before` action which fires right before the validation checks are made prior to saving.
JS changes:
* Introduces `wp.customize.Notification` in JS which to represent `WP_Error` instances returned from the server when setting validation fails.
* Introduces `wp.customize.Setting.prototype.notifications`.
* Introduces `wp.customize.Control.prototype.notifications`, which are synced with a control's settings' notifications.
* Introduces `wp.customize.Control.prototype.renderNotifications()` to re-render a control's notifications in its notification area. This is called automatically when the notifications collection changes.
* Introduces `wp.customize.settingConstructor`, allowing custom setting types to be used in the same way that custom controls, panels, and sections can be made.
* Injects a notification area into existing controls which is populated in response to the control's `notifications` collection changing. A custom control can customize the placement of the notification area by overriding the new `getNotificationsContainerElement` method.
* When a save fails due to setting invalidity, the invalidity errors will be added to the settings to then populate in the controls' notification areas, and the first such invalid control will be focused.
Props westonruter, celloexpressions, mrahmadawais.
See #35210.
See #30937.
Fixes #34893.
Built from https://develop.svn.wordpress.org/trunk@37476
git-svn-id: http://core.svn.wordpress.org/trunk@37444 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-05-20 17:10:27 -04:00
|
|
|
*
|
|
|
|
* Plugins should amend the `$validity` object via its `WP_Error::add()` method.
|
|
|
|
*
|
2016-07-20 12:57:32 -04:00
|
|
|
* The dynamic portion of the hook name, `$this->ID`, refers to the setting ID.
|
|
|
|
*
|
Customize: Add setting validation model and control notifications to augment setting sanitization.
When a setting is invalid, not only will it be blocked from being saved but all other settings will be blocked as well. This ensures that Customizer saves aren't partial but are more transactional. User will be displayed the error in a notification so that they can fix and re-attempt saving.
PHP changes:
* Introduces `WP_Customize_Setting::validate()`, `WP_Customize_Setting::$validate_callback`, and the `customize_validate_{$setting_id}` filter.
* Introduces `WP_Customize_Manager::validate_setting_values()` to do validation (and sanitization) for the setting values supplied, returning a list of `WP_Error` instances for invalid settings.
* Attempting to save settings that are invalid will result in the save being blocked entirely, with the errors being sent in the `customize_save_response`. Modifies `WP_Customize_Manager::save()` to check all settings for validity issues prior to calling their `save` methods.
* Introduces `WP_Customize_Setting::json()` for parity with the other Customizer classes. This includes exporting of the `type`.
* Modifies `WP_Customize_Manager::post_value()` to apply `validate` after `sanitize`, and if validation fails, to return the `$default`.
* Introduces `customize_save_validation_before` action which fires right before the validation checks are made prior to saving.
JS changes:
* Introduces `wp.customize.Notification` in JS which to represent `WP_Error` instances returned from the server when setting validation fails.
* Introduces `wp.customize.Setting.prototype.notifications`.
* Introduces `wp.customize.Control.prototype.notifications`, which are synced with a control's settings' notifications.
* Introduces `wp.customize.Control.prototype.renderNotifications()` to re-render a control's notifications in its notification area. This is called automatically when the notifications collection changes.
* Introduces `wp.customize.settingConstructor`, allowing custom setting types to be used in the same way that custom controls, panels, and sections can be made.
* Injects a notification area into existing controls which is populated in response to the control's `notifications` collection changing. A custom control can customize the placement of the notification area by overriding the new `getNotificationsContainerElement` method.
* When a save fails due to setting invalidity, the invalidity errors will be added to the settings to then populate in the controls' notification areas, and the first such invalid control will be focused.
Props westonruter, celloexpressions, mrahmadawais.
See #35210.
See #30937.
Fixes #34893.
Built from https://develop.svn.wordpress.org/trunk@37476
git-svn-id: http://core.svn.wordpress.org/trunk@37444 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-05-20 17:10:27 -04:00
|
|
|
* @since 4.6.0
|
|
|
|
*
|
|
|
|
* @param WP_Error $validity Filtered from `true` to `WP_Error` when invalid.
|
|
|
|
* @param mixed $value Value of the setting.
|
|
|
|
* @param WP_Customize_Setting $this WP_Customize_Setting instance.
|
|
|
|
*/
|
|
|
|
$validity = apply_filters( "customize_validate_{$this->id}", $validity, $value, $this );
|
|
|
|
|
|
|
|
if ( is_wp_error( $validity ) && empty( $validity->errors ) ) {
|
|
|
|
$validity = true;
|
|
|
|
}
|
|
|
|
return $validity;
|
|
|
|
}
|
|
|
|
|
2012-02-24 23:12:43 -05:00
|
|
|
/**
|
2015-10-10 05:06:25 -04:00
|
|
|
* Get the root value for a setting, especially for multidimensional ones.
|
2012-02-24 23:12:43 -05:00
|
|
|
*
|
2015-10-10 05:06:25 -04:00
|
|
|
* @since 4.4.0
|
|
|
|
* @access protected
|
2012-02-24 23:12:43 -05:00
|
|
|
*
|
2015-10-10 05:06:25 -04:00
|
|
|
* @param mixed $default Value to return if root does not exist.
|
|
|
|
* @return mixed
|
2012-02-24 23:12:43 -05:00
|
|
|
*/
|
2015-10-10 05:06:25 -04:00
|
|
|
protected function get_root_value( $default = null ) {
|
|
|
|
$id_base = $this->id_data['base'];
|
|
|
|
if ( 'option' === $this->type ) {
|
|
|
|
return get_option( $id_base, $default );
|
|
|
|
} else if ( 'theme_mod' ) {
|
|
|
|
return get_theme_mod( $id_base, $default );
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Any WP_Customize_Setting subclass implementing aggregate multidimensional
|
|
|
|
* will need to override this method to obtain the data from the appropriate
|
|
|
|
* location.
|
|
|
|
*/
|
|
|
|
return $default;
|
2012-02-24 23:12:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-10 05:06:25 -04:00
|
|
|
* Set the root value for a setting, especially for multidimensional ones.
|
2012-02-24 23:12:43 -05:00
|
|
|
*
|
2015-10-10 05:06:25 -04:00
|
|
|
* @since 4.4.0
|
|
|
|
* @access protected
|
2012-02-24 23:12:43 -05:00
|
|
|
*
|
2015-10-10 05:06:25 -04:00
|
|
|
* @param mixed $value Value to set as root of multidimensional setting.
|
|
|
|
* @return bool Whether the multidimensional root was updated successfully.
|
2012-02-24 23:12:43 -05:00
|
|
|
*/
|
2015-10-10 05:06:25 -04:00
|
|
|
protected function set_root_value( $value ) {
|
|
|
|
$id_base = $this->id_data['base'];
|
|
|
|
if ( 'option' === $this->type ) {
|
2015-10-20 17:19:25 -04:00
|
|
|
$autoload = true;
|
|
|
|
if ( isset( self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['autoload'] ) ) {
|
|
|
|
$autoload = self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['autoload'];
|
|
|
|
}
|
|
|
|
return update_option( $id_base, $value, $autoload );
|
2015-10-10 05:06:25 -04:00
|
|
|
} else if ( 'theme_mod' ) {
|
|
|
|
set_theme_mod( $id_base, $value );
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Any WP_Customize_Setting subclass implementing aggregate multidimensional
|
|
|
|
* will need to override this method to obtain the data from the appropriate
|
|
|
|
* location.
|
|
|
|
*/
|
|
|
|
return false;
|
2015-05-21 18:05:24 -04:00
|
|
|
}
|
2012-02-24 23:12:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-10 05:06:25 -04:00
|
|
|
* Save the value of the setting, using the related API.
|
2012-02-24 23:12:43 -05:00
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
2012-07-26 17:45:33 -04:00
|
|
|
* @param mixed $value The value to update.
|
2015-05-21 18:05:24 -04:00
|
|
|
* @return bool The result of saving the value.
|
2012-02-24 23:12:43 -05:00
|
|
|
*/
|
2015-10-10 05:06:25 -04:00
|
|
|
protected function update( $value ) {
|
|
|
|
$id_base = $this->id_data['base'];
|
|
|
|
if ( 'option' === $this->type || 'theme_mod' === $this->type ) {
|
|
|
|
if ( ! $this->is_multidimensional_aggregated ) {
|
|
|
|
return $this->set_root_value( $value );
|
|
|
|
} else {
|
|
|
|
$root = self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'];
|
|
|
|
$root = $this->multidimensional_replace( $root, $this->id_data['keys'], $value );
|
|
|
|
self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'] = $root;
|
|
|
|
return $this->set_root_value( $root );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/**
|
2016-05-02 00:00:28 -04:00
|
|
|
* Fires when the WP_Customize_Setting::update() method is called for settings
|
2015-10-10 05:06:25 -04:00
|
|
|
* not handled as theme_mods or options.
|
|
|
|
*
|
|
|
|
* The dynamic portion of the hook name, `$this->type`, refers to the type of setting.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @param mixed $value Value of the setting.
|
|
|
|
* @param WP_Customize_Setting $this WP_Customize_Setting instance.
|
|
|
|
*/
|
|
|
|
do_action( "customize_update_{$this->type}", $value, $this );
|
|
|
|
|
|
|
|
return has_action( "customize_update_{$this->type}" );
|
|
|
|
}
|
|
|
|
}
|
2012-02-24 23:12:43 -05:00
|
|
|
|
2015-10-10 05:06:25 -04:00
|
|
|
/**
|
|
|
|
* Deprecated method.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
* @deprecated 4.4.0 Deprecated in favor of update() method.
|
|
|
|
*/
|
|
|
|
protected function _update_theme_mod() {
|
|
|
|
_deprecated_function( __METHOD__, '4.4.0', __CLASS__ . '::update()' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deprecated method.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
* @deprecated 4.4.0 Deprecated in favor of update() method.
|
|
|
|
*/
|
|
|
|
protected function _update_option() {
|
|
|
|
_deprecated_function( __METHOD__, '4.4.0', __CLASS__ . '::update()' );
|
2012-02-24 23:12:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-04 15:21:14 -05:00
|
|
|
* Fetch the value of the setting.
|
2012-02-24 23:12:43 -05:00
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
2014-03-04 15:21:14 -05:00
|
|
|
* @return mixed The value.
|
2012-02-24 23:12:43 -05:00
|
|
|
*/
|
|
|
|
public function value() {
|
2015-10-10 05:06:25 -04:00
|
|
|
$id_base = $this->id_data['base'];
|
|
|
|
$is_core_type = ( 'option' === $this->type || 'theme_mod' === $this->type );
|
|
|
|
|
|
|
|
if ( ! $is_core_type && ! $this->is_multidimensional_aggregated ) {
|
|
|
|
$value = $this->get_root_value( $this->default );
|
|
|
|
|
|
|
|
/**
|
2016-05-22 14:10:29 -04:00
|
|
|
* Filters a Customize setting value not handled as a theme_mod or option.
|
2015-10-10 05:06:25 -04:00
|
|
|
*
|
2016-07-20 12:57:32 -04:00
|
|
|
* The dynamic portion of the hook name, `$id_base`, refers to
|
|
|
|
* the base slug of the setting name, initialized from `$this->id_data['base']`.
|
2015-10-10 05:06:25 -04:00
|
|
|
*
|
|
|
|
* For settings handled as theme_mods or options, see those corresponding
|
|
|
|
* functions for available hooks.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
2016-07-20 12:57:32 -04:00
|
|
|
* @since 4.6.0 Added the `$this` setting instance as the second parameter.
|
2015-10-10 05:06:25 -04:00
|
|
|
*
|
2016-05-02 18:42:26 -04:00
|
|
|
* @param mixed $default The setting default value. Default empty.
|
|
|
|
* @param WP_Customize_Setting $this The setting instance.
|
2015-10-10 05:06:25 -04:00
|
|
|
*/
|
2016-05-02 18:42:26 -04:00
|
|
|
$value = apply_filters( "customize_value_{$id_base}", $value, $this );
|
|
|
|
} elseif ( $this->is_multidimensional_aggregated ) {
|
2015-10-10 05:06:25 -04:00
|
|
|
$root_value = self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'];
|
|
|
|
$value = $this->multidimensional_get( $root_value, $this->id_data['keys'], $this->default );
|
2016-07-06 01:59:28 -04:00
|
|
|
|
|
|
|
// Ensure that the post value is used if the setting is previewed, since preview filters aren't applying on cached $root_value.
|
|
|
|
if ( $this->is_previewed ) {
|
|
|
|
$value = $this->post_value( $value );
|
|
|
|
}
|
2015-10-10 05:06:25 -04:00
|
|
|
} else {
|
|
|
|
$value = $this->get_root_value( $this->default );
|
2012-02-24 23:12:43 -05:00
|
|
|
}
|
2015-10-10 05:06:25 -04:00
|
|
|
return $value;
|
2012-02-24 23:12:43 -05:00
|
|
|
}
|
|
|
|
|
2012-05-16 16:59:02 -04:00
|
|
|
/**
|
2014-03-04 15:21:14 -05:00
|
|
|
* Sanitize the setting's value for use in JavaScript.
|
2012-05-16 16:59:02 -04:00
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @return mixed The requested escaped value.
|
|
|
|
*/
|
|
|
|
public function js_value() {
|
2014-03-06 09:04:13 -05:00
|
|
|
|
|
|
|
/**
|
2016-05-22 14:10:29 -04:00
|
|
|
* Filters a Customize setting value for use in JavaScript.
|
2014-03-06 09:04:13 -05:00
|
|
|
*
|
2014-11-30 07:10:23 -05:00
|
|
|
* The dynamic portion of the hook name, `$this->id`, refers to the setting ID.
|
2014-03-06 09:04:13 -05:00
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @param mixed $value The setting value.
|
2016-05-02 00:00:28 -04:00
|
|
|
* @param WP_Customize_Setting $this WP_Customize_Setting instance.
|
2014-03-06 09:04:13 -05:00
|
|
|
*/
|
2012-05-26 14:44:31 -04:00
|
|
|
$value = apply_filters( "customize_sanitize_js_{$this->id}", $this->value(), $this );
|
2012-05-16 16:59:02 -04:00
|
|
|
|
|
|
|
if ( is_string( $value ) )
|
|
|
|
return html_entity_decode( $value, ENT_QUOTES, 'UTF-8');
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
Customize: Add setting validation model and control notifications to augment setting sanitization.
When a setting is invalid, not only will it be blocked from being saved but all other settings will be blocked as well. This ensures that Customizer saves aren't partial but are more transactional. User will be displayed the error in a notification so that they can fix and re-attempt saving.
PHP changes:
* Introduces `WP_Customize_Setting::validate()`, `WP_Customize_Setting::$validate_callback`, and the `customize_validate_{$setting_id}` filter.
* Introduces `WP_Customize_Manager::validate_setting_values()` to do validation (and sanitization) for the setting values supplied, returning a list of `WP_Error` instances for invalid settings.
* Attempting to save settings that are invalid will result in the save being blocked entirely, with the errors being sent in the `customize_save_response`. Modifies `WP_Customize_Manager::save()` to check all settings for validity issues prior to calling their `save` methods.
* Introduces `WP_Customize_Setting::json()` for parity with the other Customizer classes. This includes exporting of the `type`.
* Modifies `WP_Customize_Manager::post_value()` to apply `validate` after `sanitize`, and if validation fails, to return the `$default`.
* Introduces `customize_save_validation_before` action which fires right before the validation checks are made prior to saving.
JS changes:
* Introduces `wp.customize.Notification` in JS which to represent `WP_Error` instances returned from the server when setting validation fails.
* Introduces `wp.customize.Setting.prototype.notifications`.
* Introduces `wp.customize.Control.prototype.notifications`, which are synced with a control's settings' notifications.
* Introduces `wp.customize.Control.prototype.renderNotifications()` to re-render a control's notifications in its notification area. This is called automatically when the notifications collection changes.
* Introduces `wp.customize.settingConstructor`, allowing custom setting types to be used in the same way that custom controls, panels, and sections can be made.
* Injects a notification area into existing controls which is populated in response to the control's `notifications` collection changing. A custom control can customize the placement of the notification area by overriding the new `getNotificationsContainerElement` method.
* When a save fails due to setting invalidity, the invalidity errors will be added to the settings to then populate in the controls' notification areas, and the first such invalid control will be focused.
Props westonruter, celloexpressions, mrahmadawais.
See #35210.
See #30937.
Fixes #34893.
Built from https://develop.svn.wordpress.org/trunk@37476
git-svn-id: http://core.svn.wordpress.org/trunk@37444 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-05-20 17:10:27 -04:00
|
|
|
/**
|
2016-07-20 12:57:32 -04:00
|
|
|
* Retrieves the data to export to the client via JSON.
|
Customize: Add setting validation model and control notifications to augment setting sanitization.
When a setting is invalid, not only will it be blocked from being saved but all other settings will be blocked as well. This ensures that Customizer saves aren't partial but are more transactional. User will be displayed the error in a notification so that they can fix and re-attempt saving.
PHP changes:
* Introduces `WP_Customize_Setting::validate()`, `WP_Customize_Setting::$validate_callback`, and the `customize_validate_{$setting_id}` filter.
* Introduces `WP_Customize_Manager::validate_setting_values()` to do validation (and sanitization) for the setting values supplied, returning a list of `WP_Error` instances for invalid settings.
* Attempting to save settings that are invalid will result in the save being blocked entirely, with the errors being sent in the `customize_save_response`. Modifies `WP_Customize_Manager::save()` to check all settings for validity issues prior to calling their `save` methods.
* Introduces `WP_Customize_Setting::json()` for parity with the other Customizer classes. This includes exporting of the `type`.
* Modifies `WP_Customize_Manager::post_value()` to apply `validate` after `sanitize`, and if validation fails, to return the `$default`.
* Introduces `customize_save_validation_before` action which fires right before the validation checks are made prior to saving.
JS changes:
* Introduces `wp.customize.Notification` in JS which to represent `WP_Error` instances returned from the server when setting validation fails.
* Introduces `wp.customize.Setting.prototype.notifications`.
* Introduces `wp.customize.Control.prototype.notifications`, which are synced with a control's settings' notifications.
* Introduces `wp.customize.Control.prototype.renderNotifications()` to re-render a control's notifications in its notification area. This is called automatically when the notifications collection changes.
* Introduces `wp.customize.settingConstructor`, allowing custom setting types to be used in the same way that custom controls, panels, and sections can be made.
* Injects a notification area into existing controls which is populated in response to the control's `notifications` collection changing. A custom control can customize the placement of the notification area by overriding the new `getNotificationsContainerElement` method.
* When a save fails due to setting invalidity, the invalidity errors will be added to the settings to then populate in the controls' notification areas, and the first such invalid control will be focused.
Props westonruter, celloexpressions, mrahmadawais.
See #35210.
See #30937.
Fixes #34893.
Built from https://develop.svn.wordpress.org/trunk@37476
git-svn-id: http://core.svn.wordpress.org/trunk@37444 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-05-20 17:10:27 -04:00
|
|
|
*
|
|
|
|
* @since 4.6.0
|
2016-07-20 12:57:32 -04:00
|
|
|
* @access public
|
Customize: Add setting validation model and control notifications to augment setting sanitization.
When a setting is invalid, not only will it be blocked from being saved but all other settings will be blocked as well. This ensures that Customizer saves aren't partial but are more transactional. User will be displayed the error in a notification so that they can fix and re-attempt saving.
PHP changes:
* Introduces `WP_Customize_Setting::validate()`, `WP_Customize_Setting::$validate_callback`, and the `customize_validate_{$setting_id}` filter.
* Introduces `WP_Customize_Manager::validate_setting_values()` to do validation (and sanitization) for the setting values supplied, returning a list of `WP_Error` instances for invalid settings.
* Attempting to save settings that are invalid will result in the save being blocked entirely, with the errors being sent in the `customize_save_response`. Modifies `WP_Customize_Manager::save()` to check all settings for validity issues prior to calling their `save` methods.
* Introduces `WP_Customize_Setting::json()` for parity with the other Customizer classes. This includes exporting of the `type`.
* Modifies `WP_Customize_Manager::post_value()` to apply `validate` after `sanitize`, and if validation fails, to return the `$default`.
* Introduces `customize_save_validation_before` action which fires right before the validation checks are made prior to saving.
JS changes:
* Introduces `wp.customize.Notification` in JS which to represent `WP_Error` instances returned from the server when setting validation fails.
* Introduces `wp.customize.Setting.prototype.notifications`.
* Introduces `wp.customize.Control.prototype.notifications`, which are synced with a control's settings' notifications.
* Introduces `wp.customize.Control.prototype.renderNotifications()` to re-render a control's notifications in its notification area. This is called automatically when the notifications collection changes.
* Introduces `wp.customize.settingConstructor`, allowing custom setting types to be used in the same way that custom controls, panels, and sections can be made.
* Injects a notification area into existing controls which is populated in response to the control's `notifications` collection changing. A custom control can customize the placement of the notification area by overriding the new `getNotificationsContainerElement` method.
* When a save fails due to setting invalidity, the invalidity errors will be added to the settings to then populate in the controls' notification areas, and the first such invalid control will be focused.
Props westonruter, celloexpressions, mrahmadawais.
See #35210.
See #30937.
Fixes #34893.
Built from https://develop.svn.wordpress.org/trunk@37476
git-svn-id: http://core.svn.wordpress.org/trunk@37444 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-05-20 17:10:27 -04:00
|
|
|
*
|
|
|
|
* @return array Array of parameters passed to JavaScript.
|
|
|
|
*/
|
|
|
|
public function json() {
|
|
|
|
return array(
|
|
|
|
'value' => $this->js_value(),
|
|
|
|
'transport' => $this->transport,
|
|
|
|
'dirty' => $this->dirty,
|
|
|
|
'type' => $this->type,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-02-24 23:12:43 -05:00
|
|
|
/**
|
2014-03-04 15:21:14 -05:00
|
|
|
* Validate user capabilities whether the theme supports the setting.
|
2012-02-24 23:12:43 -05:00
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @return bool False if theme doesn't support the setting or user can't change setting, otherwise true.
|
|
|
|
*/
|
2015-01-08 01:02:24 -05:00
|
|
|
final public function check_capabilities() {
|
2012-03-21 00:59:57 -04:00
|
|
|
if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) )
|
2012-02-24 23:12:43 -05:00
|
|
|
return false;
|
|
|
|
|
2012-03-21 00:59:57 -04:00
|
|
|
if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) )
|
2012-02-24 23:12:43 -05:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Multidimensional helper function.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @param $root
|
|
|
|
* @param $keys
|
|
|
|
* @param bool $create Default is false.
|
2015-05-24 01:40:25 -04:00
|
|
|
* @return array|void Keys are 'root', 'node', and 'key'.
|
2012-02-24 23:12:43 -05:00
|
|
|
*/
|
2012-03-28 05:27:26 -04:00
|
|
|
final protected function multidimensional( &$root, $keys, $create = false ) {
|
2012-02-24 23:12:43 -05:00
|
|
|
if ( $create && empty( $root ) )
|
|
|
|
$root = array();
|
|
|
|
|
|
|
|
if ( ! isset( $root ) || empty( $keys ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
$last = array_pop( $keys );
|
|
|
|
$node = &$root;
|
|
|
|
|
|
|
|
foreach ( $keys as $key ) {
|
|
|
|
if ( $create && ! isset( $node[ $key ] ) )
|
|
|
|
$node[ $key ] = array();
|
|
|
|
|
|
|
|
if ( ! is_array( $node ) || ! isset( $node[ $key ] ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
$node = &$node[ $key ];
|
|
|
|
}
|
|
|
|
|
2015-02-03 05:15:21 -05:00
|
|
|
if ( $create ) {
|
|
|
|
if ( ! is_array( $node ) ) {
|
|
|
|
// account for an array overriding a string or object value
|
|
|
|
$node = array();
|
|
|
|
}
|
|
|
|
if ( ! isset( $node[ $last ] ) ) {
|
|
|
|
$node[ $last ] = array();
|
|
|
|
}
|
|
|
|
}
|
2012-02-24 23:12:43 -05:00
|
|
|
|
|
|
|
if ( ! isset( $node[ $last ] ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'root' => &$root,
|
|
|
|
'node' => &$node,
|
|
|
|
'key' => $last,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Will attempt to replace a specific value in a multidimensional array.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @param $root
|
|
|
|
* @param $keys
|
|
|
|
* @param mixed $value The value to update.
|
2015-10-10 05:06:25 -04:00
|
|
|
* @return mixed
|
2012-02-24 23:12:43 -05:00
|
|
|
*/
|
|
|
|
final protected function multidimensional_replace( $root, $keys, $value ) {
|
|
|
|
if ( ! isset( $value ) )
|
|
|
|
return $root;
|
|
|
|
elseif ( empty( $keys ) ) // If there are no keys, we're replacing the root.
|
|
|
|
return $value;
|
|
|
|
|
2012-03-07 11:32:11 -05:00
|
|
|
$result = $this->multidimensional( $root, $keys, true );
|
2012-02-24 23:12:43 -05:00
|
|
|
|
|
|
|
if ( isset( $result ) )
|
|
|
|
$result['node'][ $result['key'] ] = $value;
|
|
|
|
|
|
|
|
return $root;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Will attempt to fetch a specific value from a multidimensional array.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @param $root
|
|
|
|
* @param $keys
|
2014-11-30 18:33:23 -05:00
|
|
|
* @param mixed $default A default value which is used as a fallback. Default is null.
|
2012-02-24 23:12:43 -05:00
|
|
|
* @return mixed The requested value or the default value.
|
|
|
|
*/
|
|
|
|
final protected function multidimensional_get( $root, $keys, $default = null ) {
|
|
|
|
if ( empty( $keys ) ) // If there are no keys, test the root.
|
|
|
|
return isset( $root ) ? $root : $default;
|
|
|
|
|
|
|
|
$result = $this->multidimensional( $root, $keys );
|
|
|
|
return isset( $result ) ? $result['node'][ $result['key'] ] : $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Will attempt to check if a specific value in a multidimensional array is set.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @param $root
|
|
|
|
* @param $keys
|
|
|
|
* @return bool True if value is set, false if not.
|
|
|
|
*/
|
|
|
|
final protected function multidimensional_isset( $root, $keys ) {
|
|
|
|
$result = $this->multidimensional_get( $root, $keys );
|
|
|
|
return isset( $result );
|
|
|
|
}
|
|
|
|
}
|