| @@ -23,11 +23,9 @@ | ||
| 23 | 23 | * @codeCoverageIgnore It only sets dependencies. |
| 24 | 24 | * |
| 25 | 25 | * @param WP_Query_Wrapper $wp_query_wrapper The wrapper for WP_Query. |
| 26 | 26 | */ |
| 27 | - public function __construct( | |
| 28 | - WP_Query_Wrapper $wp_query_wrapper | |
| 29 | - ) { | |
| 27 | + public function __construct( WP_Query_Wrapper $wp_query_wrapper ) { | |
| 30 | 28 | $this->wp_query_wrapper = $wp_query_wrapper; |
| 31 | 29 | } |
| 32 | 30 | |
| 33 | 31 | /** |
| @@ -89,9 +87,9 @@ | ||
| 89 | 87 | |
| 90 | 88 | /** |
| 91 | 89 | * Filter: Allow changing the default page id. |
| 92 | 90 | * |
| 93 | - * @api int $page_id The default page id. | |
| 91 | + * @param int $page_id The default page id. | |
| 94 | 92 | */ |
| 95 | 93 | return \apply_filters( 'wpseo_frontend_page_type_simple_page_id', 0 ); |
| 96 | 94 | } |
| 97 | 95 | |
| @@ -128,15 +126,9 @@ | ||
| 128 | 126 | */ |
| 129 | 127 | public function get_term_id() { |
| 130 | 128 | $wp_query = $this->wp_query_wrapper->get_main_query(); |
| 131 | 129 | |
| 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() ) { | |
| 130 | + if ( $wp_query->is_tax() || $wp_query->is_tag() || $wp_query->is_category() ) { | |
| 139 | 131 | $queried_object = $wp_query->get_queried_object(); |
| 140 | 132 | if ( $queried_object && ! \is_wp_error( $queried_object ) ) { |
| 141 | 133 | return $queried_object->term_id; |
| 142 | 134 | } |
| @@ -406,21 +398,42 @@ | ||
| 406 | 398 | return $pagenow; |
| 407 | 399 | } |
| 408 | 400 | |
| 409 | 401 | /** |
| 402 | + * Determines whether the current admin screen is the block editor. | |
| 403 | + * | |
| 404 | + * Deliberately uses get_current_screen() and not WP_Screen::get(): the latter re-applies | |
| 405 | + * the replace_editor filter on every call, which is a render hook with side effects | |
| 406 | + * (e.g. it breaks the WooCommerce block email editor). | |
| 407 | + * | |
| 408 | + * Only reliable after set_current_screen() has run (during admin page load, after | |
| 409 | + * admin_init); earlier calls return false. | |
| 410 | + * | |
| 411 | + * @return bool Whether the current screen is the block editor. | |
| 412 | + */ | |
| 413 | + public function is_block_editor() { | |
| 414 | + if ( ! \function_exists( 'get_current_screen' ) ) { | |
| 415 | + return false; | |
| 416 | + } | |
| 417 | + | |
| 418 | + $screen = \get_current_screen(); | |
| 419 | + | |
| 420 | + return $screen !== null && $screen->is_block_editor(); | |
| 421 | + } | |
| 422 | + | |
| 423 | + /** | |
| 410 | 424 | * Check if the current opened page is a Yoast SEO page. |
| 411 | 425 | * |
| 412 | 426 | * @return bool True when current page is a yoast seo plugin page. |
| 413 | 427 | */ |
| 414 | 428 | 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 ); | |
| 429 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. | |
| 430 | + if ( isset( $_GET['page'] ) && \is_string( $_GET['page'] ) ) { | |
| 431 | + // 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. | |
| 432 | + $current_page = \wp_unslash( $_GET['page'] ); | |
| 433 | + return \strpos( $current_page, 'wpseo_' ) === 0; | |
| 420 | 434 | } |
| 421 | - | |
| 422 | - return $is_yoast_seo; | |
| 435 | + return false; | |
| 423 | 436 | } |
| 424 | 437 | |
| 425 | 438 | /** |
| 426 | 439 | * Returns the current Yoast SEO page. |
| @@ -428,15 +441,27 @@ | ||
| 428 | 441 | * |
| 429 | 442 | * @return string The current Yoast SEO page. |
| 430 | 443 | */ |
| 431 | 444 | public function get_current_yoast_seo_page() { |
| 432 | - static $current_yoast_seo_page; | |
| 445 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. | |
| 446 | + if ( isset( $_GET['page'] ) && \is_string( $_GET['page'] ) ) { | |
| 447 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. | |
| 448 | + return \sanitize_text_field( \wp_unslash( $_GET['page'] ) ); | |
| 449 | + } | |
| 433 | 450 | |
| 434 | - if ( $current_yoast_seo_page === null ) { | |
| 435 | - $current_yoast_seo_page = \filter_input( \INPUT_GET, 'page' ); | |
| 436 | - } | |
| 451 | + return ''; | |
| 452 | + } | |
| 437 | 453 | |
| 438 | - return $current_yoast_seo_page; | |
| 454 | + /** | |
| 455 | + * Returns whether the current admin overview (list table) shows the trash. | |
| 456 | + * | |
| 457 | + * Mirrors the check WP_Posts_List_Table itself uses to decide it is on the trash view. | |
| 458 | + * | |
| 459 | + * @return bool Whether the current admin overview shows the trash. | |
| 460 | + */ | |
| 461 | + public function is_trash_overview(): bool { | |
| 462 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. | |
| 463 | + return isset( $_REQUEST['post_status'] ) && $_REQUEST['post_status'] === 'trash'; | |
| 439 | 464 | } |
| 440 | 465 | |
| 441 | 466 | /** |
| 442 | 467 | * Checks if the current global post is the privacy policy page. |
| @@ -449,9 +474,9 @@ | ||
| 449 | 474 | if ( ! isset( $post->ID ) ) { |
| 450 | 475 | return false; |
| 451 | 476 | } |
| 452 | 477 | |
| 453 | - return intval( $post->ID ) === intval( \get_option( 'wp_page_for_privacy_policy', false ) ); | |
| 478 | + return (int) $post->ID === (int) \get_option( 'wp_page_for_privacy_policy', false ); | |
| 454 | 479 | } |
| 455 | 480 | |
| 456 | 481 | /** |
| 457 | 482 | * Returns the permalink of the currently opened date archive. |
| @@ -482,14 +507,86 @@ | ||
| 482 | 507 | * |
| 483 | 508 | * @return int The amoumt of queried terms. |
| 484 | 509 | */ |
| 485 | 510 | protected function count_queried_terms() { |
| 486 | - $wp_query = $this->wp_query_wrapper->get_main_query(); | |
| 487 | - $term = $wp_query->get_queried_object(); | |
| 511 | + $wp_query = $this->wp_query_wrapper->get_main_query(); | |
| 512 | + $term = $wp_query->get_queried_object(); | |
| 513 | + | |
| 488 | 514 | $queried_terms = $wp_query->tax_query->queried_terms; |
| 489 | - if ( empty( $queried_terms[ $term->taxonomy ]['terms'] ) ) { | |
| 515 | + if ( $term === null || empty( $queried_terms[ $term->taxonomy ]['terms'] ) ) { | |
| 490 | 516 | return 0; |
| 491 | 517 | } |
| 492 | 518 | |
| 493 | 519 | return \count( $queried_terms[ $term->taxonomy ]['terms'] ); |
| 520 | + } | |
| 521 | + | |
| 522 | + /** | |
| 523 | + * Retrieves the current post id. | |
| 524 | + * Returns 0 if no post id is found. | |
| 525 | + * | |
| 526 | + * @return int The post id. | |
| 527 | + */ | |
| 528 | + public function get_current_post_id(): int { | |
| 529 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are not processing form information, We are casting to an integer. | |
| 530 | + if ( isset( $_GET['post'] ) && \is_string( $_GET['post'] ) && (int) \wp_unslash( $_GET['post'] ) > 0 ) { | |
| 531 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are not processing form information, We are casting to an integer, also this is a helper function. | |
| 532 | + return (int) \wp_unslash( $_GET['post'] ); | |
| 533 | + } | |
| 534 | + return 0; | |
| 535 | + } | |
| 536 | + | |
| 537 | + /** | |
| 538 | + * Retrieves the current post type. | |
| 539 | + * | |
| 540 | + * @return string The post type. | |
| 541 | + */ | |
| 542 | + public function get_current_post_type(): string { | |
| 543 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. | |
| 544 | + if ( isset( $_GET['post_type'] ) && \is_string( $_GET['post_type'] ) ) { | |
| 545 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. | |
| 546 | + return \sanitize_text_field( \wp_unslash( $_GET['post_type'] ) ); | |
| 547 | + } | |
| 548 | + | |
| 549 | + // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Reason: should be done outside the helper function. | |
| 550 | + if ( isset( $_POST['post_type'] ) && \is_string( $_POST['post_type'] ) ) { | |
| 551 | + // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Reason: should be done outside the helper function. | |
| 552 | + return \sanitize_text_field( \wp_unslash( $_POST['post_type'] ) ); | |
| 553 | + } | |
| 554 | + | |
| 555 | + $post_id = $this->get_current_post_id(); | |
| 556 | + | |
| 557 | + if ( $post_id ) { | |
| 558 | + return \get_post_type( $post_id ); | |
| 559 | + } | |
| 560 | + | |
| 561 | + return 'post'; | |
| 562 | + } | |
| 563 | + | |
| 564 | + /** | |
| 565 | + * Retrieves the current taxonomy. | |
| 566 | + * | |
| 567 | + * @return string The taxonomy. | |
| 568 | + */ | |
| 569 | + public function get_current_taxonomy(): string { | |
| 570 | + if ( ! isset( $_SERVER['REQUEST_METHOD'] ) || ! \in_array( $_SERVER['REQUEST_METHOD'], [ 'GET', 'POST' ], true ) ) { | |
| 571 | + return ''; | |
| 572 | + } | |
| 573 | + | |
| 574 | + // phpcs:ignore WordPress.Security.NonceVerification -- Reason: We are not processing form information. | |
| 575 | + if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) { | |
| 576 | + // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Reason: should be done outside the helper function. | |
| 577 | + if ( isset( $_POST['taxonomy'] ) && \is_string( $_POST['taxonomy'] ) ) { | |
| 578 | + // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Reason: should be done outside the helper function. | |
| 579 | + return \sanitize_text_field( \wp_unslash( $_POST['taxonomy'] ) ); | |
| 580 | + } | |
| 581 | + return ''; | |
| 582 | + } | |
| 583 | + | |
| 584 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. | |
| 585 | + if ( isset( $_GET['taxonomy'] ) && \is_string( $_GET['taxonomy'] ) ) { | |
| 586 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. | |
| 587 | + return \sanitize_text_field( \wp_unslash( $_GET['taxonomy'] ) ); | |
| 588 | + } | |
| 589 | + | |
| 590 | + return ''; | |
| 494 | 591 | } |
| 495 | 592 | } |