Example #1
0
/**
 * Sets structured data for product thumbnails.
 *
 * @param  string $html Product thumbnail HTML.
 *
 * @since  4.0
 *
 * @return string $html Product thumbnail HTML with strucutred data.
 */
function wpsc_set_structured_image_data($html)
{
    if (!wpsc_is_single()) {
        return $html;
    }
    return str_replace('<img', '<img itemprop="image"', $html);
}
Example #2
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);
}