mirror of
https://github.com/WordPress/WordPress.git
synced 2024-11-17 16:15:24 +01:00
cc5ed3a485
The exceptions to this are update_post_meta() and add_post_meta() which are often used by plugins in POST handlers and will continue accepting slashed data for now. Introduce wp_upate_post_meta() and wp_add_post_meta() as unslashed alternatives to update_post_meta() and add_post_meta(). These functions could become methods in WP_Post so don't use them too heavily yet. Remove all escape() calls from wp_xmlrpc_server. Now that core expects unslashed data this is no longer needed. Remove addslashes(), addslashes_gpc(), add_magic_quotes() calls on data being prepared for handoff to core functions that until now expected slashed data. Adding slashes in no longer necessary. Introduce wp_unslash() and use to it remove slashes from GPCS data before using it in core API. Almost every instance of stripslashes() in core should now be wp_unslash(). In the future (a release or three) when GPCS is no longer slashed, wp_unslash() will stop stripping slashes and simply return what is passed. At this point wp_unslash() calls can be removed from core. Introduce wp_slash() for slashing GPCS data. This will also turn into a noop once GPCS is no longer slashed. wp_slash() should almost never be used. It is mainly of use in unit tests. Plugins should use wp_unslash() on data being passed to core API. Plugins should no longer slash data being passed to core. So when you get_post() and then wp_insert_post() the post data from get_post() no longer needs addslashes(). Most plugins were not bothering with this. They will magically start doing the right thing. Unfortunately, those few souls who did it properly will now have to avoid calling addslashes() for 3.6 and newer. Use wp_kses_post() and wp_kses_data(), which expect unslashed data, instead of wp_filter_post_kses() and wp_filter_kses(), which expect slashed data. Filters are no longer passed slashed data. Remove many no longer necessary calls to $wpdb->escape() and esc_sql(). In wp_get_referer() and wp_get_original_referer(), return unslashed data. Remove old stripslashes() calls from WP_Widget::update() handlers. These haven't been necessary since WP_Widget. Switch several queries over to prepare(). Expect something to break. Props alexkingorg see #21767 git-svn-id: http://core.svn.wordpress.org/trunk@23416 1a063a9b-81f0-0310-95a4-ce76da25c4cd
440 lines
11 KiB
PHP
440 lines
11 KiB
PHP
<?php
|
|
/**
|
|
* Customize Setting Class.
|
|
*
|
|
* @package WordPress
|
|
* @subpackage Customize
|
|
* @since 3.4.0
|
|
*/
|
|
class WP_Customize_Setting {
|
|
public $manager;
|
|
public $id;
|
|
|
|
public $type = 'theme_mod';
|
|
public $capability = 'edit_theme_options';
|
|
public $theme_supports = '';
|
|
public $default = '';
|
|
public $transport = 'refresh';
|
|
|
|
public $sanitize_callback = '';
|
|
public $sanitize_js_callback = '';
|
|
|
|
protected $id_data = array();
|
|
private $_post_value; // Cached, sanitized $_POST value.
|
|
|
|
/**
|
|
* Constructor.
|
|
*
|
|
* @since 3.4.0
|
|
*
|
|
* @param WP_Customize_Manager $manager
|
|
* @param string $id An specific ID of the setting. Can be a
|
|
* theme mod or option name.
|
|
* @param array $args Setting arguments.
|
|
* @return WP_Customize_Setting
|
|
*/
|
|
function __construct( $manager, $id, $args = array() ) {
|
|
$keys = array_keys( get_class_vars( __CLASS__ ) );
|
|
foreach ( $keys as $key ) {
|
|
if ( isset( $args[ $key ] ) )
|
|
$this->$key = $args[ $key ];
|
|
}
|
|
|
|
$this->manager = $manager;
|
|
$this->id = $id;
|
|
|
|
// Parse the ID for array keys.
|
|
$this->id_data[ 'keys' ] = preg_split( '/\[/', str_replace( ']', '', $this->id ) );
|
|
$this->id_data[ 'base' ] = array_shift( $this->id_data[ 'keys' ] );
|
|
|
|
// Rebuild the ID.
|
|
$this->id = $this->id_data[ 'base' ];
|
|
if ( ! empty( $this->id_data[ 'keys' ] ) )
|
|
$this->id .= '[' . implode( '][', $this->id_data[ 'keys' ] ) . ']';
|
|
|
|
if ( $this->sanitize_callback )
|
|
add_filter( "customize_sanitize_{$this->id}", $this->sanitize_callback, 10, 2 );
|
|
|
|
if ( $this->sanitize_js_callback )
|
|
add_filter( "customize_sanitize_js_{$this->id}", $this->sanitize_js_callback, 10, 2 );
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Handle previewing the setting.
|
|
*
|
|
* @since 3.4.0
|
|
*/
|
|
public function preview() {
|
|
switch( $this->type ) {
|
|
case 'theme_mod' :
|
|
add_filter( 'theme_mod_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
|
|
break;
|
|
case 'option' :
|
|
if ( empty( $this->id_data[ 'keys' ] ) )
|
|
add_filter( 'pre_option_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
|
|
else {
|
|
add_filter( 'option_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
|
|
add_filter( 'default_option_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
|
|
}
|
|
break;
|
|
default :
|
|
do_action( 'customize_preview_' . $this->id );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Callback function to filter the theme mods and options.
|
|
*
|
|
* @since 3.4.0
|
|
* @uses WP_Customize_Setting::multidimensional_replace()
|
|
*
|
|
* @param mixed $original Old value.
|
|
* @return mixed New or old value.
|
|
*/
|
|
public function _preview_filter( $original ) {
|
|
return $this->multidimensional_replace( $original, $this->id_data[ 'keys' ], $this->post_value() );
|
|
}
|
|
|
|
/**
|
|
* Set the value of the parameter for a specific theme.
|
|
*
|
|
* @since 3.4.0
|
|
*
|
|
* @return bool False if cap check fails or value isn't set.
|
|
*/
|
|
public final function save() {
|
|
$value = $this->post_value();
|
|
|
|
if ( ! $this->check_capabilities() || ! isset( $value ) )
|
|
return false;
|
|
|
|
do_action( 'customize_save_' . $this->id_data[ 'base' ] );
|
|
|
|
$this->update( $value );
|
|
}
|
|
|
|
/**
|
|
* Fetches, validates, and sanitizes the $_POST value.
|
|
*
|
|
* @since 3.4.0
|
|
*
|
|
* @param mixed $default A default value which is used as a fallback. Default is null.
|
|
* @return mixed The default value on failure, otherwise the sanitized value.
|
|
*/
|
|
public final function post_value( $default = null ) {
|
|
if ( isset( $this->_post_value ) )
|
|
return $this->_post_value;
|
|
|
|
$result = $this->manager->post_value( $this );
|
|
|
|
if ( isset( $result ) )
|
|
return $this->_post_value = $result;
|
|
else
|
|
return $default;
|
|
}
|
|
|
|
/**
|
|
* Sanitize an input.
|
|
*
|
|
* @since 3.4.0
|
|
*
|
|
* @param mixed $value The value to sanitize.
|
|
* @return mixed Null if an input isn't valid, otherwise the sanitized value.
|
|
*/
|
|
public function sanitize( $value ) {
|
|
$value = wp_unslash( $value );
|
|
return apply_filters( "customize_sanitize_{$this->id}", $value, $this );
|
|
}
|
|
|
|
/**
|
|
* Set the value of the parameter for a specific theme.
|
|
*
|
|
* @since 3.4.0
|
|
*
|
|
* @param mixed $value The value to update.
|
|
* @return mixed The result of saving the value.
|
|
*/
|
|
protected function update( $value ) {
|
|
switch( $this->type ) {
|
|
case 'theme_mod' :
|
|
return $this->_update_theme_mod( $value );
|
|
break;
|
|
case 'option' :
|
|
return $this->_update_option( $value );
|
|
break;
|
|
default :
|
|
return do_action( 'customize_update_' . $this->type, $value );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update the theme mod from the value of the parameter.
|
|
*
|
|
* @since 3.4.0
|
|
*
|
|
* @param mixed $value The value to update.
|
|
* @return mixed The result of saving the value.
|
|
*/
|
|
protected function _update_theme_mod( $value ) {
|
|
// Handle non-array theme mod.
|
|
if ( empty( $this->id_data[ 'keys' ] ) )
|
|
return set_theme_mod( $this->id_data[ 'base' ], $value );
|
|
|
|
// Handle array-based theme mod.
|
|
$mods = get_theme_mod( $this->id_data[ 'base' ] );
|
|
$mods = $this->multidimensional_replace( $mods, $this->id_data[ 'keys' ], $value );
|
|
if ( isset( $mods ) )
|
|
return set_theme_mod( $this->id_data[ 'base' ], $mods );
|
|
}
|
|
|
|
/**
|
|
* Update the theme mod from the value of the parameter.
|
|
*
|
|
* @since 3.4.0
|
|
*
|
|
* @param mixed $value The value to update.
|
|
* @return mixed The result of saving the value.
|
|
*/
|
|
protected function _update_option( $value ) {
|
|
// Handle non-array option.
|
|
if ( empty( $this->id_data[ 'keys' ] ) )
|
|
return update_option( $this->id_data[ 'base' ], $value );
|
|
|
|
// Handle array-based options.
|
|
$options = get_option( $this->id_data[ 'base' ] );
|
|
$options = $this->multidimensional_replace( $options, $this->id_data[ 'keys' ], $value );
|
|
if ( isset( $options ) )
|
|
return update_option( $this->id_data[ 'base' ], $options );
|
|
}
|
|
|
|
/**
|
|
* Fetch the value of the parameter for a specific theme.
|
|
*
|
|
* @since 3.4.0
|
|
*
|
|
* @return mixed The requested value.
|
|
*/
|
|
public function value() {
|
|
switch( $this->type ) {
|
|
case 'theme_mod' :
|
|
$function = 'get_theme_mod';
|
|
break;
|
|
case 'option' :
|
|
$function = 'get_option';
|
|
break;
|
|
default :
|
|
return apply_filters( 'customize_value_' . $this->id_data[ 'base' ], $this->default );
|
|
}
|
|
|
|
// Handle non-array value
|
|
if ( empty( $this->id_data[ 'keys' ] ) )
|
|
return $function( $this->id_data[ 'base' ], $this->default );
|
|
|
|
// Handle array-based value
|
|
$values = $function( $this->id_data[ 'base' ] );
|
|
return $this->multidimensional_get( $values, $this->id_data[ 'keys' ], $this->default );
|
|
}
|
|
|
|
/**
|
|
* Escape the parameter's value for use in JavaScript.
|
|
*
|
|
* @since 3.4.0
|
|
*
|
|
* @return mixed The requested escaped value.
|
|
*/
|
|
public function js_value() {
|
|
$value = apply_filters( "customize_sanitize_js_{$this->id}", $this->value(), $this );
|
|
|
|
if ( is_string( $value ) )
|
|
return html_entity_decode( $value, ENT_QUOTES, 'UTF-8');
|
|
|
|
return $value;
|
|
}
|
|
|
|
/**
|
|
* Check if the theme supports the setting and check user capabilities.
|
|
*
|
|
* @since 3.4.0
|
|
*
|
|
* @return bool False if theme doesn't support the setting or user can't change setting, otherwise true.
|
|
*/
|
|
public final function check_capabilities() {
|
|
if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) )
|
|
return false;
|
|
|
|
if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) )
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Multidimensional helper function.
|
|
*
|
|
* @since 3.4.0
|
|
*
|
|
* @param $root
|
|
* @param $keys
|
|
* @param bool $create Default is false.
|
|
* @return null|array Keys are 'root', 'node', and 'key'.
|
|
*/
|
|
final protected function multidimensional( &$root, $keys, $create = false ) {
|
|
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 ];
|
|
}
|
|
|
|
if ( $create && ! isset( $node[ $last ] ) )
|
|
$node[ $last ] = array();
|
|
|
|
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.
|
|
* @return
|
|
*/
|
|
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;
|
|
|
|
$result = $this->multidimensional( $root, $keys, true );
|
|
|
|
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
|
|
* @param $default A default value which is used as a fallback. Default is null.
|
|
* @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 );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* A setting that is used to filter a value, but will not save the results.
|
|
*
|
|
* Results should be properly handled using another setting or callback.
|
|
*
|
|
* @package WordPress
|
|
* @subpackage Customize
|
|
* @since 3.4.0
|
|
*/
|
|
class WP_Customize_Filter_Setting extends WP_Customize_Setting {
|
|
|
|
/**
|
|
* @since 3.4.0
|
|
*/
|
|
public function update( $value ) {}
|
|
}
|
|
|
|
/**
|
|
* A setting that is used to filter a value, but will not save the results.
|
|
*
|
|
* Results should be properly handled using another setting or callback.
|
|
*
|
|
* @package WordPress
|
|
* @subpackage Customize
|
|
* @since 3.4.0
|
|
*/
|
|
final class WP_Customize_Header_Image_Setting extends WP_Customize_Setting {
|
|
public $id = 'header_image_data';
|
|
|
|
/**
|
|
* @since 3.4.0
|
|
*
|
|
* @param $value
|
|
*/
|
|
public function update( $value ) {
|
|
global $custom_image_header;
|
|
|
|
// If the value doesn't exist (removed or random),
|
|
// use the header_image value.
|
|
if ( ! $value )
|
|
$value = $this->manager->get_setting('header_image')->post_value();
|
|
|
|
if ( is_array( $value ) && isset( $value['choice'] ) )
|
|
$custom_image_header->set_header_image( $value['choice'] );
|
|
else
|
|
$custom_image_header->set_header_image( $value );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @package WordPress
|
|
* @subpackage Customize
|
|
* @since 3.4.0
|
|
*/
|
|
final class WP_Customize_Background_Image_Setting extends WP_Customize_Setting {
|
|
public $id = 'background_image_thumb';
|
|
|
|
/**
|
|
* @since 3.4.0
|
|
* @uses remove_theme_mod()
|
|
*
|
|
* @param $value
|
|
*/
|
|
public function update( $value ) {
|
|
remove_theme_mod( 'background_image_thumb' );
|
|
}
|
|
}
|