PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.5
28.6 28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 All 130 releases
wordpress-seo / src / schema-aggregator / infrastructure / schema-pieces / schema-piece-repository.php

schema-piece-repository.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.5, at src/schema-aggregator/infrastructure/schema-pieces/schema-piece-repository.php

198 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 Yoast\WP\SEO\Helpers\Indexable_Helper;
6 use Yoast\WP\SEO\Memoizers\Meta_Tags_Context_Memoizer;
7 use Yoast\WP\SEO\Schema_Aggregator\Application\Enhancement\Schema_Enhancement_Factory;
8 use Yoast\WP\SEO\Schema_Aggregator\Domain\External_Schema_Piece_Repository_Interface;
9 use Yoast\WP\SEO\Schema_Aggregator\Domain\Schema_Piece;
10 use Yoast\WP\SEO\Schema_Aggregator\Domain\Schema_Piece_Collection;
11 use Yoast\WP\SEO\Schema_Aggregator\Domain\Schema_Piece_Repository_Interface;
12 use Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Aggregator_Config;
13 use Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Indexable_Repository\Indexable_Repository_Factory;
14 use Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Meta_Tags_Context_Memoizer_Adapter;
15
16 /**
17 * The Schema_Piece repository.
18 */
19 class Schema_Piece_Repository implements Schema_Piece_Repository_Interface {
20
21 /**
22 * The meta tags context memoizer.
23 *
24 * @var Meta_Tags_Context_Memoizer
25 */
26 private $memoizer;
27
28 /**
29 * The indexable helper.
30 *
31 * @var Indexable_Helper
32 */
33 private $indexable_helper;
34
35 /**
36 * The meta tags context adapter factory.
37 *
38 * @var Meta_Tags_Context_Memoizer_Adapter
39 */
40 private $adapter;
41
42 /**
43 * Configuration provider.
44 *
45 * @var Aggregator_Config
46 */
47 private $config;
48
49 /**
50 * The schema enhancement factory.
51 *
52 * @var Schema_Enhancement_Factory
53 */
54 private $enhancement_factory;
55
56 /**
57 * The indexable repository factory.
58 *
59 * @var Indexable_Repository_Factory
60 */
61 private $indexable_repository_factory;
62
63 /**
64 * The WordPress global state adapter.
65 *
66 * @var WordPress_Global_State_Adapter
67 */
68 private $global_state_adapter;
69
70 /**
71 * External schema piece repositories.
72 *
73 * @var array<External_Schema_Piece_Repository_Interface>
74 */
75 private $external_repositories;
76
77 /**
78 * Constructor.
79 *
80 * @param Meta_Tags_Context_Memoizer $memoizer The meta tags context memoizer.
81 * @param Indexable_Helper $indexable_helper The indexable helper.
82 * @param Meta_Tags_Context_Memoizer_Adapter $adapter The adapter factory.
83 * @param Aggregator_Config $config The configuration provider.
84 * @param Schema_Enhancement_Factory $enhancement_factory The schema enhancement factory.
85 * @param Indexable_Repository_Factory $indexable_repository_factory The indexable repository factory.
86 * @param WordPress_Global_State_Adapter $global_state_adapter The global state adapter.
87 * @param External_Schema_Piece_Repository_Interface ...$external_repositories The external schema piece repositories.
88 */
89 public function __construct(
90 Meta_Tags_Context_Memoizer $memoizer,
91 Indexable_Helper $indexable_helper,
92 Meta_Tags_Context_Memoizer_Adapter $adapter,
93 Aggregator_Config $config,
94 Schema_Enhancement_Factory $enhancement_factory,
95 Indexable_Repository_Factory $indexable_repository_factory,
96 WordPress_Global_State_Adapter $global_state_adapter,
97 External_Schema_Piece_Repository_Interface ...$external_repositories
98 ) {
99 $this->memoizer = $memoizer;
100 $this->indexable_helper = $indexable_helper;
101 $this->adapter = $adapter;
102 $this->config = $config;
103 $this->enhancement_factory = $enhancement_factory;
104 $this->indexable_repository_factory = $indexable_repository_factory;
105 $this->global_state_adapter = $global_state_adapter;
106 $this->external_repositories = $external_repositories;
107 }
108
109 /**
110 * Gets the indexables to be aggregated.
111 *
112 * @param int $page The page number (1-based).
113 * @param int $page_size The number of items per page.
114 * @param string $post_type The post type to filter by.
115 *
116 * @return Schema_Piece_Collection The aggregated schema.
117 */
118 public function get( int $page, int $page_size, string $post_type ): Schema_Piece_Collection {
119 $indexable_repository = $this->indexable_repository_factory->get_repository( $this->indexable_helper->should_index_indexables() );
120 $indexables = $indexable_repository->get( $page, $page_size, $post_type );
121 $schema_pieces = [];
122
123 foreach ( $indexables as $indexable ) {
124 if ( ! \in_array( $indexable->object_sub_type, $this->config->get_allowed_post_types(), true ) ) {
125 continue;
126 }
127
128 $this->global_state_adapter->set_global_state( $indexable );
129 $page_type = $this->indexable_helper->get_page_type_for_indexable( $indexable );
130 $context = $this->memoizer->get( $indexable, $page_type );
131 $context_array = $this->adapter->meta_tags_context_to_array( $context );
132 $pieces_data = $context_array['@graph'];
133
134 // Collect external schema pieces from all supporting repositories.
135 $pieces_data = $this->collect_external_schema( $pieces_data, $post_type, $indexable->object_id );
136
137 foreach ( $pieces_data as $piece_data ) {
138 $schema_piece = new Schema_Piece( $piece_data, $piece_data['@type'] );
139 $enhancer = $this->enhancement_factory->get_enhancer( $this->get_all_schema_types( $context_array['@graph'] ) );
140 if ( $enhancer !== null ) {
141 $schema_piece = $enhancer->enhance( $schema_piece, $indexable );
142 }
143 $schema_pieces[] = $schema_piece;
144 }
145
146 $this->global_state_adapter->reset_global_state();
147 }
148
149 return new Schema_Piece_Collection( $schema_pieces );
150 }
151
152 /**
153 * Collects external schema pieces from all supporting repositories.
154 *
155 * @param array<array<string,string|int|bool|array>> $pieces_data The existing schema pieces.
156 * @param string $post_type The post type.
157 * @param int $post_id The post ID.
158 *
159 * @return array<array<string,string|int|bool|array>> The schema pieces with external pieces added.
160 */
161 private function collect_external_schema( array $pieces_data, string $post_type, int $post_id ): array {
162 foreach ( $this->external_repositories as $repository ) {
163 if ( $repository->supports( $post_type ) ) {
164 $external_pieces = $repository->collect( $post_id );
165 $pieces_data = \array_merge( $pieces_data, $external_pieces );
166 }
167 }
168
169 return $pieces_data;
170 }
171
172 /**
173 * All schema types present in the schema piece.
174 *
175 * @param array<array<string>> $graph The current graph.
176 *
177 * @return array<string>
178 */
179 private function get_all_schema_types( array $graph ): array {
180 $schema_types = [];
181 foreach ( $graph as $schema_values ) {
182 foreach ( $schema_values as $key => $value ) {
183 if ( $key === '@type' ) {
184 if ( \is_array( $value ) ) {
185 foreach ( $value as $type_value ) {
186 $schema_types[ $type_value ] = $type_value;
187 }
188 continue;
189 }
190 $schema_types[ $value ] = $value;
191 }
192 }
193 }
194
195 return $schema_types;
196 }
197 }
198