Esempio n. 1
0
function _wpsc_filter_title($title)
{
    if (wpsc_is_controller()) {
        $controller = _wpsc_get_current_controller();
        if (wpsc_is_store() || get_post_type() == 'page') {
            return $controller->title;
        }
    }
    return $title;
}
Esempio n. 2
0
function wpsc_get_category_filter($args = '')
{
    if (!wpsc_get_option('display_category_filter')) {
        return '';
    }
    if (!wpsc_is_store() && !wpsc_is_product_category()) {
        return '';
    }
    $defaults = array('before' => '<div class="%s">', 'after' => '</div>', 'before_cat_list' => '<ul class="%s">', 'after_cat_list' => '</ul>', 'before_drill_down' => '<ul class="%s">', 'after_drill_down' => '</ul>', 'before_item' => '<li class="%s">', 'after_item' => '</li>', 'before_divider' => '<span class="%s">', 'after_divider' => '</span>', 'before_drill_down_divider' => '<span class="%s">', 'after_drill_down_divider' => '<span class="%s">', 'divider' => '|', 'drill_down_divider' => '&raquo;', 'padding' => 1, 'all_text' => _x('All', 'category filter', 'wpsc'));
    $defaults = apply_filters('wpsc_get_category_filter_default_args', $defaults);
    $r = wp_parse_args($args, $defaults);
    extract($r);
    $before = sprintf($before, 'wpsc-category-filter');
    $before_cat_list = sprintf($before_cat_list, 'wpsc-category-filter-list');
    $before_drill_down = sprintf($before_drill_down, 'wpsc-category-filter-drill-down');
    $before_divider = sprintf($before_divider, 'wpsc-category-filter-divider');
    $before_drill_down_divider = sprintf($before_drill_down_divider, 'wpsc-category-filter-drill-down-divider');
    if ($padding) {
        $length = strlen($divider) + $padding * 2;
        $padding = str_repeat("&nbsp;", $padding);
        $divider = $padding . $divider . $padding;
        $drill_down_divider = $padding . $drill_down_divider . $padding;
    }
    $divider = $before_divider . $divider . $after_divider;
    $drill_down_divider = $before_drill_down_divider . $drill_down_divider . $after_drill_down_divider;
    $displayed_categories = _wpsc_get_filtered_categories();
    $ids = wp_list_pluck($displayed_categories, 'term_id');
    $filters = array();
    $drilldown = array();
    // When drill down is enabled and we're not on store page
    if (wpsc_get_option('category_filter_drill_down') && !wpsc_is_store()) {
        $term = get_queried_object();
        $current = $term;
        // we need to trace back this category ancestors, if none of its ancestors
        // is one of the categories selected in the settings, the filter is not
        // output
        $ancestor_is_displayed = in_array($term->term_id, $ids);
        $ancestors = array();
        while ($term->parent) {
            $term = get_term($term->parent, 'wpsc_product_category');
            $ancestors[] = $term;
            if (in_array($term->term_id, $ids)) {
                $ancestor_is_displayed = true;
                break;
            }
        }
        if (!$ancestor_is_displayed) {
            return '';
        }
        // First item is always "All"
        $before_all = sprintf($before_item, 'wpsc-category-filter-drill-down-item wpsc-category-filter-drill-down-item-all');
        $link = '<a href="' . esc_url(wpsc_get_store_url()) . '">' . esc_html_x('All', 'category filter', 'wpsc') . '</a>';
        $drilldown[] = $before_all . $link . $drill_down_divider . $after_item;
        $ancestors = array_reverse($ancestors);
        foreach ($ancestors as $ancestor) {
            $before_this_item = sprintf($before_item, 'wpsc-category-filter-drill-down-item');
            $url = add_query_arg('wpsc_category_filter', 1, wpsc_get_product_category_permalink($ancestor->term_id));
            $link = '<a href="' . esc_url($url) . '">' . esc_html($ancestor->name) . '</a>';
            $drilldown[] = $before_this_item . $link . $drill_down_divider . $after_item;
        }
        // current category
        $before_this_item = sprintf($before_item, 'wpsc-category-filter-drill-down-item wpsc-category-filter-drill-down-item-active');
        $url = add_query_arg('wpsc_category_filter', 1, wpsc_get_product_category_permalink($current->term_id));
        $link = '<a href="' . esc_url($url) . '">' . esc_html($current->name) . '</a>';
        $drilldown[] = $before_this_item . $link . $after_item;
        $displayed_categories = get_terms('wpsc_product_category', array('parent' => $current->term_id, 'hide_empty' => 0));
        // When drill down is disabled, or when it is enabled and we're on store page
    } else {
        // If we're in store page, just simply display all categories as selected in
        // Settings->Store->Presentation
        // if we're in the wrong category, don't display the filter
        $current = get_queried_object_id();
        if (!wpsc_is_store() && !in_array($current, $ids)) {
            return '';
        }
        // if we're not on store page, and the 'wpsc_category_filter' query arg
        // is not set to 1, don't display the filter
        if (!wpsc_is_store() && empty($_GET['wpsc_category_filter'])) {
            return '';
        }
        // First item is always "All"
        $before_all = sprintf($before_item, 'wpsc-category-filter-item wpsc-category-filter-item-all');
        $link = '<a href="' . esc_url(wpsc_get_store_url()) . '">' . esc_html_x('All', 'category filter', 'wpsc') . '</a>';
        $filters[] = $before_all . $link . $divider . $after_item;
    }
    $cats_count = count($displayed_categories);
    // Subsequent items are extracted from $displayed_categories
    for ($i = 0; $i < $cats_count; $i++) {
        $cat = $displayed_categories[$i];
        $classes = 'wpsc-category-filter-item';
        // mark current category
        if (!wpsc_is_store() && $cat->term_id == $current) {
            $classes .= ' wpsc-category-filter-item-active';
        }
        $before_this_item = sprintf($before_item, $classes);
        $url = add_query_arg('wpsc_category_filter', 1, wpsc_get_product_category_permalink($cat->term_id));
        $link = '<a href="' . esc_url($url) . '">' . esc_html($cat->name) . '</a>';
        $filter = $before_this_item . $link;
        if ($i < $cats_count - 1) {
            $filter .= $divider;
        }
        $filter .= $after_item;
        $filters[] = $filter;
    }
    $html = $before;
    if (!empty($drilldown)) {
        $html .= $before_drill_down . implode('', $drilldown) . $after_drill_down;
    }
    if (!empty($filters)) {
        $html .= $before_cat_list . implode('', $filters) . $after_cat_list;
    }
    $html .= $after;
    return apply_filters('wpsc_get_category_filter', $html, $filters, $r);
}
Esempio n. 3
0
/**
 * Return the HTML for the breadcrumb of a shop page.
 *
 * The available options to customize the output include:
 *     'before'          - HTML before the breadcrumb. Defaults to '<p class="%s">'. The %s
 *                         placeholder will be replaced by the class attribute.
 *     'after'           - HTML after the breadcrumb. Defaults to '</p>'.
 *     'separator'       - The separator between breadcrumb items. Defaults to &rsaquo; .
 *     'padding'         - The number of spaces you want to insert to the both sides of the
 *                         separator. Defaults to 1.
 *     'include_home'    - Whether to include a link to home in the breadcrumb. Defaults to true.
 *     'home_text'       - The text for the home link. Defaults to "Home".
 *     'include_store' - Whether to include a link to the main store in the breadcrumb.
 *                         Defaults to true.
 *     'store_text'    - The text for the store link. Defaults to "Products".
 *     'include_current' - Whether to include a link to the current page in the breadcrumb.
 *                         Defaults to true.
 *     'current_text'    - The text for the current link. Defaults to the category / product title.
 *
 * @since 4.0
 * @uses  apply_filters()      Applies 'wpsc_breadcrumb_array'     filter.
 * @uses  apply_filters()      Applies 'wpsc_breadcrumb_class'     filter.
 * @uses  apply_filters()      Applies 'wpsc_breadcrumb_separator' filter.
 * @uses  apply_filters()      Applies 'wpsc_get_breadcrumb'       filter.
 * @uses  get_option()         Get the 'page_on_front' option.
 * @uses  get_queried_object()
 * @uses  get_term_field()
 * @uses  get_the_title()
 * @uses  wp_get_object_terms()
 * @uses  wp_parse_args()
 * @uses  wpsc_is_store()
 * @uses  wpsc_get_store_url()
 * @uses  wpsc_get_store_title()
 * @uses  wpsc_get_product_category_name()
 * @uses  wpsc_get_product_category_permalink()
 * @uses  wpsc_get_product_tag_name()
 * @uses  wpsc_get_product_title()
 * @uses  wpsc_is_product_category()
 * @uses  wpsc_is_product_tag()
 * @uses  wpsc_is_single()
 *
 * @param  string|array $args Optional. Query string or array of options. Defaults to ''.
 * @return string
 */
function wpsc_get_breadcrumb($args = '')
{
    $args = wp_parse_args($args);
    $pre_front_text = $pre_current_text = '';
    // No custom home text
    if (empty($args['home_text'])) {
        // Set home text to page title
        if ($front_id = get_option('page_on_front')) {
            $pre_front_text = get_the_title($front_id);
            // Default to 'Home'
        } else {
            $pre_front_text = __('Home', 'wp-e-commerce');
        }
    }
    // No custom store text
    if (empty($args['store_text'])) {
        $pre_store_text = wpsc_get_store_title();
    } else {
        $pre_store_text = $args['store_text'];
    }
    $parent = null;
    if (wpsc_is_single()) {
        // if this is a single product, find its product category
        $pre_current_text = wpsc_get_product_title();
        $product_categories = wp_get_object_terms(wpsc_get_product_id(), 'wpsc_product_category');
        // if there are multiple product categories associated with this product, choose the most
        // appropriate one based on the context
        if (!empty($product_categories)) {
            $parent = $product_categories[0];
            $context = get_query_var('wpsc_product_category');
            if ($context && in_array($context, wp_list_pluck($product_categories, 'slug'))) {
                $parent = get_term_by('slug', $context, 'wpsc_product_category');
            }
        }
    } elseif (wpsc_is_store()) {
        // if this is the main store, default the "current_text" argument to
        // the store title
        $pre_current_text = wpsc_get_store_title();
    } elseif (wpsc_is_product_category()) {
        // if this is a product category, find its parent category if it has
        // one
        $pre_current_text = wpsc_get_product_category_name();
        $term = get_queried_object();
        if ($term->parent) {
            $parent = get_term($term->parent, 'wpsc_product_category');
        }
    } elseif (wpsc_is_product_tag()) {
        // if this is a product tag, set "current_text" to the tag name by default
        $pre_current_text = wpsc_get_product_tag_name();
    } elseif (wpsc_is_customer_account()) {
        // if this is the customer account page
        $c = _wpsc_get_current_controller();
        // if we're displaying an order's details, set the parent to the
        // "Your Account" page
        if ($c->order_id) {
            $pre_current_text = $c->order_id;
            $parent = array(array('title' => __('Your Account', 'wp-e-commerce'), 'url' => wpsc_get_customer_account_url()));
        } else {
            // otherwise, set the "current_text" argument to "Your Account"
            $pre_current_text = __('Your Account', 'wp-e-commerce');
        }
    }
    $defaults = array('before' => '<ul class="%s">', 'after' => '</ul>', 'before_item' => '<li class="%s">', 'after_item' => '</li>', 'before_divider' => '<span class="%s">', 'after_divider' => '</span>', 'divider' => '&raquo;', 'padding' => 0, 'include_home' => true, 'home_text' => $pre_front_text, 'include_store' => true, 'store_text' => $pre_store_text, 'include_current' => true, 'current_text' => $pre_current_text);
    $defaults = apply_filters('wpsc_get_breadcrumb_default_args', $defaults);
    $r = array_merge($defaults, $args);
    extract($r);
    // replace placeholders in arguments
    $before = sprintf($before, 'wpsc-breadcrumb');
    $before_item = sprintf($before_item, 'wpsc-breadcrumb-item');
    $before_divider = sprintf($before_divider, 'wpsc-breadcrumb-divider');
    // if padding is set, prepare the length, padding string and divider
    if ($padding) {
        $length = strlen(html_entity_decode($divider, ENT_COMPAT, 'UTF-8')) + $padding * 2;
        $padding = str_repeat("&nbsp;", $length);
        $divider = $padding . $divider . $padding;
    }
    $divider = $before_divider . $divider . $after_divider;
    // generate the breadcrumb array in reverse
    $breadcrumbs = array();
    // include current page in breadcrumb
    if ($include_current && !empty($current_text)) {
        $before_current_item = sprintf($before_item, 'wpsc-breadcrumb-item wpsc-breadcrumb-current');
        $breadcrumbs[] = $before_current_item . $current_text . $after_item;
    }
    // include ancestors in breadcrumb
    $ancestors = array();
    // if the current page has a parent
    if ($parent) {
        if (is_array($parent)) {
            // if $parent is an array, then use the 'url' and 'title' elements
            foreach ($parent as $p) {
                $before_this_item = sprintf($before_item, 'wpsc-breadcrumb-item wpsc-breadcrumb-ancestor');
                $link = '<a href="' . esc_url($p['url']) . '">' . esc_html($p['title']) . '</a>';
                $breadcrumbs[] = $before_this_item . $link . $divider . $after_item;
            }
        } else {
            // if $parent is a term object, recursively find all ancestors and
            // include them in the breadcrumb array
            while (!is_wp_error($parent) && is_object($parent)) {
                if (in_array($parent->parent, $ancestors)) {
                    break;
                }
                $ancestors[] = $parent->parent;
                $before_this_item = sprintf($before_item, 'wpsc-breadcrumb-item wpsc-breadcrumb-ancestor');
                $link = '<a href="' . wpsc_get_product_category_permalink($parent) . '">' . esc_html($parent->name) . '</a>';
                $breadcrumbs[] = $before_this_item . $link . $divider . $after_item;
                $parent = get_term($parent->parent, 'wpsc_product_category');
            }
        }
    }
    // include the store link if this is not the main store itself
    if ($include_store && !empty($store_text) && !wpsc_is_store()) {
        $before_this_item = sprintf($before_item, 'wpsc-breadcrumb-item wpsc-breadcrumb-store');
        $link = '<a href="' . wpsc_get_store_url() . '">' . $store_text . '</a>';
        $breadcrumbs[] = $before_this_item . $link . $divider . $after_item;
    }
    // include the home link
    if ($include_home && !empty($home_text) && !is_home() && !wpsc_get_option('store_as_front_page')) {
        $before_this_item = sprintf($before_item, 'wpsc-breadcrumb-item wpsc-breadcrumb-home');
        $link = '<a href="' . trailingslashit(home_url()) . '">' . $home_text . '</a>';
        $breadcrumbs[] = $before_this_item . $link . $divider . $after_item;
    }
    // reverse the breadcrumb array
    $breadcrumbs = apply_filters('wpsc_breadcrumb_array', array_reverse($breadcrumbs), $r);
    $html = $before . implode('', $breadcrumbs) . $after;
    return apply_filters('wpsc_get_breadcrumb', $html, $breadcrumbs, $r);
}