Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Schema_Pieces

Woo_Schema_Piece_Repository::collectpublicYoast 1.0

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.

Метод класса: Woo_Schema_Piece_Repository{}

Хуков нет.

Возвращает

Массив<Массив<Строку,Строку|int|true|false|Массив>>. Product schema pieces (empty array if unavailable).

Использование

$Woo_Schema_Piece_Repository = new Woo_Schema_Piece_Repository();
$Woo_Schema_Piece_Repository->collect( $post_id ): array;
$post_id(int) (обязательный)
Product post ID.

Код Woo_Schema_Piece_Repository::collect() Yoast 27.7

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 [];
	}
}