current-page-helper.php
12.7 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<?php
namespace Yoast\WP\SEO\Helpers;
use WP_Post;
use Yoast\WP\SEO\Wrappers\WP_Query_Wrapper;
/**
* A helper object for WordPress posts.
*/
class Current_Page_Helper {
/**
* The WP Query wrapper.
*
* @var WP_Query_Wrapper
*/
private $wp_query_wrapper;
/**
* Current_Page_Helper constructor.
*
* @codeCoverageIgnore It only sets dependencies.
*
* @param WP_Query_Wrapper $wp_query_wrapper The wrapper for WP_Query.
*/
public function __construct(
WP_Query_Wrapper $wp_query_wrapper
) {
$this->wp_query_wrapper = $wp_query_wrapper;
}
/**
* Returns the page type for the current request.
*
* @codeCoverageIgnore It just depends on other functions for its result.
*
* @return string Page type.
*/
public function get_page_type() {
switch ( true ) {
case $this->is_search_result():
return 'Search_Result_Page';
case $this->is_static_posts_page():
return 'Static_Posts_Page';
case $this->is_home_static_page():
return 'Static_Home_Page';
case $this->is_home_posts_page():
return 'Home_Page';
case $this->is_simple_page():
return 'Post_Type';
case $this->is_post_type_archive():
return 'Post_Type_Archive';
case $this->is_term_archive():
return 'Term_Archive';
case $this->is_author_archive():
return 'Author_Archive';
case $this->is_date_archive():
return 'Date_Archive';
case $this->is_404():
return 'Error_Page';
}
return 'Fallback';
}
/**
* Checks if the currently opened page is a simple page.
*
* @return bool Whether the currently opened page is a simple page.
*/
public function is_simple_page() {
return $this->get_simple_page_id() > 0;
}
/**
* Returns the id of the currently opened page.
*
* @return int The id of the currently opened page.
*/
public function get_simple_page_id() {
if ( \is_singular() ) {
return \get_the_ID();
}
if ( $this->is_posts_page() ) {
return \get_option( 'page_for_posts' );
}
/**
* Filter: Allow changing the default page id.
*
* @api int $page_id The default page id.
*/
return \apply_filters( 'wpseo_frontend_page_type_simple_page_id', 0 );
}
/**
* Returns the id of the currently opened author archive.
*
* @codeCoverageIgnore It wraps WordPress functionality.
*
* @return int The id of the currently opened author archive.
*/
public function get_author_id() {
$wp_query = $this->wp_query_wrapper->get_main_query();
return $wp_query->get( 'author' );
}
/**
* Returns the id of the front page.
*
* @return int The id of the front page. 0 if the front page is not a static page.
*/
public function get_front_page_id() {
if ( \get_option( 'show_on_front' ) !== 'page' ) {
return 0;
}
return (int) \get_option( 'page_on_front' );
}
/**
* Returns the id of the currently opened term archive.
*
* @return int The id of the currently opened term archive.
*/
public function get_term_id() {
$wp_query = $this->wp_query_wrapper->get_main_query();
if ( $wp_query->is_category() ) {
return $wp_query->get( 'cat' );
}
if ( $wp_query->is_tag() ) {
return $wp_query->get( 'tag_id' );
}
if ( $wp_query->is_tax() ) {
$queried_object = $wp_query->get_queried_object();
if ( $queried_object && ! \is_wp_error( $queried_object ) ) {
return $queried_object->term_id;
}
}
return 0;
}
/**
* Returns the post type of the main query.
*
* @return string The post type of the main query.
*/
public function get_queried_post_type() {
$wp_query = $this->wp_query_wrapper->get_main_query();
$post_type = $wp_query->get( 'post_type' );
if ( \is_array( $post_type ) ) {
$post_type = \reset( $post_type );
}
return $post_type;
}
/**
* Returns the permalink of the currently opened date archive.
* If the permalink was cached, it returns this permalink.
* If not, we call another function to get the permalink through wp_query.
*
* @return string The permalink of the currently opened date archive.
*/
public function get_date_archive_permalink() {
static $date_archive_permalink;
if ( isset( $date_archive_permalink ) ) {
return $date_archive_permalink;
}
$date_archive_permalink = $this->get_non_cached_date_archive_permalink();
return $date_archive_permalink;
}
/**
* Determine whether this is the homepage and shows posts.
*
* @return bool Whether or not the current page is the homepage that displays posts.
*/
public function is_home_posts_page() {
$wp_query = $this->wp_query_wrapper->get_main_query();
if ( ! $wp_query->is_home() ) {
return false;
}
/*
* Whether the static page's `Homepage` option is actually not set to a page.
* Otherwise WordPress proceeds to handle the homepage as a `Your latest posts` page.
*/
if ( (int) \get_option( 'page_on_front' ) === 0 ) {
return true;
}
return \get_option( 'show_on_front' ) === 'posts';
}
/**
* Determine whether this is the static frontpage.
*
* @return bool Whether or not the current page is a static frontpage.
*/
public function is_home_static_page() {
$wp_query = $this->wp_query_wrapper->get_main_query();
if ( ! $wp_query->is_front_page() ) {
return false;
}
if ( \get_option( 'show_on_front' ) !== 'page' ) {
return false;
}
return $wp_query->is_page( \get_option( 'page_on_front' ) );
}
/**
* Determine whether this is the static posts page.
*
* @return bool Whether or not the current page is a static posts page.
*/
public function is_static_posts_page() {
$wp_query = $this->wp_query_wrapper->get_main_query();
$queried_object = $wp_query->get_queried_object();
return (
$wp_query->is_posts_page
&& \is_a( $queried_object, WP_Post::class )
&& $queried_object->post_type === 'page'
);
}
/**
* Determine whether this is the statically set posts page, when it's not the frontpage.
*
* @return bool Whether or not it's a non-frontpage, statically set posts page.
*/
public function is_posts_page() {
$wp_query = $this->wp_query_wrapper->get_main_query();
if ( ! $wp_query->is_home() ) {
return false;
}
return \get_option( 'show_on_front' ) === 'page';
}
/**
* Determine whether this is a post type archive.
*
* @codeCoverageIgnore It wraps WordPress functionality.
*
* @return bool Whether nor not the current page is a post type archive.
*/
public function is_post_type_archive() {
$wp_query = $this->wp_query_wrapper->get_main_query();
return $wp_query->is_post_type_archive();
}
/**
* Determine whether this is a term archive.
*
* @codeCoverageIgnore It wraps WordPress functionality.
*
* @return bool Whether nor not the current page is a term archive.
*/
public function is_term_archive() {
$wp_query = $this->wp_query_wrapper->get_main_query();
return $wp_query->is_tax || $wp_query->is_tag || $wp_query->is_category;
}
/**
* Determine whether this is an attachment page.
*
* @codeCoverageIgnore It wraps WordPress functionality.
*
* @return bool Whether nor not the current page is an attachment page.
*/
public function is_attachment() {
$wp_query = $this->wp_query_wrapper->get_main_query();
return $wp_query->is_attachment;
}
/**
* Determine whether this is an author archive.
*
* @codeCoverageIgnore It wraps WordPress functionality.
*
* @return bool Whether nor not the current page is an author archive.
*/
public function is_author_archive() {
$wp_query = $this->wp_query_wrapper->get_main_query();
return $wp_query->is_author();
}
/**
* Determine whether this is an date archive.
*
* @codeCoverageIgnore It wraps WordPress functionality.
*
* @return bool Whether nor not the current page is an date archive.
*/
public function is_date_archive() {
$wp_query = $this->wp_query_wrapper->get_main_query();
return $wp_query->is_date();
}
/**
* Determine whether this is a search result.
*
* @codeCoverageIgnore It wraps WordPress functionality.
*
* @return bool Whether nor not the current page is a search result.
*/
public function is_search_result() {
$wp_query = $this->wp_query_wrapper->get_main_query();
return $wp_query->is_search();
}
/**
* Determine whether this is a 404 page.
*
* @codeCoverageIgnore It wraps WordPress functionality.
*
* @return bool Whether nor not the current page is a 404 page.
*/
public function is_404() {
$wp_query = $this->wp_query_wrapper->get_main_query();
return $wp_query->is_404();
}
/**
* Checks if the current page is the post format archive.
*
* @codeCoverageIgnore It wraps WordPress functionality.
*
* @return bool Whether or not the current page is the post format archive.
*/
public function is_post_format_archive() {
$wp_query = $this->wp_query_wrapper->get_main_query();
return $wp_query->is_tax( 'post_format' );
}
/**
* Determine whether this page is an taxonomy archive page for multiple terms (url: /term-1,term2/).
*
* @return bool Whether or not the current page is an archive page for multiple terms.
*/
public function is_multiple_terms_page() {
if ( ! $this->is_term_archive() ) {
return false;
}
return $this->count_queried_terms() > 1;
}
/**
* Checks whether the current page is paged.
*
* @codeCoverageIgnore This method only calls a WordPress function.
*
* @return bool Whether the current page is paged.
*/
public function is_paged() {
return \is_paged();
}
/**
* Checks if the current page is the front page.
*
* @codeCoverageIgnore It wraps WordPress functionality.
*
* @return bool Whether or not the current page is the front page.
*/
public function is_front_page() {
$wp_query = $this->wp_query_wrapper->get_main_query();
return $wp_query->is_front_page();
}
/**
* Retrieves the current admin page.
*
* @codeCoverageIgnore It only wraps a global WordPress variable.
*
* @return string The current page.
*/
public function get_current_admin_page() {
global $pagenow;
return $pagenow;
}
/**
* Check if the current opened page is a Yoast SEO page.
*
* @return bool True when current page is a yoast seo plugin page.
*/
public function is_yoast_seo_page() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
if ( isset( $_GET['page'] ) && \is_string( $_GET['page'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are not processing form information, We are only using the variable in the strpos function.
$current_page = \wp_unslash( $_GET['page'] );
return \strpos( $current_page, 'wpseo_' ) === 0;
}
return false;
}
/**
* Returns the current Yoast SEO page.
* (E.g. the `page` query variable in the URL).
*
* @return string The current Yoast SEO page.
*/
public function get_current_yoast_seo_page() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
if ( isset( $_GET['page'] ) && \is_string( $_GET['page'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
return \sanitize_text_field( \wp_unslash( $_GET['page'] ) );
}
return '';
}
/**
* Checks if the current global post is the privacy policy page.
*
* @return bool current global post is set as privacy page
*/
public function current_post_is_privacy_policy() {
global $post;
if ( ! isset( $post->ID ) ) {
return false;
}
return \intval( $post->ID ) === \intval( \get_option( 'wp_page_for_privacy_policy', false ) );
}
/**
* Returns the permalink of the currently opened date archive.
*
* @return string The permalink of the currently opened date archive.
*/
protected function get_non_cached_date_archive_permalink() {
$date_archive_permalink = '';
$wp_query = $this->wp_query_wrapper->get_main_query();
if ( $wp_query->is_day() ) {
$date_archive_permalink = \get_day_link( $wp_query->get( 'year' ), $wp_query->get( 'monthnum' ), $wp_query->get( 'day' ) );
}
if ( $wp_query->is_month() ) {
$date_archive_permalink = \get_month_link( $wp_query->get( 'year' ), $wp_query->get( 'monthnum' ) );
}
if ( $wp_query->is_year() ) {
$date_archive_permalink = \get_year_link( $wp_query->get( 'year' ) );
}
return $date_archive_permalink;
}
/**
* Counts the total amount of queried terms.
*
* @codeCoverageIgnore This relies too much on WordPress dependencies.
*
* @return int The amoumt of queried terms.
*/
protected function count_queried_terms() {
$wp_query = $this->wp_query_wrapper->get_main_query();
$term = $wp_query->get_queried_object();
$queried_terms = $wp_query->tax_query->queried_terms;
if ( empty( $queried_terms[ $term->taxonomy ]['terms'] ) ) {
return 0;
}
return \count( $queried_terms[ $term->taxonomy ]['terms'] );
}
}