public static function get_customizer_sidebar_args($previewer = false) { // Bail if lower than WordPress 4.1 if (Note::wp_version_compare('4.1', '<')) { return array(); } global $post, $wp_customize; // Grab the Note Sidebars instance $note_sidebars = Note_Sidebars(); // Grab the sidebars widget option $sidebars_widgets = get_option('sidebars_widgets'); // Bail if in the Customizer isn't ready if (!is_a($wp_customize, 'WP_Customize_Manager')) { return $note_sidebars->sidebar_args; } // Setup post ID, section prefix, and widgets panel flag $post_id = !empty($post) && is_a($post, 'WP_Post') ? $post->ID : false; $post_id = apply_filters('note_customizer_sidebar_args_post_id', $post_id, $previewer, $note_sidebars); $section_prefix = 'sidebar-widgets-'; $remove_widgets_panel = false; // If the widgets panel doesn't exist yet, create a mock one now with a title for the json() methods used below if (!$wp_customize->get_panel('widgets')) { // Widgets Panel $wp_customize->add_panel('widgets', array('type' => 'widgets', 'title' => __('Widgets', 'note'))); // Set the flag $remove_widgets_panel = true; } // Format sidebar locations for localizations foreach ($note_sidebars->sidebar_locations as $sidebar_location) { // Loop through each sidebar within this location foreach ($sidebar_location as $sidebar_id) { // Note Sidebar arguments for this sidebar $sidebar_args = self::note_sidebar_args($sidebar_id, $post_id); // Get the sidebar ID $customizer_sidebar_id = self::get_sidebar_arg('id', $sidebar_args); // Generate a setting ID $setting_id = 'sidebars_widgets[' . $customizer_sidebar_id . ']'; // Create a mock Customizer Setting $wp_customize->add_setting($setting_id, $wp_customize->widgets->get_setting_args($setting_id)); // Generate a section ID $section_id = $section_prefix . $customizer_sidebar_id; // Create a mock Customizer Section $customizer_section = new Note_Customizer_Sidebar_Section($wp_customize, $section_id, array('id' => $section_id, 'title' => self::get_sidebar_arg('name', $sidebar_args), 'description' => self::get_sidebar_arg('description', $sidebar_args), 'sidebar_id' => $customizer_sidebar_id, 'panel' => 'widgets')); $wp_customize->add_section($customizer_section); // Create a mock Customizer Control $customizer_control = new Note_Customizer_Sidebar_Control($wp_customize, $setting_id, array('description' => self::get_sidebar_arg('description', $sidebar_args), 'section' => $section_id, 'sidebar_id' => $customizer_sidebar_id, 'priority' => 0)); $wp_customize->add_control($customizer_control); // Customizer data $sidebar_args['customizer'] = array('setting' => array('id' => $setting_id, 'transport' => 'refresh', 'value' => isset($sidebars_widgets[$customizer_sidebar_id]) ? $sidebars_widgets[$customizer_sidebar_id] : array()), 'section' => $customizer_section && method_exists($customizer_section, 'json') ? $customizer_section->json() : array(), 'control' => $customizer_control && method_exists($customizer_control, 'json') ? $customizer_control->json() : array()); /* * Adjust section data */ $sidebar_args['customizer']['section']['active'] = true; // Activate this section within the Customizer when it is created unset($sidebar_args['customizer']['section']['instanceNumber']); // Remove instance number /* * Adjust control data */ $sidebar_args['customizer']['control']['id'] = $setting_id; // ID $sidebar_args['customizer']['control']['active'] = true; // Activate this control within the Customizer when it is created $sidebar_args['customizer']['control']['priority'] = 0; // No active widgets unset($sidebar_args['customizer']['control']['instanceNumber']); // Remove instance number // If we're not in the Previewer, remove the mock Settings, Sections, and Controls active if (!$previewer) { // Remove Customizer mock setting, section, and control $wp_customize->remove_setting($setting_id); $wp_customize->remove_section($section_id); $wp_customize->remove_control($setting_id); } // Store a reference to the sidebar arguments $note_sidebars->sidebar_args[$sidebar_id] = $sidebar_args; } } // Remove the "mock" Widgets panel (it will be added by WordPress core on the "wp" action) if ($remove_widgets_panel) { $wp_customize->remove_panel('widgets'); } return $note_sidebars->sidebar_args; }
/** * This function runs after the WP and WP_Query objects are set up. */ function wp() { // Bail if lower than WordPress 4.1 if (Note::wp_version_compare('4.1', '<')) { return; } // Note Sidebars (single content types only or if filter returns true) if (is_singular() || apply_filters('note_customizer_localize_sidebar_args', false, $this)) { // Grab Note Sidebar Customizer arguments (keep Customizer Sections/Controls active for Previewer) $this->note_sidebar_args = Note_Sidebars::get_customizer_sidebar_args(true); // Note Sidebar args if (!isset($this->note_localize['sidebars'])) { $this->note_localize['sidebars'] = array(); } $this->note_localize['sidebars']['args'] = apply_filters('note_localize_sidebar_args', $this->note_sidebar_args, $this); /* * Inactive Widgets * * WordPress does not create controls for inactive widgets, but we need those controls * because sidebars can be removed and added/re-added dynamically. Only do this in the * Customizer and only do this for Note Sidebars. */ $this->register_inactive_note_widgets(true); } }
/** * This function determines we're currently being previewed in the Customizer. */ public function is_customize_preview() { $is_gte_wp_4 = Note::wp_version_compare('4.0'); // Less than 4.0 if (!$is_gte_wp_4) { global $wp_customize; return is_a($wp_customize, 'WP_Customize_Manager') && $wp_customize->is_preview(); } else { return is_customize_preview(); } }