Commit realizado el 12:13:52 08-04-2024
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
/**
|
||||
* Helper class for handling WooComerce product attributes for rich snippets.
|
||||
*
|
||||
* @since 0.9.0
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Schema
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Schema;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* WC_Attributes class.
|
||||
*/
|
||||
class WC_Attributes {
|
||||
|
||||
/**
|
||||
* Hold product attributes.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $_attributes;
|
||||
|
||||
/**
|
||||
* Hold product object.
|
||||
*
|
||||
* @var WC_Product
|
||||
*/
|
||||
private $_product;
|
||||
|
||||
/**
|
||||
* The Constructor.
|
||||
*
|
||||
* @param WC_Product $product The Product.
|
||||
*/
|
||||
public function __construct( $product ) {
|
||||
$this->_product = $product;
|
||||
$this->_attributes = $product->get_attributes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find attribute for property.
|
||||
*
|
||||
* @param array $entity Entity to attach data to.
|
||||
* @param string $needle Assign this property.
|
||||
*/
|
||||
public function assign_property( &$entity, $needle ) {
|
||||
foreach ( $this->_attributes as $key => $attrib ) {
|
||||
if ( stristr( $key, $needle ) ) {
|
||||
$entity[ $needle ] = $this->_product->get_attribute( $key );
|
||||
unset( $this->_attributes[ $key ] );
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Map remaining attributes as PropertyValue.
|
||||
*
|
||||
* @param array $entity Entity to attach data to.
|
||||
*/
|
||||
public function assign_remaining( &$entity ) {
|
||||
foreach ( $this->_attributes as $key => $attrib ) {
|
||||
if ( $attrib['is_visible'] && ! $attrib['is_variation'] ) {
|
||||
$entity['additionalProperty'][] = [
|
||||
'@type' => 'PropertyValue',
|
||||
'name' => $key,
|
||||
'value' => $this->_product->get_attribute( $key ),
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user