/** * Export data from PHP to JS. * * @since 4.3.0 * @access public */ public function export_preview_data() { // Why not wp_localize_script? Because we're not localizing, and it forces values into strings. $exports = array('renderQueryVar' => self::RENDER_QUERY_VAR, 'renderNonceValue' => wp_create_nonce(self::RENDER_AJAX_ACTION), 'renderNoncePostKey' => self::RENDER_NONCE_POST_KEY, 'requestUri' => '/', 'theme' => array('stylesheet' => $this->manager->get_stylesheet(), 'active' => $this->manager->is_theme_active()), 'previewCustomizeNonce' => wp_create_nonce('preview-customize_' . $this->manager->get_stylesheet()), 'navMenuInstanceArgs' => $this->preview_nav_menu_instance_args); if (!empty($_SERVER['REQUEST_URI'])) { $exports['requestUri'] = esc_url_raw(home_url(wp_unslash($_SERVER['REQUEST_URI']))); } printf('<script>var _wpCustomizePreviewNavMenusExports = %s;</script>', wp_json_encode($exports)); }
/** * Register Customizer settings and controls for all sidebars and 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($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 = $this->get_setting_id($widget_id); $setting_args = $this->get_setting_args($setting_id); if (!$this->manager->get_setting($setting_id)) { $this->manager->add_setting($setting_id, $setting_args); } $new_setting_ids[] = $setting_id; } /* * Add a setting which will be supplied for the theme's sidebars_widgets * theme_mod when the the theme is switched. */ if (!$this->manager->is_theme_active()) { $setting_id = 'old_sidebars_widgets_data'; $setting_args = $this->get_setting_args($setting_id, array('type' => 'global_variable', 'dirty' => true)); $this->manager->add_setting($setting_id, $setting_args); } $this->manager->add_panel('widgets', array('type' => 'widgets', 'title' => __('Widgets'), 'description' => __('Widgets are independent sections of content that can be placed into widgetized areas provided by your theme (commonly called sidebars).'), 'priority' => 110, 'active_callback' => array($this, 'is_panel_active'))); foreach ($sidebars_widgets as $sidebar_id => $sidebar_widget_ids) { if (empty($sidebar_widget_ids)) { $sidebar_widget_ids = array(); } $is_registered_sidebar = is_registered_sidebar($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 = $this->get_setting_args($setting_id); if (!$this->manager->get_setting($setting_id)) { if (!$this->manager->is_theme_active()) { $setting_args['dirty'] = true; } $this->manager->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' => $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); /** * Filter Customizer widget section arguments for a given sidebar. * * @since 3.9.0 * * @param array $section_args Array of Customizer widget section arguments. * @param string $section_id Customizer section ID. * @param int|string $sidebar_id Sidebar ID. */ $section_args = apply_filters('customizer_widgets_section_args', $section_args, $section_id, $sidebar_id); $section = new WP_Customize_Sidebar_Section($this->manager, $section_id, $section_args); $this->manager->add_section($section); $control = new WP_Widget_Area_Customize_Control($this->manager, $setting_id, array('section' => $section_id, 'sidebar_id' => $sidebar_id, 'priority' => count($sidebar_widget_ids))); $new_setting_ids[] = $setting_id; $this->manager->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($wp_registered_widgets[$widget_id])) { continue; } $registered_widget = $wp_registered_widgets[$widget_id]; $setting_id = $this->get_setting_id($widget_id); $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'], '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' => $this->is_wide_widget($widget_id))); $this->manager->add_control($control); } } if (!$this->manager->doing_ajax('customize_save')) { foreach ($new_setting_ids as $new_setting_id) { $this->manager->get_setting($new_setting_id)->preview(); } } add_filter('sidebars_widgets', array($this, 'preview_sidebars_widgets'), 1); }
/** * Test WP_Customize_Manager::unsanitized_post_values(). * * @ticket 30937 * @covers WP_Customize_Manager::unsanitized_post_values() */ function test_unsanitized_post_values_with_changeset_and_stashed_theme_mods() { wp_set_current_user(self::$admin_user_id); $preview_theme = $this->get_inactive_core_theme(); $stashed_theme_mods = array($preview_theme => array('background_color' => array('value' => '#000000'))); $stashed_theme_mods[get_stylesheet()] = array('background_color' => array('value' => '#FFFFFF')); update_option('customize_stashed_theme_mods', $stashed_theme_mods); $post_values = array('blogdescription' => 'Post Input Tagline'); $_POST['customized'] = wp_slash(wp_json_encode($post_values)); $uuid = wp_generate_uuid4(); $changeset_data = array('blogname' => array('value' => 'Changeset Title'), 'blogdescription' => array('value' => 'Changeset Tagline')); $this->factory()->post->create(array('post_type' => 'customize_changeset', 'post_status' => 'auto-draft', 'post_name' => $uuid, 'post_content' => wp_json_encode($changeset_data))); $manager = new WP_Customize_Manager(array('changeset_uuid' => $uuid)); $this->assertTrue($manager->is_theme_active()); $this->assertArrayNotHasKey('background_color', $manager->unsanitized_post_values()); $this->assertEquals(array('blogname' => 'Changeset Title', 'blogdescription' => 'Post Input Tagline'), $manager->unsanitized_post_values()); $this->assertEquals(array('blogdescription' => 'Post Input Tagline'), $manager->unsanitized_post_values(array('exclude_changeset' => true))); $manager->set_post_value('blogdescription', 'Post Override Tagline'); $this->assertEquals(array('blogname' => 'Changeset Title', 'blogdescription' => 'Post Override Tagline'), $manager->unsanitized_post_values()); $this->assertEquals(array('blogname' => 'Changeset Title', 'blogdescription' => 'Changeset Tagline'), $manager->unsanitized_post_values(array('exclude_post_data' => true))); $this->assertEmpty($manager->unsanitized_post_values(array('exclude_post_data' => true, 'exclude_changeset' => true))); // Test unstashing theme mods. $manager = new WP_Customize_Manager(array('changeset_uuid' => $uuid, 'theme' => $preview_theme)); $this->assertFalse($manager->is_theme_active()); $values = $manager->unsanitized_post_values(array('exclude_post_data' => true, 'exclude_changeset' => true)); $this->assertNotEmpty($values); $this->assertArrayHasKey('background_color', $values); $this->assertEquals('#000000', $values['background_color']); $values = $manager->unsanitized_post_values(array('exclude_post_data' => false, 'exclude_changeset' => false)); $this->assertArrayHasKey('background_color', $values); $this->assertArrayHasKey('blogname', $values); $this->assertArrayHasKey('blogdescription', $values); }
/** * Register customizer settings and controls for all sidebars and widgets. * * @since 3.9.0 * @access public */ 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()), 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 = $this->get_setting_id($widget_id); $setting_args = $this->get_setting_args($setting_id); $setting_args['sanitize_callback'] = array($this, 'sanitize_widget_instance'); $setting_args['sanitize_js_callback'] = array($this, 'sanitize_widget_js_instance'); $this->manager->add_setting($setting_id, $setting_args); $new_setting_ids[] = $setting_id; } /* * Add a setting which will be supplied for the theme's sidebars_widgets * theme_mod when the the theme is switched. */ if (!$this->manager->is_theme_active()) { $setting_id = 'old_sidebars_widgets_data'; $setting_args = $this->get_setting_args($setting_id, array('type' => 'global_variable')); $this->manager->add_setting($setting_id, $setting_args); } 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 = $this->get_setting_args($setting_id); $setting_args['sanitize_callback'] = array($this, 'sanitize_sidebar_widgets'); $setting_args['sanitize_js_callback'] = array($this, 'sanitize_sidebar_widgets_js_instance'); $this->manager->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'], 'priority' => 1000 + array_search($sidebar_id, array_keys($wp_registered_sidebars))); /** * Filter Customizer widget section arguments for a given sidebar. * * @since 3.9.0 * * @param array $section_args Array of Customizer widget section arguments. * @param string $section_id Customizer section ID. * @param int|string $sidebar_id Sidebar ID. */ $section_args = apply_filters('customizer_widgets_section_args', $section_args, $section_id, $sidebar_id); $this->manager->add_section($section_id, $section_args); $control = new WP_Widget_Area_Customize_Control($this->manager, $setting_id, array('section' => $section_id, 'sidebar_id' => $sidebar_id, 'priority' => count($sidebar_widget_ids))); $new_setting_ids[] = $setting_id; $this->manager->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 = $this->get_setting_id($widget_id); $id_base = $GLOBALS['wp_registered_widget_controls'][$widget_id]['id_base']; $control = new WP_Widget_Form_Customize_Control($this->manager, $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' => $this->is_wide_widget($widget_id))); $this->manager->add_control($control); } } /* * We have to register these settings later than customize_preview_init * so that other filters have had a chance to run. */ if (did_action('customize_preview_init')) { foreach ($new_setting_ids as $new_setting_id) { $this->manager->get_setting($new_setting_id)->preview(); } } $this->remove_prepreview_filters(); }