示例#1
0
/**
 * Gather all assignments for posts into a single
 * array organized by post ID.
 *
 * @since 2.0.0
 *
 * @param $posts array all posts from WP's get_posts()
 * @return $assignments array assignments from all posts organized by ID
 */
function themeblvd_get_assignment_conflicts($posts)
{
    // Setup $conflicts/$non_conflicts arrays
    $non_conflicts = array();
    $conflicts = array();
    $locations = themeblvd_get_sidebar_locations();
    foreach ($locations as $location) {
        $conflicts[$location['location']['id']] = array();
        $non_conflicts[$location['location']['id']] = array();
    }
    // Loop through sidebar posts to construct two arrays side-by-side.
    // As we build the $non_conflicts arrays, we will be able to build
    // the $conflicts arrays off to the side by checking if items already
    // exist in the $non_conflicts.
    foreach ($posts as $post) {
        // Determine location sidebar is assigned to.
        $location = get_post_meta($post->ID, 'location', true);
        // Only run check if a location exists and this
        // is not a floating widget area.
        if ($location && $location != 'floating') {
            $assignments = get_post_meta($post->ID, 'assignments', true);
            if (is_array($assignments) && !empty($assignments)) {
                foreach ($assignments as $key => $assignment) {
                    if ($key != 'custom' && in_array($key, $non_conflicts[$location])) {
                        if (!in_array($key, $conflicts[$location])) {
                            $conflicts[$location][] = $key;
                        }
                    } else {
                        $non_conflicts[$location][] = $key;
                    }
                }
            }
        }
    }
    return $conflicts;
}
 /**
  * Generates interface to manage sidebar overrides
  * in meta box.
  *
  * @since 1.1.0
  *
  * @param array $settings Optional current selections for generated form
  */
 public function sidebar_overrides($settings = null)
 {
     global $post;
     // If the page is loading, we're going to be pulling
     // $settings from the meta data, but if this is being
     // sent from Ajax, we're most likely passing the
     // $settings in and we can skip this.
     if (!$settings) {
         $settings = get_post_meta($post->ID, '_tb_sidebars', true);
     }
     // For the meta box, if you want to show ALL widget
     // area locations, you'd change this to false. Most
     // people just want to use the fixed sidebars; so we
     // can save some clutter for the average person by
     // having this set to true.
     $fixed_only = apply_filters('themeblvd_sidebar_overrides_fixed_only', true);
     // Construct <select> of ALL custom widget areas. --
     // Because this is our override meta box, we don't care
     // about the "location" and the user can just override
     // with whatever custom widget area they want.
     $custom_sidebars = get_posts('post_type=tb_sidebar&numberposts=-1');
     $sidebars_select = array('default' => ' &#8211; ' . __('No Override', 'themeblvd_sidebars') . ' &#8211; ');
     foreach ($custom_sidebars as $sidebar) {
         $sidebars_select[$sidebar->post_name] = $sidebar->post_title;
     }
     // Setup options for sidebar locations
     $options = array(array('type' => 'info', 'desc' => __('Here you can select any custom widget areas you\'d like applied to the sidebars of this specific page. When utilizing this feature, current locations and assignments of your custom widget areas setup under <a href="themes.php?page=themeblvd_widget_areas">Appearance > Widget Areas</a> will be ignored.', 'themeblvd_sidebars'), 'class' => 'section-description'));
     $locations = themeblvd_get_sidebar_locations();
     foreach ($locations as $location) {
         // If we're only doing fixed sidebars and this
         // isn't a fixed sidebar, move onto the next location.
         if ($fixed_only && $location['type'] != 'fixed') {
             continue;
         }
         // Add option for this location
         $options[] = array('name' => $location['location']['name'], 'desc' => sprintf(__('Select from any of your custom widget areas to override the %s location on this page only.', 'themeblvd_sidebars'), $location['location']['name']), 'id' => $location['location']['id'], 'type' => 'select', 'options' => $sidebars_select);
     }
     $options = apply_filters('themeblvd_sidebar_overrides', $options);
     // Build form
     $form = themeblvd_option_fields('_tb_sidebars', $options, $settings, false);
     // And spit it out
     $nonce = wp_create_nonce('themeblvd_sidebar_overrides');
     echo '<input type="hidden" name="_tb_sidebar_overrides_nonce" value="' . $nonce . '" />';
     echo $form[0];
 }
 /**
  * Save sidebar
  *
  * @since 1.0.0
  */
 public function save_sidebar()
 {
     // Make sure Satan isn't lurking
     check_ajax_referer('themeblvd_save_sidebar', 'security');
     // Handle form data
     parse_str($_POST['data'], $config);
     // Sidebar info
     $post_id = $config['sidebar_id'];
     $sidebar = get_post($post_id);
     $post_slug = $sidebar->post_name;
     // Setup location
     $location = null;
     if (isset($config['options']['sidebar_location'])) {
         // Sanitize location
         if ($config['options']['sidebar_location'] == 'floating') {
             $location = $config['options']['sidebar_location'];
         } else {
             $exists = false;
             $framework_sidebars = themeblvd_get_sidebar_locations();
             foreach ($framework_sidebars as $framework_sidebar) {
                 if ($framework_sidebar['location']['id'] == $config['options']['sidebar_location']) {
                     $exists = true;
                 }
             }
             if ($exists) {
                 $location = $config['options']['sidebar_location'];
             }
         }
     }
     // Setup assignments
     $assignments = array();
     $name = null;
     if (isset($config['options']['sidebar_assignments']) && has_filter('themeblvd_sanitize_conditionals')) {
         $assignments = apply_filters('themeblvd_sanitize_conditionals', $config['options']['sidebar_assignments'], $post_slug, $post_id);
     }
     // Update even if they're empty
     update_post_meta($post_id, 'location', $location);
     update_post_meta($post_id, 'assignments', $assignments);
     // Widget Area Information
     if (isset($config['options']['post_title']) && isset($config['options']['post_name'])) {
         // Start post data to be updated with the ID
         $post_atts = array('ID' => $post_id, 'post_title' => $config['options']['post_title'], 'post_name' => $config['options']['post_name']);
         // Update Post info
         wp_update_post($post_atts);
     }
     // Get most recent layout id after doing the above processes
     $updated_sidebar = get_post($post_id);
     $current_sidebar_id = $updated_sidebar->post_name;
     // Send current layout ID back with response
     echo $current_sidebar_id . '[(=>)]';
     // Respond with update message and management table
     echo '<div id="setting-error-save_options" class="updated fade settings-error ajax-update">';
     echo '	<p><strong>' . __('Widget Area saved.', 'themeblvd') . '</strong></p>';
     echo '</div>';
     echo '[(=>)]';
     $this->admin_page->manage_sidebars();
     die;
 }
 /**
  * Set primary configuration array.
  *
  * @since 2.3.0
  */
 public function set_config()
 {
     global $post;
     $this->config = array('id' => 0, 'builder' => false, 'builder_post_id' => 0, 'sidebar_layout' => '', 'featured' => array(), 'featured_below' => array(), 'sidebars' => array());
     /*------------------------------------------------------*/
     /* Primary Post ID
     		/*------------------------------------------------------*/
     // Store the ID of the original $post object in case
     // we modify the main query or need to ever access it.
     if (is_object($post)) {
         $this->config['id'] = $post->ID;
     }
     /*------------------------------------------------------*/
     /* Custom Layout, Builder Name/ID
     		/*------------------------------------------------------*/
     if (defined('TB_BUILDER_PLUGIN_VERSION')) {
         $layout_name = '';
         // Custom Layout on static page
         if (is_page_template('template_builder.php')) {
             if (post_password_required() || 'private' == get_post_status() && !current_user_can('edit_posts')) {
                 // Password is currently required and so
                 // the custom layout doesn't get used.
                 $layout_name = 'wp-private';
             } else {
                 $layout_name = get_post_meta($this->config['id'], '_tb_custom_layout', true);
                 if (!$layout_name) {
                     $layout_name = 'error';
                 }
             }
         }
         // Custom Layout over home "posts page"
         if (is_home() && get_option('show_on_front') == 'posts') {
             if ('custom_layout' == themeblvd_get_option('homepage_content')) {
                 $layout_name = themeblvd_get_option('homepage_custom_layout');
                 if (!$layout_name) {
                     $layout_name = 'error';
                 }
             }
         }
         // Set name, which can also be "error" or "wp-private"
         $this->config['builder'] = $layout_name;
         // If we have a layout name, setup it's ID and sidebar layout
         if ($layout_name && $layout_name != 'error' && $layout_name != 'wp-private') {
             // Set ID
             $this->config['builder_post_id'] = themeblvd_post_id_by_name($layout_name, 'tb_layout');
             // Sidebar layout
             $layout_settings = get_post_meta($this->config['builder_post_id'], 'settings', true);
             $this->config['sidebar_layout'] = $layout_settings['sidebar_layout'];
         }
     }
     /*------------------------------------------------------*/
     /* Featured Area
     		/*------------------------------------------------------*/
     // For the featured area above or below the content to be
     // enabled, it must contain at least one CSS class or else
     // it won't be displayed for the current page.
     if ($this->config['builder'] && $this->config['builder'] != 'error' && $this->config['builder'] != 'wp-private') {
         $elements = get_post_meta($this->config['builder_post_id'], 'elements', true);
         $this->config['featured'] = $this->featured_builder_classes($elements, 'featured');
         $this->config['featured_below'] = $this->featured_builder_classes($elements, 'featured_below');
     }
     if (is_home()) {
         if ('custom_layout' != themeblvd_get_option('homepage_content')) {
             if (themeblvd_get_option('blog_featured') || themeblvd_supports('featured', 'blog')) {
                 $this->config['featured'][] = 'has_blog_featured';
             }
             if (themeblvd_supports('featured_below', 'blog')) {
                 $this->config['featured_below'][] = 'has_blog_featured_below';
             }
         }
     }
     if (is_page_template('template_list.php')) {
         if (themeblvd_get_option('blog_featured') || themeblvd_supports('featured', 'blog')) {
             $this->config['featured'][] = 'has_blog_featured';
         }
         if (themeblvd_supports('featured_below', 'blog')) {
             $this->config['featured_below'][] = 'has_blog_featured_below';
         }
     }
     if (is_page_template('template_grid.php')) {
         if (themeblvd_supports('featured', 'grid')) {
             $this->config['featured'][] = 'has_grid_featured';
         }
         if (themeblvd_supports('featured_below', 'grid')) {
             $this->config['featured_below'][] = 'has_grid_featured_below';
         }
     }
     if (is_archive() || is_search()) {
         if (themeblvd_supports('featured', 'archive')) {
             $this->config['featured'][] = 'has_archive_featured';
         }
         if (themeblvd_supports('featured_below', 'archive')) {
             $this->config['featured_below'][] = 'has_archive_featured_below';
         }
     }
     if (is_page() && !is_page_template('template_builder.php')) {
         if (themeblvd_supports('featured', 'page')) {
             $this->config['featured'][] = 'has_page_featured';
         }
         if (themeblvd_supports('featured_below', 'page')) {
             $this->config['featured_below'][] = 'has_page_featured_below';
         }
     }
     if (is_single()) {
         if (themeblvd_supports('featured', 'single')) {
             $this->config['featured'][] = 'has_single_featured';
         }
         if (themeblvd_supports('featured_below', 'single')) {
             $this->config['featured_below'][] = 'has_single_featured_below';
         }
     }
     /*------------------------------------------------------*/
     /* Sidebar Layout
     		/*------------------------------------------------------*/
     // The sidebar layout is how the left and right sidebar will
     // be displayed on the current page.
     if (!$this->config['sidebar_layout'] && (is_page() || is_single())) {
         $this->config['sidebar_layout'] = get_post_meta($this->config['id'], '_tb_sidebar_layout', true);
     }
     if (!$this->config['sidebar_layout'] || 'default' == $this->config['sidebar_layout']) {
         $this->config['sidebar_layout'] = themeblvd_get_option('sidebar_layout');
     }
     if (!$this->config['sidebar_layout']) {
         $this->config['sidebar_layout'] = apply_filters('themeblvd_default_sidebar_layout', 'sidebar_right', $this->config['sidebar_layout']);
         // Keeping for backwards compatibility, although is redundant with next filter.
     }
     $this->config['sidebar_layout'] = apply_filters('themeblvd_sidebar_layout', $this->config['sidebar_layout']);
     /*------------------------------------------------------*/
     /* Sidebar ID's
     		/*------------------------------------------------------*/
     // Determine which sidebar ID's belong to each sidebar location
     // on the current page. For each location, this will be the
     // framework's cooresponding default sidebar, unless something
     // else is filtered in. -- Note: The Widget Areas plugin filters
     // into this.
     $locations = themeblvd_get_sidebar_locations();
     $custom_sidebars = defined('TB_SIDEBARS_PLUGIN_VERSION') ? get_posts('post_type=tb_sidebar&numberposts=-1') : null;
     $sidebar_overrides = defined('TB_SIDEBARS_PLUGIN_VERSION') ? get_post_meta($this->config['id'], '_tb_sidebars', true) : null;
     foreach ($locations as $location_id => $default_sidebar) {
         // By default, the sidebar ID will match the ID of the
         // current location.
         $sidebar_id = apply_filters('themeblvd_custom_sidebar_id', $location_id, $custom_sidebars, $sidebar_overrides, $this->config['id']);
         // Set current sidebar ID
         $this->config['sidebars'][$location_id]['id'] = $sidebar_id;
         $this->config['sidebars'][$location_id]['error'] = false;
         // Determine sidebar error (i.e. sidebar is empty)
         if (!is_active_sidebar($sidebar_id)) {
             if ($default_sidebar['type'] == 'collapsible') {
                 // Only an error if collapsible sidebar is custom.
                 if ($sidebar_id != $location_id) {
                     $this->config['sidebars'][$location_id]['error'] = true;
                 }
             } else {
                 // Custom or not, we need to tell the user if a
                 // fixed sidebar is empty.
                 $this->config['sidebars'][$location_id]['error'] = true;
             }
         }
     }
     /*------------------------------------------------------*/
     /* Extend
     		/*------------------------------------------------------*/
     $this->config = apply_filters('themeblvd_frontend_config', $this->config);
 }