wordpress-seo
/
src
/
schema-aggregator
/
infrastructure
/
schema-pieces
/
edd-schema-piece-repository.php
edd-schema-piece-repository.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at src/schema-aggregator/infrastructure/schema-pieces/edd-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 EDD\Structured_Data; |
| 6 | use Exception; |
| 7 | use Yoast\WP\SEO\Conditionals\Third_Party\EDD_Conditional; |
| 8 | use Yoast\WP\SEO\Schema_Aggregator\Domain\External_Schema_Piece_Repository_Interface; |
| 9 | use Yoast\WP\SEO\Surfaces\Meta_Surface; |
| 10 | |
| 11 | /** |
| 12 | * The EDD schema piece repository. |
| 13 | */ |
| 14 | class Edd_Schema_Piece_Repository implements External_Schema_Piece_Repository_Interface { |
| 15 | |
| 16 | /** |
| 17 | * The EDD Conditional. |
| 18 | * |
| 19 | * @var EDD_Conditional |
| 20 | */ |
| 21 | private $edd_conditional; |
| 22 | |
| 23 | /** |
| 24 | * The Meta Surface. |
| 25 | * |
| 26 | * @var Meta_Surface |
| 27 | */ |
| 28 | private $meta; |
| 29 | |
| 30 | /** |
| 31 | * Edd_Schema_Piece_Repository constructor. |
| 32 | * |
| 33 | * @param EDD_Conditional $edd_conditional The EDD Conditional. |
| 34 | * @param Meta_Surface $meta The Meta Surface. |
| 35 | */ |
| 36 | public function __construct( EDD_Conditional $edd_conditional, Meta_Surface $meta ) { |
| 37 | $this->edd_conditional = $edd_conditional; |
| 38 | $this->meta = $meta; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Checks if this repository supports the given post type. |
| 43 | * |
| 44 | * @param string $post_type The post type to check. |
| 45 | * |
| 46 | * @return bool True if this repository can provide schema for the post type. |
| 47 | */ |
| 48 | public function supports( string $post_type ): bool { |
| 49 | return $this->edd_conditional->is_met() && $post_type === 'download'; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Collects download schema pieces for EDD downloads. |
| 54 | * |
| 55 | * Triggers EDD's schema generation. |
| 56 | * Returns the captured Product entity. |
| 57 | * |
| 58 | * @param int $post_id Download post ID. |
| 59 | * |
| 60 | * @return array<array<string,string|int|bool|array>> Product schema pieces (empty array if unavailable). |
| 61 | */ |
| 62 | public function collect( int $post_id ): array { |
| 63 | if ( ! $this->edd_conditional->is_met() ) { |
| 64 | return []; |
| 65 | } |
| 66 | |
| 67 | try { |
| 68 | $structured_data = new Structured_Data(); |
| 69 | $structured_data->generate_download_data( $post_id ); |
| 70 | $schema_output = $structured_data->get_data(); |
| 71 | |
| 72 | if ( ! \is_array( $schema_output ) || empty( $schema_output ) ) { |
| 73 | return []; |
| 74 | } |
| 75 | |
| 76 | // Ensure each piece has an @id. |
| 77 | foreach ( $schema_output as $key => $piece ) { |
| 78 | if ( ! isset( $piece['@id'] ) ) { |
| 79 | $schema_output[ $key ]['@id'] = $this->meta->for_post( $post_id )->canonical . '#/schema/edd-product/' . $post_id; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return $schema_output; |
| 84 | } catch ( Exception $e ) { |
| 85 | return []; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 |