Skip to content

Commit

Permalink
An initial pass at adding rich markup to the 2.0 theme engine.
Browse files Browse the repository at this point in the history
* Adds price, title, image, description, currency metadata.
* There is ample opportunity for additional data for themes/plugins.
* We added a currency code function to the product helper file. This will likely move if and when #1865 lands.

Fixes #59
  • Loading branch information
JustinSainton committed Jun 14, 2015
1 parent 4a99183 commit fa23653
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function wpsc_get_breadcrumb( $args = '' ) {
'before_divider' => '<span class="%s">',
'after_divider' => '</span>',
'divider' => '&raquo;',
'padding' => 1,
'padding' => 0,

// Home
'include_home' => true,
Expand All @@ -175,9 +175,8 @@ function wpsc_get_breadcrumb( $args = '' ) {

// if padding is set, prepare the length, padding string and divider
if ( $padding ) {
/* @todo: Check if $padding is supposed to use $length, rather than $padding. Otherwise, $length is dead. */
$length = strlen( $divider ) + $padding * 2;
$padding = str_repeat( "&nbsp;", $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;
Expand Down
58 changes: 58 additions & 0 deletions wpsc-components/theme-engine-v2/helpers/template-tags/product.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,64 @@ function wpsc_get_product_thumbnail_id( $product_id = null ) {
return $thumbnail_id;
}

/**
* Sets the structured product name to the product title.
*
* @param array $crumbs Array of breadcrumb elements.
*
* @since 4.0
*
* @return array Array of breadcrumb elements with product title structured semantically.
*/
function wpsc_set_structured_product_name( $crumbs ) {

if ( wpsc_is_single() ) {
$product = str_replace( 'wpsc-breadcrumb-item"', 'wpsc-breadcrumb-item" itemprop="name"', array_pop( $crumbs ) );
$crumbs[] = $product;
}

return $crumbs;
}

add_filter( 'wpsc_breadcrumb_array', 'wpsc_set_structured_product_name' );

/**
* Template tag for base country currency code.
*
* Helpful for templates using structured data, likely other use cases.
* Temporarily located here, until #1865 lands.
*
* @since 4.0
*
* @return string Base country currency code.
*/
function wpsc_base_country_code() {

$base = new WPSC_Country( wpsc_get_base_country() );

echo esc_attr( $base->get_currency_code() );
}

/**
* 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 );
}

add_filter( 'post_thumbnail_html', 'wpsc_set_structured_image_data' );

/**
* Return the HTML of a product's featured thumbnail.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
<?php wpsc_user_messages(); ?>
<div id="product-<?php wpsc_product_id(); ?>">
<div id="product-<?php wpsc_product_id(); ?>" itemscope itemtype="http://schema.org/Product">
<?php wpsc_breadcrumb(); ?>

<div class="wpsc-product-summary">
<div class="wpsc-product-price">

<div class="wpsc-product-price" itemprop="offers" itemscope itemtype="http://schema.org/Offer">
<meta itemprop="priceCurrency" content="<?php wpsc_base_country_code(); ?>" />
<?php if ( wpsc_is_product_on_sale() ): ?>
<del class="wpsc-old-price">
<strong><?php esc_html_e( 'Old Price', 'wpsc' ); ?>:</strong> <span class="wpsc-amount"><?php wpsc_product_original_price(); ?></span>
</del><br />
<ins class="wpsc-sale-price">
<strong><?php esc_html_e( 'Price', 'wpsc' ); ?>:</strong> <span class="wpsc-amount"><?php wpsc_product_sale_price(); ?></span>
<strong><?php esc_html_e( 'Price', 'wpsc' ); ?>:</strong> <span class="wpsc-amount" itemprop="price"><?php wpsc_product_sale_price(); ?></span>
</ins><br />
<span class="wpsc-you-save">
<strong><?php esc_html_e( 'You save', 'wpsc' ); ?>:</strong> <span class="wpsc-amount"><?php wpsc_product_you_save(); ?></span>
</span>
<?php else: ?>
<strong><?php esc_html_e( 'Price', 'wpsc' ); ?>:</strong> <span class="wpsc-amount"><?php wpsc_product_original_price(); ?></span>
<strong><?php esc_html_e( 'Price', 'wpsc' ); ?>:</strong> <span class="wpsc-amount" itemprop="price"><?php wpsc_product_original_price(); ?></span>
<?php endif; ?>
</div>

<div class="wpsc-product-description">
<div class="wpsc-product-description" itemprop="description">
<?php wpsc_product_description(); ?>
</div>

Expand All @@ -45,4 +47,4 @@ class="wpsc-thumbnail wpsc-product-thumbnail"
<?php endif; ?>
</a>
</div>
</div><!-- #post-<?php the_ID(); ?> -->
</div><!-- #product-<?php the_ID(); ?> -->

0 comments on commit fa23653

Please sign in to comment.