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
|
2019-11-09 20:43:02 -05:00
|
|
|
* @link https://developer.wordpress.org/themes/customize-api
|
2015-02-25 03:18:26 -05:00
|
|
|
*
|
|
|
|
* @see WP_Customize_Manager
|
|
|
|
*/
|
2012-02-24 23:12:43 -05:00
|
|
|
class WP_Customize_Setting {
|
2014-03-04 15:21:14 -05:00
|
|
|
/**
|
2017-05-19 16:25:41 -04:00
|
|
|
* Customizer bootstrap instance.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
2014-03-04 15:21:14 -05:00
|
|
|
* @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.
|
|
|
|
*
|
2017-05-19 16:25:41 -04:00
|
|
|
* @since 3.4.0
|
2014-03-04 15:21:14 -05:00
|
|
|
* @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
|
|
|
/**
|
2017-05-19 16:25:41 -04:00
|
|
|
* Type of customize settings.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
2014-03-04 15:21:14 -05:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $type = 'theme_mod';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Capability required to edit this setting.
|
|
|
|
*
|
2017-05-19 16:25:41 -04:00
|
|
|
* @since 3.4.0
|
|
|
|
* @var string|array
|
2014-03-04 15:21:14 -05:00
|
|
|
*/
|
|
|
|
public $capability = 'edit_theme_options';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Feature a theme is required to support to enable this setting.
|
|
|
|
*
|
2017-05-19 16:25:41 -04:00
|
|
|
* @since 3.4.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $theme_supports = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default value for the setting.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $default = '';
|
|
|
|
|
|
|
|
/**
|
2019-04-17 09:26:51 -04:00
|
|
|
* Options for rendering the live preview of changes in Customizer.
|
2017-05-19 16:25:41 -04:00
|
|
|
*
|
2019-04-17 09:26:51 -04:00
|
|
|
* Set this value to 'postMessage' to enable a custom JavaScript handler to render changes to this setting
|
2017-05-19 16:25:41 -04:00
|
|
|
* as opposed to reloading the whole page.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
2014-03-04 15:21:14 -05:00
|
|
|
* @var string
|
|
|
|
*/
|
2017-05-19 16:25:41 -04:00
|
|
|
public $transport = 'refresh';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Server-side validation callback for the setting's value.
|
|
|
|
*
|
|
|
|
* @since 4.6.0
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
public $validate_callback = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback to filter a Customize setting value in un-slashed form.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
public $sanitize_callback = '';
|
2012-05-26 14:44:31 -04:00
|
|
|
|
2014-03-04 15:21:14 -05:00
|
|
|
/**
|
2017-05-19 16:25:41 -04:00
|
|
|
* Callback to convert a Customize PHP setting value to a value that is JSON serializable.
|
2014-03-04 15:21:14 -05:00
|
|
|
*
|
2017-05-19 16:25:41 -04:00
|
|
|
* @since 3.4.0
|
|
|
|
* @var string
|
2014-03-04 15:21:14 -05:00
|
|
|
*/
|
2012-05-26 14:44:31 -04:00
|
|
|
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
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $dirty = false;
|
|
|
|
|
2015-09-05 15:53:24 -04:00
|
|
|
/**
|
2017-05-19 16:25:41 -04:00
|
|
|
* ID Data.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
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
|
|
|
|
* @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
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $aggregated_multidimensionals = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the multidimensional setting is aggregated.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
* @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;
|
2017-11-30 18:11:00 -05:00
|
|
|
$this->id = $id;
|
2012-02-24 23:12:43 -05:00
|
|
|
|
|
|
|
// 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.
|
2017-11-30 18:11:00 -05:00
|
|
|
$this->id = $this->id_data['base'];
|
|
|
|
if ( ! empty( $this->id_data['keys'] ) ) {
|
2015-10-10 05:06:25 -04:00
|
|
|
$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
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
* @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
|
|
|
|
* @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
|
|
|
*
|
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.
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
$id_base = $this->id_data['base'];
|
|
|
|
$is_multidimensional = ! empty( $this->id_data['keys'] );
|
2015-10-10 05:06:25 -04:00
|
|
|
$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.
|
|
|
|
*/
|
2017-11-30 18:11:00 -05:00
|
|
|
$undefined = new stdClass();
|
2015-10-10 05:06:25 -04:00
|
|
|
$needs_preview = ( $undefined !== $this->post_value( $undefined ) );
|
2017-11-30 18:11:00 -05:00
|
|
|
$value = null;
|
2015-10-10 05:06:25 -04:00
|
|
|
|
|
|
|
// Since no post value was defined, check if we have an initial value set.
|
|
|
|
if ( ! $needs_preview ) {
|
|
|
|
if ( $this->is_multidimensional_aggregated ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
$root = self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'];
|
2015-10-10 05:06:25 -04:00
|
|
|
$value = $this->multidimensional_get( $root, $this->id_data['keys'], $undefined );
|
|
|
|
} else {
|
2017-11-30 18:11:00 -05:00
|
|
|
$default = $this->default;
|
2015-10-10 05:06:25 -04:00
|
|
|
$this->default = $undefined; // Temporarily set default to undefined so we can detect if existing value is set.
|
2017-11-30 18:11:00 -05:00
|
|
|
$value = $this->value();
|
2015-10-10 05:06:25 -04:00
|
|
|
$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 ) {
|
2017-11-30 18:11:00 -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;
|
2017-11-30 18:11:00 -05:00
|
|
|
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;
|
2017-11-30 18:11:00 -05:00
|
|
|
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
|
2017-05-19 16:25:41 -04:00
|
|
|
*
|
2015-11-20 21:52:27 -05:00
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2017-11-30 18:11:00 -05: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
|
|
|
|
*
|
|
|
|
* @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 );
|
2017-11-30 18:11:00 -05:00
|
|
|
$root = self::$aggregated_multidimensionals[ $previewed_setting->type ][ $id_base ]['root_value'];
|
|
|
|
$root = $previewed_setting->multidimensional_replace( $root, $previewed_setting->id_data['keys'], $value );
|
2015-10-10 05:06:25 -04:00
|
|
|
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
|
|
|
*
|
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
|
|
|
|
2016-12-13 23:18:42 -05:00
|
|
|
$id_base = $this->id_data['base'];
|
|
|
|
|
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
|
|
|
*
|
2016-12-13 23:18:42 -05:00
|
|
|
* The dynamic portion of the hook name, `$id_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
|
|
|
*/
|
2016-12-13 23:18:42 -05:00
|
|
|
do_action( "customize_save_{$id_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
|
|
|
*
|
Customize: Allow users to more seamlessly create page-based nav menus during customization.
Introduces the ability to create stubs for the various post types to add to a given menu. This eliminates the need to leave the customizer to first create the post in the admin and then return to managing menus. Only the title of the newly-created post can be supplied; the post content will be blank and will need to be provided in the normal edit post screen outside the customizer, unless a plugin enables a post editing in the customizer experience. When a post is created and added to a nav menu in the customizer, the newly created post that is added to a menu is given the `auto-draft` status, and if the changes are not published, the `auto-draft` post will be automatically deleted within 7 days via `wp_delete_auto_drafts()`. However, if the customizer changes are saved, then these nav menu item `auto-draft` post stubs will be transitioned to `publish`.
Includes portions of code from the Customize Posts <https://github.com/xwp/wp-customize-posts> and Front-end Editor <https://github.com/iseulde/wp-front-end-editor> plugins.
For more information, see https://make.wordpress.org/core/2016/06/16/feature-proposal-content-authorship-in-menus-with-live-preview/
Props celloexpressions, westonruter, valendesigns, afercia, melchoyce, mapk, iseulde, mrahmadawais.
Fixes #34923.
Built from https://develop.svn.wordpress.org/trunk@38436
git-svn-id: http://core.svn.wordpress.org/trunk@38377 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-08-29 18:59:28 -04:00
|
|
|
* During a save request prior to save, post_value() provides the new value while value() does not.
|
|
|
|
*
|
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
|
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 );
|
|
|
|
|
2018-02-26 21:31:31 -05:00
|
|
|
if ( is_wp_error( $validity ) && ! $validity->has_errors() ) {
|
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
|
|
|
$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
|
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 );
|
2017-01-31 20:40:52 -05:00
|
|
|
} elseif ( 'theme_mod' === $this->type ) {
|
2015-10-10 05:06:25 -04:00
|
|
|
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
|
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 );
|
2017-01-31 20:40:52 -05:00
|
|
|
} elseif ( 'theme_mod' === $this->type ) {
|
2015-10-10 05:06:25 -04:00
|
|
|
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() {
|
2017-11-30 18:11:00 -05:00
|
|
|
$id_base = $this->id_data['base'];
|
2015-10-10 05:06:25 -04:00
|
|
|
$is_core_type = ( 'option' === $this->type || 'theme_mod' === $this->type );
|
|
|
|
|
|
|
|
if ( ! $is_core_type && ! $this->is_multidimensional_aggregated ) {
|
2016-11-19 01:00:34 -05:00
|
|
|
|
|
|
|
// Use post value if previewed and a post value is present.
|
|
|
|
if ( $this->is_previewed ) {
|
|
|
|
$value = $this->post_value( null );
|
|
|
|
if ( null !== $value ) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-10 05:06:25 -04:00
|
|
|
$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'];
|
2017-11-30 18:11:00 -05:00
|
|
|
$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
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( is_string( $value ) ) {
|
|
|
|
return html_entity_decode( $value, ENT_QUOTES, 'UTF-8' );
|
|
|
|
}
|
2012-05-16 16:59:02 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @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() {
|
2019-09-15 07:27:55 -04:00
|
|
|
if ( $this->capability && ! current_user_can( $this->capability ) ) {
|
2012-02-24 23:12:43 -05:00
|
|
|
return false;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2012-02-24 23:12:43 -05:00
|
|
|
|
2019-09-15 07:27:55 -04:00
|
|
|
if ( $this->theme_supports && ! current_theme_supports( ... (array) $this->theme_supports ) ) {
|
2012-02-24 23:12:43 -05:00
|
|
|
return false;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2012-02-24 23:12:43 -05:00
|
|
|
|
|
|
|
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 ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( $create && empty( $root ) ) {
|
2012-02-24 23:12:43 -05:00
|
|
|
$root = array();
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2012-02-24 23:12:43 -05:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! isset( $root ) || empty( $keys ) ) {
|
2012-02-24 23:12:43 -05:00
|
|
|
return;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2012-02-24 23:12:43 -05:00
|
|
|
|
|
|
|
$last = array_pop( $keys );
|
|
|
|
$node = &$root;
|
|
|
|
|
|
|
|
foreach ( $keys as $key ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( $create && ! isset( $node[ $key ] ) ) {
|
2012-02-24 23:12:43 -05:00
|
|
|
$node[ $key ] = array();
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2012-02-24 23:12:43 -05:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! is_array( $node ) || ! isset( $node[ $key ] ) ) {
|
2012-02-24 23:12:43 -05:00
|
|
|
return;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2012-02-24 23:12:43 -05:00
|
|
|
|
|
|
|
$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
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! isset( $node[ $last ] ) ) {
|
2012-02-24 23:12:43 -05:00
|
|
|
return;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2012-02-24 23:12:43 -05:00
|
|
|
|
|
|
|
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 ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! isset( $value ) ) {
|
2012-02-24 23:12:43 -05:00
|
|
|
return $root;
|
2017-11-30 18:11:00 -05:00
|
|
|
} elseif ( empty( $keys ) ) { // If there are no keys, we're replacing the root.
|
2012-02-24 23:12:43 -05:00
|
|
|
return $value;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2012-02-24 23:12:43 -05:00
|
|
|
|
2012-03-07 11:32:11 -05:00
|
|
|
$result = $this->multidimensional( $root, $keys, true );
|
2012-02-24 23:12:43 -05:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( isset( $result ) ) {
|
2012-02-24 23:12:43 -05:00
|
|
|
$result['node'][ $result['key'] ] = $value;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2012-02-24 23:12:43 -05:00
|
|
|
|
|
|
|
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 ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( empty( $keys ) ) { // If there are no keys, test the root.
|
2012-02-24 23:12:43 -05:00
|
|
|
return isset( $root ) ? $root : $default;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2012-02-24 23:12:43 -05:00
|
|
|
|
|
|
|
$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 );
|
|
|
|
}
|
|
|
|
}
|
2016-08-31 12:31:29 -04:00
|
|
|
|
2017-05-19 16:25:41 -04:00
|
|
|
/**
|
|
|
|
* WP_Customize_Filter_Setting class.
|
|
|
|
*/
|
2016-08-31 12:31:29 -04:00
|
|
|
require_once( ABSPATH . WPINC . '/customize/class-wp-customize-filter-setting.php' );
|
|
|
|
|
2017-05-19 16:25:41 -04:00
|
|
|
/**
|
|
|
|
* WP_Customize_Header_Image_Setting class.
|
|
|
|
*/
|
2016-08-31 12:31:29 -04:00
|
|
|
require_once( ABSPATH . WPINC . '/customize/class-wp-customize-header-image-setting.php' );
|
|
|
|
|
2017-05-19 16:25:41 -04:00
|
|
|
/**
|
|
|
|
* WP_Customize_Background_Image_Setting class.
|
|
|
|
*/
|
2016-08-31 12:31:29 -04:00
|
|
|
require_once( ABSPATH . WPINC . '/customize/class-wp-customize-background-image-setting.php' );
|
|
|
|
|
2017-05-19 16:25:41 -04:00
|
|
|
/**
|
|
|
|
* WP_Customize_Nav_Menu_Item_Setting class.
|
|
|
|
*/
|
2016-08-31 12:31:29 -04:00
|
|
|
require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-item-setting.php' );
|
|
|
|
|
2017-05-19 16:25:41 -04:00
|
|
|
/**
|
|
|
|
* WP_Customize_Nav_Menu_Setting class.
|
|
|
|
*/
|
2016-08-31 12:31:29 -04:00
|
|
|
require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-setting.php' );
|