| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
use WP_Post; |
| 6 |
use Yoast\WP\SEO\Wrappers\WP_Query_Wrapper; |
| 7 |
|
| 8 |
/** |
| 9 |
* A helper object for WordPress posts. |
| 10 |
*/ |
| 11 |
class Current_Page_Helper { |
| 12 |
|
| 13 |
/** |
| 14 |
* The WP Query wrapper. |
| 15 |
* |
| 16 |
* @var WP_Query_Wrapper |
| 17 |
*/ |
| 18 |
private $wp_query_wrapper; |
| 19 |
|
| 20 |
/** |
| 21 |
* Current_Page_Helper constructor. |
| 22 |
* |
| 23 |
* @codeCoverageIgnore It only sets dependencies. |
| 24 |
* |
| 25 |
* @param WP_Query_Wrapper $wp_query_wrapper The wrapper for WP_Query. |
| 26 |
*/ |
| 27 |
public function __construct( |
| 28 |
WP_Query_Wrapper $wp_query_wrapper |
| 29 |
) { |
| 30 |
$this->wp_query_wrapper = $wp_query_wrapper; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Returns the page type for the current request. |
| 35 |
* |
| 36 |
* @codeCoverageIgnore It just depends on other functions for its result. |
| 37 |
* |
| 38 |
* @return string Page type. |
| 39 |
*/ |
| 40 |
public function get_page_type() { |
| 41 |
switch ( true ) { |
| 42 |
case $this->is_search_result(): |
| 43 |
return 'Search_Result_Page'; |
| 44 |
case $this->is_static_posts_page(): |
| 45 |
return 'Static_Posts_Page'; |
| 46 |
case $this->is_home_static_page(): |
| 47 |
return 'Static_Home_Page'; |
| 48 |
case $this->is_home_posts_page(): |
| 49 |
return 'Home_Page'; |
| 50 |
case $this->is_simple_page(): |
| 51 |
return 'Post_Type'; |
| 52 |
case $this->is_post_type_archive(): |
| 53 |
return 'Post_Type_Archive'; |
| 54 |
case $this->is_term_archive(): |
| 55 |
return 'Term_Archive'; |
| 56 |
case $this->is_author_archive(): |
| 57 |
return 'Author_Archive'; |
| 58 |
case $this->is_date_archive(): |
| 59 |
return 'Date_Archive'; |
| 60 |
case $this->is_404(): |
| 61 |
return 'Error_Page'; |
| 62 |
} |
| 63 |
|
| 64 |
return 'Fallback'; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Checks if the currently opened page is a simple page. |
| 69 |
* |
| 70 |
* @return bool Whether the currently opened page is a simple page. |
| 71 |
*/ |
| 72 |
public function is_simple_page() { |
| 73 |
return $this->get_simple_page_id() > 0; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Returns the id of the currently opened page. |
| 78 |
* |
| 79 |
* @return int The id of the currently opened page. |
| 80 |
*/ |
| 81 |
public function get_simple_page_id() { |
| 82 |
if ( \is_singular() ) { |
| 83 |
return \get_the_ID(); |
| 84 |
} |
| 85 |
|
| 86 |
if ( $this->is_posts_page() ) { |
| 87 |
return \get_option( 'page_for_posts' ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Filter: Allow changing the default page id. |
| 92 |
* |
| 93 |
* @api int $page_id The default page id. |
| 94 |
*/ |
| 95 |
return \apply_filters( 'wpseo_frontend_page_type_simple_page_id', 0 ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Returns the id of the currently opened author archive. |
| 100 |
* |
| 101 |
* @codeCoverageIgnore It wraps WordPress functionality. |
| 102 |
* |
| 103 |
* @return int The id of the currently opened author archive. |
| 104 |
*/ |
| 105 |
public function get_author_id() { |
| 106 |
$wp_query = $this->wp_query_wrapper->get_main_query(); |
| 107 |
|
| 108 |
return $wp_query->get( 'author' ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Returns the id of the front page. |
| 113 |
* |
| 114 |
* @return int The id of the front page. 0 if the front page is not a static page. |
| 115 |
*/ |
| 116 |
public function get_front_page_id() { |
| 117 |
if ( \get_option( 'show_on_front' ) !== 'page' ) { |
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
return (int) \get_option( 'page_on_front' ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Returns the id of the currently opened term archive. |
| 126 |
* |
| 127 |
* @return int The id of the currently opened term archive. |
| 128 |
*/ |
| 129 |
public function get_term_id() { |
| 130 |
$wp_query = $this->wp_query_wrapper->get_main_query(); |
| 131 |
|
| 132 |
if ( $wp_query->is_category() ) { |
| 133 |
return $wp_query->get( 'cat' ); |
| 134 |
} |
| 135 |
if ( $wp_query->is_tag() ) { |
| 136 |
return $wp_query->get( 'tag_id' ); |
| 137 |
} |
| 138 |
if ( $wp_query->is_tax() ) { |
| 139 |
$queried_object = $wp_query->get_queried_object(); |
| 140 |
if ( $queried_object && ! \is_wp_error( $queried_object ) ) { |
| 141 |
return $queried_object->term_id; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Returns the post type of the main query. |
| 150 |
* |
| 151 |
* @return string The post type of the main query. |
| 152 |
*/ |
| 153 |
public function get_queried_post_type() { |
| 154 |
$wp_query = $this->wp_query_wrapper->get_main_query(); |
| 155 |
$post_type = $wp_query->get( 'post_type' ); |
| 156 |
if ( \is_array( $post_type ) ) { |
| 157 |
$post_type = \reset( $post_type ); |
| 158 |
} |
| 159 |
|
| 160 |
return $post_type; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Returns the permalink of the currently opened date archive. |
| 165 |
* If the permalink was cached, it returns this permalink. |
| 166 |
* If not, we call another function to get the permalink through wp_query. |
| 167 |
* |
| 168 |
* @return string The permalink of the currently opened date archive. |
| 169 |
*/ |
| 170 |
public function get_date_archive_permalink() { |
| 171 |
static $date_archive_permalink; |
| 172 |
|
| 173 |
if ( isset( $date_archive_permalink ) ) { |
| 174 |
return $date_archive_permalink; |
| 175 |
} |
| 176 |
|
| 177 |
$date_archive_permalink = $this->get_non_cached_date_archive_permalink(); |
| 178 |
|
| 179 |
return $date_archive_permalink; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Determine whether this is the homepage and shows posts. |
| 184 |
* |
| 185 |
* @return bool Whether or not the current page is the homepage that displays posts. |
| 186 |
*/ |
| 187 |
public function is_home_posts_page() { |
| 188 |
$wp_query = $this->wp_query_wrapper->get_main_query(); |
| 189 |
|
| 190 |
if ( ! $wp_query->is_home() ) { |
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
/* |
| 195 |
* Whether the static page's `Homepage` option is actually not set to a page. |
| 196 |
* Otherwise WordPress proceeds to handle the homepage as a `Your latest posts` page. |
| 197 |
*/ |
| 198 |
if ( (int) \get_option( 'page_on_front' ) === 0 ) { |
| 199 |
return true; |
| 200 |
} |
| 201 |
|
| 202 |
return \get_option( 'show_on_front' ) === 'posts'; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Determine whether this is the static frontpage. |
| 207 |
* |
| 208 |
* @return bool Whether or not the current page is a static frontpage. |
| 209 |
*/ |
| 210 |
public function is_home_static_page() { |
| 211 |
$wp_query = $this->wp_query_wrapper->get_main_query(); |
| 212 |
|
| 213 |
if ( ! $wp_query->is_front_page() ) { |
| 214 |
return false; |
| 215 |
} |
| 216 |
|
| 217 |
if ( \get_option( 'show_on_front' ) !== 'page' ) { |
| 218 |
return false; |
| 219 |
} |
| 220 |
|
| 221 |
return $wp_query->is_page( \get_option( 'page_on_front' ) ); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Determine whether this is the static posts page. |
| 226 |
* |
| 227 |
* @return bool Whether or not the current page is a static posts page. |
| 228 |
*/ |
| 229 |
public function is_static_posts_page() { |
| 230 |
$wp_query = $this->wp_query_wrapper->get_main_query(); |
| 231 |
$queried_object = $wp_query->get_queried_object(); |
| 232 |
|
| 233 |
return ( |
| 234 |
$wp_query->is_posts_page |
| 235 |
&& \is_a( $queried_object, WP_Post::class ) |
| 236 |
&& $queried_object->post_type === 'page' |
| 237 |
); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Determine whether this is the statically set posts page, when it's not the frontpage. |
| 242 |
* |
| 243 |
* @return bool Whether or not it's a non-frontpage, statically set posts page. |
| 244 |
*/ |
| 245 |
public function is_posts_page() { |
| 246 |
$wp_query = $this->wp_query_wrapper->get_main_query(); |
| 247 |
|
| 248 |
if ( ! $wp_query->is_home() ) { |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
return \get_option( 'show_on_front' ) === 'page'; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Determine whether this is a post type archive. |
| 257 |
* |
| 258 |
* @codeCoverageIgnore It wraps WordPress functionality. |
| 259 |
* |
| 260 |
* @return bool Whether nor not the current page is a post type archive. |
| 261 |
*/ |
| 262 |
public function is_post_type_archive() { |
| 263 |
$wp_query = $this->wp_query_wrapper->get_main_query(); |
| 264 |
|
| 265 |
return $wp_query->is_post_type_archive(); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Determine whether this is a term archive. |
| 270 |
* |
| 271 |
* @codeCoverageIgnore It wraps WordPress functionality. |
| 272 |
* |
| 273 |
* @return bool Whether nor not the current page is a term archive. |
| 274 |
*/ |
| 275 |
public function is_term_archive() { |
| 276 |
$wp_query = $this->wp_query_wrapper->get_main_query(); |
| 277 |
|
| 278 |
return $wp_query->is_tax || $wp_query->is_tag || $wp_query->is_category; |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Determine whether this is an attachment page. |
| 283 |
* |
| 284 |
* @codeCoverageIgnore It wraps WordPress functionality. |
| 285 |
* |
| 286 |
* @return bool Whether nor not the current page is an attachment page. |
| 287 |
*/ |
| 288 |
public function is_attachment() { |
| 289 |
$wp_query = $this->wp_query_wrapper->get_main_query(); |
| 290 |
|
| 291 |
return $wp_query->is_attachment; |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Determine whether this is an author archive. |
| 296 |
* |
| 297 |
* @codeCoverageIgnore It wraps WordPress functionality. |
| 298 |
* |
| 299 |
* @return bool Whether nor not the current page is an author archive. |
| 300 |
*/ |
| 301 |
public function is_author_archive() { |
| 302 |
$wp_query = $this->wp_query_wrapper->get_main_query(); |
| 303 |
|
| 304 |
return $wp_query->is_author(); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Determine whether this is an date archive. |
| 309 |
* |
| 310 |
* @codeCoverageIgnore It wraps WordPress functionality. |
| 311 |
* |
| 312 |
* @return bool Whether nor not the current page is an date archive. |
| 313 |
*/ |
| 314 |
public function is_date_archive() { |
| 315 |
$wp_query = $this->wp_query_wrapper->get_main_query(); |
| 316 |
|
| 317 |
return $wp_query->is_date(); |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Determine whether this is a search result. |
| 322 |
* |
| 323 |
* @codeCoverageIgnore It wraps WordPress functionality. |
| 324 |
* |
| 325 |
* @return bool Whether nor not the current page is a search result. |
| 326 |
*/ |
| 327 |
public function is_search_result() { |
| 328 |
$wp_query = $this->wp_query_wrapper->get_main_query(); |
| 329 |
|
| 330 |
return $wp_query->is_search(); |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Determine whether this is a 404 page. |
| 335 |
* |
| 336 |
* @codeCoverageIgnore It wraps WordPress functionality. |
| 337 |
* |
| 338 |
* @return bool Whether nor not the current page is a 404 page. |
| 339 |
*/ |
| 340 |
public function is_404() { |
| 341 |
$wp_query = $this->wp_query_wrapper->get_main_query(); |
| 342 |
|
| 343 |
return $wp_query->is_404(); |
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
* Checks if the current page is the post format archive. |
| 348 |
* |
| 349 |
* @codeCoverageIgnore It wraps WordPress functionality. |
| 350 |
* |
| 351 |
* @return bool Whether or not the current page is the post format archive. |
| 352 |
*/ |
| 353 |
public function is_post_format_archive() { |
| 354 |
$wp_query = $this->wp_query_wrapper->get_main_query(); |
| 355 |
|
| 356 |
return $wp_query->is_tax( 'post_format' ); |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Determine whether this page is an taxonomy archive page for multiple terms (url: /term-1,term2/). |
| 361 |
* |
| 362 |
* @return bool Whether or not the current page is an archive page for multiple terms. |
| 363 |
*/ |
| 364 |
public function is_multiple_terms_page() { |
| 365 |
if ( ! $this->is_term_archive() ) { |
| 366 |
return false; |
| 367 |
} |
| 368 |
|
| 369 |
return $this->count_queried_terms() > 1; |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* Checks whether the current page is paged. |
| 374 |
* |
| 375 |
* @codeCoverageIgnore This method only calls a WordPress function. |
| 376 |
* |
| 377 |
* @return bool Whether the current page is paged. |
| 378 |
*/ |
| 379 |
public function is_paged() { |
| 380 |
return \is_paged(); |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* Checks if the current page is the front page. |
| 385 |
* |
| 386 |
* @codeCoverageIgnore It wraps WordPress functionality. |
| 387 |
* |
| 388 |
* @return bool Whether or not the current page is the front page. |
| 389 |
*/ |
| 390 |
public function is_front_page() { |
| 391 |
$wp_query = $this->wp_query_wrapper->get_main_query(); |
| 392 |
|
| 393 |
return $wp_query->is_front_page(); |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* Retrieves the current admin page. |
| 398 |
* |
| 399 |
* @codeCoverageIgnore It only wraps a global WordPress variable. |
| 400 |
* |
| 401 |
* @return string The current page. |
| 402 |
*/ |
| 403 |
public function get_current_admin_page() { |
| 404 |
global $pagenow; |
| 405 |
|
| 406 |
return $pagenow; |
| 407 |
} |
| 408 |
|
| 409 |
/** |
| 410 |
* Check if the current opened page is a Yoast SEO page. |
| 411 |
* |
| 412 |
* @return bool True when current page is a yoast seo plugin page. |
| 413 |
*/ |
| 414 |
public function is_yoast_seo_page() { |
| 415 |
static $is_yoast_seo; |
| 416 |
|
| 417 |
if ( $is_yoast_seo === null ) { |
| 418 |
$current_page = \filter_input( \INPUT_GET, 'page' ); |
| 419 |
$is_yoast_seo = ( \is_string( $current_page ) && \strpos( $current_page, 'wpseo_' ) === 0 ); |
| 420 |
} |
| 421 |
|
| 422 |
return $is_yoast_seo; |
| 423 |
} |
| 424 |
|
| 425 |
/** |
| 426 |
* Returns the current Yoast SEO page. |
| 427 |
* (E.g. the `page` query variable in the URL). |
| 428 |
* |
| 429 |
* @return string The current Yoast SEO page. |
| 430 |
*/ |
| 431 |
public function get_current_yoast_seo_page() { |
| 432 |
static $current_yoast_seo_page; |
| 433 |
|
| 434 |
if ( $current_yoast_seo_page === null ) { |
| 435 |
$current_yoast_seo_page = \filter_input( \INPUT_GET, 'page' ); |
| 436 |
} |
| 437 |
|
| 438 |
return $current_yoast_seo_page; |
| 439 |
} |
| 440 |
|
| 441 |
/** |
| 442 |
* Checks if the current global post is the privacy policy page. |
| 443 |
* |
| 444 |
* @return bool current global post is set as privacy page |
| 445 |
*/ |
| 446 |
public function current_post_is_privacy_policy() { |
| 447 |
global $post; |
| 448 |
|
| 449 |
if ( ! isset( $post->ID ) ) { |
| 450 |
return false; |
| 451 |
} |
| 452 |
|
| 453 |
return intval( $post->ID ) === intval( \get_option( 'wp_page_for_privacy_policy', false ) ); |
| 454 |
} |
| 455 |
|
| 456 |
/** |
| 457 |
* Returns the permalink of the currently opened date archive. |
| 458 |
* |
| 459 |
* @return string The permalink of the currently opened date archive. |
| 460 |
*/ |
| 461 |
protected function get_non_cached_date_archive_permalink() { |
| 462 |
$date_archive_permalink = ''; |
| 463 |
$wp_query = $this->wp_query_wrapper->get_main_query(); |
| 464 |
|
| 465 |
if ( $wp_query->is_day() ) { |
| 466 |
$date_archive_permalink = \get_day_link( $wp_query->get( 'year' ), $wp_query->get( 'monthnum' ), $wp_query->get( 'day' ) ); |
| 467 |
} |
| 468 |
if ( $wp_query->is_month() ) { |
| 469 |
$date_archive_permalink = \get_month_link( $wp_query->get( 'year' ), $wp_query->get( 'monthnum' ) ); |
| 470 |
} |
| 471 |
if ( $wp_query->is_year() ) { |
| 472 |
$date_archive_permalink = \get_year_link( $wp_query->get( 'year' ) ); |
| 473 |
} |
| 474 |
|
| 475 |
return $date_archive_permalink; |
| 476 |
} |
| 477 |
|
| 478 |
/** |
| 479 |
* Counts the total amount of queried terms. |
| 480 |
* |
| 481 |
* @codeCoverageIgnore This relies too much on WordPress dependencies. |
| 482 |
* |
| 483 |
* @return int The amoumt of queried terms. |
| 484 |
*/ |
| 485 |
protected function count_queried_terms() { |
| 486 |
$wp_query = $this->wp_query_wrapper->get_main_query(); |
| 487 |
$term = $wp_query->get_queried_object(); |
| 488 |
$queried_terms = $wp_query->tax_query->queried_terms; |
| 489 |
if ( empty( $queried_terms[ $term->taxonomy ]['terms'] ) ) { |
| 490 |
return 0; |
| 491 |
} |
| 492 |
|
| 493 |
return \count( $queried_terms[ $term->taxonomy ]['terms'] ); |
| 494 |
} |
| 495 |
} |
| 496 |
|