function note_register_sidebar_location($args) { // Parse arguments $defaults = array('location_id' => '', 'location' => '', 'sidebar_id' => '', 'in_the_loop' => true); $args = wp_parse_args($args, $defaults); // Grab the Note Sidebars instance $note_sidebars = Note_Sidebars(); // Populate sidebar arguments $sidebar_args = array($args['location'] => $args['sidebar_id']); // Determine if this Note Sidebar location already exists if (array_key_exists($args['location_id'], $note_sidebars->registered_sidebar_locations)) { // Store a reference to the sidebar location $sidebar_location = $note_sidebars->registered_sidebar_locations[$args['location_id']]; // Merge the data $note_sidebars->registered_sidebar_locations[$args['location_id']] = array_merge($sidebar_location, $sidebar_args); } else { // Add the data $note_sidebars->registered_sidebar_locations[$args['location_id']] = $sidebar_args; // Add the filter tag data $note_sidebars->registered_sidebar_location_filters[$args['location_id']] = array('filter' => $args['location_id'], 'in_the_loop' => $args['in_the_loop']); } }
/** * This function determines whether the current sidebar is rendered on the front-end. */ public function active_callback() { // Grab the Note Sidebars instance $note_sidebars = Note_Sidebars(); // If we have a valid array of sidebars if (is_array($note_sidebars->sidebars)) { // Loop through posts foreach ($note_sidebars->sidebars as $post_id => $note_sidebar_ids) { // Loop through Note Sidebar IDs foreach ($note_sidebar_ids as $sidebar_id) { // Note Sidebar arguments for this sidebar $sidebar_args = Note_Sidebars::note_sidebar_args($sidebar_id, $post_id, false); // Generate a section ID $section_id = $this->section_prefix . Note_Sidebars::get_sidebar_arg('id', $sidebar_args); // If this sidebar ID matches the section ID, it is rendered on the front-end if ($this->id === $section_id) { return true; } } } } // Otherwise, the sidebar is not rendered on the front-end return false; }
public function sanitize_option($input) { // TODO Reset to defaults? //if ( isset( $input['reset'] ) ) // return Note_Options::get_option_defaults(); // Store the raw input values from the user which will be used in certain validation checks $raw_input = $input; // Parse arguments, replacing defaults with user input $input = wp_parse_args($input, Note_Options::get_option_defaults()); // Note Sidebars if (!empty($input['sidebars']) && is_array($input['sidebars'])) { $note_sidebars = Note_Sidebars(); // Grab the Note Sidebars instance // Sanitize sidebars $input['sidebars'] = $note_sidebars->sanitize_callback($input['sidebars']); } else { // Grab current version of Note Options $note_options = Note_Options::get_options(); // Previously sanitized by the Customizer logic (@see Note_Sidebars::sanitize_callback()) $input['sidebars'] = $note_options['sidebars']; } // Note Uninstall $input['uninstall']['data'] = isset($raw_input['uninstall']['data']) && $input['uninstall']['data'] ? true : false; // Remove Note data on uninstall (checking isset() here due to the nested arrays) return $input; }
/** * This function registers sections and settings for use in the Customizer. */ public function customize_register($wp_customize) { // Bail if lower than WordPress 4.1 if (Note::wp_version_compare('4.1', '<')) { return; } $note_option_defaults = Note_Options::get_option_defaults(); $note_sidebars = Note_Sidebars(); // Grab the Note Sidebars instance /** * Note */ /* * Note Sidebars */ // Setting (data is sanitized upon update_option() call using the sanitize function in Note_Admin_Options) $wp_customize->add_setting(new WP_Customize_Setting($wp_customize, 'note[sidebars]', array('default' => $note_option_defaults['sidebars'], 'type' => 'option', 'sanitize_callback' => array($note_sidebars, 'sanitize_callback'), 'sanitize_js_callback' => array($note_sidebars, 'sanitize_js_callback')))); // Section $wp_customize->add_section(new WP_Customize_Section($wp_customize, 'note_sidebars', array('title' => __('Note Sidebars', 'note'), 'priority' => 999))); // Control $wp_customize->add_control(new WP_Customize_Control($wp_customize, 'note_sidebars', array('label' => __('Note Sidebars', 'note'), 'section' => 'note_sidebars', 'settings' => 'note[sidebars]', 'input_attrs' => array('class' => 'note-sidebars note-hidden'), 'active_callback' => '__return_false'))); /* * Note Temporary Sidebar */ // Setting $wp_customize->add_setting(new WP_Customize_Setting($wp_customize, 'sidebars_widgets[note-temporary-inactive-sidebar]', array('default' => array(), 'type' => 'option', 'sanitize_callback' => array($wp_customize->widgets, 'sanitize_sidebar_widgets'), 'sanitize_js_callback' => array($wp_customize->widgets, 'sanitize_sidebar_widgets_js_instance')))); // Section $wp_customize->add_section(new WP_Customize_Sidebar_Section($wp_customize, 'sidebar-widgets-note-temporary-inactive-sidebar', array('title' => __('Note Temporary Inactive Sidebar', 'note'), 'description' => __('This is a temporary sidebar registered by Note in the Customizer only. It will hold inactive Note Sidebar widgets during a session', 'note'), 'priority' => 999, 'panel' => 'widgets', 'sidebar_id' => 'note-temporary-inactive-sidebar'))); // Control $wp_customize->add_control(new WP_Widget_Area_Customize_Control($wp_customize, 'sidebars_widgets[note-temporary-inactive-sidebar]', array('section' => 'sidebar-widgets-note-temporary-inactive-sidebar', 'sidebar_id' => 'note-temporary-inactive-sidebar', 'priority' => 999, 'active_callback' => '__return_false'))); /* * Inactive Widgets * * WordPress does not create controls for inactive widgets, but we need those controls * because sidebars can be removed and added dynamically. Only do this in the Customizer * and only do this for Note Sidebars. * * The Previewer controls are added in Note_Customizer::wp() after the core filters have been run. */ // Admin if (is_admin()) { $this->register_inactive_note_widgets(); } }
/** * This function outputs a Note Sidebar based on parameters. It will fallback to the placeholder if the current * page is being output in the Previewer. Otherwise it will return an empty string. */ public static function sidebar($sidebar_id, $post_id) { // Reference to sidebar HTML $sidebar_html = ''; // Grab the Note Sidebars instance $note_sidebars = Note_Sidebars(); $registered_sidebars = isset($note_sidebars->sidebars[$post_id]) ? (array) $note_sidebars->sidebars[$post_id] : array(); $note_sidebar_id = self::get_sidebar_id($sidebar_id, $post_id); // If the sidebar ID is in the array of registered sidebars for this post, it is already registered on Note_Sidebars::widgets_init() if (in_array($sidebar_id, $registered_sidebars)) { // If the sidebar is active if (is_active_sidebar($note_sidebar_id)) { ob_start(); // Note Sidebar arguments $sidebar_args = $note_sidebars->sidebar_args[$sidebar_id]; // CSS Classes $css_classes = array('note-sidebar', 'note-sidebar-active', 'note-cf', sprintf('note-%1$s-sidebar', $sidebar_id)); // Sanitize CSS classes $css_classes = array_filter($css_classes, 'sanitize_html_class'); ?> <div id="<?php echo esc_attr(self::get_sidebar_arg('id', $sidebar_args)); ?> " class="<?php echo esc_attr(implode(' ', $css_classes)); ?> " data-post-id="<?php echo esc_attr($post_id); ?> " data-note-sidebar-id="<?php echo esc_attr($sidebar_id); ?> " data-note-sidebar="true"> <?php echo is_customize_preview() && !is_admin() ? Note_Customizer::note_sidebar_ui_buttons() : false; ?> <?php // Remove Note UI filters // TODO: Remove? //remove_filter( 'dynamic_sidebar_before', array( $note_sidebars, 'dynamic_sidebar_before' ) ); //remove_filter( 'dynamic_sidebar_after', array( $note_sidebars, 'dynamic_sidebar_after' ) ); ?> <?php dynamic_sidebar($note_sidebar_id); ?> <?php // Re-add Note UI filters // TODO: Remove? //add_filter( 'dynamic_sidebar_before', array( $note_sidebars, 'dynamic_sidebar_before' ), 10, 2 ); //add_filter( 'dynamic_sidebar_after', array( $note_sidebars, 'dynamic_sidebar_after' ), 10 ,2 ); ?> </div> <?php $sidebar_html = ob_get_clean(); } else { if (is_customize_preview() && !is_admin()) { // Note Sidebar Placeholder (inactive sidebar) $sidebar_html = Note_Customizer::note_sidebar_placeholder($sidebar_id, $post_id, true); } } } else { if (is_customize_preview() && !is_admin()) { // Note Sidebar Placeholder (no registered sidebar) $sidebar_html = Note_Customizer::note_sidebar_placeholder($sidebar_id, $post_id); } } return $sidebar_html; }