| @@ -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,46 @@ | ||
| 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' ); | |
| 33 | - | |
| 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' ); | |
| 56 | + return $query_vars; | |
| 37 | 57 | } |
| 38 | 58 | |
| 39 | 59 | /** |
| 40 | 60 | * Stop trailing slashes on sitemap.xml URLs. |
| @@ -53,8 +73,10 @@ | ||
| 53 | 73 | } |
| 54 | 74 | |
| 55 | 75 | /** |
| 56 | 76 | * Redirects sitemap.xml to sitemap_index.xml. |
| 77 | + * | |
| 78 | + * @return void | |
| 57 | 79 | */ |
| 58 | 80 | public function template_redirect() { |
| 59 | 81 | if ( ! $this->needs_sitemap_index_redirect() ) { |
| 60 | 82 | return; |
| @@ -59,10 +81,9 @@ | ||
| 59 | 81 | if ( ! $this->needs_sitemap_index_redirect() ) { |
| 60 | 82 | return; |
| 61 | 83 | } |
| 62 | 84 | |
| 63 | - wp_safe_redirect( home_url( '/sitemap_index.xml' ), 301, 'Yoast SEO' ); | |
| 64 | - exit; | |
| 85 | + YoastSEO()->helpers->redirect->do_safe_redirect( home_url( '/sitemap_index.xml' ), 301, 'Yoast SEO' ); | |
| 65 | 86 | } |
| 66 | 87 | |
| 67 | 88 | /** |
| 68 | 89 | * Checks whether the current request needs to be redirected to sitemap_index.xml. |
| @@ -74,9 +95,10 @@ | ||
| 74 | 95 | public function needs_sitemap_index_redirect() { |
| 75 | 96 | global $wp_query; |
| 76 | 97 | |
| 77 | 98 | $protocol = 'http://'; |
| 78 | - if ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on' ) { | |
| 99 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 100 | + if ( ! empty( $_SERVER['HTTPS'] ) && strtolower( $_SERVER['HTTPS'] ) === 'on' ) { | |
| 79 | 101 | $protocol = 'https://'; |
| 80 | 102 | } |
| 81 | 103 | |
| 82 | 104 | $domain = ''; |