meta-surface.php
10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
namespace Yoast\WP\SEO\Surfaces;
use Yoast\WP\SEO\Context\Meta_Tags_Context;
use Yoast\WP\SEO\Helpers\Indexable_Helper;
use Yoast\WP\SEO\Memoizers\Meta_Tags_Context_Memoizer;
use Yoast\WP\SEO\Models\Indexable;
use Yoast\WP\SEO\Repositories\Indexable_Repository;
use Yoast\WP\SEO\Surfaces\Values\Meta;
use Yoast\WP\SEO\Wrappers\WP_Rewrite_Wrapper;
use YoastSEO_Vendor\Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Meta_Surface class.
*
* Surface for the indexables.
*/
class Meta_Surface {
/**
* The container.
*
* @var ContainerInterface
*/
private $container;
/**
* The memoizer for the meta tags context.
*
* @var Meta_Tags_Context_Memoizer
*/
private $context_memoizer;
/**
* The indexable repository.
*
* @var Indexable_Repository
*/
private $repository;
/**
* Holds the WP rewrite wrapper instance.
*
* @var WP_Rewrite_Wrapper
*/
private $wp_rewrite_wrapper;
/**
* The indexable helper.
*
* @var Indexable_Helper
*/
private $indexable_helper;
/**
* Meta_Surface constructor.
*
* @param ContainerInterface $container The DI container.
* @param Meta_Tags_Context_Memoizer $context_memoizer The meta tags context memoizer.
* @param Indexable_Repository $indexable_repository The indexable repository.
* @param WP_Rewrite_Wrapper $wp_rewrite_wrapper The WP rewrite wrapper.
* @param Indexable_Helper $indexable_helper The indexable helper.
*/
public function __construct(
ContainerInterface $container,
Meta_Tags_Context_Memoizer $context_memoizer,
Indexable_Repository $indexable_repository,
WP_Rewrite_Wrapper $wp_rewrite_wrapper,
Indexable_Helper $indexable_helper
) {
$this->container = $container;
$this->context_memoizer = $context_memoizer;
$this->repository = $indexable_repository;
$this->wp_rewrite_wrapper = $wp_rewrite_wrapper;
$this->indexable_helper = $indexable_helper;
}
/**
* Returns the meta tags context for the current page.
*
* @return Meta The meta values.
*/
public function for_current_page() {
return $this->build_meta( $this->context_memoizer->for_current_page() );
}
/**
* Returns the meta tags context for the home page.
*
* @return Meta|false The meta values. False if none could be found.
*/
public function for_home_page() {
$front_page_id = (int) \get_option( 'page_on_front' );
if ( \get_option( 'show_on_front' ) === 'page' && $front_page_id !== 0 ) {
$indexable = $this->repository->find_by_id_and_type( $front_page_id, 'post' );
if ( ! $indexable ) {
return false;
}
return $this->build_meta( $this->context_memoizer->get( $indexable, 'Static_Home_Page' ) );
}
$indexable = $this->repository->find_for_home_page();
if ( ! $indexable ) {
return false;
}
return $this->build_meta( $this->context_memoizer->get( $indexable, 'Home_Page' ) );
}
/**
* Returns the meta tags context for the posts page.
*
* @return Meta|false The meta values. False if none could be found.
*/
public function for_posts_page() {
$posts_page_id = (int) \get_option( 'page_for_posts' );
if ( $posts_page_id !== 0 ) {
$indexable = $this->repository->find_by_id_and_type( $posts_page_id, 'post' );
if ( ! $indexable ) {
return false;
}
return $this->build_meta( $this->context_memoizer->get( $indexable, 'Static_Posts_Page' ) );
}
$indexable = $this->repository->find_for_home_page();
if ( ! $indexable ) {
return false;
}
return $this->build_meta( $this->context_memoizer->get( $indexable, 'Home_Page' ) );
}
/**
* Returns the meta tags context for a post type archive.
*
* @param string|null $post_type Optional. The post type to get the archive meta for. Defaults to the current post type.
*
* @return Meta|false The meta values. False if none could be found.
*/
public function for_post_type_archive( $post_type = null ) {
if ( $post_type === null ) {
$post_type = \get_post_type();
}
$indexable = $this->repository->find_for_post_type_archive( $post_type );
if ( ! $indexable ) {
return false;
}
return $this->build_meta( $this->context_memoizer->get( $indexable, 'Post_Type_Archive' ) );
}
/**
* Returns the meta tags context for the search result page.
*
* @return Meta|false The meta values. False if none could be found.
*/
public function for_search_result() {
$indexable = $this->repository->find_for_system_page( 'search-result' );
if ( ! $indexable ) {
return false;
}
return $this->build_meta( $this->context_memoizer->get( $indexable, 'Search_Result_Page' ) );
}
/**
* Returns the meta tags context for the search result page.
*
* @return Meta|false The meta values. False if none could be found.
*/
public function for_404() {
$indexable = $this->repository->find_for_system_page( '404' );
if ( ! $indexable ) {
return false;
}
return $this->build_meta( $this->context_memoizer->get( $indexable, 'Error_Page' ) );
}
/**
* Returns the meta tags context for a post.
*
* @param int $id The ID of the post.
*
* @return Meta|false The meta values. False if none could be found.
*/
public function for_post( $id ) {
$indexable = $this->repository->find_by_id_and_type( $id, 'post' );
if ( ! $indexable ) {
return false;
}
return $this->build_meta( $this->context_memoizer->get( $indexable, 'Post_Type' ) );
}
/**
* Returns the meta tags context for a number of posts.
*
* @param int[] $ids The IDs of the posts.
*
* @return Meta[]|false The meta values. False if none could be found.
*/
public function for_posts( $ids ) {
$indexables = $this->repository->find_by_multiple_ids_and_type( $ids, 'post' );
if ( empty( $indexables ) ) {
return false;
}
// Remove all false values.
$indexables = \array_filter( $indexables );
return \array_map(
function( $indexable ) {
return $this->build_meta( $this->context_memoizer->get( $indexable, 'Post_Type' ) );
},
$indexables
);
}
/**
* Returns the meta tags context for a term.
*
* @param int $id The ID of the term.
*
* @return Meta|false The meta values. False if none could be found.
*/
public function for_term( $id ) {
$indexable = $this->repository->find_by_id_and_type( $id, 'term' );
if ( ! $indexable ) {
return false;
}
return $this->build_meta( $this->context_memoizer->get( $indexable, 'Term_Archive' ) );
}
/**
* Returns the meta tags context for an author.
*
* @param int $id The ID of the author.
*
* @return Meta|false The meta values. False if none could be found.
*/
public function for_author( $id ) {
$indexable = $this->repository->find_by_id_and_type( $id, 'user' );
if ( ! $indexable ) {
return false;
}
return $this->build_meta( $this->context_memoizer->get( $indexable, 'Author_Archive' ) );
}
/**
* Returns the meta for an indexable.
*
* @param Indexable $indexable The indexable.
* @param string|null $page_type Optional. The page type if already known.
*
* @return Meta|false The meta values. False if none could be found.
*/
public function for_indexable( $indexable, $page_type = null ) {
if ( ! \is_a( $indexable, Indexable::class ) ) {
return false;
}
if ( \is_null( $page_type ) ) {
$page_type = $this->indexable_helper->get_page_type_for_indexable( $indexable );
}
return $this->build_meta( $this->context_memoizer->get( $indexable, $page_type ) );
}
/**
* Returns the meta for an indexable.
*
* @param Indexable[] $indexables The indexables.
* @param string|null $page_type Optional. The page type if already known.
*
* @return Meta|false The meta values. False if none could be found.
*/
public function for_indexables( $indexables, $page_type = null ) {
$closure = function( $indexable ) use ( $page_type ) {
$this_page_type = $page_type;
if ( \is_null( $this_page_type ) ) {
$this_page_type = $this->indexable_helper->get_page_type_for_indexable( $indexable );
}
return $this->build_meta( $this->context_memoizer->get( $indexable, $this_page_type ) );
};
return \array_map( $closure, $indexables );
}
/**
* Returns the meta tags context for a url.
*
* @param string $url The url of the page. Required to be relative to the site url.
*
* @return Meta|false The meta values. False if none could be found.
*/
public function for_url( $url ) {
$url_parts = \wp_parse_url( $url );
$site_parts = \wp_parse_url( \site_url() );
if ( ( ! \is_array( $url_parts ) || ! \is_array( $site_parts ) )
|| ! isset( $url_parts['host'], $url_parts['path'], $site_parts['host'], $site_parts['scheme'] )
) {
return false;
}
if ( $url_parts['host'] !== $site_parts['host'] ) {
return false;
}
// Ensure the scheme is consistent with values in the DB.
$url = $site_parts['scheme'] . '://' . $url_parts['host'] . $url_parts['path'];
if ( $this->is_date_archive_url( $url ) ) {
$indexable = $this->repository->find_for_date_archive();
}
else {
$indexable = $this->repository->find_by_permalink( $url );
}
// If we still don't have an indexable abort, the WP globals could be anything so we can't use the unknown indexable.
if ( ! $indexable ) {
return false;
}
$page_type = $this->indexable_helper->get_page_type_for_indexable( $indexable );
if ( $page_type === false ) {
return false;
}
return $this->build_meta( $this->context_memoizer->get( $indexable, $page_type ) );
}
/**
* Checks if a given URL is a date archive URL.
*
* @param string $url The url.
*
* @return bool
*/
protected function is_date_archive_url( $url ) {
$path = \wp_parse_url( $url, \PHP_URL_PATH );
if ( $path === null ) {
return false;
}
$path = \ltrim( $path, '/' );
$wp_rewrite = $this->wp_rewrite_wrapper->get();
$date_rewrite = $wp_rewrite->generate_rewrite_rules( $wp_rewrite->get_date_permastruct(), \EP_DATE );
$date_rewrite = \apply_filters( 'date_rewrite_rules', $date_rewrite );
foreach ( (array) $date_rewrite as $match => $query ) {
if ( \preg_match( "#^$match#", $path ) ) {
return true;
}
}
return false;
}
/**
* Creates a new meta value object
*
* @param Meta_Tags_Context $context The meta tags context.
*
* @return Meta The meta value
*/
protected function build_meta( Meta_Tags_Context $context ) {
return new Meta( $context, $this->container );
}
}