improved structured data - email order structured data integration +

WC_Structured_Data refactor + coding standards tweaks
This commit is contained in:
opportus 2016-08-11 00:23:26 +02:00
parent a918db0983
commit b61aae01bf
26 changed files with 402 additions and 279 deletions

View File

@ -109,7 +109,6 @@ class WC_Emails {
add_action( 'woocommerce_email_header', array( $this, 'email_header' ) ); add_action( 'woocommerce_email_header', array( $this, 'email_header' ) );
add_action( 'woocommerce_email_footer', array( $this, 'email_footer' ) ); add_action( 'woocommerce_email_footer', array( $this, 'email_footer' ) );
add_action( 'woocommerce_email_order_details', array( $this, 'order_details' ), 10, 4 ); add_action( 'woocommerce_email_order_details', array( $this, 'order_details' ), 10, 4 );
add_action( 'woocommerce_email_order_details', array( $this, 'order_schema_markup' ), 20, 4 );
add_action( 'woocommerce_email_order_meta', array( $this, 'order_meta' ), 10, 3 ); add_action( 'woocommerce_email_order_meta', array( $this, 'order_meta' ), 10, 3 );
add_action( 'woocommerce_email_customer_details', array( $this, 'customer_details' ), 10, 3 ); add_action( 'woocommerce_email_customer_details', array( $this, 'customer_details' ), 10, 3 );
add_action( 'woocommerce_email_customer_details', array( $this, 'email_addresses' ), 20, 3 ); add_action( 'woocommerce_email_customer_details', array( $this, 'email_addresses' ), 20, 3 );
@ -269,116 +268,6 @@ class WC_Emails {
} }
} }
/**
* Adds Schema.org markup for order in JSON-LD format.
*
* @since 2.6.0
* @param mixed $order
* @param bool $sent_to_admin (default: false)
* @param bool $plain_text (default: false)
*/
public function order_schema_markup( $order, $sent_to_admin = false, $plain_text = false ) {
if ( $plain_text ) {
return;
}
$accepted_offers = array();
foreach ( $order->get_items() as $item ) {
if ( ! apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
continue;
}
$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();
$item_offered = array(
'@type' => 'Product',
'name' => apply_filters( 'woocommerce_order_item_name', $item['name'], $item, $is_visible ),
);
if ( $product_exists ) {
if ( $sku = $product->get_sku() ) {
$item_offered['sku'] = $sku;
}
if ( $image_id = $product->get_image_id() ) {
$item_offered['image'] = wp_get_attachment_image_url( $image_id, 'thumbnail' );
}
}
if ( $is_visible ) {
$item_offered['url'] = get_permalink( $product->get_id() );
} else {
$item_offered['url'] = get_home_url();
}
$accepted_offer = (object) array(
'@type' => 'Offer',
'itemOffered' => $item_offered,
'price' => $order->get_line_subtotal( $item ),
'priceCurrency' => $order->get_order_currency(),
'eligibleQuantity' => (object) array(
'@type' => 'QuantitativeValue',
'value' => apply_filters( 'woocommerce_email_order_item_quantity', $item['qty'], $item )
),
'url' => get_home_url(),
);
$accepted_offers[] = $accepted_offer;
}
$markup = array(
'@context' => 'http://schema.org',
'@type' => 'Order',
'merchant' => (object) array(
'@type' => 'Organization',
'name' => get_bloginfo( 'name' ),
),
'orderNumber' => strval( $order->get_order_number() ),
'priceCurrency' => $order->get_order_currency(),
'price' => $order->get_total(),
'acceptedOffer' => $accepted_offers,
'url' => $order->get_view_order_url(),
);
switch ( $order->get_status() ) {
case 'pending':
$markup['orderStatus'] = 'http://schema.org/OrderPaymentDue';
break;
case 'processing':
$markup['orderStatus'] = 'http://schema.org/OrderProcessing';
break;
case 'on-hold':
$markup['orderStatus'] = 'http://schema.org/OrderProblem';
break;
case 'completed':
$markup['orderStatus'] = 'http://schema.org/OrderDelivered';
break;
case 'cancelled':
$markup['orderStatus'] = 'http://schema.org/OrderCancelled';
break;
case 'refunded':
$markup['orderStatus'] = 'http://schema.org/OrderReturned';
break;
case 'failed':
$markup['orderStatus'] = 'http://schema.org/OrderProblem';
break;
}
if ( $sent_to_admin ) {
$markup['potentialAction'] = (object) array(
'@type' => 'ViewAction',
'target' => admin_url( 'post.php?post=' . absint( $order->id ) . '&action=edit' ),
);
}
$markup = apply_filters( 'woocommerce_email_order_schema_markup', $markup, $sent_to_admin, $order );
echo '<div style="display:none;"><script type="application/ld+json">' . wp_json_encode( (object) $markup ) . '</script></div>';
}
/** /**
* Add order meta to email templates. * Add order meta to email templates.
* *
@ -564,5 +453,22 @@ class WC_Emails {
apply_filters( 'woocommerce_email_headers', '', 'backorder', $args ), apply_filters( 'woocommerce_email_headers', '', 'backorder', $args ),
apply_filters( 'woocommerce_email_attachments', array(), 'backorder', $args ) apply_filters( 'woocommerce_email_attachments', array(), 'backorder', $args )
); );
} }
/**
* Adds Schema.org markup for order in JSON-LD format.
*
* @deprecated 2.7.0 See WC_Structured_Data class
*
* @since 2.6.0
* @param mixed $order
* @param bool $sent_to_admin (default: false)
* @param bool $plain_text (default: false)
*/
public function order_schema_markup( $order, $sent_to_admin = false, $plain_text = false ) {
_deprecated_function( 'WC_Emails::order_schema_markup', '2.7', 'WC_Structured_Data::generate_email_order_data' );
WC()->structured_data->generate_email_order_data( $order, $sent_to_admin, $plain_text );
WC()->structured_data->enqueue_data( array( 'Order' ) );
}
} }

View File

@ -16,101 +16,160 @@ if ( ! defined( 'ABSPATH' ) ) {
class WC_Structured_Data { class WC_Structured_Data {
/** /**
* @var array Partially structured data * @var array Partially structured data from `generate_*` methods
*/ */
private $data; private $_data;
/** /**
* Checks if the passed $json variable is an array and stores it into $this->data... * @var array Structured data
*
* @param array $json Partially structured data
* @return bool Returns false If the param $json is not an array
*/ */
private function set_data( $json ) { private $_structured_data;
if ( ! is_array( $json ) ) {
return false;
}
$this->data[] = $json;
}
/** /**
* Structures and returns the data... * Constructor.
*
* @return array If data is set, returns the structured data, otherwise returns empty array
*/
private function get_data() {
if ( ! $this->data ) {
return array();
}
foreach ( $this->data as $value ) {
$type = isset( $value['@type'] ) ? $value['@type'] : false;
if ( 'Product' === $type || 'SoftwareApplication' === $type || 'MusicAlbum' === $type ) {
$products[] = $value;
} elseif ( 'Review' === $type ) {
$reviews[] = $value;
} elseif ( 'WebSite' === $type || 'BreadcrumbList' === $type ) {
$data[] = $value;
}
}
if ( isset( $products ) ) {
if ( count( $products ) > 1 ) {
$data[] = array( '@graph' => $products );
} else {
$data[] = isset( $reviews ) ? $products[0] + array( 'review' => $reviews ) : $products[0];
}
}
if ( ! isset( $data ) ) {
return array();
}
$context['@context'] = apply_filters( 'woocommerce_structured_data_context', 'http://schema.org/' );
foreach( $data as $key => $value ) {
$data[ $key ] = $context + $value;
}
if ( count( $data ) > 1 ) {
return $data = array( '@graph' => $data );
} else {
return $data[0];
}
}
/**
* Contructor
*/ */
public function __construct() { public function __construct() {
add_action( 'woocommerce_before_main_content', array( $this, 'generate_shop_data' ) ); // Generate data...
add_action( 'woocommerce_before_main_content', array( $this, 'generate_shop_data' ), 30 );
add_action( 'woocommerce_breadcrumb', array( $this, 'generate_breadcrumb_data' ), 10, 1 ); add_action( 'woocommerce_breadcrumb', array( $this, 'generate_breadcrumb_data' ), 10, 1 );
add_action( 'woocommerce_before_shop_loop_item', array( $this, 'generate_product_category_data' ) ); add_action( 'woocommerce_before_shop_loop_item', array( $this, 'generate_product_category_data' ), 20 );
add_action( 'woocommerce_single_product_summary', array( $this, 'generate_product_data' ) ); add_action( 'woocommerce_single_product_summary', array( $this, 'generate_product_data' ), 50 );
add_action( 'woocommerce_review_meta', array( $this, 'generate_product_review_data' ), 10, 1 ); add_action( 'woocommerce_review_meta', array( $this, 'generate_product_review_data' ), 20, 1 );
add_action( 'woocommerce_email_order_details', array( $this, 'generate_email_order_data' ), 20, 4 );
// Enqueue structured data...
add_action( 'woocommerce_email_order_details', array( $this, 'enqueue_data' ), 30 );
add_action( 'wp_footer', array( $this, 'enqueue_data' ) ); add_action( 'wp_footer', array( $this, 'enqueue_data' ) );
} }
/** /**
* Sanitizes, encodes and echoes the structured data into `wp_footer` action hook. * Sets `$this->_data` after `$json` validation.
*
* @param array $json Partially structured data from `generate_*` methods
* @param bool $overwrite (default: false)
* @return bool `false` if invalid `$json`, otherwise `true`
*/ */
public function enqueue_data() { public function set_data( $json, $overwrite = false ) {
if ( $data = $this->get_data() ) { if ( ! is_array( $json ) || ! array_key_exists( '@type', $json ) ) {
$data = $this->sanitize_data( $data ); return false;
}
echo '<script type="application/ld+json">' . wp_json_encode( $data ) . '</script>'; if ( $overwrite && isset( $this->_data ) ) {
unset( $this->_data );
}
$this->_data[] = $json;
return true;
}
/**
* Gets `$this->_data`.
*
* @return array $data Or empty array if `$this->_data` is not set
*/
public function get_data() {
$data = isset( $this->_data ) ? $this->_data : array();
return $data;
}
/**
* Sets `$this->_structured_data`.
*
* @return bool `false` if there is no `$this->_data` to structure, otherwise `true`
*/
public function set_structured_data() {
if ( ! isset( $this->_data ) ) {
return false;
}
foreach ( $this->get_data() as $value ) {
$type = $value['@type'];
switch ( $type ) {
case 'MusicAlbum':
case 'SoftwareApplication':
$type = 'Product';
break;
}
$data[ $type ][] = $value;
}
foreach ( $data as $type => $value ) {
if ( count( $value ) > 1 ) {
$data[ $type ] = array( '@graph' => $value );
} else {
$data[ $type ] = $value[0];
}
$data[ $type ] = apply_filters( 'woocommerce_structured_data_context', array( '@context' => 'http://schema.org/' ), $data, $type, $value ) + $data[ $type ];
}
$this->_structured_data = $data;
return true;
}
/**
* Gets `$this->_structured_data` after `$requested_types` validation.
*
* @param mixed $requested_types bool|array (default: false) Array of requested types
* @return array $structured_data Or empty array if there is no structured data or if `$requested_types` is not valid
*/
public function get_structured_data( $requested_types = false ) {
if ( ! $this->set_structured_data() ) {
return array();
} elseif ( $requested_types && ! is_array( $requested_types ) ) {
return array();
}
if ( $requested_types ) {
foreach ( $this->_structured_data as $type => $value ) {
foreach ( $requested_types as $requested_type ) {
if ( $requested_type === $type ) {
$structured_data[] = $value;
}
}
}
} else {
foreach ( $this->_structured_data as $value ) {
$structured_data[] = $value;
}
}
if ( count( $structured_data ) > 1 ) {
return $structured_data = array( '@graph' => $structured_data );
} else {
return $structured_data[0];
} }
} }
/** /**
* Sanitizes the structured data. * Sanitizes, encodes and echoes structured data.
* Hooked into the `wp_footer` action hook.
* Hooked into the `woocommerce_email_order_details` action hook.
*/
public function enqueue_data( $requested_types = false ) {
if ( $structured_data = $this->sanitize_data( $this->get_structured_data( $requested_types ) ) ) {
// Testing/Debugging
//echo json_encode( $structured_data, JSON_UNESCAPED_SLASHES );
echo '<script type="application/ld+json">' . wp_json_encode( $structured_data ) . '</script>';
}
}
/**
* Sanitizes structured data.
* *
* @param array $data * @param array $data
* @return array $sanitized_data * @return array $sanitized_data Or empty array if there is no data to sanitize
*/ */
private function sanitize_data( $data ) { private function sanitize_data( $data ) {
if ( ! $data ) {
return array();
}
foreach ( $data as $key => $value ) { foreach ( $data as $key => $value ) {
$sanitized_data[ sanitize_text_field( $key ) ] = is_array( $value ) ? $this->sanitize_data( $value ) : sanitize_text_field( $value ); $sanitized_data[ sanitize_text_field( $key ) ] = is_array( $value ) ? $this->sanitize_data( $value ) : sanitize_text_field( $value );
} }
@ -119,8 +178,8 @@ class WC_Structured_Data {
} }
/** /**
* Generates the product category structured data... * Generates structured data for product categories.
* Hooked into the `woocommerce_before_shop_loop_item` action hook... * Hooked into the `woocommerce_before_shop_loop_item` action hook.
*/ */
public function generate_product_category_data() { public function generate_product_category_data() {
if ( ! is_product_category() && ! is_shop() ) { if ( ! is_product_category() && ! is_shop() ) {
@ -131,13 +190,37 @@ class WC_Structured_Data {
} }
/** /**
* Generates the product structured data... * Generates structured data for single products.
* Hooked into the `woocommerce_single_product_summary` action hook... * Hooked into the `woocommerce_single_product_summary` action hook.
* Applies the `woocommerce_structured_data_product` filter hook for clean structured data customization...
*/ */
public function generate_product_data() { public function generate_product_data() {
global $product; global $product;
if ( $is_multi_variation = count( $product->get_children() ) > 1 ? true : false ) {
$variations = $product->get_available_variations();
} else {
$variations = array( null );
}
foreach ( $variations as $variation ) {
$product_variation = $is_multi_variation ? wc_get_product( $variation['variation_id'] ) : $product;
$json_offers[] = array(
'@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() ),
'description' => $is_multi_variation ? $product_variation->get_variation_description() : '',
'seller' => array(
'@type' => 'Organization',
'name' => get_bloginfo( 'name' ),
'url' => get_bloginfo( 'url' ),
),
);
}
if ( $product->is_downloadable() ) { if ( $product->is_downloadable() ) {
switch ( $product->download_type ) { switch ( $product->download_type ) {
case 'application' : case 'application' :
@ -154,77 +237,58 @@ class WC_Structured_Data {
$type = "Product"; $type = "Product";
} }
$json['@type'] = $type; $json['@type'] = $type;
$json['@id'] = get_the_permalink(); $json['@id'] = get_the_permalink();
$json['name'] = get_the_title(); $json['name'] = get_the_title();
$json['description'] = get_the_excerpt(); $json['description'] = get_the_excerpt();
$json['url'] = get_the_permalink(); $json['url'] = get_the_permalink();
$json['offers'] = $json_offers;
if ( $product->get_rating_count() ) { if ( $product->get_rating_count() ) {
$json['aggregateRating'] = array( $json['aggregateRating'] = array(
'@type' => 'AggregateRating', '@type' => 'AggregateRating',
'ratingValue' => $product->get_average_rating(), 'ratingValue' => $product->get_average_rating(),
'ratingCount' => $product->get_rating_count(), 'ratingCount' => $product->get_rating_count(),
'reviewCount' => $product->get_review_count() 'reviewCount' => $product->get_review_count(),
); );
} }
if ( $is_multi_variation = count( $product->get_children() ) > 1 ? true : false ) {
$variations = $product->get_available_variations();
} else {
$variations = array( null );
}
foreach ( $variations as $variation ) {
$product_variation = $is_multi_variation ? wc_get_product( $variation['variation_id'] ) : $product;
$json_offers[] = array(
'@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() ),
'description' => $is_multi_variation ? $product_variation->get_variation_description() : ''
);
}
$json['offers'] = $json_offers;
$this->set_data( apply_filters( 'woocommerce_structured_data_product', $json, $product ) ); $this->set_data( apply_filters( 'woocommerce_structured_data_product', $json, $product ) );
} }
/** /**
* Generates the product review structured data... * Generates structured data for product reviews.
* Hooked into the `woocommerce_review_meta` action hook... * Hooked into the `woocommerce_review_meta` action hook.
* Applies the `woocommerce_structured_data_product_review` filter hook for clean structured data customization...
* *
* @param object $comment From `woocommerce_review_meta` action hook * @param object $comment
*/ */
public function generate_product_review_data( $comment ) { public function generate_product_review_data( $comment ) {
$json['@type'] = 'Review'; $json['@type'] = 'Review';
$json['@id'] = get_the_permalink() . '#li-comment-' . get_comment_ID(); $json['@id'] = get_the_permalink() . '#li-comment-' . get_comment_ID();
$json['datePublished'] = get_comment_date( 'c' ); $json['datePublished'] = get_comment_date( 'c' );
$json['description'] = get_comment_text(); $json['description'] = get_comment_text();
$json['reviewRating'] = array( $json['itemReviewed'] = array(
'@type' => 'rating', '@type' => 'Product',
'ratingValue' => intval( get_comment_meta( $comment->comment_ID, 'rating', true ) ) 'name' => get_the_title(),
); );
$json['author'] = array( $json['reviewRating'] = array(
'@type' => 'Person', '@type' => 'rating',
'name' => get_comment_author() 'ratingValue' => intval( get_comment_meta( $comment->comment_ID, 'rating', true ) ),
);
$json['author'] = array(
'@type' => 'Person',
'name' => get_comment_author(),
); );
$this->set_data( apply_filters( 'woocommerce_structured_data_product_review', $json, $comment ) ); $this->set_data( apply_filters( 'woocommerce_structured_data_product_review', $json, $comment ) );
} }
/** /**
* Generates the breadcrumbs structured data... * Generates structured data for the breadcrumb.
* Hooked into the `woocommerce_breadcrumb` action hook... * Hooked into the `woocommerce_breadcrumb` action hook.
* Applies the `woocommerce_structured_data_breadcrumb` filter hook for clean structured data customization...
* *
* @param array $args From `woocommerce_breadcrumb` action hook * @param array $args
*/ */
public function generate_breadcrumb_data( $args ) { public function generate_breadcrumb_data( $args ) {
if ( empty( $args['breadcrumb'] ) ) { if ( empty( $args['breadcrumb'] ) ) {
@ -236,48 +300,166 @@ class WC_Structured_Data {
foreach ( $breadcrumb as $key => $value ) { foreach ( $breadcrumb as $key => $value ) {
if ( ! empty( $value[1] ) && sizeof( $breadcrumb ) !== $key + 1 ) { if ( ! empty( $value[1] ) && sizeof( $breadcrumb ) !== $key + 1 ) {
$json_crumbs_item = array( $json_crumbs_item = array(
'@id' => $value[1], '@id' => $value[1],
'name' => $value[0] 'name' => $value[0],
); );
} else { } else {
$json_crumbs_item = array( $json_crumbs_item = array(
'name' => $value[0] 'name' => $value[0]
); );
} }
$json_crumbs[] = array( $json_crumbs[] = array(
'@type' => 'ListItem', '@type' => 'ListItem',
'position' => $position ++, 'position' => $position ++,
'item' => $json_crumbs_item 'item' => $json_crumbs_item,
); );
} }
$json['@type'] = 'BreadcrumbList'; $json['@type'] = 'BreadcrumbList';
$json['itemListElement'] = $json_crumbs; $json['itemListElement'] = $json_crumbs;
$this->set_data( apply_filters( 'woocommerce_structured_data_breadcrumb', $json, $breadcrumb ) ); $this->set_data( apply_filters( 'woocommerce_structured_data_breadcrumb', $json, $breadcrumb ) );
} }
/** /**
* Generates the shop related structured data... * Generates structured data related to the shop.
* Hooked into the `woocommerce_before_main_content` action hook... * Hooked into the `woocommerce_before_main_content` action hook.
* Applies the `woocommerce_structured_data_shop` filter hook for clean structured data customization...
*/ */
public function generate_shop_data() { public function generate_shop_data() {
if ( ! is_shop() || ! is_front_page() ) { if ( ! is_shop() || ! is_front_page() ) {
return; return;
} }
$json['@type'] = 'WebSite'; $json['@type'] = 'WebSite';
$json['name'] = get_bloginfo( 'name' ); $json['name'] = get_bloginfo( 'name' );
$json['url'] = get_bloginfo( 'url' ); $json['url'] = get_bloginfo( 'url' );
$json['potentialAction'] = array( $json['potentialAction'] = array(
'@type' => 'SearchAction', '@type' => 'SearchAction',
'target' => get_bloginfo( 'url' ) . '/?s={search_term_string}&post_type=product', 'target' => get_bloginfo( 'url' ) . '/?s={search_term_string}&post_type=product',
'query-input' => 'required name=search_term_string' 'query-input' => 'required name=search_term_string',
); );
$this->set_data( apply_filters( 'woocommerce_structured_data_shop', $json ) ); $this->set_data( apply_filters( 'woocommerce_structured_data_shop', $json ) );
} }
/**
* Generates structured data for the email order.
* Hooked into the `woocommerce_email_order_details` action hook.
*
* @param mixed $order
* @param bool $sent_to_admin (default: false)
* @param bool $plain_text (default: false)
*/
public function generate_email_order_data( $order, $sent_to_admin = false, $plain_text = false ) {
if ( $plain_text ) {
return;
}
foreach ( $order->get_items() as $item ) {
if ( ! apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
continue;
}
$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();
$order_url = $sent_to_admin ? admin_url( 'post.php?post=' . absint( $order->id ) . '&action=edit' ) : $order->get_view_order_url();
$json_offers[] = array(
'@type' => 'Offer',
'price' => $order->get_line_subtotal( $item ),
'priceCurrency' => $order->get_currency(),
'priceSpecification' => array(
'price' => $order->get_line_subtotal( $item ),
'priceCurrency' => $order->get_currency(),
'eligibleQuantity' => array(
'@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' ),
),
);
}
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;
}
$json['@type'] = 'Order';
$json['orderStatus'] = $order_status;
$json['orderNumber'] = $order->get_order_number();
$json['orderDate'] = date( 'c', $order->get_date_created() );
$json['url'] = $order_url;
$json['acceptedOffer'] = $json_offers;
$json['discount'] = $order->get_total_discount();
$json['discountCurrency'] = $order->get_currency();
$json['price'] = $order->get_total();
$json['priceCurrency'] = $order->get_currency();
$json['priceSpecification'] = array(
'price' => $order->get_total(),
'priceCurrency' => $order->get_currency(),
'valueAddedTaxIncluded' => true,
);
$json['billingAddress'] = array(
'@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(),
);
$json['customer'] = array(
'@type' => 'Person',
'name' => $order->get_formatted_billing_full_name(),
);
$json['merchant'] = array(
'@type' => 'Organization',
'name' => get_bloginfo( 'name' ),
'url' => get_bloginfo( 'url' ),
);
$json['potentialAction'] = array(
'@type' => 'ViewAction',
'name' => 'View Order',
'url' => $order_url,
'target' => $order_url,
);
$this->set_data( apply_filters( 'woocommerce_structured_data_email_order', $json, $sent_to_admin, $order ), true );
}
} }

View File

@ -509,7 +509,8 @@ global $wc_map_deprecated_filters;
$wc_map_deprecated_filters = array( $wc_map_deprecated_filters = array(
'woocommerce_add_to_cart_fragments' => 'add_to_cart_fragments', 'woocommerce_add_to_cart_fragments' => 'add_to_cart_fragments',
'woocommerce_add_to_cart_redirect' => 'add_to_cart_redirect' 'woocommerce_add_to_cart_redirect' => 'add_to_cart_redirect',
'woocommerce_structured_data_email_order' => 'woocommerce_email_order_schema_markup'
); );
foreach ( $wc_map_deprecated_filters as $new => $old ) { foreach ( $wc_map_deprecated_filters as $new => $old ) {
@ -740,10 +741,13 @@ function woocommerce_calc_shipping_backwards_compatibility( $value ) {
add_filter( 'pre_option_woocommerce_calc_shipping', 'woocommerce_calc_shipping_backwards_compatibility' ); add_filter( 'pre_option_woocommerce_calc_shipping', 'woocommerce_calc_shipping_backwards_compatibility' );
/** /**
* @deprecated see `class-wc-structured-data.php` * @deprecated 2.7.0 See WC_Structured_Data class
*
* @return string * @return string
*/ */
function woocommerce_get_product_schema() { function woocommerce_get_product_schema() {
_deprecated_function( 'woocommerce_get_product_schema', '2.7' );
global $product; global $product;
$schema = "Product"; $schema = "Product";

View File

@ -28,6 +28,7 @@ get_header( 'shop' ); ?>
* *
* @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content) * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
* @hooked woocommerce_breadcrumb - 20 * @hooked woocommerce_breadcrumb - 20
* @hooked WC_Structured_data::generate_shop_data() - 30
*/ */
do_action( 'woocommerce_before_main_content' ); do_action( 'woocommerce_before_main_content' );
?> ?>

View File

@ -33,6 +33,7 @@ if ( empty( $product ) || ! $product->is_visible() ) {
* woocommerce_before_shop_loop_item hook. * woocommerce_before_shop_loop_item hook.
* *
* @hooked woocommerce_template_loop_product_link_open - 10 * @hooked woocommerce_template_loop_product_link_open - 10
* @hooked WC_Structured_Data::generate_product_category_data() - 20
*/ */
do_action( 'woocommerce_before_shop_loop_item' ); do_action( 'woocommerce_before_shop_loop_item' );

View File

@ -61,6 +61,7 @@ if ( ! defined( 'ABSPATH' ) ) {
* @hooked woocommerce_template_single_add_to_cart - 30 * @hooked woocommerce_template_single_add_to_cart - 30
* @hooked woocommerce_template_single_meta - 40 * @hooked woocommerce_template_single_meta - 40
* @hooked woocommerce_template_single_sharing - 50 * @hooked woocommerce_template_single_sharing - 50
* @hooked WC_Structured_Data::generate_product_data() - 50
*/ */
do_action( 'woocommerce_single_product_summary' ); do_action( 'woocommerce_single_product_summary' );
?> ?>

View File

@ -31,7 +31,8 @@
/** /**
* @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @hooked WC_Structured_Data::generate_email_order_data() Generates structured data.
* @hooked WC_Structured_Data::enqueue_data() Enqueues structured data.
* @since 2.5.0 * @since 2.5.0
*/ */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

View File

@ -31,7 +31,8 @@
/** /**
* @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @hooked WC_Structured_Data::generate_email_order_data() Generates structured data.
* @hooked WC_Structured_Data::enqueue_data() Enqueues structured data.
* @since 2.5.0 * @since 2.5.0
*/ */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

View File

@ -31,7 +31,8 @@
/** /**
* @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @hooked WC_Structured_Data::generate_email_order_data() Generates structured data.
* @hooked WC_Structured_Data::enqueue_data() Enqueues structured data.
* @since 2.5.0 * @since 2.5.0
*/ */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

View File

@ -31,7 +31,8 @@ do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
/** /**
* @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @hooked WC_Structured_Data::generate_email_order_data() Generates structured data.
* @hooked WC_Structured_Data::enqueue_data() Enqueues structured data.
* @since 2.5.0 * @since 2.5.0
*/ */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

View File

@ -33,7 +33,8 @@ do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
/** /**
* @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @hooked WC_Structured_Data::generate_email_order_data() Generates structured data.
* @hooked WC_Structured_Data::enqueue_data() Enqueues structured data.
* @since 2.5.0 * @since 2.5.0
*/ */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

View File

@ -35,7 +35,8 @@ do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
/** /**
* @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @hooked WC_Structured_Data::generate_email_order_data() Generates structured data.
* @hooked WC_Structured_Data::enqueue_data() Enqueues structured data.
* @since 2.5.0 * @since 2.5.0
*/ */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

View File

@ -31,6 +31,8 @@ do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
/** /**
* @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Structured_Data::generate_email_order_data() Generates structured data.
* @hooked WC_Structured_Data::enqueue_data() Enqueues structured data.
* @since 2.5.0 * @since 2.5.0
*/ */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

View File

@ -31,7 +31,8 @@ do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
/** /**
* @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @hooked WC_Structured_Data::generate_email_order_data() Generates structured data.
* @hooked WC_Structured_Data::enqueue_data() Enqueues structured data.
* @since 2.5.0 * @since 2.5.0
*/ */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

View File

@ -38,7 +38,8 @@ do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
/** /**
* @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @hooked WC_Structured_Data::generate_email_order_data() Generates structured data.
* @hooked WC_Structured_Data::enqueue_data() Enqueues structured data.
* @since 2.5.0 * @since 2.5.0
*/ */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

View File

@ -28,7 +28,8 @@ echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n"
/** /**
* @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @hooked WC_Structured_Data::generate_email_order_data() Generates structured data.
* @hooked WC_Structured_Data::enqueue_data() Enqueues structured data.
* @since 2.5.0 * @since 2.5.0
*/ */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

View File

@ -28,7 +28,8 @@ echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n"
/** /**
* @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @hooked WC_Structured_Data::generate_email_order_data() Generates structured data.
* @hooked WC_Structured_Data::enqueue_data() Enqueues structured data.
* @since 2.5.0 * @since 2.5.0
*/ */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

View File

@ -28,7 +28,8 @@ echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n"
/** /**
* @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @hooked WC_Structured_Data::generate_email_order_data() Generates structured data.
* @hooked WC_Structured_Data::enqueue_data() Enqueues structured data.
* @since 2.5.0 * @since 2.5.0
*/ */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

View File

@ -28,7 +28,8 @@ echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n"
/** /**
* @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @hooked WC_Structured_Data::generate_email_order_data() Generates structured data.
* @hooked WC_Structured_Data::enqueue_data() Enqueues structured data.
* @since 2.5.0 * @since 2.5.0
*/ */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

View File

@ -30,7 +30,8 @@ echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n"
/** /**
* @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @hooked WC_Structured_Data::generate_email_order_data() Generates structured data.
* @hooked WC_Structured_Data::enqueue_data() Enqueues structured data.
* @since 2.5.0 * @since 2.5.0
*/ */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

View File

@ -36,7 +36,8 @@ echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n"
/** /**
* @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @hooked WC_Structured_Data::generate_email_order_data() Generates structured data.
* @hooked WC_Structured_Data::enqueue_data() Enqueues structured data.
* @since 2.5.0 * @since 2.5.0
*/ */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

View File

@ -28,6 +28,8 @@ echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n"
/** /**
* @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Structured_Data::generate_email_order_data() Generates structured data.
* @hooked WC_Structured_Data::enqueue_data() Enqueues structured data.
* @since 2.5.0 * @since 2.5.0
*/ */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

View File

@ -28,7 +28,8 @@ echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n"
/** /**
* @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @hooked WC_Structured_Data::generate_email_order_data() Generates structured data.
* @hooked WC_Structured_Data::enqueue_data() Enqueues structured data.
* @since 2.5.0 * @since 2.5.0
*/ */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

View File

@ -28,7 +28,8 @@ echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n"
/** /**
* @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @hooked WC_Structured_Data::generate_email_order_data() Generates structured data.
* @hooked WC_Structured_Data::enqueue_data() Enqueues structured data.
* @since 2.5.0 * @since 2.5.0
*/ */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

View File

@ -49,6 +49,7 @@ if ( ! defined( 'ABSPATH' ) ) {
* The woocommerce_review_meta hook. * The woocommerce_review_meta hook.
* *
* @hooked woocommerce_review_display_meta - 10 * @hooked woocommerce_review_display_meta - 10
* @hooked WC_Structured_Data::generate_product_review_data() - 20
*/ */
do_action( 'woocommerce_review_meta', $comment ); do_action( 'woocommerce_review_meta', $comment );

View File

@ -102,6 +102,13 @@ final class WooCommerce {
public $order_factory = null; public $order_factory = null;
/** /**
* Structured data instance.
*
* @var WC_Structured_Data
*/
public $structured_data = null;
/**
* Main WooCommerce Instance. * Main WooCommerce Instance.
* *
* Ensures only one instance of WooCommerce is loaded or can be loaded. * Ensures only one instance of WooCommerce is loaded or can be loaded.
@ -334,7 +341,7 @@ final class WooCommerce {
if ( $this->is_request( 'frontend' ) ) { if ( $this->is_request( 'frontend' ) ) {
$this->cart = new WC_Cart(); // Cart class, stores the cart contents $this->cart = new WC_Cart(); // Cart class, stores the cart contents
$this->customer = new WC_Customer(); // Customer class, handles data such as customer location $this->customer = new WC_Customer(); // Customer class, handles data such as customer location
new WC_Structured_Data(); // Structured Data class, generates and handles structured data $this->structured_data = new WC_Structured_Data(); // Structured Data class, generates and handles structured data
} }
$this->load_webhooks(); $this->load_webhooks();