WordPress 4.3
[autoinstalls/wordpress.git] / wp-includes / class-wp-customize-widgets.php
index 728bb5f04b12bec14ef85ab55d5525033fb81cd0..8569228e4670eafd9d92a16bb86b30cf0bd30ef7 100644 (file)
@@ -108,8 +108,10 @@ final class WP_Customize_Widgets {
         * @since 4.2.0
         * @access protected
         *
+        * @staticvar array $cache
+        *
         * @param $setting_id Setting ID.
-        * @return string|null Setting type. Null otherwise.
+        * @return string|void Setting type.
         */
        protected function get_setting_type( $setting_id ) {
                static $cache = array();
@@ -122,7 +124,6 @@ final class WP_Customize_Widgets {
                                return $type;
                        }
                }
-               return null;
        }
 
        /**
@@ -204,6 +205,9 @@ final class WP_Customize_Widgets {
         *
         * @since 3.9.0
         * @access public
+        *
+        * @global array $sidebars_widgets
+        * @global array $_wp_sidebars_widgets
         */
        public function override_sidebars_widgets_for_theme_switch() {
                global $sidebars_widgets;
@@ -219,7 +223,8 @@ final class WP_Customize_Widgets {
                $sidebars_widgets = $this->old_sidebars_widgets;
                $sidebars_widgets = retrieve_widgets( 'customize' );
                add_filter( 'option_sidebars_widgets', array( $this, 'filter_option_sidebars_widgets_for_theme_switch' ), 1 );
-               unset( $GLOBALS['_wp_sidebars_widgets'] ); // reset global cache var used by wp_get_sidebars_widgets()
+               // reset global cache var used by wp_get_sidebars_widgets()
+               unset( $GLOBALS['_wp_sidebars_widgets'] );
        }
 
        /**
@@ -236,6 +241,7 @@ final class WP_Customize_Widgets {
         * @access public
         *
         * @param array $old_sidebars_widgets
+        * @return array
         */
        public function filter_customize_value_old_sidebars_widgets_data( $old_sidebars_widgets ) {
                return $this->old_sidebars_widgets;
@@ -252,7 +258,10 @@ final class WP_Customize_Widgets {
         * @since 3.9.0
         * @access public
         *
+        * @global array $sidebars_widgets
+        *
         * @param array $sidebars_widgets
+        * @return array
         */
        public function filter_option_sidebars_widgets_for_theme_switch( $sidebars_widgets ) {
                $sidebars_widgets = $GLOBALS['sidebars_widgets'];
@@ -302,13 +311,17 @@ final class WP_Customize_Widgets {
         *
         * @since 3.9.0
         * @access public
+        *
+        * @global array $wp_registered_widgets
+        * @global array $wp_registered_widget_controls
+        * @global array $wp_registered_sidebars
         */
        public function customize_register() {
                global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_sidebars;
 
                $sidebars_widgets = array_merge(
                        array( 'wp_inactive_widgets' => array() ),
-                       array_fill_keys( array_keys( $GLOBALS['wp_registered_sidebars'] ), array() ),
+                       array_fill_keys( array_keys( $wp_registered_sidebars ), array() ),
                        wp_get_sidebars_widgets()
                );
 
@@ -352,7 +365,7 @@ final class WP_Customize_Widgets {
                                $sidebar_widget_ids = array();
                        }
 
-                       $is_registered_sidebar = isset( $GLOBALS['wp_registered_sidebars'][$sidebar_id] );
+                       $is_registered_sidebar = isset( $wp_registered_sidebars[ $sidebar_id ] );
                        $is_inactive_widgets   = ( 'wp_inactive_widgets' === $sidebar_id );
                        $is_active_sidebar     = ( $is_registered_sidebar && ! $is_inactive_widgets );
 
@@ -373,8 +386,8 @@ final class WP_Customize_Widgets {
                                if ( $is_active_sidebar ) {
 
                                        $section_args = array(
-                                               'title' => $GLOBALS['wp_registered_sidebars'][ $sidebar_id ]['name'],
-                                               'description' => $GLOBALS['wp_registered_sidebars'][ $sidebar_id ]['description'],
+                                               'title' => $wp_registered_sidebars[ $sidebar_id ]['name'],
+                                               'description' => $wp_registered_sidebars[ $sidebar_id ]['description'],
                                                'priority' => array_search( $sidebar_id, array_keys( $wp_registered_sidebars ) ),
                                                'panel' => 'widgets',
                                                'sidebar_id' => $sidebar_id,
@@ -409,13 +422,13 @@ final class WP_Customize_Widgets {
                        foreach ( $sidebar_widget_ids as $i => $widget_id ) {
 
                                // Skip widgets that may have gone away due to a plugin being deactivated.
-                               if ( ! $is_active_sidebar || ! isset( $GLOBALS['wp_registered_widgets'][$widget_id] ) ) {
+                               if ( ! $is_active_sidebar || ! isset( $wp_registered_widgets[$widget_id] ) ) {
                                        continue;
                                }
 
-                               $registered_widget = $GLOBALS['wp_registered_widgets'][$widget_id];
+                               $registered_widget = $wp_registered_widgets[$widget_id];
                                $setting_id        = $this->get_setting_id( $widget_id );
-                               $id_base           = $GLOBALS['wp_registered_widget_controls'][$widget_id]['id_base'];
+                               $id_base           = $wp_registered_widget_controls[$widget_id]['id_base'];
 
                                $control = new WP_Widget_Form_Customize_Control( $this->manager, $setting_id, array(
                                        'label'          => $registered_widget['name'],
@@ -473,6 +486,8 @@ final class WP_Customize_Widgets {
         * @since 3.9.0
         * @access public
         *
+        * @global $wp_registered_widget_controls
+        *
         * @param string $widget_id Widget ID.
         * @return bool Whether or not the widget is a "wide" widget.
         */
@@ -576,8 +591,14 @@ final class WP_Customize_Widgets {
         *
         * @since 3.9.0
         * @access public
+        *
+        * @global WP_Scripts $wp_scripts
+        * @global array $wp_registered_sidebars
+        * @global array $wp_registered_widgets
         */
        public function enqueue_scripts() {
+               global $wp_scripts, $wp_registered_sidebars, $wp_registered_widgets;
+
                wp_enqueue_style( 'customize-widgets' );
                wp_enqueue_script( 'customize-widgets' );
 
@@ -621,12 +642,10 @@ final class WP_Customize_Widgets {
                        </div>'
                );
 
-               global $wp_scripts;
-
                $settings = array(
                        'nonce'                => wp_create_nonce( 'update-widget' ),
-                       'registeredSidebars'   => array_values( $GLOBALS['wp_registered_sidebars'] ),
-                       'registeredWidgets'    => $GLOBALS['wp_registered_widgets'],
+                       'registeredSidebars'   => array_values( $wp_registered_sidebars ),
+                       'registeredWidgets'    => $wp_registered_widgets,
                        'availableWidgets'     => $available_widgets, // @todo Merge this with registered_widgets
                        'l10n' => array(
                                'saveBtnLabel'     => __( 'Apply' ),
@@ -664,6 +683,18 @@ final class WP_Customize_Widgets {
                ?>
                <div id="widgets-left"><!-- compatibility with JS which looks for widget templates here -->
                <div id="available-widgets">
+                       <div class="customize-section-title">
+                               <button class="customize-section-back" tabindex="-1">
+                                       <span class="screen-reader-text"><?php _e( 'Back' ); ?></span>
+                               </button>
+                               <h3>
+                                       <span class="customize-action"><?php
+                                               /* translators: &#9656; is the unicode right-pointing triangle, and %s is the section title in the Customizer */
+                                               echo sprintf( __( 'Customizing &#9656; %s' ), esc_html( $this->manager->get_panel( 'widgets' )->title ) );
+                                       ?></span>
+                                       <?php _e( 'Add a Widget' ); ?>
+                               </h3>
+                       </div>
                        <div id="available-widgets-filter">
                                <label class="screen-reader-text" for="widgets-search"><?php _e( 'Search Widgets' ); ?></label>
                                <input type="search" id="widgets-search" placeholder="<?php esc_attr_e( 'Search widgets&hellip;' ) ?>" />
@@ -716,7 +747,7 @@ final class WP_Customize_Widgets {
                if ( preg_match( $this->setting_id_patterns['sidebar_widgets'], $id, $matches ) ) {
                        $args['sanitize_callback'] = array( $this, 'sanitize_sidebar_widgets' );
                        $args['sanitize_js_callback'] = array( $this, 'sanitize_sidebar_widgets_js_instance' );
-               } else if ( preg_match( $this->setting_id_patterns['widget_instance'], $id, $matches ) ) {
+               } elseif ( preg_match( $this->setting_id_patterns['widget_instance'], $id, $matches ) ) {
                        $args['sanitize_callback'] = array( $this, 'sanitize_widget_instance' );
                        $args['sanitize_js_callback'] = array( $this, 'sanitize_widget_js_instance' );
                }
@@ -762,6 +793,10 @@ final class WP_Customize_Widgets {
         * @since 3.9.0
         * @access public
         *
+        * @global array $wp_registered_widgets
+        * @global array $wp_registered_widget_controls
+        * @staticvar array $available_widgets
+        *
         * @see wp_list_widgets()
         *
         * @return array List of available widgets.
@@ -843,7 +878,6 @@ final class WP_Customize_Widgets {
         * Naturally order available widgets by name.
         *
         * @since 3.9.0
-        * @static
         * @access protected
         *
         * @param array $widget_a The first widget to compare.
@@ -918,6 +952,7 @@ final class WP_Customize_Widgets {
         * @access public
         *
         * @param array $sidebars_widgets List of widgets for the current sidebar.
+        * @return array
         */
        public function preview_sidebars_widgets( $sidebars_widgets ) {
                $sidebars_widgets = get_option( 'sidebars_widgets' );
@@ -965,15 +1000,18 @@ final class WP_Customize_Widgets {
         *
         * @since 3.9.0
         * @access public
+     *
+        * @global array $wp_registered_sidebars
+        * @global array $wp_registered_widgets
         */
        public function export_preview_data() {
-
+               global $wp_registered_sidebars, $wp_registered_widgets;
                // Prepare Customizer settings to pass to JavaScript.
                $settings = array(
                        'renderedSidebars'   => array_fill_keys( array_unique( $this->rendered_sidebars ), true ),
                        'renderedWidgets'    => array_fill_keys( array_keys( $this->rendered_widgets ), true ),
-                       'registeredSidebars' => array_values( $GLOBALS['wp_registered_sidebars'] ),
-                       'registeredWidgets'  => $GLOBALS['wp_registered_widgets'],
+                       'registeredSidebars' => array_values( $wp_registered_sidebars ),
+                       'registeredWidgets'  => $wp_registered_widgets,
                        'l10n'               => array(
                                'widgetTooltip' => __( 'Shift-click to edit this widget.' ),
                        ),
@@ -1038,8 +1076,11 @@ final class WP_Customize_Widgets {
         * @since 3.9.0
         * @access public
         *
+        * @global array $wp_registered_sidebars
+        *
         * @param bool   $is_active  Whether the sidebar is active.
         * @param string $sidebar_id Sidebar ID.
+        * @return bool
         */
        public function tally_sidebars_via_is_active_sidebar_calls( $is_active, $sidebar_id ) {
                if ( isset( $GLOBALS['wp_registered_sidebars'][$sidebar_id] ) ) {
@@ -1063,8 +1104,11 @@ final class WP_Customize_Widgets {
         * @since 3.9.0
         * @access public
         *
+        * @global array $wp_registered_sidebars
+        *
         * @param bool   $has_widgets Whether the current sidebar has widgets.
         * @param string $sidebar_id  Sidebar ID.
+        * @return bool
         */
        public function tally_sidebars_via_dynamic_sidebar_calls( $has_widgets, $sidebar_id ) {
                if ( isset( $GLOBALS['wp_registered_sidebars'][$sidebar_id] ) ) {
@@ -1105,7 +1149,7 @@ final class WP_Customize_Widgets {
         * @access public
         *
         * @param array $value Widget instance to sanitize.
-        * @return array Sanitized widget instance.
+        * @return array|void Sanitized widget instance.
         */
        public function sanitize_widget_instance( $value ) {
                if ( $value === array() ) {
@@ -1116,21 +1160,21 @@ final class WP_Customize_Widgets {
                        || empty( $value['instance_hash_key'] )
                        || empty( $value['encoded_serialized_instance'] ) )
                {
-                       return null;
+                       return;
                }
 
                $decoded = base64_decode( $value['encoded_serialized_instance'], true );
                if ( false === $decoded ) {
-                       return null;
+                       return;
                }
 
                if ( ! hash_equals( $this->get_instance_hash_key( $decoded ), $value['instance_hash_key'] ) ) {
-                       return null;
+                       return;
                }
 
                $instance = unserialize( $decoded );
                if ( false === $instance ) {
-                       return null;
+                       return;
                }
 
                return $instance;
@@ -1168,6 +1212,8 @@ final class WP_Customize_Widgets {
         * @since 3.9.0
         * @access public
         *
+        * @global array $wp_registered_widgets
+        *
         * @param array $widget_ids List of widget IDs.
         * @return array Parsed list of widget IDs.
         */
@@ -1185,6 +1231,9 @@ final class WP_Customize_Widgets {
         * @since 3.9.0
         * @access public
         *
+        * @global array $wp_registered_widget_updates
+        * @global array $wp_registered_widget_controls
+        *
         * @param  string $widget_id Widget ID.
         * @return WP_Error|array Array containing the updated widget information.
         *                        A WP_Error object, otherwise.
@@ -1192,6 +1241,20 @@ final class WP_Customize_Widgets {
        public function call_widget_update( $widget_id ) {
                global $wp_registered_widget_updates, $wp_registered_widget_controls;
 
+               $setting_id = $this->get_setting_id( $widget_id );
+
+               /*
+                * Make sure that other setting changes have previewed since this widget
+                * may depend on them (e.g. Menus being present for Custom Menu widget).
+                */
+               if ( ! did_action( 'customize_preview_init' ) ) {
+                       foreach ( $this->manager->settings() as $setting ) {
+                               if ( $setting->id !== $setting_id ) {
+                                       $setting->preview();
+                               }
+                       }
+               }
+
                $this->start_capturing_option_updates();
                $parsed_id   = $this->parse_widget_id( $widget_id );
                $option_name = 'widget_' . $parsed_id['id_base'];
@@ -1272,7 +1335,6 @@ final class WP_Customize_Widgets {
                 * in place from WP_Customize_Setting::preview() will use this value
                 * instead of the default widget instance value (an empty array).
                 */
-               $setting_id = $this->get_setting_id( $widget_id );
                $this->manager->set_post_value( $setting_id, $instance );
 
                // Obtain the widget control with the updated instance in place.
@@ -1344,7 +1406,7 @@ final class WP_Customize_Widgets {
 
                $updated_widget = $this->call_widget_update( $widget_id ); // => {instance,form}
                if ( is_wp_error( $updated_widget ) ) {
-                       wp_send_json_error( $updated_widget->get_error_message() );
+                       wp_send_json_error( $updated_widget->get_error_code() );
                }
 
                $form = $updated_widget['form'];
@@ -1382,7 +1444,7 @@ final class WP_Customize_Widgets {
         * @access protected
         *
         * @param string $option_name Option name.
-        * @return boolean Whether the option capture is ignored.
+        * @return bool Whether the option capture is ignored.
         */
        protected function is_option_capture_ignored( $option_name ) {
                return ( 0 === strpos( $option_name, '_transient_' ) );