2016-07-17 02:42:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Structured data's handler and generator using JSON-LD format.
|
|
|
|
*
|
2016-07-24 21:46:30 +00:00
|
|
|
* @class WC_Structured_Data
|
2016-08-11 12:58:05 +00:00
|
|
|
* @since 2.7.0
|
2016-07-24 21:46:30 +00:00
|
|
|
* @version 2.7.0
|
|
|
|
* @package WooCommerce/Classes
|
2016-08-14 22:39:01 +00:00
|
|
|
* @author Clément Cazaud <opportus@gmail.com>
|
2016-07-17 02:42:46 +00:00
|
|
|
*/
|
2016-07-19 19:36:52 +00:00
|
|
|
class WC_Structured_Data {
|
2016-08-11 09:17:14 +00:00
|
|
|
|
|
|
|
/**
|
2016-08-12 21:53:37 +00:00
|
|
|
* @var null|array $_data
|
2016-08-11 09:17:14 +00:00
|
|
|
*/
|
|
|
|
private $_data;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
2016-08-11 10:04:12 +00:00
|
|
|
*/
|
2016-08-11 09:17:14 +00:00
|
|
|
public function __construct() {
|
|
|
|
// Generate data...
|
2016-08-26 16:37:19 +00:00
|
|
|
add_action( 'woocommerce_before_main_content', array( $this, 'generate_website_data' ), 30, 0 );
|
|
|
|
add_action( 'woocommerce_breadcrumb', array( $this, 'generate_breadcrumblist_data' ), 10, 1 );
|
|
|
|
add_action( 'woocommerce_shop_loop', array( $this, 'generate_product_data' ), 10, 0 );
|
|
|
|
add_action( 'woocommerce_single_product_summary', array( $this, 'generate_product_data' ), 60, 0 );
|
|
|
|
add_action( 'woocommerce_review_meta', array( $this, 'generate_review_data' ), 20, 1 );
|
|
|
|
add_action( 'woocommerce_email_order_details', array( $this, 'generate_order_data' ), 20, 3 );
|
2016-08-14 22:39:01 +00:00
|
|
|
|
|
|
|
// Output structured data...
|
2016-08-26 16:37:19 +00:00
|
|
|
add_action( 'woocommerce_email_order_details', array( $this, 'output_structured_data' ), 30, 0 );
|
|
|
|
add_action( 'wp_footer', array( $this, 'output_structured_data' ), 10, 0 );
|
2016-08-23 13:44:46 +00:00
|
|
|
|
|
|
|
// Filters...
|
2016-08-26 16:37:19 +00:00
|
|
|
add_filter( 'woocommerce_structured_data_product_limit', array( $this, 'limit_product_data_in_loops' ), 10, 1 );
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-11 12:58:05 +00:00
|
|
|
* Sets `$this->_data`.
|
2016-08-10 22:23:26 +00:00
|
|
|
*
|
2016-08-12 00:55:52 +00:00
|
|
|
* @param array $data
|
2016-08-12 20:58:23 +00:00
|
|
|
* @param bool $reset (default: false)
|
2016-08-11 12:58:05 +00:00
|
|
|
* @return bool
|
2016-08-11 09:17:14 +00:00
|
|
|
*/
|
2016-08-12 00:55:52 +00:00
|
|
|
public function set_data( $data, $reset = false ) {
|
2016-08-14 22:39:01 +00:00
|
|
|
if ( ! isset( $data['@type'] ) ) {
|
|
|
|
return false;
|
|
|
|
} elseif ( ! is_string( $data['@type'] ) ) {
|
2016-08-11 09:17:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-12 00:55:52 +00:00
|
|
|
if ( $reset && isset( $this->_data ) ) {
|
2016-08-11 09:17:14 +00:00
|
|
|
unset( $this->_data );
|
|
|
|
}
|
|
|
|
|
2016-08-11 23:29:35 +00:00
|
|
|
$this->_data[] = $data;
|
2016-08-11 09:17:14 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets `$this->_data`.
|
|
|
|
*
|
2016-08-23 13:44:46 +00:00
|
|
|
* @return array
|
2016-08-11 09:17:14 +00:00
|
|
|
*/
|
|
|
|
public function get_data() {
|
2016-08-23 13:44:46 +00:00
|
|
|
return isset( $this->_data ) ? $this->_data : array();
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-12 21:53:37 +00:00
|
|
|
* Structures and returns data.
|
2016-08-11 09:17:14 +00:00
|
|
|
*
|
2016-08-15 12:10:14 +00:00
|
|
|
* List of types available by default for specific request:
|
|
|
|
*
|
2016-08-14 22:39:01 +00:00
|
|
|
* 'product',
|
|
|
|
* 'review',
|
|
|
|
* 'breadcrumblist',
|
|
|
|
* 'website',
|
|
|
|
* 'order',
|
2016-08-12 20:58:23 +00:00
|
|
|
*
|
2016-08-14 22:39:01 +00:00
|
|
|
* @param bool|array|string $requested_types (default: false)
|
2016-08-12 20:58:23 +00:00
|
|
|
* @return array
|
2016-08-11 09:17:14 +00:00
|
|
|
*/
|
2016-08-12 20:58:23 +00:00
|
|
|
public function get_structured_data( $requested_types = false ) {
|
2016-08-17 23:02:43 +00:00
|
|
|
$data = $this->get_data();
|
|
|
|
|
|
|
|
if ( empty( $data ) || ( $requested_types && ! is_array( $requested_types ) && ! is_string( $requested_types ) || is_null( $requested_types ) ) ) {
|
2016-08-12 20:58:23 +00:00
|
|
|
return array();
|
2016-08-17 23:02:43 +00:00
|
|
|
} elseif ( $requested_types && is_string( $requested_types ) ) {
|
|
|
|
$requested_types = array( $requested_types );
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 16:37:19 +00:00
|
|
|
// Put together the values of same type of structured data.
|
2016-08-17 23:02:43 +00:00
|
|
|
foreach ( $data as $value ) {
|
|
|
|
$structured_data[ strtolower( $value['@type'] ) ][] = $value;
|
2016-08-12 20:58:23 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 16:37:19 +00:00
|
|
|
// Wrap the multiple values of each type inside a graph... Then add context to each type.
|
2016-08-17 23:02:43 +00:00
|
|
|
foreach ( $structured_data as $type => $value ) {
|
|
|
|
$structured_data[ $type ] = count( $value ) > 1 ? array( '@graph' => $value ) : $value[0];
|
|
|
|
$structured_data[ $type ] = apply_filters( 'woocommerce_structured_data_context', array( '@context' => 'http://schema.org/' ), $structured_data, $type, $value ) + $structured_data[ $type ];
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 16:37:19 +00:00
|
|
|
// If requested types, pick them up... Finally change the associative array to an indexed one.
|
2016-08-17 23:02:43 +00:00
|
|
|
$structured_data = $requested_types ? array_values( array_intersect_key( $structured_data, array_flip( $requested_types ) ) ) : array_values( $structured_data );
|
2016-08-14 22:39:01 +00:00
|
|
|
|
2016-08-17 23:02:43 +00:00
|
|
|
if ( ! empty( $structured_data ) ) {
|
|
|
|
$structured_data = count( $structured_data ) > 1 ? array( '@graph' => $structured_data ) : $structured_data[0];
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 20:58:23 +00:00
|
|
|
return $structured_data;
|
2016-08-11 23:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-14 22:39:01 +00:00
|
|
|
* Sanitizes, encodes and outputs structured data.
|
2016-08-11 23:29:35 +00:00
|
|
|
*
|
2016-08-14 22:39:01 +00:00
|
|
|
* @uses `wp_footer` action hook
|
2016-08-12 00:55:52 +00:00
|
|
|
* @uses `woocommerce_email_order_details` action hook
|
2016-08-14 22:39:01 +00:00
|
|
|
* @param bool|array|string $requested_types (default: true)
|
2016-08-11 23:29:35 +00:00
|
|
|
* @return bool
|
2016-08-11 09:17:14 +00:00
|
|
|
*/
|
2016-08-14 22:39:01 +00:00
|
|
|
public function output_structured_data( $requested_types = true ) {
|
|
|
|
if ( $requested_types === true ) {
|
2016-08-17 23:02:43 +00:00
|
|
|
$requested_types = array_filter( apply_filters( 'woocommerce_structured_data_type_for_page', array(
|
2016-08-15 12:10:14 +00:00
|
|
|
is_shop() || is_product_category() || is_product() ? 'product' : null,
|
|
|
|
is_shop() && is_front_page() ? 'website' : null,
|
|
|
|
is_product() ? 'review' : null,
|
2016-08-14 22:39:01 +00:00
|
|
|
! is_shop() ? 'breadcrumblist' : null,
|
|
|
|
'order',
|
2016-08-17 23:02:43 +00:00
|
|
|
) ) );
|
2016-08-14 22:39:01 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 20:58:23 +00:00
|
|
|
if ( $structured_data = $this->sanitize_data( $this->get_structured_data( $requested_types ) ) ) {
|
|
|
|
echo '<script type="application/ld+json">' . wp_json_encode( $structured_data ) . '</script>';
|
2016-08-12 21:53:37 +00:00
|
|
|
|
2016-08-12 00:55:52 +00:00
|
|
|
return true;
|
2016-08-12 20:58:23 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 12:10:14 +00:00
|
|
|
/**
|
|
|
|
* Sanitizes data.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function sanitize_data( $data ) {
|
|
|
|
if ( ! $data || ! is_array( $data ) ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $data as $key => $value ) {
|
|
|
|
$sanitized_data[ sanitize_text_field( $key ) ] = is_array( $value ) ? $this->sanitize_data( $value ) : sanitize_text_field( $value );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sanitized_data;
|
|
|
|
}
|
|
|
|
|
2016-08-11 09:17:14 +00:00
|
|
|
/**
|
2016-08-14 22:39:01 +00:00
|
|
|
* Generates, sanitizes, encodes and outputs specific structured data type.
|
|
|
|
*
|
|
|
|
* @param string $type
|
2016-08-15 12:10:14 +00:00
|
|
|
* @param mixed $object (default: null)
|
2016-08-14 22:39:01 +00:00
|
|
|
* @param mixed $param_1 (default: null)
|
|
|
|
* @param mixed $param_2 (default: null)
|
|
|
|
* @param mixed $param_3 (default: null)
|
2016-08-11 23:29:35 +00:00
|
|
|
* @return bool
|
2016-08-11 09:17:14 +00:00
|
|
|
*/
|
2016-08-15 12:10:14 +00:00
|
|
|
public function generate_output_structured_data( $type, $object = null, $param_1 = null, $param_2 = null, $param_3 = null ) {
|
|
|
|
if ( ! is_string( $type ) ) {
|
2016-08-14 22:39:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$generate = 'generate_' . $type . '_data';
|
|
|
|
|
2016-08-17 19:14:59 +00:00
|
|
|
if ( method_exists( $this, $generate ) && $this->$generate( $object, $param_1, $param_2, $param_3 ) ) {
|
2016-08-14 22:39:01 +00:00
|
|
|
return $this->output_structured_data( $type );
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
|
|
|
|
2016-08-23 13:44:46 +00:00
|
|
|
/**
|
2016-08-26 16:37:19 +00:00
|
|
|
* Limits Product structured data on taxonomies and shop page.
|
2016-08-23 13:44:46 +00:00
|
|
|
*
|
2016-08-26 16:37:19 +00:00
|
|
|
* @uses `woocommerce_structured_data_product_limit` filter hook
|
2016-08-26 14:57:49 +00:00
|
|
|
* @param bool $limit_data
|
|
|
|
* @return bool $limit_data
|
2016-08-23 13:44:46 +00:00
|
|
|
*/
|
2016-08-26 16:52:31 +00:00
|
|
|
public function limit_product_data_in_loops( $limit_data ) {
|
2016-08-26 14:57:49 +00:00
|
|
|
return $limit_data = is_product_taxonomy() || is_shop() ? true : false;
|
2016-08-23 13:44:46 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 12:10:14 +00:00
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Generators
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
| Methods for generating specific structured data types:
|
|
|
|
|
|
|
|
|
| - Product
|
|
|
|
| - Review
|
|
|
|
| - BreadcrumbList
|
|
|
|
| - WebSite
|
|
|
|
| - Order
|
|
|
|
|
|
|
|
|
| The generated data is stored into `$this->_data`.
|
|
|
|
| See the methods above for handling `$this->_data`.
|
|
|
|
|
|
|
|
|
*/
|
2016-08-11 09:17:14 +00:00
|
|
|
|
|
|
|
/**
|
2016-08-11 23:29:35 +00:00
|
|
|
* Generates Product structured data.
|
2016-08-11 12:58:05 +00:00
|
|
|
*
|
2016-08-11 23:29:35 +00:00
|
|
|
* @uses `woocommerce_single_product_summary` action hook
|
2016-08-12 00:55:52 +00:00
|
|
|
* @uses `woocommerce_shop_loop` action hook
|
2016-08-26 14:57:49 +00:00
|
|
|
* @param bool|object $product (default: false)
|
|
|
|
* @param bool $limit_data (default: false)
|
2016-08-11 23:29:35 +00:00
|
|
|
* @return bool
|
2016-08-11 09:17:14 +00:00
|
|
|
*/
|
2016-08-26 14:57:49 +00:00
|
|
|
public function generate_product_data( $product = false, $limit_data = false ) {
|
2016-08-18 11:24:17 +00:00
|
|
|
if ( $product === false ) {
|
2016-08-11 23:29:35 +00:00
|
|
|
global $product;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! is_object( $product ) ) {
|
|
|
|
return false;
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 14:57:49 +00:00
|
|
|
$limit_data = apply_filters( 'woocommerce_structured_data_product_limit', $limit_data );
|
|
|
|
|
|
|
|
$markup['@type'] = 'Product';
|
|
|
|
$markup['@id'] = get_permalink( $product->get_id() );
|
|
|
|
$markup['url'] = $markup['@id'];
|
|
|
|
$markup['name'] = $product->get_title();
|
2016-08-23 13:44:46 +00:00
|
|
|
|
2016-08-26 14:57:49 +00:00
|
|
|
if ( $limit_data ) {
|
2016-08-26 16:37:19 +00:00
|
|
|
return $this->set_data( apply_filters( 'woocommerce_structured_data_product_limited', $markup, $product ) );
|
2016-08-26 14:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $is_variable = $product->is_type( 'variable' ) ) {
|
2016-08-18 18:37:34 +00:00
|
|
|
$variations = $product->get_available_variations();
|
2016-08-11 09:17:14 +00:00
|
|
|
|
2016-08-18 18:37:34 +00:00
|
|
|
foreach ( $variations as $variation ) {
|
|
|
|
$product_variations[] = wc_get_product( $variation['variation_id'] );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$product_variations[] = $product;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $product_variations as $product_variation ) {
|
2016-08-11 23:29:35 +00:00
|
|
|
$markup_offers[] = array(
|
2016-08-11 09:17:14 +00:00
|
|
|
'@type' => 'Offer',
|
|
|
|
'priceCurrency' => get_woocommerce_currency(),
|
|
|
|
'price' => $product_variation->get_price(),
|
|
|
|
'availability' => 'http://schema.org/' . $stock = ( $product_variation->is_in_stock() ? 'InStock' : 'OutOfStock' ),
|
|
|
|
'sku' => $product_variation->get_sku(),
|
|
|
|
'image' => wp_get_attachment_url( $product_variation->get_image_id() ),
|
2016-08-18 18:37:34 +00:00
|
|
|
'description' => $is_variable ? $product_variation->get_variation_description() : '',
|
2016-08-11 09:17:14 +00:00
|
|
|
'seller' => array(
|
|
|
|
'@type' => 'Organization',
|
|
|
|
'name' => get_bloginfo( 'name' ),
|
|
|
|
'url' => get_bloginfo( 'url' ),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-08-18 11:24:17 +00:00
|
|
|
$markup['description'] = get_the_excerpt( $product->get_id() );
|
2016-08-11 23:29:35 +00:00
|
|
|
$markup['offers'] = $markup_offers;
|
2016-08-11 09:17:14 +00:00
|
|
|
|
|
|
|
if ( $product->get_rating_count() ) {
|
2016-08-11 23:29:35 +00:00
|
|
|
$markup['aggregateRating'] = array(
|
2016-08-11 09:17:14 +00:00
|
|
|
'@type' => 'AggregateRating',
|
|
|
|
'ratingValue' => $product->get_average_rating(),
|
|
|
|
'ratingCount' => $product->get_rating_count(),
|
|
|
|
'reviewCount' => $product->get_review_count(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-08-11 23:29:35 +00:00
|
|
|
return $this->set_data( apply_filters( 'woocommerce_structured_data_product', $markup, $product ) );
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-14 22:39:01 +00:00
|
|
|
* Generates Review structured data.
|
2016-08-11 09:17:14 +00:00
|
|
|
*
|
2016-08-11 23:29:35 +00:00
|
|
|
* @uses `woocommerce_review_meta` action hook
|
2016-08-12 00:55:52 +00:00
|
|
|
* @param object $comment
|
2016-08-11 23:29:35 +00:00
|
|
|
* @return bool
|
2016-08-11 09:17:14 +00:00
|
|
|
*/
|
2016-08-14 22:39:01 +00:00
|
|
|
public function generate_review_data( $comment ) {
|
2016-08-12 00:55:52 +00:00
|
|
|
if ( ! is_object( $comment ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-11 23:29:35 +00:00
|
|
|
$markup['@type'] = 'Review';
|
2016-08-18 11:24:17 +00:00
|
|
|
$markup['@id'] = get_comment_link( $comment->comment_ID );
|
|
|
|
$markup['datePublished'] = get_comment_date( 'c', $comment->comment_ID );
|
|
|
|
$markup['description'] = get_comment_text( $comment->comment_ID );
|
2016-08-11 23:29:35 +00:00
|
|
|
$markup['itemReviewed'] = array(
|
2016-08-11 09:17:14 +00:00
|
|
|
'@type' => 'Product',
|
2016-08-18 11:24:17 +00:00
|
|
|
'name' => get_the_title( $comment->post_ID ),
|
2016-08-11 09:17:14 +00:00
|
|
|
);
|
2016-08-11 23:29:35 +00:00
|
|
|
$markup['reviewRating'] = array(
|
2016-08-11 09:17:14 +00:00
|
|
|
'@type' => 'rating',
|
2016-08-18 11:24:17 +00:00
|
|
|
'ratingValue' => get_comment_meta( $comment->comment_ID, 'rating', true ),
|
2016-08-11 09:17:14 +00:00
|
|
|
);
|
2016-08-11 23:29:35 +00:00
|
|
|
$markup['author'] = array(
|
2016-08-11 10:04:12 +00:00
|
|
|
'@type' => 'Person',
|
2016-08-18 11:24:17 +00:00
|
|
|
'name' => get_comment_author( $comment->comment_ID ),
|
2016-08-11 09:17:14 +00:00
|
|
|
);
|
|
|
|
|
2016-08-14 22:39:01 +00:00
|
|
|
return $this->set_data( apply_filters( 'woocommerce_structured_data_review', $markup, $comment ) );
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-11 23:29:35 +00:00
|
|
|
* Generates BreadcrumbList structured data.
|
2016-08-11 09:17:14 +00:00
|
|
|
*
|
2016-08-11 23:29:35 +00:00
|
|
|
* @uses `woocommerce_breadcrumb` action hook
|
2016-08-18 18:37:34 +00:00
|
|
|
* @param object $breadcrumbs
|
2016-08-12 00:55:52 +00:00
|
|
|
* @return bool|void
|
2016-08-11 09:17:14 +00:00
|
|
|
*/
|
2016-08-18 18:37:34 +00:00
|
|
|
public function generate_breadcrumblist_data( $breadcrumbs ) {
|
|
|
|
if ( ! is_object( $breadcrumbs ) ) {
|
2016-08-12 00:55:52 +00:00
|
|
|
return false;
|
2016-08-18 18:37:34 +00:00
|
|
|
} elseif ( ! $crumbs = $breadcrumbs->get_breadcrumb() ) {
|
2016-08-11 09:17:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-18 18:37:34 +00:00
|
|
|
foreach ( $crumbs as $key => $crumb ) {
|
2016-08-18 00:31:13 +00:00
|
|
|
$markup_crumbs[ $key ] = array(
|
2016-08-11 09:17:14 +00:00
|
|
|
'@type' => 'ListItem',
|
2016-08-18 18:57:11 +00:00
|
|
|
'position' => $key + 1,
|
2016-08-12 00:55:52 +00:00
|
|
|
'item' => array(
|
2016-08-18 00:31:13 +00:00
|
|
|
'name' => $crumb[0],
|
2016-08-12 00:55:52 +00:00
|
|
|
),
|
2016-08-11 09:17:14 +00:00
|
|
|
);
|
2016-08-18 00:31:13 +00:00
|
|
|
|
2016-08-18 18:37:34 +00:00
|
|
|
if ( ! empty( $crumb[1] ) && sizeof( $crumbs ) !== $key + 1 ) {
|
2016-08-18 00:31:13 +00:00
|
|
|
$markup_crumbs[ $key ]['item'] += array( '@id' => $crumb[1] );
|
|
|
|
}
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 23:29:35 +00:00
|
|
|
$markup['@type'] = 'BreadcrumbList';
|
|
|
|
$markup['itemListElement'] = $markup_crumbs;
|
2016-08-11 09:17:14 +00:00
|
|
|
|
2016-08-18 18:37:34 +00:00
|
|
|
return $this->set_data( apply_filters( 'woocommerce_structured_data_breadcrumblist', $markup, $breadcrumbs ) );
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-11 23:29:35 +00:00
|
|
|
* Generates WebSite structured data.
|
2016-08-11 12:58:05 +00:00
|
|
|
*
|
2016-08-11 23:29:35 +00:00
|
|
|
* @uses `woocommerce_before_main_content` action hook
|
|
|
|
* @return bool
|
2016-08-11 09:17:14 +00:00
|
|
|
*/
|
2016-08-11 23:29:35 +00:00
|
|
|
public function generate_website_data() {
|
|
|
|
$markup['@type'] = 'WebSite';
|
|
|
|
$markup['name'] = get_bloginfo( 'name' );
|
|
|
|
$markup['url'] = get_bloginfo( 'url' );
|
|
|
|
$markup['potentialAction'] = array(
|
2016-08-11 09:17:14 +00:00
|
|
|
'@type' => 'SearchAction',
|
|
|
|
'target' => get_bloginfo( 'url' ) . '/?s={search_term_string}&post_type=product',
|
|
|
|
'query-input' => 'required name=search_term_string',
|
|
|
|
);
|
|
|
|
|
2016-08-11 23:29:35 +00:00
|
|
|
return $this->set_data( apply_filters( 'woocommerce_structured_data_website', $markup ) );
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-14 22:39:01 +00:00
|
|
|
* Generates Order structured data.
|
2016-08-11 12:58:05 +00:00
|
|
|
*
|
2016-08-11 23:29:35 +00:00
|
|
|
* @uses `woocommerce_email_order_details` action hook
|
2016-08-12 20:58:23 +00:00
|
|
|
* @param object $order
|
|
|
|
* @param bool $sent_to_admin (default: false)
|
|
|
|
* @param bool $plain_text (default: false)
|
2016-08-12 00:55:52 +00:00
|
|
|
* @return bool|void
|
2016-08-10 22:23:26 +00:00
|
|
|
*/
|
2016-08-14 22:39:01 +00:00
|
|
|
public function generate_order_data( $order, $sent_to_admin = false, $plain_text = false ) {
|
2016-08-12 00:55:52 +00:00
|
|
|
if ( ! is_object( $order ) ) {
|
|
|
|
return false;
|
2016-08-18 18:37:34 +00:00
|
|
|
} elseif ( $plain_text ) {
|
2016-08-10 22:23:26 +00:00
|
|
|
return;
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
2016-08-10 22:23:26 +00:00
|
|
|
|
|
|
|
foreach ( $order->get_items() as $item ) {
|
|
|
|
if ( ! apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
|
|
|
|
continue;
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
2016-08-10 22:23:26 +00:00
|
|
|
|
|
|
|
$product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
|
|
|
|
$product_exists = is_object( $product );
|
|
|
|
$is_visible = $product_exists && $product->is_visible();
|
2016-08-11 09:17:14 +00:00
|
|
|
$order_url = $sent_to_admin ? admin_url( 'post.php?post=' . absint( $order->id ) . '&action=edit' ) : $order->get_view_order_url();
|
2016-08-10 22:23:26 +00:00
|
|
|
|
2016-08-11 23:29:35 +00:00
|
|
|
$markup_offers[] = array(
|
2016-08-10 22:23:26 +00:00
|
|
|
'@type' => 'Offer',
|
|
|
|
'price' => $order->get_line_subtotal( $item ),
|
2016-08-11 09:17:14 +00:00
|
|
|
'priceCurrency' => $order->get_currency(),
|
|
|
|
'priceSpecification' => array(
|
2016-08-11 10:04:12 +00:00
|
|
|
'price' => $order->get_line_subtotal( $item ),
|
|
|
|
'priceCurrency' => $order->get_currency(),
|
|
|
|
'eligibleQuantity' => array(
|
2016-08-11 09:17:14 +00:00
|
|
|
'@type' => 'QuantitativeValue',
|
|
|
|
'value' => apply_filters( 'woocommerce_email_order_item_quantity', $item['qty'], $item ),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'itemOffered' => array(
|
|
|
|
'@type' => 'Product',
|
|
|
|
'name' => apply_filters( 'woocommerce_order_item_name', $item['name'], $item, $is_visible ),
|
|
|
|
'sku' => $product_exists ? $product->get_sku() : '',
|
|
|
|
'image' => $product_exists ? wp_get_attachment_image_url( $product->get_image_id() ) : '',
|
|
|
|
'url' => $is_visible ? get_permalink( $product->get_id() ) : get_home_url(),
|
|
|
|
),
|
|
|
|
'seller' => array(
|
|
|
|
'@type' => 'Organization',
|
|
|
|
'name' => get_bloginfo( 'name' ),
|
|
|
|
'url' => get_bloginfo( 'url' ),
|
|
|
|
),
|
2016-08-10 22:23:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( $order->get_status() ) {
|
|
|
|
case 'pending':
|
|
|
|
$order_status = 'http://schema.org/OrderPaymentDue';
|
|
|
|
break;
|
|
|
|
case 'processing':
|
|
|
|
$order_status = 'http://schema.org/OrderProcessing';
|
|
|
|
break;
|
|
|
|
case 'on-hold':
|
|
|
|
$order_status = 'http://schema.org/OrderProblem';
|
|
|
|
break;
|
|
|
|
case 'completed':
|
|
|
|
$order_status = 'http://schema.org/OrderDelivered';
|
|
|
|
break;
|
|
|
|
case 'cancelled':
|
|
|
|
$order_status = 'http://schema.org/OrderCancelled';
|
|
|
|
break;
|
|
|
|
case 'refunded':
|
|
|
|
$order_status = 'http://schema.org/OrderReturned';
|
|
|
|
break;
|
|
|
|
case 'failed':
|
|
|
|
$order_status = 'http://schema.org/OrderProblem';
|
|
|
|
break;
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
2016-08-10 22:23:26 +00:00
|
|
|
|
2016-08-11 23:29:35 +00:00
|
|
|
$markup['@type'] = 'Order';
|
2016-08-18 11:24:17 +00:00
|
|
|
$markup['url'] = $order_url;
|
2016-08-11 23:29:35 +00:00
|
|
|
$markup['orderStatus'] = $order_status;
|
|
|
|
$markup['orderNumber'] = $order->get_order_number();
|
|
|
|
$markup['orderDate'] = date( 'c', $order->get_date_created() );
|
|
|
|
$markup['acceptedOffer'] = $markup_offers;
|
|
|
|
$markup['discount'] = $order->get_total_discount();
|
|
|
|
$markup['discountCurrency'] = $order->get_currency();
|
|
|
|
$markup['price'] = $order->get_total();
|
|
|
|
$markup['priceCurrency'] = $order->get_currency();
|
|
|
|
$markup['priceSpecification'] = array(
|
2016-08-11 09:17:14 +00:00
|
|
|
'price' => $order->get_total(),
|
|
|
|
'priceCurrency' => $order->get_currency(),
|
|
|
|
'valueAddedTaxIncluded' => true,
|
|
|
|
);
|
2016-08-11 23:29:35 +00:00
|
|
|
$markup['billingAddress'] = array(
|
2016-08-11 09:17:14 +00:00
|
|
|
'@type' => 'PostalAddress',
|
|
|
|
'name' => $order->get_formatted_billing_full_name(),
|
|
|
|
'streetAddress' => $order->get_billing_address_1(),
|
|
|
|
'postalCode' => $order->get_billing_postcode(),
|
|
|
|
'addressLocality' => $order->get_billing_city(),
|
|
|
|
'addressRegion' => $order->get_billing_state(),
|
|
|
|
'addressCountry' => $order->get_billing_country(),
|
|
|
|
'email' => $order->get_billing_email(),
|
|
|
|
'telephone' => $order->get_billing_phone(),
|
|
|
|
);
|
2016-08-11 23:29:35 +00:00
|
|
|
$markup['customer'] = array(
|
2016-08-11 09:17:14 +00:00
|
|
|
'@type' => 'Person',
|
|
|
|
'name' => $order->get_formatted_billing_full_name(),
|
|
|
|
);
|
2016-08-11 23:29:35 +00:00
|
|
|
$markup['merchant'] = array(
|
2016-08-10 22:23:26 +00:00
|
|
|
'@type' => 'Organization',
|
2016-08-11 09:17:14 +00:00
|
|
|
'name' => get_bloginfo( 'name' ),
|
|
|
|
'url' => get_bloginfo( 'url' ),
|
2016-08-10 22:23:26 +00:00
|
|
|
);
|
2016-08-11 23:29:35 +00:00
|
|
|
$markup['potentialAction'] = array(
|
2016-08-10 22:23:26 +00:00
|
|
|
'@type' => 'ViewAction',
|
2016-08-11 09:17:14 +00:00
|
|
|
'name' => 'View Order',
|
|
|
|
'url' => $order_url,
|
2016-08-10 22:23:26 +00:00
|
|
|
'target' => $order_url,
|
|
|
|
);
|
|
|
|
|
2016-08-14 22:39:01 +00:00
|
|
|
return $this->set_data( apply_filters( 'woocommerce_structured_data_order', $markup, $sent_to_admin, $order ), true );
|
2016-08-10 22:23:26 +00:00
|
|
|
}
|
2016-07-17 02:42:46 +00:00
|
|
|
}
|