コード例 #1
0
/**
 * Conditionals
 *
 * @since 2.2.0
 */
function themeblvd_sanitize_conditionals($input, $sidebar_slug = null, $sidebar_id = null)
{
    $conditionals = themeblvd_conditionals_config();
    $output = array();
    // Prepare items that weren't straight-up arrays
    // gifted on a platter for us.
    if (!empty($input['post'])) {
        $input['post'] = str_replace(' ', '', $input['post']);
        $input['post'] = explode(',', $input['post']);
    }
    if (!empty($input['tag'])) {
        $input['tag'] = str_replace(' ', '', $input['tag']);
        $input['tag'] = explode(',', $input['tag']);
    }
    // Now loop through each group and then each item
    foreach ($input as $type => $group) {
        if (is_array($group) && !empty($group)) {
            foreach ($group as $item_id) {
                $name = '';
                switch ($type) {
                    case 'page':
                        $page_id = themeblvd_post_id_by_name($item_id, 'page');
                        $page = get_page($page_id);
                        if ($page) {
                            $name = $page->post_title;
                        }
                        break;
                    case 'post':
                        $post_id = themeblvd_post_id_by_name($item_id);
                        $post = get_post($post_id);
                        if ($post) {
                            $name = $post->post_title;
                        }
                        break;
                    case 'posts_in_category':
                        $category = get_category_by_slug($item_id);
                        if ($category) {
                            $name = $category->slug;
                        }
                        break;
                    case 'category':
                        $category = get_category_by_slug($item_id);
                        if ($category) {
                            $name = $category->slug;
                        }
                        break;
                    case 'tag':
                        $tag = get_term_by('slug', $item_id, 'post_tag');
                        if ($tag) {
                            $name = $tag->name;
                        }
                        break;
                    case 'top':
                        $name = $conditionals['top']['items'][$item_id];
                        break;
                }
                if ($name) {
                    $output[$type . '_' . $item_id] = array('type' => $type, 'id' => $item_id, 'name' => $name, 'post_slug' => $sidebar_slug, 'post_id' => $sidebar_id);
                }
            }
        }
    }
    // Add in cusotm conditional
    if (!empty($input['custom'])) {
        $output['custom'] = array('type' => 'custom', 'id' => themeblvd_sanitize_text($input['custom']), 'name' => themeblvd_sanitize_text($input['custom']), 'post_slug' => $sidebar_slug, 'post_id' => $sidebar_id);
    }
    return $output;
}
コード例 #2
0
/**
 * Create accordion panel for selecting conditional
 * assignments.
 *
 * @since 2.0.0
 *
 * @param $id string unique ID for option
 * @param $name string prefix for form name value
 * @param $val array currently saved data if exists
 * @return $output string HTML for option
 */
function themeblvd_conditionals_option($id, $name, $val = null)
{
    // Create array of all assignments seperated
    // by type to check against when displaying them
    // back to the user.
    $assignments = array('pages' => array(), 'posts' => array(), 'posts_in_category' => array(), 'categories' => array(), 'tags' => array(), 'top' => array(), 'custom' => '');
    if (is_array($val) && !empty($val)) {
        foreach ($val as $key => $group) {
            $item_id = $group['id'];
            switch ($group['type']) {
                case 'page':
                    $assignments['pages'][] = $item_id;
                    break;
                case 'post':
                    $assignments['posts'][] = $item_id;
                    break;
                case 'posts_in_category':
                    $assignments['posts_in_category'][] = $item_id;
                    break;
                case 'category':
                    $assignments['categories'][] = $item_id;
                    break;
                case 'tag':
                    $assignments['tags'][] = $item_id;
                    break;
                case 'top':
                    $assignments['top'][] = $item_id;
                    break;
                case 'custom':
                    $assignments['custom'] = $item_id;
                    break;
            }
        }
    }
    // Grab all conditionals to choose from
    $conditionals = themeblvd_conditionals_config();
    // Start output
    $output = '<div class="accordion">';
    // Display each accordion element
    foreach ($conditionals as $conditional) {
        $output .= '<div class="element">';
        $output .= '<a href="#" class="element-trigger">' . $conditional['name'] . '</a>';
        $output .= '<div class="element-content">';
        switch ($conditional['id']) {
            // Pages
            case 'pages':
                $pages = get_pages(array('hierarchical' => false));
                if (!empty($pages)) {
                    $output .= '<ul>';
                    foreach ($pages as $page) {
                        $checked = false;
                        if (in_array($page->post_name, $assignments['pages'])) {
                            $checked = true;
                        }
                        $output .= sprintf('<li><input type="checkbox" %s name="%s" value="%s" /> <span>%s</span></li>', checked($checked, true, false), esc_attr($name . '[' . $id . '][page][]'), $page->post_name, $page->post_title);
                        $checked = false;
                    }
                    $output .= '</ul>';
                } else {
                    $output .= sprintf('<p class="warning">%s</p>', $conditional['empty']);
                }
                break;
                // Posts
            // Posts
            case 'posts':
                $assignment_list = '';
                if (!empty($assignments['posts'])) {
                    $assignment_list = implode(', ', $assignments['posts']);
                }
                $output .= sprintf('<textarea name="%s">%s</textarea>', esc_attr($name . '[' . $id . '][post]'), $assignment_list);
                $output .= sprintf('<p class="note">%s</p>', __('Enter in a comma-separated list of the post slugs you\'d like to add to the assignments.', 'themeblvd'));
                $output .= sprintf('<p class="note"><em>%s</em></p>', __('Example: post-1, post-2, post-3', 'themeblvd'));
                $output .= sprintf('<p class="note"><em>%s</em></p>', __('Note: Any post slugs entered that don\'t exist won\'t be saved.', 'themeblvd'));
                break;
                // Posts in Category
            // Posts in Category
            case 'posts_in_category':
                if (isset($GLOBALS['sitepress'])) {
                    // WPML compat
                    global $sitepress;
                    remove_filter('terms_clauses', array($sitepress, 'terms_clauses'));
                    $categories = get_categories(array('hide_empty' => false));
                    add_filter('terms_clauses', array($sitepress, 'terms_clauses'));
                } else {
                    $categories = get_categories(array('hide_empty' => false));
                }
                if (!empty($categories)) {
                    $output .= '<ul>';
                    foreach ($categories as $category) {
                        $checked = false;
                        if (in_array($category->slug, $assignments['posts_in_category'])) {
                            $checked = true;
                        }
                        $output .= sprintf('<li><input type="checkbox" %s name="%s" value="%s" /> <span>%s</span></li>', checked($checked, true, false), esc_attr($name . '[' . $id . '][posts_in_category][]'), $category->slug, $category->name);
                        $checked = false;
                    }
                    $output .= '</ul>';
                } else {
                    $output .= sprintf('<p class="warning">%s</p>', $conditional['empty']);
                }
                break;
                // Category Archives
            // Category Archives
            case 'categories':
                if (isset($GLOBALS['sitepress'])) {
                    // WPML compat
                    global $sitepress;
                    remove_filter('terms_clauses', array($sitepress, 'terms_clauses'));
                    $categories = get_categories(array('hide_empty' => false));
                    add_filter('terms_clauses', array($sitepress, 'terms_clauses'));
                } else {
                    $categories = get_categories(array('hide_empty' => false));
                }
                if (!empty($categories)) {
                    $output .= '<ul>';
                    foreach ($categories as $category) {
                        $checked = false;
                        if (in_array($category->slug, $assignments['categories'])) {
                            $checked = true;
                        }
                        $output .= sprintf('<li><input type="checkbox" %s name="%s" value="%s" /> <span>%s</span></li>', checked($checked, true, false), esc_attr($name . '[' . $id . '][category][]'), $category->slug, $category->name);
                        $checked = false;
                    }
                    $output .= '</ul>';
                } else {
                    $output .= sprintf('<p class="warning">%s</p>', $conditional['empty']);
                }
                break;
                // Tag Archives
            // Tag Archives
            case 'tags':
                $assignment_list = '';
                if (!empty($assignments['tags'])) {
                    $assignment_list = implode(', ', $assignments['tags']);
                }
                $output .= sprintf('<textarea name="%s">%s</textarea>', esc_attr($name . '[' . $id . '][tag]'), $assignment_list);
                $output .= sprintf('<p class="note">%s</p>', __('Enter in a comma-separated list of the tags you\'d like to add to the assignments.', 'themeblvd'));
                $output .= sprintf('<p class="note"><em>%s</em></p>', __('Example: tag-1, tag-2, tag-3', 'themeblvd'));
                $output .= sprintf('<p class="note"><em>%s</em></p>', __('Note: Any tags entered that don\'t exist won\'t be saved.', 'themeblvd'));
                break;
                // Hierarchy
            // Hierarchy
            case 'top':
                if (!empty($conditional['items'])) {
                    foreach ($conditional['items'] as $item_id => $item_name) {
                        $checked = false;
                        if (in_array($item_id, $assignments['top'])) {
                            $checked = true;
                        }
                        $output .= sprintf('<li><input type="checkbox" %s name="%s" value="%s" /> <span>%s</span></li>', checked($checked, true, false), esc_attr($name . '[' . $id . '][top][]'), $item_id, $item_name);
                        $checked = false;
                    }
                }
                break;
                // Custom
            // Custom
            case 'custom':
                // If someone feels unsafe having this option which uses eval(),
                // they can disable it here.
                // NOTE: The actual call to eval() happens in the Theme Blvd Widget
                // Areas plugin, and not in the theme framework.
                $disable = apply_filters('themeblvd_disable_sidebar_custom_conditional', false);
                if (!$disable) {
                    $output .= sprintf('<input type="text" name="%s" value="%s" />', esc_attr($name . '[' . $id . '][custom]'), $assignments['custom']);
                    $output .= sprintf('<p class="note">%s</p>', __('Enter in a custom <a href="http://codex.wordpress.org/Conditional_Tags" target="_blank">conditional statement</a>.', 'themeblvd'));
                    $output .= sprintf('<p class="note"><em>%s</em><br /><code>is_home()</code><br /><code>is_home() || is_single()</code><br /><code>"book" == get_post_type() || is_tax("author")</code></p>', __('Examples:', 'themeblvd'));
                    $output .= sprintf('<p class="note"><em>%s</em></p>', __('Warning: Make sure you know what you\'re doing here. If you enter invalid conditional functions, you will most likely get PHP errors on the frontend of your website.', 'themeblvd'));
                }
                break;
        }
        $output .= '<ul>';
        $output .= '</div><!-- .element-content (end) -->';
        $output .= '</div><!-- .element (end) -->';
    }
    $output .= '</div><!-- .accordion (end) -->';
    return $output;
}