Make WordPress Core


Ignore:
Timestamp:
03/21/2016 09:58:02 PM (10 years ago)
Author:
westonruter
Message:

Customize: Require opt-in for selective refresh of widgets.

  • Introduces customize-selective-refresh-widgets theme support feature and adds to themes.
  • Introduces customize_selective_refresh arg for WP_Widget::$widget_options and adds to all core widgets.
  • Remove selective_refresh from being a component that can be removed via customize_loaded_components filter.
  • Add WP_Customize_Widgets::get_selective_refreshable_widgets() and WP_Customize_Widgets::is_widget_selective_refreshable().
  • Fix default selector for Partial instances.
  • Implement and improve Masronry sidebar refresh logic in Twenty Thirteen and Twenty Fourteen, including preservation of initial widget position after refresh.
  • Re-initialize ME.js when refreshing Twenty_Fourteen_Ephemera_Widget.

See #27355.
Fixes #35855.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-widgets.php

    r36842 r37040  
    6363
    6464        /**
     65         * Mapping of widget ID base to whether it supports selective refresh.
     66         *
     67         * @since 4.5.0
     68         * @access protected
     69         * @var array
     70         */
     71        protected $selective_refreshable_widgets;
     72
     73        /**
    6574         * Mapping of setting type to setting ID pattern.
    6675         *
     
    7079         */
    7180        protected $setting_id_patterns = array(
    72                 'widget_instance' => '/^(widget_.+?)(?:\[(\d+)\])?$/',
    73                 'sidebar_widgets' => '/^sidebars_widgets\[(.+?)\]$/',
     81                'widget_instance' => '/^widget_(?P<id_base>.+?)(?:\[(?P<widget_number>\d+)\])?$/',
     82                'sidebar_widgets' => '/^sidebars_widgets\[(?P<sidebar_id>.+?)\]$/',
    7483        );
    7584
     
    113122
    114123        /**
     124         * List whether each registered widget can be use selective refresh.
     125         *
     126         * If the theme does not support the customize-selective-refresh-widgets feature,
     127         * then this will always return an empty array.
     128         *
     129         * @since 4.5.0
     130         * @access public
     131         *
     132         * @return array Mapping of id_base to support. If theme doesn't support
     133         *               selective refresh, an empty array is returned.
     134         */
     135        public function get_selective_refreshable_widgets() {
     136                global $wp_widget_factory;
     137                if ( ! current_theme_supports( 'customize-selective-refresh-widgets' ) ) {
     138                        return array();
     139                }
     140                if ( ! isset( $this->selective_refreshable_widgets ) ) {
     141                        $this->selective_refreshable_widgets = array();
     142                        foreach ( $wp_widget_factory->widgets as $wp_widget ) {
     143                                $this->selective_refreshable_widgets[ $wp_widget->id_base ] = ! empty( $wp_widget->widget_options['customize_selective_refresh'] );
     144                        }
     145                }
     146                return $this->selective_refreshable_widgets;
     147        }
     148
     149        /**
     150         * Determines if a widget supports selective refresh.
     151         *
     152         * @since 4.5.0
     153         * @access public
     154         *
     155         * @param string $id_base Widget ID Base.
     156         * @return bool Whether the widget can be selective refreshed.
     157         */
     158        public function is_widget_selective_refreshable( $id_base ) {
     159                $selective_refreshable_widgets = $this->get_selective_refreshable_widgets();
     160                return ! empty( $selective_refreshable_widgets[ $id_base ] );
     161        }
     162
     163        /**
    115164         * Retrieves the widget setting type given a setting ID.
    116165         *
     
    120169         * @staticvar array $cache
    121170         *
    122          * @param $setting_id Setting ID.
     171         * @param string $setting_id Setting ID.
    123172         * @return string|void Setting type.
    124173         */
     
    691740                                'moveWidgetArea'   => $move_widget_area_tpl,
    692741                        ),
    693                         'selectiveRefresh'     => isset( $this->manager->selective_refresh ),
     742                        'selectiveRefreshableWidgets' => $this->get_selective_refreshable_widgets(),
    694743                );
    695744
     
    772821                        'type'       => 'option',
    773822                        'capability' => 'edit_theme_options',
    774                         'transport'  => isset( $this->manager->selective_refresh ) ? 'postMessage' : 'refresh',
    775823                        'default'    => array(),
    776824                );
     
    779827                        $args['sanitize_callback'] = array( $this, 'sanitize_sidebar_widgets' );
    780828                        $args['sanitize_js_callback'] = array( $this, 'sanitize_sidebar_widgets_js_instance' );
     829                        $args['transport'] = current_theme_supports( 'customize-selective-refresh-widgets' ) ? 'postMessage' : 'refresh';
    781830                } elseif ( preg_match( $this->setting_id_patterns['widget_instance'], $id, $matches ) ) {
    782831                        $args['sanitize_callback'] = array( $this, 'sanitize_widget_instance' );
    783832                        $args['sanitize_js_callback'] = array( $this, 'sanitize_widget_js_instance' );
     833                        $args['transport'] = $this->is_widget_selective_refreshable( $matches['id_base'] ) ? 'postMessage' : 'refresh';
    784834                }
    785835
     
    894944                                'is_disabled'  => $is_disabled,
    895945                                'id_base'      => $id_base,
    896                                 'transport'    => isset( $this->manager->selective_refresh ) ? 'postMessage' : 'refresh',
     946                                'transport'    => $this->is_widget_selective_refreshable( $id_base ) ? 'postMessage' : 'refresh',
    897947                                'width'        => $wp_registered_widget_controls[$widget['id']]['width'],
    898948                                'height'       => $wp_registered_widget_controls[$widget['id']]['height'],
     
    10261076        public function customize_preview_enqueue() {
    10271077                wp_enqueue_script( 'customize-preview-widgets' );
     1078                wp_enqueue_style( 'customize-preview' );
    10281079        }
    10291080
     
    10611112        public function export_preview_data() {
    10621113                global $wp_registered_sidebars, $wp_registered_widgets;
     1114
    10631115                // Prepare Customizer settings to pass to JavaScript.
    10641116                $settings = array(
     
    10701122                                'widgetTooltip'  => __( 'Shift-click to edit this widget.' ),
    10711123                        ),
    1072                         'selectiveRefresh'   => isset( $this->manager->selective_refresh ),
     1124                        'selectiveRefreshableWidgets' => $this->get_selective_refreshable_widgets(),
    10731125                );
    10741126                foreach ( $settings['registeredWidgets'] as &$registered_widget ) {
     
    14801532         */
    14811533        public function customize_dynamic_partial_args( $partial_args, $partial_id ) {
     1534                if ( ! current_theme_supports( 'customize-selective-refresh-widgets' ) ) {
     1535                        return $partial_args;
     1536                }
    14821537
    14831538                if ( preg_match( '/^widget\[(?P<widget_id>.+)\]$/', $partial_id, $matches ) ) {
     
    15071562         */
    15081563        public function selective_refresh_init() {
    1509                 if ( ! isset( $this->manager->selective_refresh ) ) {
     1564                if ( ! current_theme_supports( 'customize-selective-refresh-widgets' ) ) {
    15101565                        return;
    15111566                }
    1512 
    1513                 add_action( 'wp_enqueue_scripts', array( $this, 'customize_preview_enqueue_deps' ) );
    15141567                add_filter( 'dynamic_sidebar_params', array( $this, 'filter_dynamic_sidebar_params' ) );
    15151568                add_filter( 'wp_kses_allowed_html', array( $this, 'filter_wp_kses_allowed_data_attributes' ) );
    15161569                add_action( 'dynamic_sidebar_before', array( $this, 'start_dynamic_sidebar' ) );
    15171570                add_action( 'dynamic_sidebar_after', array( $this, 'end_dynamic_sidebar' ) );
    1518         }
    1519 
    1520         /**
    1521          * Enqueues scripts for the Customizer preview.
    1522          *
    1523          * @since 4.5.0
    1524          * @access public
    1525          */
    1526         public function customize_preview_enqueue_deps() {
    1527                 if ( isset( $this->manager->selective_refresh ) ) {
    1528                         $script = wp_scripts()->registered['customize-preview-widgets'];
    1529                         $script->deps[] = 'customize-selective-refresh';
    1530                 }
    1531 
    1532                 wp_enqueue_script( 'customize-preview-widgets' );
    1533                 wp_enqueue_style( 'customize-preview' );
    15341571        }
    15351572
Note: See TracChangeset for help on using the changeset viewer.