improved structured data - patch5
This commit is contained in:
parent
73be376235
commit
913ddd6afc
|
@ -16,15 +16,15 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
class WC_Structured_Data {
|
||||
|
||||
/**
|
||||
* @var array Partially formatted structured data
|
||||
* @var array Partially structured data
|
||||
*/
|
||||
private $data;
|
||||
|
||||
/**
|
||||
* Checks if the passed $json variable is an array and stores it into $this->data...
|
||||
*
|
||||
* @param array $json Partially formatted JSON-LD
|
||||
* @return false If the param $json is not an array
|
||||
* @param array $json Partially structured data
|
||||
* @return bool false If the param $json is not an array
|
||||
*/
|
||||
private function set_data( $json ) {
|
||||
if ( ! is_array( $json ) ) {
|
||||
|
@ -35,9 +35,9 @@ class WC_Structured_Data {
|
|||
}
|
||||
|
||||
/**
|
||||
* Formats and returns the structured data...
|
||||
* Structures and returns the data...
|
||||
*
|
||||
* @return array If data is set, returns the fully formatted JSON-LD array, otherwise returns empty array
|
||||
* @return array If data is set, returns the fully structured JSON-LD array, otherwise returns empty array
|
||||
*/
|
||||
private function get_data() {
|
||||
if ( ! $this->data ) {
|
||||
|
@ -51,30 +51,44 @@ class WC_Structured_Data {
|
|||
$products[] = $value;
|
||||
} elseif ( 'Review' === $type ) {
|
||||
$reviews[] = $value;
|
||||
} elseif ( 'BreadcrumbList' === $type ) {
|
||||
$data[] = $value;
|
||||
} elseif ( 'WebSite' === $type ) {
|
||||
$data[] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$product_count = isset( $products ) ? count( $products ) : 0;
|
||||
|
||||
if ( $product_count === 1 ) {
|
||||
$data = isset( $reviews ) ? $products[0] + array( 'review' => $reviews ) : $products[0];
|
||||
$data[] = isset( $reviews ) ? $products[0] + array( 'review' => $reviews ) : $products[0];
|
||||
} elseif ( $product_count > 1 ) {
|
||||
$data = array( '@graph' => $products );
|
||||
$data[] = array( '@graph' => $products );
|
||||
}
|
||||
|
||||
|
||||
if ( ! isset( $data ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$context['@context'] = apply_filters( 'woocommerce_structured_data_context', 'http://schema.org/' );
|
||||
|
||||
return $context + $data;
|
||||
foreach( $data as $key => $value ) {
|
||||
$data[ $key ] = $context + $value;
|
||||
}
|
||||
|
||||
if ( count( $data ) > 1 ) {
|
||||
$data = array( '@graph' => $data );
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Contructor
|
||||
*/
|
||||
public function __construct() {
|
||||
add_action( 'woocommerce_before_main_content', array( $this, 'generate_shop_data' ) );
|
||||
add_action( 'woocommerce_before_main_content', array( $this, 'generate_breadcrumbs_data' ) );
|
||||
add_action( 'woocommerce_before_shop_loop_item', array( $this, 'generate_product_category_data' ) );
|
||||
add_action( 'woocommerce_single_product_summary', array( $this, 'generate_product_data' ) );
|
||||
add_action( 'woocommerce_review_meta', array( $this, 'generate_product_review_data' ) );
|
||||
|
@ -85,18 +99,18 @@ class WC_Structured_Data {
|
|||
* Sanitizes, encodes and echoes the structured data into `wp_footer` action hook.
|
||||
*/
|
||||
public function enqueue_data() {
|
||||
if ( $structured_data = $this->get_data() ) {
|
||||
if ( $data = $this->get_data() ) {
|
||||
|
||||
array_walk_recursive( $structured_data, array( $this, 'sanitize_data' ) );
|
||||
array_walk_recursive( $data, array( $this, 'sanitize_data' ) );
|
||||
|
||||
echo '<script type="application/ld+json">' . wp_json_encode( $structured_data ) . '</script>';
|
||||
echo '<script type="application/ld+json">' . wp_json_encode( $data ) . '</script>';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function for sanitizing the structured data.
|
||||
*
|
||||
* @param ref
|
||||
* @param ref $value
|
||||
*/
|
||||
private function sanitize_data( &$value ) {
|
||||
$value = sanitize_text_field( $value );
|
||||
|
@ -117,7 +131,7 @@ class WC_Structured_Data {
|
|||
/**
|
||||
* Generates the product structured data...
|
||||
* Hooked into the `woocommerce_single_product_summary` action hook...
|
||||
* Applies the `woocommerce_product_structured_data` filter hook for clean structured data customization...
|
||||
* Applies the `woocommerce_structured_data_product` filter hook for clean structured data customization...
|
||||
*/
|
||||
public function generate_product_data() {
|
||||
global $product;
|
||||
|
@ -168,7 +182,7 @@ class WC_Structured_Data {
|
|||
/**
|
||||
* Generates the product review structured data...
|
||||
* Hooked into the `woocommerce_review_meta` action hook...
|
||||
* Applies the `woocommerce_product_review_structured_data` filter hook for clean structured data customization...
|
||||
* Applies the `woocommerce_structured_data_product_review` filter hook for clean structured data customization...
|
||||
*
|
||||
* @param object $comment
|
||||
*/
|
||||
|
@ -189,4 +203,66 @@ class WC_Structured_Data {
|
|||
|
||||
$this->set_data( apply_filters( 'woocommerce_structured_data_product_review', $json, $comment ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the breadcrumbs structured data...
|
||||
* Hooked into the `woocommerce_before_main_content` action hook...
|
||||
* Applies the `woocommerce_structured_data_breadcrumbs` filter hook for clean structured data customization...
|
||||
*
|
||||
*/
|
||||
public function generate_breadcrumbs_data() {
|
||||
if ( is_front_page() || is_search()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$breadcrumbs = new WC_Breadcrumb();
|
||||
$breadcrumb = $breadcrumbs->generate();
|
||||
$position = 1;
|
||||
|
||||
foreach ( $breadcrumb as $key => $value ) {
|
||||
if ( ! empty( $value[1] ) && sizeof( $breadcrumb ) !== $key + 1 ) {
|
||||
$json_crumbs_item = array(
|
||||
'@id' => $value[1],
|
||||
'name' => $value[0]
|
||||
);
|
||||
} else {
|
||||
$json_crumbs_item = array(
|
||||
'name' => $value[0]
|
||||
);
|
||||
}
|
||||
|
||||
$json_crumbs[] = array(
|
||||
'@type' => 'ListItem',
|
||||
'position' => $position ++,
|
||||
'item' => $json_crumbs_item
|
||||
);
|
||||
}
|
||||
|
||||
$json['@type'] = 'BreadcrumbList';
|
||||
$json['itemListElement'] = $json_crumbs;
|
||||
|
||||
$this->set_data( apply_filters( 'woocommerce_structured_data_breadcrumbs', $json, $breadcrumbs ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the shop structured data...
|
||||
* 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() {
|
||||
$json['@type'] = 'WebSite';
|
||||
$json['name'] = get_bloginfo( 'name' );
|
||||
$json['url'] = get_bloginfo( 'url' );
|
||||
|
||||
if ( is_shop() && is_front_page() ) {
|
||||
$json['potentialAction'] = array(
|
||||
'@type' => 'SearchAction',
|
||||
'target' => get_bloginfo( 'url' ) . '/?s={search_term_string}&post_type=product',
|
||||
'query-input' => 'required name=search_term_string'
|
||||
);
|
||||
}
|
||||
|
||||
$this->set_data( apply_filters( 'woocommerce_structured_data_shop', $json ) );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1398,7 +1398,7 @@ if ( ! function_exists( 'woocommerce_breadcrumb' ) ) {
|
|||
function woocommerce_breadcrumb( $args = array() ) {
|
||||
$args = wp_parse_args( $args, apply_filters( 'woocommerce_breadcrumb_defaults', array(
|
||||
'delimiter' => ' / ',
|
||||
'wrap_before' => '<nav class="woocommerce-breadcrumb" ' . ( is_single() ? 'itemprop="breadcrumb"' : '' ) . '>',
|
||||
'wrap_before' => '<nav class="woocommerce-breadcrumb">',
|
||||
'wrap_after' => '</nav>',
|
||||
'before' => '',
|
||||
'after' => '',
|
||||
|
|
Loading…
Reference in New Issue