woocommerce_conditional = $woocommerce_conditional; } /** * Checks if this repository supports the given post type. * * @param string $post_type The post type to check. * * @return bool True if this repository can provide schema for the post type. */ public function supports( string $post_type ): bool { return $this->woocommerce_conditional->is_met() && $post_type === 'product'; } /** * Collects product schema pieces for WooCommerce products. * * Hooks into 'wpseo_schema_product' filter to capture enriched Product schema. * Triggers WooCommerce's schema generation via WC_Structured_Data. * Returns the captured Product entity. * * @param int $post_id Product post ID. * * @return array> Product schema pieces (empty array if unavailable). */ public function collect( int $post_id ): array { if ( ! $this->woocommerce_conditional->is_met() ) { return []; } try { $product = \wc_get_product( $post_id ); if ( ! $product || ! \is_a( $product, 'WC_Product' ) ) { return []; } $captured_schema = null; $capture_filter = static function ( $schema_data ) use ( &$captured_schema ) { $captured_schema = $schema_data; return $schema_data; }; \add_filter( 'wpseo_schema_product', $capture_filter, 999 ); // This will trigger the woocommerce_structured_data_product filter. // which Yoast WooCommerce SEO hooks into to enrich the schema. // which then triggers our wpseo_schema_product filter above. $structured_data = new WC_Structured_Data(); $structured_data->generate_product_data( $product ); \remove_filter( 'wpseo_schema_product', $capture_filter, 999 ); if ( ! \is_array( $captured_schema ) ) { return []; } return [ $captured_schema ]; } catch ( Exception $e ) { return []; } } }