Ticket #30936: 30936.6.diff
| File 30936.6.diff, 41.6 KB (added by , 11 years ago) |
|---|
-
src/wp-includes/class-wp-customize-manager.php
diff --git src/wp-includes/class-wp-customize-manager.php src/wp-includes/class-wp-customize-manager.php index e620ca9..2e05c6c 100644
final class WP_Customize_Manager { 65 65 /** 66 66 * Unsanitized values for Customize Settings parsed from $_POST['customized']. 67 67 * 68 * @var array |false68 * @var array 69 69 */ 70 70 private $_post_values; 71 71 … … final class WP_Customize_Manager { 102 102 add_action( 'wp_ajax_customize_save', array( $this, 'save' ) ); 103 103 104 104 add_action( 'customize_register', array( $this, 'register_controls' ) ); 105 add_action( 'customize_register', array( $this, 'register_dynamic_settings' ), 11 ); // allow code to create settings first 105 106 add_action( 'customize_controls_init', array( $this, 'prepare_controls' ) ); 106 107 add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_control_scripts' ) ); 107 108 } … … final class WP_Customize_Manager { 110 111 * Return true if it's an AJAX request. 111 112 * 112 113 * @since 3.4.0 114 * @since 4.2.0 Added $action param. 115 * 116 * @param string|null $action whether the supplied Ajax action is being run. 113 117 * 114 118 * @return bool 115 119 */ 116 public function doing_ajax() { 117 return isset( $_POST['customized'] ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ); 120 public function doing_ajax( $action = null ) { 121 $doing_ajax = ( defined( 'DOING_AJAX' ) && DOING_AJAX ); 122 if ( ! $doing_ajax ) { 123 return false; 124 } 125 126 if ( ! $action ) { 127 return true; 128 } else { 129 // Note: we can't just use doing_action( "wp_ajax_{$action}" ) because we need to check before admin-ajax.php gets to that point 130 return isset( $_REQUEST['action'] ) && wp_unslash( $_REQUEST['action'] ) === $action; 131 } 118 132 } 119 133 120 134 /** … … final class WP_Customize_Manager { 411 425 if ( isset( $_POST['customized'] ) ) { 412 426 $this->_post_values = json_decode( wp_unslash( $_POST['customized'] ), true ); 413 427 } 414 if ( empty( $this->_post_values ) ) { // if not isset or of JSON error415 $this->_post_values = false;428 if ( empty( $this->_post_values ) ) { // if not isset or if JSON error 429 $this->_post_values = array(); 416 430 } 417 431 } 418 432 if ( empty( $this->_post_values ) ) { … … final class WP_Customize_Manager { 442 456 } 443 457 444 458 /** 459 * Override a setting's (unsanitized) value as found in any incoming $_POST['customized'] 460 * 461 * @since 4.2.0 462 * 463 * @param string $setting_id The ID for the WP_Customize_Setting instance. 464 * @param mixed $value 465 */ 466 public function set_post_value( $setting_id, $value ) { 467 $this->unsanitized_post_values(); 468 $this->_post_values[ $setting_id ] = $value; 469 } 470 471 /** 445 472 * Print JavaScript settings. 446 473 * 447 474 * @since 3.4.0 … … final class WP_Customize_Manager { 727 754 } 728 755 729 756 /** 757 * Register any dynamically-created settings, such as those from $_POST['customized'] that have no corresponding setting created. 758 * 759 * This is a mechanism to "wake up" settings that have been dynamically created 760 * on the frontend and have been sent to WordPress in $_POST['customized']. When WP 761 * loads, the dynamically-created settings then will get created and previewed 762 * even though they are not directly created statically with code. 763 * 764 * @since 4.2.0 765 * 766 * @param string[] $setting_ids The setting IDs to add. 767 * @return WP_Customize_Setting[] The settings added. 768 */ 769 public function add_dynamic_settings( $setting_ids ) { 770 $new_settings = array(); 771 foreach ( $setting_ids as $setting_id ) { 772 // Skip settings already created 773 if ( $this->get_setting( $setting_id ) ) { 774 continue; 775 } 776 777 $setting_args = false; 778 $setting_class = 'WP_Customize_Setting'; 779 780 /** 781 * Filter a dynamic setting's constructor args. 782 * 783 * For a dynamic setting to be registered, this filter must be employed 784 * to override the default false value with an array of args to pass to 785 * the WP_Customize_Setting constructor. 786 * 787 * @since 4.2.0 788 * 789 * @param false|array $setting_args The arguments to the WP_Customize_Setting constructor. 790 * @param string $setting_id ID for dynamic setting, usually coming from $_POST['customized']. 791 */ 792 $setting_args = apply_filters( 'customize_dynamic_setting_args', $setting_args, $setting_id ); 793 if ( false === $setting_args ) { 794 continue; 795 } 796 797 /** 798 * Allow non-statically created settings to be constructed with custom WP_Customize_Setting subclass. 799 * 800 * @since 4.2.0 801 * 802 * @param string $setting_class WP_Customize_Setting or a subclass. 803 * @param string $setting_id ID for dynamic setting, usually coming from $_POST['customized']. 804 * @param string $setting_args WP_Customize_Setting or a subclass. 805 */ 806 $setting_class = apply_filters( 'customize_dynamic_setting_class', $setting_class, $setting_id, $setting_args ); 807 808 $setting = new $setting_class( $this, $setting_id, $setting_args ); 809 $this->add_setting( $setting ); 810 $new_settings[] = $setting; 811 } 812 return $new_settings; 813 } 814 815 /** 730 816 * Retrieve a customize setting. 731 817 * 732 818 * @since 3.4.0 … … final class WP_Customize_Manager { 735 821 * @return WP_Customize_Setting 736 822 */ 737 823 public function get_setting( $id ) { 738 if ( isset( $this->settings[ $id ] ) ) 824 if ( isset( $this->settings[ $id ] ) ) { 739 825 return $this->settings[ $id ]; 826 } 740 827 } 741 828 742 829 /** … … final class WP_Customize_Manager { 1275 1362 } 1276 1363 1277 1364 /** 1365 * Add settings from the POST data that were not added with code, e.g. dynamically-created settings for Widgets 1366 * 1367 * @since 4.2.0 1368 */ 1369 public function register_dynamic_settings() { 1370 $this->add_dynamic_settings( array_keys( $this->unsanitized_post_values() ) ); 1371 } 1372 1373 /** 1278 1374 * Callback for validating the header_textcolor value. 1279 1375 * 1280 1376 * Accepts 'blank', and otherwise uses sanitize_hex_color_no_hash(). -
src/wp-includes/class-wp-customize-setting.php
diff --git src/wp-includes/class-wp-customize-setting.php src/wp-includes/class-wp-customize-setting.php index 6cbaf3d..1634c63 100644
class WP_Customize_Setting { 55 55 protected $id_data = array(); 56 56 57 57 /** 58 * Cached and sanitized $_POST value for the setting.59 *60 * @access private61 * @var mixed62 */63 private $_post_value;64 65 /**66 58 * Constructor. 67 59 * 68 60 * Any supplied $args override class property defaults. … … class WP_Customize_Setting { 163 155 */ 164 156 public function _preview_filter( $original ) { 165 157 $undefined = new stdClass(); // symbol hack 166 $post_value = $this-> manager->post_value( $this,$undefined );158 $post_value = $this->post_value( $undefined ); 167 159 if ( $undefined === $post_value ) { 168 160 $value = $this->_original_value; 169 161 } else { … … class WP_Customize_Setting { 211 203 * @return mixed The default value on failure, otherwise the sanitized value. 212 204 */ 213 205 final public function post_value( $default = null ) { 214 // Check for a cached value 215 if ( isset( $this->_post_value ) ) 216 return $this->_post_value; 217 218 // Call the manager for the post value 219 $result = $this->manager->post_value( $this ); 220 221 if ( isset( $result ) ) 222 return $this->_post_value = $result; 223 else 224 return $default; 206 return $this->manager->post_value( $this, $default ); 225 207 } 226 208 227 209 /** -
src/wp-includes/class-wp-customize-widgets.php
diff --git src/wp-includes/class-wp-customize-widgets.php src/wp-includes/class-wp-customize-widgets.php index ba14828..22c51ea 100644
final class WP_Customize_Widgets { 35 35 /** 36 36 * @since 3.9.0 37 37 * @access protected 38 * @var39 */40 protected $_customized;41 42 /**43 * @since 3.9.044 * @access protected45 38 * @var array 46 39 */ 47 protected $ _prepreview_added_filters = array();40 protected $rendered_sidebars = array(); 48 41 49 42 /** 50 43 * @since 3.9.0 51 44 * @access protected 52 45 * @var array 53 46 */ 54 protected $rendered_ sidebars = array();47 protected $rendered_widgets = array(); 55 48 56 49 /** 57 50 * @since 3.9.0 58 51 * @access protected 59 52 * @var array 60 53 */ 61 protected $ rendered_widgets = array();54 protected $old_sidebars_widgets = array(); 62 55 63 56 /** 64 * @since 3.9.0 57 * Mapping of setting type to setting ID pattern. 58 * 59 * @since 4.2.0 65 60 * @access protected 66 61 * @var array 67 62 */ 68 protected $old_sidebars_widgets = array(); 63 protected $setting_id_patterns = array( 64 'widget_instance' => '/^(widget_.+?)(?:\[(\d+)\])?$/', 65 'sidebar_widgets' => '/^sidebars_widgets\[(.+?)\]$/', 66 ); 69 67 70 68 /** 71 69 * Initial loader. … … final class WP_Customize_Widgets { 78 76 public function __construct( $manager ) { 79 77 $this->manager = $manager; 80 78 81 add_action( 'after_setup_theme', array( $this, 'setup_widget_addition_previews' ) ); 79 add_filter( 'customize_dynamic_setting_args', array( $this, 'filter_customize_dynamic_setting_args' ), 10, 2 ); 80 add_action( 'after_setup_theme', array( $this, 'register_settings' ) ); 82 81 add_action( 'wp_loaded', array( $this, 'override_sidebars_widgets_for_theme_switch' ) ); 83 82 add_action( 'customize_controls_init', array( $this, 'customize_controls_init' ) ); 84 83 add_action( 'customize_register', array( $this, 'schedule_customize_register' ), 1 ); … … final class WP_Customize_Widgets { 95 94 } 96 95 97 96 /** 98 * Get an unslashed post value or return a default.97 * Get the widget setting type given a setting ID. 99 98 * 100 * @since 3.9.099 * @since 4.2.0 101 100 * 102 * @ access protected101 * @param $setting_id 103 102 * 104 * @param string $name Post value. 105 * @param mixed $default Default post value. 106 * @return mixed Unslashed post value or default value. 103 * @return string|null 107 104 */ 108 protected function get_post_value( $name, $default = null ) { 109 if ( ! isset( $_POST[ $name ] ) ) { 110 return $default; 105 protected function get_setting_type( $setting_id ) { 106 static $cache = array(); 107 if ( isset( $cache[ $setting_id ] ) ) { 108 return $cache[ $setting_id ]; 111 109 } 112 113 return wp_unslash( $_POST[$name] ); 110 foreach ( $this->setting_id_patterns as $type => $pattern ) { 111 if ( preg_match( $pattern, $setting_id ) ) { 112 $cache[ $setting_id ] = $type; 113 return $type; 114 } 115 } 116 return null; 114 117 } 115 118 116 119 /** 117 * Set up widget addition previews.120 * Inspect the incoming customized data for any widget settings, and dynamically add them up-front so widgets will be initialized properly. 118 121 * 119 * Since the widgets get registered on 'widgets_init' before the Customizer 120 * settings are set up on 'customize_register', we have to filter the options 121 * similarly to how the setting previewer will filter the options later. 122 * 123 * @since 3.9.0 124 * 125 * @access public 122 * @since 4.2.0 126 123 */ 127 public function setup_widget_addition_previews() { 128 $is_customize_preview = false; 129 130 if ( ! empty( $this->manager ) && ! is_admin() && 'on' === $this->get_post_value( 'wp_customize' ) ) { 131 $is_customize_preview = check_ajax_referer( 'preview-customize_' . $this->manager->get_stylesheet(), 'nonce', false ); 132 } 133 134 $is_ajax_widget_update = false; 135 if ( $this->manager->doing_ajax() && 'update-widget' === $this->get_post_value( 'action' ) ) { 136 $is_ajax_widget_update = check_ajax_referer( 'update-widget', 'nonce', false ); 137 } 138 139 $is_ajax_customize_save = false; 140 if ( $this->manager->doing_ajax() && 'customize_save' === $this->get_post_value( 'action' ) ) { 141 $is_ajax_customize_save = check_ajax_referer( 'save-customize_' . $this->manager->get_stylesheet(), 'nonce', false ); 142 } 143 144 $is_valid_request = ( $is_ajax_widget_update || $is_customize_preview || $is_ajax_customize_save ); 145 if ( ! $is_valid_request ) { 146 return; 147 } 148 149 // Input from Customizer preview. 150 if ( isset( $_POST['customized'] ) ) { 151 $this->_customized = json_decode( $this->get_post_value( 'customized' ), true ); 152 } else { // Input from ajax widget update request. 153 $this->_customized = array(); 154 $id_base = $this->get_post_value( 'id_base' ); 155 $widget_number = $this->get_post_value( 'widget_number', false ); 156 $option_name = 'widget_' . $id_base; 157 $this->_customized[ $option_name ] = array(); 158 if ( preg_match( '/^[0-9]+$/', $widget_number ) ) { 159 $option_name .= '[' . $widget_number . ']'; 160 $this->_customized[ $option_name ][ $widget_number ] = array(); 124 public function register_settings() { 125 $widget_setting_ids = array(); 126 $incoming_setting_ids = array_keys( $this->manager->unsanitized_post_values() ); 127 foreach ( $incoming_setting_ids as $setting_id ) { 128 if ( ! is_null( $this->get_setting_type( $setting_id ) ) ) { 129 $widget_setting_ids[] = $setting_id; 161 130 } 162 131 } 132 if ( $this->manager->doing_ajax( 'update-widget' ) && isset( $_REQUEST['widget-id'] ) ) { 133 $widget_setting_ids[] = $this->get_setting_id( wp_unslash( $_REQUEST['widget-id'] ) ); 134 } 163 135 164 $function = array( $this, 'prepreview_added_sidebars_widgets' ); 165 166 $hook = 'option_sidebars_widgets'; 167 add_filter( $hook, $function ); 168 $this->_prepreview_added_filters[] = compact( 'hook', 'function' ); 169 170 $hook = 'default_option_sidebars_widgets'; 171 add_filter( $hook, $function ); 172 $this->_prepreview_added_filters[] = compact( 'hook', 'function' ); 173 174 $function = array( $this, 'prepreview_added_widget_instance' ); 175 foreach ( $this->_customized as $setting_id => $value ) { 176 if ( preg_match( '/^(widget_.+?)(?:\[(\d+)\])?$/', $setting_id, $matches ) ) { 177 $option = $matches[1]; 178 179 $hook = sprintf( 'option_%s', $option ); 180 if ( ! has_filter( $hook, $function ) ) { 181 add_filter( $hook, $function ); 182 $this->_prepreview_added_filters[] = compact( 'hook', 'function' ); 183 } 184 185 $hook = sprintf( 'default_option_%s', $option ); 186 if ( ! has_filter( $hook, $function ) ) { 187 add_filter( $hook, $function ); 188 $this->_prepreview_added_filters[] = compact( 'hook', 'function' ); 189 } 136 $settings = $this->manager->add_dynamic_settings( array_unique( $widget_setting_ids ) ); 190 137 191 /* 192 * Make sure the option is registered so that the update_option() 193 * won't fail due to the filters providing a default value, which 194 * causes the update_option() to get confused. 195 */ 196 add_option( $option, array() ); 138 /* 139 * Preview settings right away so that widgets and sidebars will get registered properly. 140 * But don't do this if a customize_save because this will cause WP to think there is nothing 141 * changed that needs to be saved. 142 */ 143 if ( ! $this->manager->doing_ajax( 'customize_save' ) ) { 144 foreach ( $settings as $setting ) { 145 $setting->preview(); 197 146 } 198 147 } 199 148 } 200 149 201 150 /** 202 * Ensure that newly-added widgets will appear in the widgets_sidebars.151 * Determine the arguments for a dynamically-created setting. 203 152 * 204 * This is necessary because the Customizer's setting preview filters 205 * are added after the widgets_init action, which is too late for the 206 * widgets to be set up properly. 153 * @since 4.2.0 207 154 * 208 * @since 3.9.0 209 * @access public 210 * 211 * @param array $sidebars_widgets Associative array of sidebars and their widgets. 212 * @return array Filtered array of sidebars and their widgets. 155 * @param false|array $args 156 * @param string $setting_id 157 * @return false|array 213 158 */ 214 public function prepreview_added_sidebars_widgets( $sidebars_widgets ) { 215 foreach ( $this->_customized as $setting_id => $value ) { 216 if ( preg_match( '/^sidebars_widgets\[(.+?)\]$/', $setting_id, $matches ) ) { 217 $sidebar_id = $matches[1]; 218 $sidebars_widgets[ $sidebar_id ] = $value; 219 } 159 public function filter_customize_dynamic_setting_args( $args, $setting_id ) { 160 if ( $this->get_setting_type( $setting_id ) ) { 161 $args = $this->get_setting_args( $setting_id ); 220 162 } 221 return $ sidebars_widgets;163 return $args; 222 164 } 223 165 224 166 /** 225 * Ensure newly-added widgets have empty instances so they 226 * will be recognized. 227 * 228 * This is necessary because the Customizer's setting preview 229 * filters are added after the widgets_init action, which is 230 * too late for the widgets to be set up properly. 167 * Get an unslashed post value or return a default. 231 168 * 232 169 * @since 3.9.0 233 * @access public234 170 * 235 * @param array|bool|mixed $value Widget instance(s), false if open was empty. 236 * @return array|mixed Widget instance(s) with additions. 237 */ 238 public function prepreview_added_widget_instance( $value = false ) { 239 if ( ! preg_match( '/^(?:default_)?option_(widget_(.+))/', current_filter(), $matches ) ) { 240 return $value; 241 } 242 $id_base = $matches[2]; 243 244 foreach ( $this->_customized as $setting_id => $setting ) { 245 $parsed_setting_id = $this->parse_widget_setting_id( $setting_id ); 246 if ( is_wp_error( $parsed_setting_id ) || $id_base !== $parsed_setting_id['id_base'] ) { 247 continue; 248 } 249 $widget_number = $parsed_setting_id['number']; 250 251 if ( is_null( $widget_number ) ) { 252 // Single widget. 253 if ( false === $value ) { 254 $value = array(); 255 } 256 } else { 257 // Multi widget. 258 if ( empty( $value ) ) { 259 $value = array( '_multiwidget' => 1 ); 260 } 261 if ( ! isset( $value[ $widget_number ] ) ) { 262 $value[ $widget_number ] = array(); 263 } 264 } 265 } 266 267 return $value; 268 } 269 270 /** 271 * Remove pre-preview filters. 272 * 273 * Removes filters added in setup_widget_addition_previews() 274 * to ensure widgets are populating the options during 275 * 'widgets_init'. 171 * @access protected 276 172 * 277 * @since 3.9.0 278 * @access public 173 * @param string $name Post value. 174 * @param mixed $default Default post value. 175 * @return mixed Unslashed post value or default value. 279 176 */ 280 p ublic function remove_prepreview_filters() {281 foreach ( $this->_prepreview_added_filters as $prepreview_added_filter) {282 re move_filter( $prepreview_added_filter['hook'], $prepreview_added_filter['function'] );177 protected function get_post_value( $name, $default = null ) { 178 if ( ! isset( $_POST[ $name ] ) ) { 179 return $default; 283 180 } 284 $this->_prepreview_added_filters = array(); 181 182 return wp_unslash( $_POST[ $name ] ); 285 183 } 286 184 287 185 /** … … final class WP_Customize_Widgets { 380 278 * @access public 381 279 */ 382 280 public function schedule_customize_register() { 383 if ( is_admin() ) { // @todo for some reason, $wp_customize->is_preview() is true here?281 if ( is_admin() ) { 384 282 $this->customize_register(); 385 283 } else { 386 284 add_action( 'wp', array( $this, 'customize_register' ) ); … … final class WP_Customize_Widgets { 412 310 foreach ( array_keys( $wp_registered_widgets ) as $widget_id ) { 413 311 $setting_id = $this->get_setting_id( $widget_id ); 414 312 $setting_args = $this->get_setting_args( $setting_id ); 415 416 $setting_args['sanitize_callback'] = array( $this, 'sanitize_widget_instance' ); 417 $setting_args['sanitize_js_callback'] = array( $this, 'sanitize_widget_js_instance' ); 418 419 $this->manager->add_setting( $setting_id, $setting_args ); 420 313 if ( ! $this->manager->get_setting( $setting_id ) ) { 314 $this->manager->add_setting( $setting_id, $setting_args ); 315 } 421 316 $new_setting_ids[] = $setting_id; 422 317 } 423 318 … … final class WP_Customize_Widgets { 452 347 if ( $is_registered_sidebar || $is_inactive_widgets ) { 453 348 $setting_id = sprintf( 'sidebars_widgets[%s]', $sidebar_id ); 454 349 $setting_args = $this->get_setting_args( $setting_id ); 455 456 $setting_args['sanitize_callback'] = array( $this, 'sanitize_sidebar_widgets' ); 457 $setting_args['sanitize_js_callback'] = array( $this, 'sanitize_sidebar_widgets_js_instance' ); 458 459 $this->manager->add_setting( $setting_id, $setting_args ); 350 if ( ! $this->manager->get_setting( $setting_id ) ) { 351 $this->manager->add_setting( $setting_id, $setting_args ); 352 } 460 353 $new_setting_ids[] = $setting_id; 461 354 462 355 // Add section to contain controls. … … final class WP_Customize_Widgets { 523 416 } 524 417 } 525 418 526 /* 527 * We have to register these settings later than customize_preview_init 528 * so that other filters have had a chance to run. 529 */ 530 if ( did_action( 'customize_preview_init' ) ) { 419 if ( ! $this->manager->doing_ajax( 'customize_save' ) ) { 531 420 foreach ( $new_setting_ids as $new_setting_id ) { 532 421 $this->manager->get_setting( $new_setting_id )->preview(); 533 422 } 534 423 } 535 $this->remove_prepreview_filters(); 424 425 add_filter( 'sidebars_widgets', array( $this, 'preview_sidebars_widgets' ), 1 ); 536 426 } 537 427 538 428 /** … … final class WP_Customize_Widgets { 804 694 'transport' => 'refresh', 805 695 'default' => array(), 806 696 ); 697 698 if ( preg_match( $this->setting_id_patterns['sidebar_widgets'], $id, $matches ) ) { 699 $args['sanitize_callback'] = array( $this, 'sanitize_sidebar_widgets' ); 700 $args['sanitize_js_callback'] = array( $this, 'sanitize_sidebar_widgets_js_instance' ); 701 } else if ( preg_match( $this->setting_id_patterns['widget_instance'], $id, $matches ) ) { 702 $args['sanitize_callback'] = array( $this, 'sanitize_widget_instance' ); 703 $args['sanitize_js_callback'] = array( $this, 'sanitize_widget_js_instance' ); 704 } 705 807 706 $args = array_merge( $args, $overrides ); 808 707 809 708 /** … … final class WP_Customize_Widgets { 831 730 * @return array Array of sanitized widget IDs. 832 731 */ 833 732 public function sanitize_sidebar_widgets( $widget_ids ) { 834 global $wp_registered_widgets; 835 836 $widget_ids = array_map( 'strval', (array) $widget_ids ); 733 $widget_ids = array_map( 'strval', (array) $widget_ids ); 837 734 $sanitized_widget_ids = array(); 838 839 735 foreach ( $widget_ids as $widget_id ) { 840 if ( array_key_exists( $widget_id, $wp_registered_widgets ) ) { 841 $sanitized_widget_ids[] = $widget_id; 842 } 736 $sanitized_widget_ids[] = preg_replace( '/[^a-z0-9_\-]/', '', $widget_id ); 843 737 } 844 738 return $sanitized_widget_ids; 845 739 } … … final class WP_Customize_Widgets { 974 868 * @access public 975 869 */ 976 870 public function customize_preview_init() { 977 add_filter( 'sidebars_widgets', array( $this, 'preview_sidebars_widgets' ), 1 );978 871 add_action( 'wp_enqueue_scripts', array( $this, 'customize_preview_enqueue' ) ); 979 872 add_action( 'wp_print_styles', array( $this, 'print_preview_css' ), 1 ); 980 873 add_action( 'wp_footer', array( $this, 'export_preview_data' ), 20 ); … … final class WP_Customize_Widgets { 1315 1208 1316 1209 // Clean up any input vars that were manually added 1317 1210 foreach ( $added_input_vars as $key ) { 1318 unset( $_POST[ $key] );1319 unset( $_REQUEST[ $key] );1211 unset( $_POST[ $key ] ); 1212 unset( $_REQUEST[ $key ] ); 1320 1213 } 1321 1214 1322 1215 // Make sure the expected option was updated. … … final class WP_Customize_Widgets { 1333 1226 } 1334 1227 } 1335 1228 1229 // Obtain the widget instance. 1230 $option = $this->get_captured_option( $option_name ); 1231 if ( null !== $parsed_id['number'] ) { 1232 $instance = $option[ $parsed_id['number'] ]; 1233 } else { 1234 $instance = $option; 1235 } 1236 1237 /* 1238 * Override the incoming $_POST['customized'] for a newly-created widget's 1239 * setting with the new $instance so that the preview filter currently 1240 * in place from WP_Customize_Setting::preview() will use this value 1241 * instead of the default widget instance value (an empty array). 1242 */ 1243 $setting_id = $this->get_setting_id( $widget_id ); 1244 $this->manager->set_post_value( $setting_id, $instance ); 1245 1336 1246 // Obtain the widget control with the updated instance in place. 1337 1247 ob_start(); 1338 1339 $form = $wp_registered_widget_controls[$widget_id]; 1248 $form = $wp_registered_widget_controls[ $widget_id ]; 1340 1249 if ( $form ) { 1341 1250 call_user_func_array( $form['callback'], $form['params'] ); 1342 1251 } 1343 1344 1252 $form = ob_get_clean(); 1345 1253 1346 // Obtain the widget instance.1347 $option = get_option( $option_name );1348 1349 if ( null !== $parsed_id['number'] ) {1350 $instance = $option[$parsed_id['number']];1351 } else {1352 $instance = $option;1353 }1354 1355 1254 $this->stop_capturing_option_updates(); 1356 1255 1357 1256 return compact( 'instance', 'form' ); … … final class WP_Customize_Widgets { 1383 1282 wp_die( -1 ); 1384 1283 } 1385 1284 1386 if ( ! isset( $_POST['widget-id'] ) ) {1387 wp_send_json_error( );1285 if ( empty( $_POST['widget-id'] ) ) { 1286 wp_send_json_error( 'missing_widget-id' ); 1388 1287 } 1389 1288 1390 1289 /** This action is documented in wp-admin/includes/ajax-actions.php */ … … final class WP_Customize_Widgets { 1398 1297 1399 1298 $widget_id = $this->get_post_value( 'widget-id' ); 1400 1299 $parsed_id = $this->parse_widget_id( $widget_id ); 1401 $id_base = $parsed_id['id_base']; 1402 1403 if ( isset( $_POST['widget-' . $id_base] ) && is_array( $_POST['widget-' . $id_base] ) && preg_match( '/__i__|%i%/', key( $_POST['widget-' . $id_base] ) ) ) { 1404 wp_send_json_error(); 1300 $id_base = $parsed_id['id_base']; 1301 1302 $is_updating_widget_template = ( 1303 isset( $_POST[ 'widget-' . $id_base ] ) 1304 && 1305 is_array( $_POST[ 'widget-' . $id_base ] ) 1306 && 1307 preg_match( '/__i__|%i%/', key( $_POST[ 'widget-' . $id_base ] ) ) 1308 ); 1309 if ( $is_updating_widget_template ) { 1310 wp_send_json_error( 'template_widget_not_updatable' ); 1405 1311 } 1406 1312 1407 1313 $updated_widget = $this->call_widget_update( $widget_id ); // => {instance,form} 1408 1314 if ( is_wp_error( $updated_widget ) ) { 1409 wp_send_json_error( );1315 wp_send_json_error( $updated_widget->get_error_message() ); 1410 1316 } 1411 1317 1412 1318 $form = $updated_widget['form']; … … final class WP_Customize_Widgets { 1463 1369 } 1464 1370 1465 1371 /** 1372 * Get the option that was captured from being saved. 1373 * 1374 * @since 4.2.0 1375 * @access protected 1376 * 1377 * @param string $option_name Option name. 1378 * @param mixed $default Optional. Default value to return if the option does not exist. 1379 * @return mixed Value set for the option. 1380 */ 1381 protected function get_captured_option( $option_name, $default = false ) { 1382 if ( array_key_exists( $option_name, $this->_captured_options ) ) { 1383 $value = $this->_captured_options[ $option_name ]; 1384 } else { 1385 $value = $default; 1386 } 1387 return $value; 1388 } 1389 1390 /** 1466 1391 * Get the number of captured widget option updates. 1467 1392 * 1468 1393 * @since 3.9.0 … … final class WP_Customize_Widgets { 1496 1421 * @since 3.9.0 1497 1422 * @access public 1498 1423 * 1499 * @param mixed $new_value1500 * @param string $option_name 1501 * @param mixed $old_value1502 * @return mixed 1424 * @param mixed $new_value The new option value. 1425 * @param string $option_name Name of the option. 1426 * @param mixed $old_value The old option value. 1427 * @return mixed Filtered option value. 1503 1428 */ 1504 1429 public function capture_filter_pre_update_option( $new_value, $option_name, $old_value ) { 1505 1430 if ( $this->is_option_capture_ignored( $option_name ) ) { 1506 1431 return; 1507 1432 } 1508 1433 1509 if ( ! isset( $this->_captured_options[ $option_name] ) ) {1434 if ( ! isset( $this->_captured_options[ $option_name ] ) ) { 1510 1435 add_filter( "pre_option_{$option_name}", array( $this, 'capture_filter_pre_get_option' ) ); 1511 1436 } 1512 1437 1513 $this->_captured_options[ $option_name] = $new_value;1438 $this->_captured_options[ $option_name ] = $new_value; 1514 1439 1515 1440 return $old_value; 1516 1441 } … … final class WP_Customize_Widgets { 1521 1446 * @since 3.9.0 1522 1447 * @access public 1523 1448 * 1524 * @param mixed $value Option1525 * @return mixed 1449 * @param mixed $value Value to return instead of the option value. 1450 * @return mixed Filtered option value. 1526 1451 */ 1527 1452 public function capture_filter_pre_get_option( $value ) { 1528 1453 $option_name = preg_replace( '/^pre_option_/', '', current_filter() ); 1529 1454 1530 if ( isset( $this->_captured_options[ $option_name] ) ) {1531 $value = $this->_captured_options[ $option_name];1455 if ( isset( $this->_captured_options[ $option_name ] ) ) { 1456 $value = $this->_captured_options[ $option_name ]; 1532 1457 1533 1458 /** This filter is documented in wp-includes/option.php */ 1534 1459 $value = apply_filters( 'option_' . $option_name, $value ); … … final class WP_Customize_Widgets { 1557 1482 $this->_captured_options = array(); 1558 1483 $this->_is_capturing_option_updates = false; 1559 1484 } 1485 1486 /** 1487 * @since 3.9.0 1488 * @deprecated 4.2.0 Deprecated in favor of customize_dynamic_setting_args filter. 1489 */ 1490 public function setup_widget_addition_previews() { 1491 _deprecated_function( __METHOD__, '4.2.0' ); 1492 } 1493 1494 /** 1495 * @since 3.9.0 1496 * @deprecated 4.2.0 Deprecated in favor of customize_dynamic_setting_args filter. 1497 */ 1498 public function prepreview_added_sidebars_widgets() { 1499 _deprecated_function( __METHOD__, '4.2.0' ); 1500 } 1501 1502 /** 1503 * @since 3.9.0 1504 * @deprecated 4.2.0 Deprecated in favor of customize_dynamic_setting_args filter. 1505 */ 1506 public function prepreview_added_widget_instance() { 1507 _deprecated_function( __METHOD__, '4.2.0' ); 1508 } 1509 1510 /** 1511 * @since 3.9.0 1512 * @deprecated 4.2.0 Deprecated in favor of customize_dynamic_setting_args filter. 1513 */ 1514 public function remove_prepreview_filters() { 1515 _deprecated_function( __METHOD__, '4.2.0' ); 1516 } 1560 1517 } -
tests/phpunit/tests/customize/manager.php
diff --git tests/phpunit/tests/customize/manager.php tests/phpunit/tests/customize/manager.php index 41d7b78..f669d7f 100644
class Tests_WP_Customize_Manager extends WP_UnitTestCase { 32 32 } 33 33 34 34 /** 35 * Test WP_Customize_Manager::unsanitized_post_values() 35 * Test WP_Customize_Manager::doing_ajax(). 36 * 37 * @group ajax 38 */ 39 function test_doing_ajax() { 40 if ( ! defined( 'DOING_AJAX' ) ) { 41 define( 'DOING_AJAX', true ); 42 } 43 44 $manager = $this->instantiate(); 45 $this->assertTrue( $manager->doing_ajax() ); 46 47 $_REQUEST['action'] = 'customize_save'; 48 $this->assertTrue( $manager->doing_ajax( 'customize_save' ) ); 49 $this->assertFalse( $manager->doing_ajax( 'update-widget' ) ); 50 } 51 52 /** 53 * Test ! WP_Customize_Manager::doing_ajax(). 54 */ 55 function test_not_doing_ajax() { 56 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { 57 $this->markTestSkipped( 'Cannot test when DOING_AJAX' ); 58 } 59 60 $manager = $this->instantiate(); 61 $this->assertFalse( $manager->doing_ajax() ); 62 } 63 64 /** 65 * Test WP_Customize_Manager::unsanitized_post_values(). 36 66 * 37 67 * @ticket 30988 38 68 */ … … class Tests_WP_Customize_Manager extends WP_UnitTestCase { 49 79 } 50 80 51 81 /** 52 * Test the WP_Customize_Manager::post_value() method 82 * Test the WP_Customize_Manager::post_value() method. 53 83 * 54 84 * @ticket 30988 55 85 */ … … class Tests_WP_Customize_Manager extends WP_UnitTestCase { 71 101 $this->assertEquals( 'post_value_bar_default', $manager->post_value( $bar_setting, 'post_value_bar_default' ), 'Expected post_value($bar_setting, $default) to return $default since no value supplied in $_POST[customized][bar]' ); 72 102 } 73 103 74 } 104 /** 105 * Test the WP_Customize_Manager::add_dynamic_settings() method. 106 * 107 * @ticket 30936 108 */ 109 function test_add_dynamic_settings() { 110 $manager = $this->instantiate(); 111 $setting_ids = array( 'foo', 'bar' ); 112 $manager->add_setting( 'foo', array( 'default' => 'foo_default' ) ); 113 $this->assertEmpty( $manager->get_setting( 'bar' ), 'Expected there to not be a bar setting up front.' ); 114 $manager->add_dynamic_settings( $setting_ids ); 115 $this->assertEmpty( $manager->get_setting( 'bar' ), 'Expected the bar setting to remain absent since filters not added.' ); 116 117 $this->action_customize_register_for_dynamic_settings(); 118 $manager->add_dynamic_settings( $setting_ids ); 119 $this->assertNotEmpty( $manager->get_setting( 'bar' ), 'Expected bar setting to be created since filters were added.' ); 120 $this->assertEquals( 'foo_default', $manager->get_setting( 'foo' )->default, 'Expected static foo setting to not get overridden by dynamic setting.' ); 121 $this->assertEquals( 'dynamic_bar_default', $manager->get_setting( 'bar' )->default, 'Expected dynamic setting bar to have default providd by filter.' ); 122 } 75 123 124 /** 125 * Test the WP_Customize_Manager::register_dynamic_settings() method. 126 * 127 * This is similar to test_add_dynamic_settings, except the settings are passed via $_POST['customized']. 128 * 129 * @ticket 30936 130 */ 131 function test_register_dynamic_settings() { 132 $posted_settings = array( 133 'foo' => 'OOF', 134 'bar' => 'RAB', 135 ); 136 $_POST['customized'] = wp_slash( wp_json_encode( $posted_settings ) ); 137 138 add_action( 'customize_register', array( $this, 'action_customize_register_for_dynamic_settings' ) ); 139 140 $manager = $this->instantiate(); 141 $manager->add_setting( 'foo', array( 'default' => 'foo_default' ) ); 142 143 $this->assertEmpty( $manager->get_setting( 'bar' ), 'Expected dynamic setting "bar" to not be registered.' ); 144 do_action( 'customize_register', $manager ); 145 $this->assertNotEmpty( $manager->get_setting( 'bar' ), 'Expected dynamic setting "bar" to be automatically registered after customize_register action.' ); 146 $this->assertEmpty( $manager->get_setting( 'baz' ), 'Expected unrecognized dynamic setting "baz" to remain unregistered.' ); 147 } 148 149 /** 150 * In lieu of closures, callback for customize_register action added in test_register_dynamic_settings(). 151 */ 152 function action_customize_register_for_dynamic_settings() { 153 add_filter( 'customize_dynamic_setting_args', array( $this, 'filter_customize_dynamic_setting_args_for_test_dynamic_settings' ), 10, 2 ); 154 add_filter( 'customize_dynamic_setting_class', array( $this, 'filter_customize_dynamic_setting_class_for_test_dynamic_settings' ), 10, 3 ); 155 } 156 157 /** 158 * In lieu of closures, callback for customize_dynamic_setting_args filter added for test_register_dynamic_settings(). 159 */ 160 function filter_customize_dynamic_setting_args_for_test_dynamic_settings( $setting_args, $setting_id ) { 161 $this->assertEquals( false, $setting_args, 'Expected $setting_args to be false by default.' ); 162 $this->assertInternalType( 'string', $setting_id ); 163 if ( in_array( $setting_id, array( 'foo', 'bar' ) ) ) { 164 $setting_args = array( 'default' => "dynamic_{$setting_id}_default" ); 165 } 166 return $setting_args; 167 } 168 169 /** 170 * In lieu of closures, callback for customize_dynamic_setting_class filter added for test_register_dynamic_settings(). 171 */ 172 function filter_customize_dynamic_setting_class_for_test_dynamic_settings( $setting_class, $setting_id, $setting_args ) { 173 $this->assertEquals( 'WP_Customize_Setting', $setting_class ); 174 $this->assertInternalType( 'string', $setting_id ); 175 $this->assertInternalType( 'array', $setting_args ); 176 return $setting_class; 177 } 178 } -
new file tests/phpunit/tests/customize/widgets.php
diff --git tests/phpunit/tests/customize/widgets.php tests/phpunit/tests/customize/widgets.php new file mode 100644 index 0000000..9e1f61e
- + 1 <?php 2 3 /** 4 * Tests for the WP_Customize_Widgets class. 5 * 6 * @group customize 7 */ 8 class Tests_WP_Customize_Widgets extends WP_UnitTestCase { 9 10 /** 11 * @var WP_Customize_Manager 12 */ 13 protected $manager; 14 15 function setUp() { 16 parent::setUp(); 17 require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' ); 18 $GLOBALS['wp_customize'] = new WP_Customize_Manager(); // wpcs: override ok 19 $this->manager = $GLOBALS['wp_customize']; 20 21 unset( $GLOBALS['_wp_sidebars_widgets'] ); // clear out cache set by wp_get_sidebars_widgets() 22 $sidebars_widgets = wp_get_sidebars_widgets(); 23 $this->assertEqualSets( array( 'wp_inactive_widgets', 'sidebar-1' ), array_keys( wp_get_sidebars_widgets() ) ); 24 $this->assertContains( 'search-2', $sidebars_widgets['sidebar-1'] ); 25 $this->assertContains( 'categories-2', $sidebars_widgets['sidebar-1'] ); 26 $this->assertArrayHasKey( 2, get_option( 'widget_search' ) ); 27 $widget_categories = get_option( 'widget_categories' ); 28 $this->assertArrayHasKey( 2, $widget_categories ); 29 $this->assertEquals( '', $widget_categories[2]['title'] ); 30 31 remove_action( 'after_setup_theme', 'twentyfifteen_setup' ); // @todo We should not be including a theme anyway 32 33 $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); 34 wp_set_current_user( $user_id ); 35 } 36 37 function tearDown() { 38 parent::tearDown(); 39 $this->manager = null; 40 unset( $GLOBALS['wp_customize'] ); 41 } 42 43 function set_customized_post_data( $customized ) { 44 $_POST['customized'] = wp_slash( wp_json_encode( $customized ) ); 45 } 46 47 function do_customize_boot_actions() { 48 $_SERVER['REQUEST_METHOD'] = 'POST'; 49 do_action( 'setup_theme' ); 50 $_REQUEST['nonce'] = wp_create_nonce( 'preview-customize_' . $this->manager->theme()->get_stylesheet() ); 51 do_action( 'after_setup_theme' ); 52 do_action( 'init' ); 53 do_action( 'wp_loaded' ); 54 do_action( 'wp', $GLOBALS['wp'] ); 55 } 56 57 /** 58 * Test WP_Customize_Widgets::__construct() 59 */ 60 function test_construct() { 61 $this->assertInstanceOf( 'WP_Customize_Widgets', $this->manager->widgets ); 62 $this->assertEquals( $this->manager, $this->manager->widgets->manager ); 63 } 64 65 /** 66 * Test WP_Customize_Widgets::register_settings() 67 * 68 * @ticket 30988 69 */ 70 function test_register_settings() { 71 72 $raw_widget_customized = array( 73 'widget_categories[2]' => array( 74 'title' => 'Taxonomies Brand New Value', 75 'count' => 0, 76 'hierarchical' => 0, 77 'dropdown' => 0, 78 ), 79 'widget_search[3]' => array( 80 'title' => 'Not as good as Google!', 81 ), 82 ); 83 $customized = array(); 84 foreach ( $raw_widget_customized as $setting_id => $instance ) { 85 $customized[ $setting_id ] = $this->manager->widgets->sanitize_widget_js_instance( $instance ); 86 } 87 88 $this->set_customized_post_data( $customized ); 89 $this->do_customize_boot_actions(); 90 $this->assertTrue( is_customize_preview() ); 91 92 $this->assertNotEmpty( $this->manager->get_setting( 'widget_categories[2]' ), 'Expected setting for pre-existing widget category-2, being customized.' ); 93 $this->assertNotEmpty( $this->manager->get_setting( 'widget_search[2]' ), 'Expected setting for pre-existing widget search-2, not being customized.' ); 94 $this->assertNotEmpty( $this->manager->get_setting( 'widget_search[3]' ), 'Expected dynamic setting for non-existing widget search-3, being customized.' ); 95 96 $widget_categories = get_option( 'widget_categories' ); 97 $this->assertEquals( $raw_widget_customized['widget_categories[2]'], $widget_categories[2], 'Expected $wp_customize->get_setting(widget_categories[2])->preview() to have been called.' ); 98 } 99 100 /** 101 * Test WP_Customize_Widgets::get_setting_args() 102 */ 103 function test_get_setting_args() { 104 105 add_filter( 'widget_customizer_setting_args', array( $this, 'filter_widget_customizer_setting_args' ), 10, 2 ); 106 107 $default_args = array( 108 'type' => 'option', 109 'capability' => 'edit_theme_options', 110 'transport' => 'refresh', 111 'default' => array(), 112 'sanitize_callback' => array( $this->manager->widgets, 'sanitize_widget_instance' ), 113 'sanitize_js_callback' => array( $this->manager->widgets, 'sanitize_widget_js_instance' ), 114 ); 115 116 $args = $this->manager->widgets->get_setting_args( 'widget_foo[2]' ); 117 foreach ( $default_args as $key => $default_value ) { 118 $this->assertEquals( $default_value, $args[ $key ] ); 119 } 120 $this->assertEquals( 'WIDGET_FOO[2]', $args['uppercase_id_set_by_filter'] ); 121 122 $override_args = array( 123 'type' => 'theme_mod', 124 'capability' => 'edit_posts', 125 'transport' => 'postMessage', 126 'default' => array( 'title' => 'asd' ), 127 'sanitize_callback' => '__return_empty_array', 128 'sanitize_js_callback' => '__return_empty_array', 129 ); 130 $args = $this->manager->widgets->get_setting_args( 'widget_bar[3]', $override_args ); 131 foreach ( $override_args as $key => $override_value ) { 132 $this->assertEquals( $override_value, $args[ $key ] ); 133 } 134 $this->assertEquals( 'WIDGET_BAR[3]', $args['uppercase_id_set_by_filter'] ); 135 136 $default_args = array( 137 'type' => 'option', 138 'capability' => 'edit_theme_options', 139 'transport' => 'refresh', 140 'default' => array(), 141 'sanitize_callback' => array( $this->manager->widgets, 'sanitize_sidebar_widgets' ), 142 'sanitize_js_callback' => array( $this->manager->widgets, 'sanitize_sidebar_widgets_js_instance' ), 143 ); 144 $args = $this->manager->widgets->get_setting_args( 'sidebars_widgets[sidebar-1]' ); 145 foreach ( $default_args as $key => $default_value ) { 146 $this->assertEquals( $default_value, $args[ $key ] ); 147 } 148 $this->assertEquals( 'SIDEBARS_WIDGETS[SIDEBAR-1]', $args['uppercase_id_set_by_filter'] ); 149 150 $override_args = array( 151 'type' => 'theme_mod', 152 'capability' => 'edit_posts', 153 'transport' => 'postMessage', 154 'default' => array( 'title' => 'asd' ), 155 'sanitize_callback' => '__return_empty_array', 156 'sanitize_js_callback' => '__return_empty_array', 157 ); 158 $args = $this->manager->widgets->get_setting_args( 'sidebars_widgets[sidebar-2]', $override_args ); 159 foreach ( $override_args as $key => $override_value ) { 160 $this->assertEquals( $override_value, $args[ $key ] ); 161 } 162 $this->assertEquals( 'SIDEBARS_WIDGETS[SIDEBAR-2]', $args['uppercase_id_set_by_filter'] ); 163 } 164 165 function filter_widget_customizer_setting_args( $args, $id ) { 166 $args['uppercase_id_set_by_filter'] = strtoupper( $id ); 167 return $args; 168 } 169 170 /** 171 * Test WP_Customize_Widgets::sanitize_widget_js_instance() and WP_Customize_Widgets::sanitize_widget_instance() 172 */ 173 function test_sanitize_widget_js_instance() { 174 $this->do_customize_boot_actions(); 175 176 $new_categories_instance = array( 177 'title' => 'Taxonomies Brand New Value', 178 'count' => '1', 179 'hierarchical' => '1', 180 'dropdown' => '1', 181 ); 182 183 $sanitized_for_js = $this->manager->widgets->sanitize_widget_js_instance( $new_categories_instance ); 184 $this->assertArrayHasKey( 'encoded_serialized_instance', $sanitized_for_js ); 185 $this->assertTrue( is_serialized( base64_decode( $sanitized_for_js['encoded_serialized_instance'] ), true ) ); 186 $this->assertEquals( $new_categories_instance['title'], $sanitized_for_js['title'] ); 187 $this->assertTrue( $sanitized_for_js['is_widget_customizer_js_value'] ); 188 $this->assertArrayHasKey( 'instance_hash_key', $sanitized_for_js ); 189 190 $corrupted_sanitized_for_js = $sanitized_for_js; 191 $corrupted_sanitized_for_js['encoded_serialized_instance'] = base64_encode( serialize( array( 'title' => 'EVIL' ) ) ); 192 $this->assertNull( $this->manager->widgets->sanitize_widget_instance( $corrupted_sanitized_for_js ), 'Expected sanitize_widget_instance to reject corrupted data.' ); 193 194 $unsanitized_from_js = $this->manager->widgets->sanitize_widget_instance( $sanitized_for_js ); 195 $this->assertEquals( $unsanitized_from_js, $new_categories_instance ); 196 } 197 }