PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
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 19.0 All 129 releases
← All changes | inc/sitemaps/class-sitemaps-router.php +47 -13 18.2 → trunk View file →
@@ -4,8 +4,10 @@
4 4 *
5 5 * @package WPSEO\XML_Sitemaps
6 6 */
7 7
8 +use Yoast\WP\SEO\Conditionals\Deactivating_Yoast_Seo_Conditional;
9 +
8 10 /**
9 11 * Rewrite setup and handling for sitemaps functionality.
10 12 */
11 13 class WPSEO_Sitemaps_Router {
@@ -13,28 +15,58 @@
13 15 /**
14 16 * Sets up init logic.
15 17 */
16 18 public function __construct() {
19 + // If we add rewrite rules during the plugin's deactivation, the flush_rewrite_rules that we perform afterwards won't properly flush those new rules.
20 + if ( YoastSEO()->classes->get( Deactivating_Yoast_Seo_Conditional::class )->is_met() ) {
21 + return;
22 + }
17 23
18 - add_action( 'init', [ $this, 'init' ], 1 );
24 + add_action( 'yoast_add_dynamic_rewrite_rules', [ $this, 'add_rewrite_rules' ] );
25 + add_filter( 'query_vars', [ $this, 'add_query_vars' ] );
26 +
19 27 add_filter( 'redirect_canonical', [ $this, 'redirect_canonical' ] );
20 28 add_action( 'template_redirect', [ $this, 'template_redirect' ], 0 );
21 29 }
22 30
23 31 /**
24 - * Sets up rewrite rules.
32 + * Adds rewrite routes for sitemaps.
33 + *
34 + * @param Yoast_Dynamic_Rewrites $dynamic_rewrites Dynamic rewrites handler instance.
35 + *
36 + * @return void
25 37 */
26 - public function init() {
38 + public function add_rewrite_rules( $dynamic_rewrites ) {
39 + $dynamic_rewrites->add_rule( 'sitemap_index\.xml$', 'index.php?sitemap=1', 'top' );
40 + $dynamic_rewrites->add_rule( '([^/]+?)-sitemap([0-9]+)?\.xml$', 'index.php?sitemap=$matches[1]&sitemap_n=$matches[2]', 'top' );
41 + $dynamic_rewrites->add_rule( '([a-z]+)?-?sitemap\.xsl$', 'index.php?yoast-sitemap-xsl=$matches[1]', 'top' );
42 + }
27 43
28 - global $wp;
44 + /**
45 + * Adds query variables for sitemaps.
46 + *
47 + * @param array<string> $query_vars List of query variables to filter.
48 + *
49 + * @return array<string> Filtered query variables.
50 + */
51 + public function add_query_vars( $query_vars ) {
52 + $query_vars[] = 'sitemap';
53 + $query_vars[] = 'sitemap_n';
54 + $query_vars[] = 'yoast-sitemap-xsl';
29 55
30 - $wp->add_query_var( 'sitemap' );
31 - $wp->add_query_var( 'sitemap_n' );
32 - $wp->add_query_var( 'yoast-sitemap-xsl' );
56 + return $query_vars;
57 + }
33 58
34 - add_rewrite_rule( 'sitemap_index\.xml$', 'index.php?sitemap=1', 'top' );
35 - add_rewrite_rule( '([^/]+?)-sitemap([0-9]+)?\.xml$', 'index.php?sitemap=$matches[1]&sitemap_n=$matches[2]', 'top' );
36 - add_rewrite_rule( '([a-z]+)?-?sitemap\.xsl$', 'index.php?yoast-sitemap-xsl=$matches[1]', 'top' );
59 + /**
60 + * Sets up rewrite rules.
61 + *
62 + * @deprecated 21.8
63 + * @codeCoverageIgnore
64 + *
65 + * @return void
66 + */
67 + public function init() {
68 + _deprecated_function( __METHOD__, 'Yoast SEO 21.8' );
37 69 }
38 70
39 71 /**
40 72 * Stop trailing slashes on sitemap.xml URLs.
@@ -53,8 +85,10 @@
53 85 }
54 86
55 87 /**
56 88 * Redirects sitemap.xml to sitemap_index.xml.
89 + *
90 + * @return void
57 91 */
58 92 public function template_redirect() {
59 93 if ( ! $this->needs_sitemap_index_redirect() ) {
60 94 return;
@@ -59,10 +93,9 @@
59 93 if ( ! $this->needs_sitemap_index_redirect() ) {
60 94 return;
61 95 }
62 96
63 - wp_safe_redirect( home_url( '/sitemap_index.xml' ), 301, 'Yoast SEO' );
64 - exit;
97 + YoastSEO()->helpers->redirect->do_safe_redirect( home_url( '/sitemap_index.xml' ), 301, 'Yoast SEO' );
65 98 }
66 99
67 100 /**
68 101 * Checks whether the current request needs to be redirected to sitemap_index.xml.
@@ -74,9 +107,10 @@
74 107 public function needs_sitemap_index_redirect() {
75 108 global $wp_query;
76 109
77 110 $protocol = 'http://';
78 - if ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on' ) {
111 + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
112 + if ( ! empty( $_SERVER['HTTPS'] ) && strtolower( $_SERVER['HTTPS'] ) === 'on' ) {
79 113 $protocol = 'https://';
80 114 }
81 115
82 116 $domain = '';