This commit is contained in:
Mike Jolley 2018-01-11 12:32:58 +00:00
parent 7a50512e16
commit 16870a30e7
1 changed files with 30 additions and 24 deletions

View File

@ -1,22 +1,25 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Structured data's handler and generator using JSON-LD format.
*
* @class WC_Structured_Data
* @since 3.0.0
* @version 3.0.0
* @package WooCommerce/Classes
* @author Clément Cazaud <opportus@gmail.com>
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WC_Structured_Data class.
*/
class WC_Structured_Data {
/**
* @var array $_data
* Stores the structured data.
*
* @var array $_data Array of structured data.
*/
private $_data = array();
@ -128,9 +131,9 @@ class WC_Structured_Data {
/**
* Makes sure email structured data only outputs on non-plain text versions.
*
* @param WP_Order $order Order data.
* @param bool $sent_to_admin Send to admin (default: false).
* @param bool $plain_text Plain text email (default: false).
* @param WP_Order $order Order data.
* @param bool $sent_to_admin Send to admin (default: false).
* @param bool $plain_text Plain text email (default: false).
*/
public function output_email_structured_data( $order, $sent_to_admin = false, $plain_text = false ) {
if ( $plain_text ) {
@ -149,8 +152,9 @@ class WC_Structured_Data {
*/
public function output_structured_data() {
$types = $this->get_data_type_for_page();
$data = wc_clean( $this->get_structured_data( $types ) );
if ( $data = wc_clean( $this->get_structured_data( $types ) ) ) {
if ( $data ) {
echo '<script type="application/ld+json">' . wp_json_encode( $data ) . '</script>';
}
}
@ -213,8 +217,8 @@ class WC_Structured_Data {
if ( '' !== $product->get_price() ) {
if ( $product->is_type( 'variable' ) ) {
$prices = $product->get_variation_prices();
$lowest = reset( $prices['price'] );
$prices = $product->get_variation_prices();
$lowest = reset( $prices['price'] );
$highest = end( $prices['price'] );
if ( $lowest === $highest ) {
@ -279,18 +283,20 @@ class WC_Structured_Data {
'@type' => 'Product',
'name' => get_the_title( $comment->comment_post_ID ),
);
if ( $rating = get_comment_meta( $comment->comment_ID, 'rating', true ) ) {
$markup['reviewRating'] = array(
// Skip replies unless they have a rating.
$rating = get_comment_meta( $comment->comment_ID, 'rating', true );
if ( $rating ) {
$markup['reviewRating'] = array(
'@type' => 'rating',
'ratingValue' => $rating,
);
// Skip replies unless they have a rating.
} elseif ( $comment->comment_parent ) {
return;
}
$markup['author'] = array(
$markup['author'] = array(
'@type' => 'Person',
'name' => get_comment_author( $comment->comment_ID ),
);
@ -325,7 +331,7 @@ class WC_Structured_Data {
),
);
if ( ! empty( $crumb[1] ) && sizeof( $crumbs ) !== $key + 1 ) {
if ( ! empty( $crumb[1] ) && count( $crumbs ) !== $key + 1 ) {
$markup['itemListElement'][ $key ]['item'] += array( '@id' => $crumb[1] );
}
}
@ -357,9 +363,9 @@ class WC_Structured_Data {
*
* Hooked into `woocommerce_email_order_details` action hook.
*
* @param WP_Order $order Order data.
* @param bool $sent_to_admin Send to admin (default: false).
* @param bool $plain_text Plain text email (default: false).
* @param WP_Order $order Order data.
* @param bool $sent_to_admin Send to admin (default: false).
* @param bool $plain_text Plain text email (default: false).
*/
public function generate_order_data( $order, $sent_to_admin = false, $plain_text = false ) {
if ( $plain_text || ! is_a( $order, 'WC_Order' ) ) {
@ -389,7 +395,7 @@ class WC_Structured_Data {
$product_exists = is_object( $product );
$is_visible = $product_exists && $product->is_visible();
$markup_offers[] = array(
$markup_offers[] = array(
'@type' => 'Offer',
'price' => $order->get_line_subtotal( $item ),
'priceCurrency' => $order->get_currency(),