get_stylesheet(), 'nonce', false ) ); $is_ajax_widget_update = ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) && self::get_post_value( 'action' ) === self::UPDATE_WIDGET_AJAX_ACTION && check_ajax_referer( self::UPDATE_WIDGET_AJAX_ACTION, self::UPDATE_WIDGET_NONCE_POST_KEY, false ) ); $is_ajax_customize_save = ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) && self::get_post_value( 'action' ) === 'customize_save' && check_ajax_referer( 'save-customize_' . $wp_customize->get_stylesheet(), 'nonce' ) ); $is_valid_request = ( $is_ajax_widget_update || $is_customize_preview || $is_ajax_customize_save ); if ( ! $is_valid_request ) { return; } // Input from customizer preview if ( isset( $_POST['customized'] ) ) { $customized = json_decode( self::get_post_value( 'customized' ), true ); } // Input from ajax widget update request else { $customized = array(); $id_base = self::get_post_value( 'id_base' ); $widget_number = (int) self::get_post_value( 'widget_number' ); $option_name = 'widget_' . $id_base; $customized[$option_name] = array(); if ( false !== $widget_number ) { $option_name .= '[' . $widget_number . ']'; $customized[$option_name][$widget_number] = array(); } } $function = array( __CLASS__, 'prepreview_added_sidebars_widgets' ); $hook = 'option_sidebars_widgets'; add_filter( $hook, $function ); self::$_prepreview_added_filters[] = compact( 'hook', 'function' ); $hook = 'default_option_sidebars_widgets'; add_filter( $hook, $function ); self::$_prepreview_added_filters[] = compact( 'hook', 'function' ); foreach ( $customized as $setting_id => $value ) { if ( preg_match( '/^(widget_.+?)(\[(\d+)\])?$/', $setting_id, $matches ) ) { $body = sprintf( 'return %s::prepreview_added_widget_instance( $value, %s );', __CLASS__, var_export( $setting_id, true ) ); $function = create_function( '$value', $body ); $option = $matches[1]; $hook = sprintf( 'option_%s', $option ); add_filter( $hook, $function ); self::$_prepreview_added_filters[] = compact( 'hook', 'function' ); $hook = sprintf( 'default_option_%s', $option ); add_filter( $hook, $function ); self::$_prepreview_added_filters[] = compact( 'hook', 'function' ); /** * Make sure the option is registered so that the update_option won't fail due to * the filters providing a default value, which causes the update_option() to get confused. */ add_option( $option, array() ); } } self::$_customized = $customized; } /** * Ensure that newly-added widgets will appear in the widgets_sidebars. * This is necessary because the customizer's setting preview filters are added after the widgets_init action, * which is too late for the widgets to be set up properly. * * @param array $sidebars_widgets * @return array */ static function prepreview_added_sidebars_widgets( $sidebars_widgets ) { foreach ( self::$_customized as $setting_id => $value ) { if ( preg_match( '/^sidebars_widgets\[(.+?)\]$/', $setting_id, $matches ) ) { $sidebar_id = $matches[1]; $sidebars_widgets[$sidebar_id] = $value; } } return $sidebars_widgets; } /** * Ensure that newly-added widgets will have empty instances so that they will be recognized. * This is necessary because the customizer's setting preview filters are added after the widgets_init action, * which is too late for the widgets to be set up properly. * * @param array $instance * @param string $setting_id * @return array */ static function prepreview_added_widget_instance( $instance, $setting_id ) { if ( isset( self::$_customized[$setting_id] ) ) { $parsed_setting_id = self::parse_widget_setting_id( $setting_id ); $widget_number = $parsed_setting_id['number']; // Single widget if ( is_null( $widget_number ) ) { if ( false === $instance && empty( $value ) ) { $instance = array(); } } // Multi widget else if ( false === $instance || ! isset( $instance[$widget_number] ) ) { if ( empty( $instance ) ) { $instance = array( '_multiwidget' => 1 ); } if ( ! isset( $instance[$widget_number] ) ) { $instance[$widget_number] = array(); } } } return $instance; } /** * Remove filters added in setup_widget_addition_previews() which ensure that * widgets are populating the options during widgets_init * * @action wp_loaded */ static function remove_prepreview_filters() { foreach ( self::$_prepreview_added_filters as $prepreview_added_filter ) { remove_filter( $prepreview_added_filter['hook'], $prepreview_added_filter['function'] ); } self::$_prepreview_added_filters = array(); } /** * Make sure that all widgets get loaded into customizer; these actions are also done in the wp_ajax_save_widget() * * @see wp_ajax_save_widget() * @action customize_controls_init */ static function customize_controls_init() { do_action( 'load-widgets.php' ); do_action( 'widgets.php' ); do_action( 'sidebar_admin_setup' ); } /** * When in preview, invoke customize_register for settings after WordPress is * loaded so that all filters have been initialized (e.g. Widget Visibility) */ static function schedule_customize_register( $wp_customize ) { if ( is_admin() ) { // @todo for some reason, $wp_customize->is_preview() is true here? self::customize_register( $wp_customize ); } else { add_action( 'wp', array( __CLASS__, 'customize_register' ) ); } } /** * Register customizer settings and controls for all sidebars and widgets * * @action customize_register */ static function customize_register( $wp_customize = null ) { global $wp_registered_widgets, $wp_registered_widget_controls; if ( ! ( $wp_customize instanceof WP_Customize_Manager ) ) { $wp_customize = $GLOBALS['wp_customize']; } $sidebars_widgets = array_merge( array( 'wp_inactive_widgets' => array() ), array_fill_keys( array_keys( $GLOBALS['wp_registered_sidebars'] ), array() ), wp_get_sidebars_widgets() ); $new_setting_ids = array(); /** * Register a setting for all widgets, including those which are active, inactive, and orphaned * since a widget may get suppressed from a sidebar via a plugin (like Widget Visibility). */ foreach ( array_keys( $wp_registered_widgets ) as $widget_id ) { $setting_id = self::get_setting_id( $widget_id ); $setting_args = self::get_setting_args( $setting_id ); $setting_args['sanitize_callback'] = array( __CLASS__, 'sanitize_widget_instance' ); $setting_args['sanitize_js_callback'] = array( __CLASS__, 'sanitize_widget_js_instance' ); $wp_customize->add_setting( $setting_id, $setting_args ); $new_setting_ids[] = $setting_id; } foreach ( $sidebars_widgets as $sidebar_id => $sidebar_widget_ids ) { if ( empty( $sidebar_widget_ids ) ) { $sidebar_widget_ids = array(); } $is_registered_sidebar = isset( $GLOBALS['wp_registered_sidebars'][$sidebar_id] ); $is_inactive_widgets = ( 'wp_inactive_widgets' === $sidebar_id ); $is_active_sidebar = ( $is_registered_sidebar && ! $is_inactive_widgets ); /** * Add setting for managing the sidebar's widgets */ if ( $is_registered_sidebar || $is_inactive_widgets ) { $setting_id = sprintf( 'sidebars_widgets[%s]', $sidebar_id ); $setting_args = self::get_setting_args( $setting_id ); $setting_args['sanitize_callback'] = array( __CLASS__, 'sanitize_sidebar_widgets' ); $setting_args['sanitize_js_callback'] = array( __CLASS__, 'sanitize_sidebar_widgets_js_instance' ); $wp_customize->add_setting( $setting_id, $setting_args ); $new_setting_ids[] = $setting_id; /** * Add section to contain controls */ $section_id = sprintf( 'sidebar-widgets-%s', $sidebar_id ); if ( $is_active_sidebar ) { $section_args = array( 'title' => sprintf( __( 'Widgets: %s' ), $GLOBALS['wp_registered_sidebars'][$sidebar_id]['name'] ), 'description' => $GLOBALS['wp_registered_sidebars'][$sidebar_id]['description'], ); $section_args = apply_filters( 'customizer_widgets_section_args', $section_args, $section_id, $sidebar_id ); $wp_customize->add_section( $section_id, $section_args ); $control = new WP_Widget_Area_Customize_Control( $wp_customize, $setting_id, array( 'section' => $section_id, 'sidebar_id' => $sidebar_id, //'priority' => 99, // so it appears at the end ) ); $new_setting_ids[] = $setting_id; $wp_customize->add_control( $control ); } } /** * Add a control for each active widget (located in a sidebar) */ 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] ) ) { continue; } $registered_widget = $GLOBALS['wp_registered_widgets'][$widget_id]; $setting_id = self::get_setting_id( $widget_id ); $id_base = $GLOBALS['wp_registered_widget_controls'][$widget_id]['id_base']; assert( false !== is_active_widget( $registered_widget['callback'], $registered_widget['id'], false, false ) ); $control = new WP_Widget_Form_Customize_Control( $wp_customize, $setting_id, array( 'label' => $registered_widget['name'], 'section' => $section_id, 'sidebar_id' => $sidebar_id, 'widget_id' => $widget_id, 'widget_id_base' => $id_base, 'priority' => $i, 'width' => $wp_registered_widget_controls[$widget_id]['width'], 'height' => $wp_registered_widget_controls[$widget_id]['height'], 'is_wide' => self::is_wide_widget( $widget_id ), ) ); $wp_customize->add_control( $control ); } } /** * We have to register these settings later than customize_preview_init so that other * filters have had a chance to run. * @see self::schedule_customize_register() */ if ( did_action( 'customize_preview_init' ) ) { foreach ( $new_setting_ids as $new_setting_id ) { $wp_customize->get_setting( $new_setting_id )->preview(); } } self::remove_prepreview_filters(); } /** * Covert a widget_id into its corresponding customizer setting id (option name) * * @param string $widget_id * @see _get_widget_id_base() * @return string */ static function get_setting_id( $widget_id ) { $parsed_widget_id = self::parse_widget_id( $widget_id ); $setting_id = sprintf( 'widget_%s', $parsed_widget_id['id_base'] ); if ( ! is_null( $parsed_widget_id['number'] ) ) { $setting_id .= sprintf( '[%d]', $parsed_widget_id['number'] ); } return $setting_id; } /** * Core widgets which may have controls wider than 250, but can still be * shown in the narrow customizer panel. The RSS and Text widgets in Core, * for example, have widths of 400 and yet they still render fine in the * customizer panel. This method will return all Core widgets as being * not wide, but this can be overridden with the is_wide_widget_in_customizer * filter. * * @param string $widget_id * @return bool */ static function is_wide_widget( $widget_id ) { global $wp_registered_widget_controls; $parsed_widget_id = self::parse_widget_id( $widget_id ); $width = $wp_registered_widget_controls[$widget_id]['width']; $is_core = in_array( $parsed_widget_id['id_base'], self::$core_widget_id_bases ); $is_wide = ( $width > 250 && ! $is_core ); $is_wide = apply_filters( 'is_wide_widget_in_customizer', $is_wide, $widget_id ); return $is_wide; } /** * Covert a widget ID into its id_base and number components * * @param string $widget_id * @return array */ static function parse_widget_id( $widget_id ) { $parsed = array( 'number' => null, 'id_base' => null, ); if ( preg_match( '/^(.+)-(\d+)$/', $widget_id, $matches ) ) { $parsed['id_base'] = $matches[1]; $parsed['number'] = intval( $matches[2] ); } else { // likely an old single widget $parsed['id_base'] = $widget_id; } return $parsed; } /** * Convert a widget setting ID (option path) to its id_base and number components * * @throws Widget_Customizer_Exception * @throws Exception * * @param string $setting_id * @param array * @return array */ static function parse_widget_setting_id( $setting_id ) { if ( ! preg_match( '/^(widget_(.+?))(?:\[(\d+)\])?$/', $setting_id, $matches ) ) { throw new Widget_Customizer_Exception( sprintf( 'Invalid widget setting ID: %s', $setting_id ) ); } $id_base = $matches[2]; $number = isset( $matches[3] ) ? intval( $matches[3] ) : null; return compact( 'id_base', 'number' ); } /** * Enqueue scripts and styles for customizer panel and export data to JS * * @action customize_controls_enqueue_scripts */ static function customize_controls_enqueue_deps() { wp_enqueue_script( 'jquery-ui-sortable' ); wp_enqueue_script( 'jquery-ui-droppable' ); wp_enqueue_style( 'widget-customizer', admin_url( 'css/customize-widgets.css' ) ); wp_enqueue_script( 'widget-customizer', admin_url( 'js/customize-widgets.js' ), array( 'jquery', 'wp-backbone', 'wp-util', 'customize-controls' ) ); // Export available widgets with control_tpl removed from model // since plugins need templates to be in the DOM $available_widgets = array(); foreach ( self::get_available_widgets() as $available_widget ) { unset( $available_widget['control_tpl'] ); $available_widgets[] = $available_widget; } $widget_reorder_nav_tpl = sprintf( '
', esc_attr__( 'Move to another area...' ), esc_html__( 'Move to another area...' ), esc_attr__( 'Move down' ), esc_html__( 'Move down' ), esc_attr__( 'Move up' ), esc_html__( 'Move up' ) ); $move_widget_area_tpl = str_replace( array( '{description}', '{btn}' ), array( esc_html__( 'Select an area to move this widget into:' ), esc_html__( 'Move' ), ), ' ' ); // Why not wp_localize_script? Because we're not localizing, and it forces values into strings global $wp_scripts; $exports = array( 'update_widget_ajax_action' => self::UPDATE_WIDGET_AJAX_ACTION, 'update_widget_nonce_value' => wp_create_nonce( self::UPDATE_WIDGET_AJAX_ACTION ), 'update_widget_nonce_post_key' => self::UPDATE_WIDGET_NONCE_POST_KEY, 'registered_sidebars' => array_values( $GLOBALS['wp_registered_sidebars'] ), 'registered_widgets' => $GLOBALS['wp_registered_widgets'], 'available_widgets' => $available_widgets, // @todo Merge this with registered_widgets 'i18n' => array( 'save_btn_label' => _x( 'Apply', 'button to save changes to a widget' ), 'save_btn_tooltip' => _x( 'Save and preview changes before publishing them.', 'tooltip on the widget save button' ), 'remove_btn_label' => _x( 'Remove', 'link to move a widget to the inactive widgets sidebar' ), 'remove_btn_tooltip' => _x( 'Trash widget by moving it to the inactive widgets sidebar.', 'tooltip on btn a widget to move it to the inactive widgets sidebar' ), ), 'tpl' => array( 'widget_reorder_nav' => $widget_reorder_nav_tpl, 'move_widget_area' => $move_widget_area_tpl, ), ); foreach ( $exports['registered_widgets'] as &$registered_widget ) { unset( $registered_widget['callback'] ); // may not be JSON-serializeable } $wp_scripts->add_data( 'widget-customizer', 'data', sprintf( 'var WidgetCustomizer_exports = %s;', json_encode( $exports ) ) ); } /** * Render the widget form control templates into the DOM so that plugin scripts can manipulate them * * @action customize_controls_print_footer_scripts */ static function output_widget_control_templates() { ?>