2016-07-17 02:42:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
2016-10-13 21:21:12 +00:00
|
|
|
exit; // Exit if accessed directly.
|
2016-07-17 02:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Structured data's handler and generator using JSON-LD format.
|
|
|
|
*
|
2016-10-13 21:21:12 +00:00
|
|
|
* @class WC_Structured_Data
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
* @version 3.0.0
|
2016-10-13 21:21:12 +00:00
|
|
|
* @package WooCommerce/Classes
|
|
|
|
* @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-09-21 14:37:54 +00:00
|
|
|
|
2016-08-11 09:17:14 +00:00
|
|
|
/**
|
2016-09-14 20:08:57 +00:00
|
|
|
* @var array $_data
|
2016-08-11 09:17:14 +00:00
|
|
|
*/
|
2016-09-14 20:08:57 +00:00
|
|
|
private $_data = array();
|
2016-08-11 09:17:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
2016-08-11 10:04:12 +00:00
|
|
|
*/
|
2016-08-11 09:17:14 +00:00
|
|
|
public function __construct() {
|
2016-10-13 21:21:12 +00:00
|
|
|
// Generate structured data.
|
|
|
|
add_action( 'woocommerce_before_main_content', array( $this, 'generate_website_data' ), 30 );
|
|
|
|
add_action( 'woocommerce_breadcrumb', array( $this, 'generate_breadcrumblist_data' ), 10 );
|
|
|
|
add_action( 'woocommerce_shop_loop', array( $this, 'generate_product_data' ), 10 );
|
|
|
|
add_action( 'woocommerce_single_product_summary', array( $this, 'generate_product_data' ), 60 );
|
|
|
|
add_action( 'woocommerce_review_meta', array( $this, 'generate_review_data' ), 20 );
|
|
|
|
add_action( 'woocommerce_email_order_details', array( $this, 'generate_order_data' ), 20, 3 );
|
2016-09-21 14:37:54 +00:00
|
|
|
|
2016-10-13 21:21:12 +00:00
|
|
|
// Output structured data.
|
2017-02-20 12:04:53 +00:00
|
|
|
add_action( 'woocommerce_email_order_details', array( $this, 'output_email_structured_data' ), 30, 3 );
|
2016-10-13 21:21:12 +00:00
|
|
|
add_action( 'wp_footer', array( $this, 'output_structured_data' ), 10 );
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-13 21:51:20 +00:00
|
|
|
* Sets data.
|
2016-08-10 22:23:26 +00:00
|
|
|
*
|
2016-10-13 23:54:47 +00:00
|
|
|
* @param array $data Structured data.
|
2016-10-13 21:51:20 +00:00
|
|
|
* @param bool $reset Unset data (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-09-14 20:08:57 +00:00
|
|
|
if ( ! isset( $data['@type'] ) || ! preg_match( '|^[a-zA-Z]{1,20}$|', $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-09-21 14:37:54 +00:00
|
|
|
|
2016-08-11 23:29:35 +00:00
|
|
|
$this->_data[] = $data;
|
2016-08-11 09:17:14 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-21 14:37:54 +00:00
|
|
|
|
2016-08-11 09:17:14 +00:00
|
|
|
/**
|
2016-10-13 21:51:20 +00:00
|
|
|
* Gets data.
|
2016-08-11 09:17:14 +00:00
|
|
|
*
|
2016-08-23 13:44:46 +00:00
|
|
|
* @return array
|
2016-08-11 09:17:14 +00:00
|
|
|
*/
|
|
|
|
public function get_data() {
|
2016-09-14 20:08:57 +00:00
|
|
|
return $this->_data;
|
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-10-13 22:40:15 +00:00
|
|
|
* @param array $types Structured data types.
|
2016-08-12 20:58:23 +00:00
|
|
|
* @return array
|
2016-08-11 09:17:14 +00:00
|
|
|
*/
|
2016-10-13 22:40:15 +00:00
|
|
|
public function get_structured_data( $types ) {
|
|
|
|
$data = array();
|
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-10-13 22:48:02 +00:00
|
|
|
foreach ( $this->get_data() as $value ) {
|
2016-10-13 22:40:15 +00:00
|
|
|
$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-10-13 22:40:15 +00:00
|
|
|
foreach ( $data as $type => $value ) {
|
|
|
|
$data[ $type ] = count( $value ) > 1 ? array( '@graph' => $value ) : $value[0];
|
2017-04-17 15:35:21 +00:00
|
|
|
$data[ $type ] = apply_filters( 'woocommerce_structured_data_context', array( '@context' => 'https://schema.org/' ), $data, $type, $value ) + $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-10-13 22:40:15 +00:00
|
|
|
$data = $types ? array_values( array_intersect_key( $data, array_flip( $types ) ) ) : array_values( $data );
|
2016-08-14 22:39:01 +00:00
|
|
|
|
2016-10-13 22:40:15 +00:00
|
|
|
if ( ! empty( $data ) ) {
|
2016-10-13 22:54:21 +00:00
|
|
|
$data = count( $data ) > 1 ? array( '@graph' => $data ) : $data[0];
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 22:40:15 +00:00
|
|
|
return $data;
|
2016-08-11 23:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-13 22:40:15 +00:00
|
|
|
* Get data types for pages.
|
2016-08-28 07:24:17 +00:00
|
|
|
*
|
2016-10-13 22:40:15 +00:00
|
|
|
* @return array
|
2016-08-11 09:17:14 +00:00
|
|
|
*/
|
2016-10-13 22:40:15 +00:00
|
|
|
protected function get_data_type_for_page() {
|
|
|
|
$types = array();
|
|
|
|
$types[] = is_shop() || is_product_category() || is_product() ? 'product' : '';
|
|
|
|
$types[] = is_shop() && is_front_page() ? 'website' : '';
|
|
|
|
$types[] = is_product() ? 'review' : '';
|
|
|
|
$types[] = ! is_shop() ? 'breadcrumblist' : '';
|
|
|
|
$types[] = 'order';
|
|
|
|
|
|
|
|
return array_filter( apply_filters( 'woocommerce_structured_data_type_for_page', $types ) );
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 12:04:53 +00:00
|
|
|
/**
|
|
|
|
* 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).
|
|
|
|
*/
|
|
|
|
public function output_email_structured_data( $order, $sent_to_admin = false, $plain_text = false ) {
|
|
|
|
if ( $plain_text ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-04-06 09:06:21 +00:00
|
|
|
echo '<div style="display: none; font-size: 0; max-height: 0; line-height: 0; padding: 0; mso-hide: all;">';
|
2017-02-20 12:04:53 +00:00
|
|
|
$this->output_structured_data();
|
2017-04-06 09:06:21 +00:00
|
|
|
echo '</div>';
|
2017-02-20 12:04:53 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 12:10:14 +00:00
|
|
|
/**
|
2016-10-13 22:40:15 +00:00
|
|
|
* Sanitizes, encodes and outputs structured data.
|
2016-08-15 12:10:14 +00:00
|
|
|
*
|
2016-10-13 22:40:15 +00:00
|
|
|
* Hooked into `wp_footer` action hook.
|
|
|
|
* Hooked into `woocommerce_email_order_details` action hook.
|
2016-08-15 12:10:14 +00:00
|
|
|
*/
|
2016-10-13 22:40:15 +00:00
|
|
|
public function output_structured_data() {
|
|
|
|
$types = $this->get_data_type_for_page();
|
2016-08-15 12:10:14 +00:00
|
|
|
|
2016-10-13 22:40:15 +00:00
|
|
|
if ( $data = wc_clean( $this->get_structured_data( $types ) ) ) {
|
|
|
|
echo '<script type="application/ld+json">' . wp_json_encode( $data ) . '</script>';
|
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-28 07:24:17 +00:00
|
|
|
* Hooked into `woocommerce_single_product_summary` action hook.
|
|
|
|
* Hooked into `woocommerce_shop_loop` action hook.
|
|
|
|
*
|
2016-10-13 22:44:12 +00:00
|
|
|
* @param WC_Product $product Product data (default: null).
|
2016-08-11 09:17:14 +00:00
|
|
|
*/
|
2016-10-13 22:44:12 +00:00
|
|
|
public function generate_product_data( $product = null ) {
|
2016-10-13 21:51:20 +00:00
|
|
|
if ( ! is_object( $product ) ) {
|
2016-08-11 23:29:35 +00:00
|
|
|
global $product;
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 11:40:53 +00:00
|
|
|
if ( ! is_a( $product, 'WC_Product' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-03 17:00:57 +00:00
|
|
|
$shop_name = get_bloginfo( 'name' );
|
|
|
|
$shop_url = home_url();
|
|
|
|
$currency = get_woocommerce_currency();
|
2016-10-13 22:05:51 +00:00
|
|
|
$markup = array();
|
2016-08-28 07:24:17 +00:00
|
|
|
$markup['@type'] = 'Product';
|
|
|
|
$markup['@id'] = get_permalink( $product->get_id() );
|
|
|
|
$markup['url'] = $markup['@id'];
|
WIP - Product CRUD (#12065)
* Created function to get the catalog visibility options
* First methods for WP_Product crud
* Product set methods
* Fixed several erros while setting data
* First methods for WP_Product crud
* Product set methods
* Fixed several erros while setting data
* Hardcode the get_type per product class
* Initial look through getters and setters and abstract data
* Missing var
* Add related product functions and deprecate those in class.
* No need to exclude ID
* Fixed coding standards and improved the docblocks
* Get cached terms from wc_get_related_terms()
* Fixed wrong variable in wc_get_related_terms
* Use count() instead of sizeof()
* Sanitize ids later
* Remove unneeded comments
* wc_get_product_term_ids instead of related wording and use in other places.
get_the_terms is used here and also handles caching, something
wp_get_post_terms does not.
* Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it.
* Bump template version
* Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function
* Made abstract function useful
* External Product CRUD
* _virtual meta should be 'no', not taxable, in product unit test helper
* Grouped product class
* Tests
* Move children to meta and update test
* Use get_upsell_ids
* Spacing in query
* Moving and refactoring methods
* Availability html
* Tidy/add todos
* Rename method
* Put back review functions (still todo)
* missing $this
* get_price_including_tax/excluding_tax functions
* wc_get_price_to_display
* Price handling
* [Product CRUD] Variable (#12146)
* [Product CRUD] Variable Products
* Handle PR feedback.
* [Product CRUD] Grouped Handling (#12151)
* Handle grouped product saving
* Update routine
* [Product CRUD] Product crud terms (#12149)
* Category and tag id handling
* Replace template functions
* Remove todo
* Handle default name in save function
* Product crud admin save routine (#12174)
* Initial props
* Work on admin saving
* Set/get attributes
* Atom was moaning about this before but no longer.
* Update get_shipping_class
* WC_Product_Attribute
* Use getter in admin panel
* Fix attribute saving
* Spacing
* Fix comment
* wc_implode_text_attributes helper function
* [Product CRUD] Product crud admin use getters (#12196)
* Initial props
* Work on admin saving
* Set/get attributes
* Atom was moaning about this before but no longer.
* Update get_shipping_class
* WC_Product_Attribute
* Use getter in admin panel
* Fix attribute saving
* Move settings into new files
* Refactor panels and use getters
* Use getters for variation panel
* Revert save variation changes for now
* Add todos
* Fix downloads
* REST API CRUD Updates
* Additional API updates/fixes. Added some todos
* Fix final failing tests and implementing setters/getters and attributes functionality.
* Fix comparison for is_on_sale and remove download_type from WC_Product.
* Add a wc_get_products wrapper.
* Remove the download type input from the product data metabox for downloadable products. (#12221)
* [Product CRUD] Variations - setters, getters and admin. (#12228)
* Started on variation changes
* Stock functions
* Variation class
* Bulk change ->id to get_id() to fix variation form display
* Missing status
* Fix add to cart
* Start on stored data save
* save variation
* Save_variations
* Variation edit panel
* Save variations code works.
* Remove stored data code and fix save
* Improve legacy class
* wc_bool_to_string
* prepare_set_attributes
* Use wc_get_products
* More feedback fixes
* Feedback fixes
* Implement CRUD in the legacy REST API
* Handle PR feedback
* [Product CRUD] Getter setter proxy methods (#12236)
* Started on variation changes
* Stock functions
* Variation class
* Bulk change ->id to get_id() to fix variation form display
* Missing status
* Fix add to cart
* Start on stored data save
* save variation
* Save_variations
* Variation edit panel
* Save variations code works.
* Remove stored data code and fix save
* Improve legacy class
* wc_bool_to_string
* prepare_set_attributes
* Use wc_get_products
* More feedback fixes
* get_prop implementation in abstract and data classes
* Implement set_prop
* Change handling
* Array key exists
* set_object_read
* Use get_the_terms() instead of wp_get_post_terms()
wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not
use the object cache, and generates a database query every time it is used.
get_the_terms() however can use data from the object cache if present.
* Allow WP_Query to preload post data, and meta in wc_get_products()
Allow WP_Query to bulk query for post data and meta if more than
just IDs are requested from wc_get_products(). Reduces query count
significantly.
* [Product CRUD] Variable, variation, notices, and stock handling (#12277)
* No longer needed
* Remove old todos
* Use getters in admin list
* Related and upsells update for CRUD
* Fix notice in gallery
* Variable fixes and todos
* Context
* Price sync
* Revert variation attributes change
* Return parent data in view context
* Defer term counting
* wc_find_matching_product_variation
* Stock manage tweaks
* Stock fixes
* Correct id
* correct id
* Better sync
* Data logic setter fix
* feedback
* First methods for WP_Product crud
* Product set methods
* Fixed several erros while setting data
* Hardcode the get_type per product class
* Initial look through getters and setters and abstract data
* Missing var
* Fixed coding standards and improved the docblocks
* Get cached terms from wc_get_related_terms()
* Fixed wrong variable in wc_get_related_terms
* Use count() instead of sizeof()
* Add related product functions and deprecate those in class.
* No need to exclude ID
* Sanitize ids later
* Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it.
* Remove unneeded comments
* wc_get_product_term_ids instead of related wording and use in other places.
get_the_terms is used here and also handles caching, something
wp_get_post_terms does not.
* Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function
* External Product CRUD
* _virtual meta should be 'no', not taxable, in product unit test helper
* Bump template version
* Made abstract function useful
* Grouped product class
* Tests
* Move children to meta and update test
* Use get_upsell_ids
* Spacing in query
* Moving and refactoring methods
* Availability html
* Tidy/add todos
* Rename method
* Put back review functions (still todo)
* missing $this
* get_price_including_tax/excluding_tax functions
* wc_get_price_to_display
* Price handling
* [Product CRUD] Variable (#12146)
* [Product CRUD] Variable Products
* Handle PR feedback.
* [Product CRUD] Grouped Handling (#12151)
* Handle grouped product saving
* Update routine
* [Product CRUD] Product crud terms (#12149)
* Category and tag id handling
* Replace template functions
* Remove todo
* Handle default name in save function
* Product crud admin save routine (#12174)
* Initial props
* Work on admin saving
* Set/get attributes
* Atom was moaning about this before but no longer.
* Update get_shipping_class
* WC_Product_Attribute
* Use getter in admin panel
* Fix attribute saving
* Spacing
* Fix comment
* wc_implode_text_attributes helper function
* [Product CRUD] Product crud admin use getters (#12196)
* Initial props
* Work on admin saving
* Set/get attributes
* Atom was moaning about this before but no longer.
* Update get_shipping_class
* WC_Product_Attribute
* Use getter in admin panel
* Fix attribute saving
* Move settings into new files
* Refactor panels and use getters
* Use getters for variation panel
* Revert save variation changes for now
* Add todos
* Fix downloads
* REST API CRUD Updates
* Additional API updates/fixes. Added some todos
* Fix final failing tests and implementing setters/getters and attributes functionality.
* Fix comparison for is_on_sale and remove download_type from WC_Product.
* Add a wc_get_products wrapper.
* Remove the download type input from the product data metabox for downloadable products. (#12221)
* [Product CRUD] Variations - setters, getters and admin. (#12228)
* Started on variation changes
* Stock functions
* Variation class
* Bulk change ->id to get_id() to fix variation form display
* Missing status
* Fix add to cart
* Start on stored data save
* save variation
* Save_variations
* Variation edit panel
* Save variations code works.
* Remove stored data code and fix save
* Improve legacy class
* wc_bool_to_string
* prepare_set_attributes
* Use wc_get_products
* More feedback fixes
* Feedback fixes
* Implement CRUD in the legacy REST API
* Handle PR feedback
* [Product CRUD] Getter setter proxy methods (#12236)
* Started on variation changes
* Stock functions
* Variation class
* Bulk change ->id to get_id() to fix variation form display
* Missing status
* Fix add to cart
* Start on stored data save
* save variation
* Save_variations
* Variation edit panel
* Save variations code works.
* Remove stored data code and fix save
* Improve legacy class
* wc_bool_to_string
* prepare_set_attributes
* Use wc_get_products
* More feedback fixes
* get_prop implementation in abstract and data classes
* Implement set_prop
* Change handling
* Array key exists
* set_object_read
* Use get_the_terms() instead of wp_get_post_terms()
wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not
use the object cache, and generates a database query every time it is used.
get_the_terms() however can use data from the object cache if present.
* [Product CRUD] Variable, variation, notices, and stock handling (#12277)
* No longer needed
* Remove old todos
* Use getters in admin list
* Related and upsells update for CRUD
* Fix notice in gallery
* Variable fixes and todos
* Context
* Price sync
* Revert variation attributes change
* Return parent data in view context
* Defer term counting
* wc_find_matching_product_variation
* Stock manage tweaks
* Stock fixes
* Correct id
* correct id
* Better sync
* Data logic setter fix
* feedback
* Prevent notices
* Handle image_id from parent
* Fix error
* Remove _wc_save_product_price
* Remove todo
* Fixed wrong variation URLs
* Fixed undefined $image_id in WC_Product_Variation::get_image_id()
* Allow wc_rest_prepare_date_response() handle timestamps
* Updated get methods on REST API for variations
* Use variations CRUD to save variations metadata
* [Product CRUD] Abstract todos (#12305)
* Get dimensions and weights, with soft deprecation
* Product attributes
* Ratings
* Fix read method
* Downloads
* Feedback
* Revert "[Product CRUD] Abstract todos (#12305)"
This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2.
* Remove deprecated get_variation_id()
* New default attributes method
* [Product CRUD] Product Datastore (#12317)
* Fix up tests in the product/* folder.
* Handle data store updates for grouped, variable, external, simple, and general data store updates for products.
* Variations & variable changes.
* Update -functions.php calls to use data store.
* Add an interface for the public product data store methods.
* Finished product factory tests
* Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods.
* Fix up delete in all versions of the api
* Handle feedback
* Match protected decloration to parent
* Product crud abstract todos (#12316)
* Get dimensions and weights, with soft deprecation
* Product attributes
* Ratings
* Fix read method
* Downloads
* Feedback
* Fix up store
* Fixed method returning in write context
* Fix error in variation admin
* Check for parent value - fixes tax class
* Remove old/complete todos
* Allow set tax class as "parent"
* Removed duplicated sync
* Fixed wrong variation URLs
* Fixed undefined $image_id in WC_Product_Variation::get_image_id()
* Allow wc_rest_prepare_date_response() handle timestamps
* Updated get methods on REST API for variations
* Use variations CRUD to save variations metadata
* Remove deprecated get_variation_id()
* New default attributes method
* Fixed method returning in write context
* Allow set tax class as "parent"
* Removed duplicated sync
* Fixed coding standards
* TODO is not accurate.
* Should pass WC_Product instancies to WC_Comments methods (#12327)
* Use new method in abstract order class to prevent headers sent issue in tests
* Fixed variable description in REST API
* Updated how create initial product variation
* Fixed a few fatal errors and warnings in Products CRUD (#12329)
* Fixed a few fatal errors and warnings in Products CRUD
* Fixed sync functions
* Add variations CRUD to legacy API (#12331)
* Apply crud to variable products in legacy API v1
* New REST API do not need fallback for default attributes
* Apply variations CRUD to legacy API v2
* Legacy v2 - save default attributes
* Variations in legacy API v2 do not have descriptions
* Fixed legacy API v2 variations params
* Applied variations CRUD to legacy API v3
* Sync before save in legacy apis
* Punc
* Removed API todos
* Removed test
* Products endpoint tweaks (#12354)
* Var type already normalized on CRUD
* Let Product CRUD handle with validation, sanitization and conditional checks
* Set downloads using WC_Product_Download
* Stop try catch exceptions more than one time
* Handle WC_Data_Exception in legacy API
* Complete remove products when fails on creating
* On creating I mean!
* Already have a method to complete delete products
* Fixed standards using WP CodeSniffer
* get_the_terms() returns false when empty
* get_manage_stock returns boolean
@claudiosanches
* Merge conflict
* Variations API endpoint fixes
* Product CRUD improvements (#12359)
* args is not used any more - remove todo
* Added test for attributes
* wc_get_price_excluding_tax usage
* parent usage
* Fix rating counts
* Test fixes
* Cleanup after tests
* Make sure status transition code runs even during API calls, not just in admin.
* Default visibility
* Fix attribute setting in API
* Use get name instead of get title
* variation id usage
* Improved cross sell templates
* variation_data
* Grouped product sync
* Notices
* Sync is not needed in API
* Delete
* Rename interfaces
* Update counts in data store
2016-11-16 12:38:24 +00:00
|
|
|
$markup['name'] = $product->get_name();
|
2016-08-23 13:44:46 +00:00
|
|
|
|
2016-10-13 22:44:12 +00:00
|
|
|
if ( apply_filters( 'woocommerce_structured_data_product_limit', is_product_taxonomy() || is_shop() ) ) {
|
2016-10-13 23:43:30 +00:00
|
|
|
$this->set_data( apply_filters( 'woocommerce_structured_data_product_limited', $markup, $product ) );
|
2016-10-13 22:44:12 +00:00
|
|
|
return;
|
2016-08-26 14:57:49 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 10:59:33 +00:00
|
|
|
if ( '' !== $product->get_price() ) {
|
|
|
|
$markup_offer = array(
|
|
|
|
'@type' => 'Offer',
|
2017-02-08 11:40:04 +00:00
|
|
|
'priceCurrency' => $currency,
|
2017-05-08 10:59:33 +00:00
|
|
|
'availability' => 'https://schema.org/' . $stock = ( $product->is_in_stock() ? 'InStock' : 'OutOfStock' ),
|
|
|
|
'sku' => $product->get_sku(),
|
|
|
|
'image' => wp_get_attachment_url( $product->get_image_id() ),
|
|
|
|
'description' => $product->get_description(),
|
|
|
|
'seller' => array(
|
|
|
|
'@type' => 'Organization',
|
|
|
|
'name' => $shop_name,
|
|
|
|
'url' => $shop_url,
|
|
|
|
),
|
2016-08-11 09:17:14 +00:00
|
|
|
);
|
2017-02-08 11:40:04 +00:00
|
|
|
|
2017-05-08 10:59:33 +00:00
|
|
|
if ( $product->is_type( 'variable' ) ) {
|
|
|
|
$prices = $product->get_variation_prices();
|
|
|
|
|
2017-05-08 18:13:12 +00:00
|
|
|
if ( current( $prices['price'] ) === end( $prices['price'] ) ) {
|
|
|
|
$markup_offer['price'] = wc_format_decimal( $product->get_price(), wc_get_price_decimals() );
|
|
|
|
} else {
|
|
|
|
$markup_offer['priceSpecification'] = array(
|
|
|
|
'price' => wc_format_decimal( $product->get_price(), wc_get_price_decimals() ),
|
|
|
|
'minPrice' => wc_format_decimal( current( $prices['price'] ), wc_get_price_decimals() ),
|
|
|
|
'maxPrice' => wc_format_decimal( end( $prices['price'] ), wc_get_price_decimals() ),
|
|
|
|
'priceCurrency' => $currency,
|
|
|
|
);
|
|
|
|
}
|
2017-05-08 15:53:27 +00:00
|
|
|
} else {
|
2017-05-08 18:13:12 +00:00
|
|
|
$markup_offer['price'] = wc_format_decimal( $product->get_price(), wc_get_price_decimals() );
|
2017-05-08 15:53:27 +00:00
|
|
|
}
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
2016-09-21 14:37:54 +00:00
|
|
|
|
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-10-13 21:51:20 +00:00
|
|
|
$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-28 07:24:17 +00:00
|
|
|
* Hooked into `woocommerce_review_meta` action hook.
|
|
|
|
*
|
2016-10-13 21:51:20 +00:00
|
|
|
* @param WP_Comment $comment Comment data.
|
2016-08-11 09:17:14 +00:00
|
|
|
*/
|
2016-08-14 22:39:01 +00:00
|
|
|
public function generate_review_data( $comment ) {
|
2016-10-13 22:05:51 +00:00
|
|
|
$markup = array();
|
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 );
|
2017-04-13 15:30:06 +00:00
|
|
|
$markup['itemReviewed'] = array(
|
|
|
|
'@type' => 'Product',
|
2017-05-25 23:59:42 +00:00
|
|
|
'name' => get_the_title( $comment->comment_post_ID ),
|
2017-04-13 15:30:06 +00:00
|
|
|
);
|
2017-02-24 12:32:39 +00:00
|
|
|
if ( $rating = get_comment_meta( $comment->comment_ID, 'rating', true ) ) {
|
|
|
|
$markup['reviewRating'] = array(
|
|
|
|
'@type' => 'rating',
|
|
|
|
'ratingValue' => $rating,
|
|
|
|
);
|
2017-02-24 12:34:09 +00:00
|
|
|
|
|
|
|
// Skip replies unless they have a rating.
|
|
|
|
} elseif ( $comment->comment_parent ) {
|
|
|
|
return;
|
2017-02-24 12:32:39 +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-09-21 14:37:54 +00:00
|
|
|
|
2016-10-13 21:51:20 +00:00
|
|
|
$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-28 07:24:17 +00:00
|
|
|
* Hooked into `woocommerce_breadcrumb` action hook.
|
|
|
|
*
|
2016-10-13 21:51:20 +00:00
|
|
|
* @param WC_Breadcrumb $breadcrumbs Breadcrumb data.
|
2016-08-11 09:17:14 +00:00
|
|
|
*/
|
2016-08-18 18:37:34 +00:00
|
|
|
public function generate_breadcrumblist_data( $breadcrumbs ) {
|
2016-10-13 21:51:20 +00:00
|
|
|
$crumbs = $breadcrumbs->get_breadcrumb();
|
2016-08-11 09:17:14 +00:00
|
|
|
|
2017-04-20 16:04:48 +00:00
|
|
|
if ( empty( $crumbs ) || ! is_array( $crumbs ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-13 22:05:51 +00:00
|
|
|
$markup = array();
|
|
|
|
$markup['@type'] = 'BreadcrumbList';
|
|
|
|
$markup['itemListElement'] = array();
|
|
|
|
|
2016-08-18 18:37:34 +00:00
|
|
|
foreach ( $crumbs as $key => $crumb ) {
|
2016-10-13 22:05:51 +00:00
|
|
|
$markup['itemListElement'][ $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-10-13 22:05:51 +00:00
|
|
|
$markup['itemListElement'][ $key ]['item'] += array( '@id' => $crumb[1] );
|
2016-08-18 00:31:13 +00:00
|
|
|
}
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 21:51:20 +00:00
|
|
|
$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-28 07:24:17 +00:00
|
|
|
* Hooked into `woocommerce_before_main_content` action hook.
|
2016-08-11 09:17:14 +00:00
|
|
|
*/
|
2016-08-11 23:29:35 +00:00
|
|
|
public function generate_website_data() {
|
2016-10-13 22:05:51 +00:00
|
|
|
$markup = array();
|
2016-08-11 23:29:35 +00:00
|
|
|
$markup['@type'] = 'WebSite';
|
|
|
|
$markup['name'] = get_bloginfo( 'name' );
|
2016-11-03 17:00:57 +00:00
|
|
|
$markup['url'] = home_url();
|
2016-08-11 23:29:35 +00:00
|
|
|
$markup['potentialAction'] = array(
|
2016-08-11 09:17:14 +00:00
|
|
|
'@type' => 'SearchAction',
|
2016-11-03 17:00:57 +00:00
|
|
|
'target' => home_url( '?s={search_term_string}&post_type=product' ),
|
2016-08-11 09:17:14 +00:00
|
|
|
'query-input' => 'required name=search_term_string',
|
|
|
|
);
|
|
|
|
|
2016-10-13 21:51:20 +00:00
|
|
|
$this->set_data( apply_filters( 'woocommerce_structured_data_website', $markup ) );
|
2016-08-11 09:17:14 +00:00
|
|
|
}
|
2016-09-21 14:37:54 +00:00
|
|
|
|
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-28 07:24:17 +00:00
|
|
|
* Hooked into `woocommerce_email_order_details` action hook.
|
|
|
|
*
|
2016-10-13 23:54:47 +00:00
|
|
|
* @param WP_Order $order Order data.
|
2016-10-13 21:51:20 +00:00
|
|
|
* @param bool $sent_to_admin Send to admin (default: false).
|
|
|
|
* @param bool $plain_text Plain text email (default: false).
|
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 ) {
|
2017-02-27 17:08:13 +00:00
|
|
|
if ( $plain_text || ! is_a( $order, 'WC_Order' ) ) {
|
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
|
|
|
|
2016-11-03 17:00:57 +00:00
|
|
|
$shop_name = get_bloginfo( 'name' );
|
|
|
|
$shop_url = home_url();
|
2017-03-02 21:09:38 +00:00
|
|
|
$order_url = $sent_to_admin ? admin_url( 'post.php?post=' . absint( $order->get_id() ) . '&action=edit' ) : $order->get_view_order_url();
|
2016-10-13 21:51:20 +00:00
|
|
|
$order_statuses = array(
|
2017-04-17 15:35:21 +00:00
|
|
|
'pending' => 'https://schema.org/OrderPaymentDue',
|
|
|
|
'processing' => 'https://schema.org/OrderProcessing',
|
|
|
|
'on-hold' => 'https://schema.org/OrderProblem',
|
|
|
|
'completed' => 'https://schema.org/OrderDelivered',
|
|
|
|
'cancelled' => 'https://schema.org/OrderCancelled',
|
|
|
|
'refunded' => 'https://schema.org/OrderReturned',
|
|
|
|
'failed' => 'https://schema.org/OrderProblem',
|
2016-10-13 21:51:20 +00:00
|
|
|
);
|
|
|
|
|
2016-10-13 22:05:51 +00:00
|
|
|
$markup_offers = array();
|
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 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',
|
2016-11-03 17:00:57 +00:00
|
|
|
'name' => $shop_name,
|
|
|
|
'url' => $shop_url,
|
2016-08-11 09:17:14 +00:00
|
|
|
),
|
2016-08-10 22:23:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-10-13 22:05:51 +00:00
|
|
|
$markup = array();
|
2016-08-11 23:29:35 +00:00
|
|
|
$markup['@type'] = 'Order';
|
2016-08-18 11:24:17 +00:00
|
|
|
$markup['url'] = $order_url;
|
2017-05-08 16:46:40 +00:00
|
|
|
$markup['orderStatus'] = isset( $order_statuses[ $order->get_status() ] ) ? $order_statuses[ $order->get_status() ] : '';
|
2016-08-11 23:29:35 +00:00
|
|
|
$markup['orderNumber'] = $order->get_order_number();
|
2017-03-10 16:43:05 +00:00
|
|
|
$markup['orderDate'] = $order->get_date_created()->format( 'c' );
|
2016-08-11 23:29:35 +00:00
|
|
|
$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-11-03 17:00:57 +00:00
|
|
|
'name' => $shop_name,
|
|
|
|
'url' => $shop_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-10-13 21:51:20 +00:00
|
|
|
$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
|
|
|
}
|