wordpress-seo
/
src
/
schema-aggregator
/
infrastructure
/
schema-pieces
/
woo-schema-piece-repository.php
woo-schema-piece-repository.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.6, at src/schema-aggregator/infrastructure/schema-pieces/woo-schema-piece-repository.php
| 1 | <?php |
| 2 | // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 3 | namespace Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Schema_Pieces; |
| 4 | |
| 5 | use Exception; |
| 6 | use WC_Structured_Data; |
| 7 | use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional; |
| 8 | use Yoast\WP\SEO\Schema_Aggregator\Domain\External_Schema_Piece_Repository_Interface; |
| 9 | |
| 10 | /** |
| 11 | * The WooCommerce schema piece repository. |
| 12 | */ |
| 13 | class Woo_Schema_Piece_Repository implements External_Schema_Piece_Repository_Interface { |
| 14 | |
| 15 | /** |
| 16 | * The WooCommerce Conditional. |
| 17 | * |
| 18 | * @var WooCommerce_Conditional |
| 19 | */ |
| 20 | private $woocommerce_conditional; |
| 21 | |
| 22 | /** |
| 23 | * Woo_Schema_Piece_Repository constructor. |
| 24 | * |
| 25 | * @param WooCommerce_Conditional $woocommerce_conditional The WooCommerce Conditional. |
| 26 | */ |
| 27 | public function __construct( WooCommerce_Conditional $woocommerce_conditional ) { |
| 28 | $this->woocommerce_conditional = $woocommerce_conditional; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Checks if this repository supports the given post type. |
| 33 | * |
| 34 | * @param string $post_type The post type to check. |
| 35 | * |
| 36 | * @return bool True if this repository can provide schema for the post type. |
| 37 | */ |
| 38 | public function supports( string $post_type ): bool { |
| 39 | return $this->woocommerce_conditional->is_met() && $post_type === 'product'; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Collects product schema pieces for WooCommerce products. |
| 44 | * |
| 45 | * Hooks into 'wpseo_schema_product' filter to capture enriched Product schema. |
| 46 | * Triggers WooCommerce's schema generation via WC_Structured_Data. |
| 47 | * Returns the captured Product entity. |
| 48 | * |
| 49 | * @param int $post_id Product post ID. |
| 50 | * |
| 51 | * @return array<array<string,string|int|bool|array>> Product schema pieces (empty array if unavailable). |
| 52 | */ |
| 53 | public function collect( int $post_id ): array { |
| 54 | if ( ! $this->woocommerce_conditional->is_met() ) { |
| 55 | return []; |
| 56 | } |
| 57 | |
| 58 | try { |
| 59 | $product = \wc_get_product( $post_id ); |
| 60 | |
| 61 | if ( ! $product || ! \is_a( $product, 'WC_Product' ) ) { |
| 62 | return []; |
| 63 | } |
| 64 | |
| 65 | $captured_schema = null; |
| 66 | |
| 67 | $capture_filter = static function ( $schema_data ) use ( &$captured_schema ) { |
| 68 | $captured_schema = $schema_data; |
| 69 | |
| 70 | return $schema_data; |
| 71 | }; |
| 72 | |
| 73 | \add_filter( 'wpseo_schema_product', $capture_filter, 999 ); |
| 74 | |
| 75 | // This will trigger the woocommerce_structured_data_product filter. |
| 76 | // which Yoast WooCommerce SEO hooks into to enrich the schema. |
| 77 | // which then triggers our wpseo_schema_product filter above. |
| 78 | $structured_data = new WC_Structured_Data(); |
| 79 | $structured_data->generate_product_data( $product ); |
| 80 | |
| 81 | \remove_filter( 'wpseo_schema_product', $capture_filter, 999 ); |
| 82 | |
| 83 | if ( ! \is_array( $captured_schema ) ) { |
| 84 | return []; |
| 85 | } |
| 86 | |
| 87 | return [ $captured_schema ]; |
| 88 | } catch ( Exception $e ) { |
| 89 | return []; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 |