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
wordpress-seo / inc / sitemaps / class-sitemaps-router.php

class-sitemaps-router.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at inc/sitemaps/class-sitemaps-router.php

162 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\XML_Sitemaps
6 */
7
8 use Yoast\WP\SEO\Conditionals\Deactivating_Yoast_Seo_Conditional;
9
10 /**
11 * Rewrite setup and handling for sitemaps functionality.
12 */
13 class WPSEO_Sitemaps_Router {
14
15 /**
16 * Sets up init logic.
17 */
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 }
23
24 add_action( 'yoast_add_dynamic_rewrite_rules', [ $this, 'add_rewrite_rules' ] );
25 add_filter( 'query_vars', [ $this, 'add_query_vars' ] );
26
27 add_filter( 'redirect_canonical', [ $this, 'redirect_canonical' ] );
28 add_action( 'template_redirect', [ $this, 'template_redirect' ], 0 );
29 }
30
31 /**
32 * Adds rewrite routes for sitemaps.
33 *
34 * @param Yoast_Dynamic_Rewrites $dynamic_rewrites Dynamic rewrites handler instance.
35 *
36 * @return void
37 */
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 }
43
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';
55
56 return $query_vars;
57 }
58
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' );
69 }
70
71 /**
72 * Stop trailing slashes on sitemap.xml URLs.
73 *
74 * @param string $redirect The redirect URL currently determined.
75 *
76 * @return bool|string
77 */
78 public function redirect_canonical( $redirect ) {
79
80 if ( get_query_var( 'sitemap' ) || get_query_var( 'yoast-sitemap-xsl' ) ) {
81 return false;
82 }
83
84 return $redirect;
85 }
86
87 /**
88 * Redirects sitemap.xml to sitemap_index.xml.
89 *
90 * @return void
91 */
92 public function template_redirect() {
93 if ( ! $this->needs_sitemap_index_redirect() ) {
94 return;
95 }
96
97 YoastSEO()->helpers->redirect->do_safe_redirect( home_url( '/sitemap_index.xml' ), 301, 'Yoast SEO' );
98 }
99
100 /**
101 * Checks whether the current request needs to be redirected to sitemap_index.xml.
102 *
103 * @global WP_Query $wp_query Current query.
104 *
105 * @return bool True if redirect is needed, false otherwise.
106 */
107 public function needs_sitemap_index_redirect() {
108 global $wp_query;
109
110 $protocol = 'http://';
111 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
112 if ( ! empty( $_SERVER['HTTPS'] ) && strtolower( $_SERVER['HTTPS'] ) === 'on' ) {
113 $protocol = 'https://';
114 }
115
116 $domain = '';
117 if ( isset( $_SERVER['SERVER_NAME'] ) ) {
118 $domain = sanitize_text_field( wp_unslash( $_SERVER['SERVER_NAME'] ) );
119 }
120
121 $path = '';
122 if ( isset( $_SERVER['REQUEST_URI'] ) ) {
123 $path = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) );
124 }
125
126 // Due to different environment configurations, we need to check both SERVER_NAME and HTTP_HOST.
127 $check_urls = [ $protocol . $domain . $path ];
128 if ( ! empty( $_SERVER['HTTP_HOST'] ) ) {
129 $check_urls[] = $protocol . sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) . $path;
130 }
131
132 return $wp_query->is_404 && in_array( home_url( '/sitemap.xml' ), $check_urls, true );
133 }
134
135 /**
136 * Create base URL for the sitemap.
137 *
138 * @param string $page Page to append to the base URL.
139 *
140 * @return string base URL (incl page)
141 */
142 public static function get_base_url( $page ) {
143
144 global $wp_rewrite;
145
146 $base = $wp_rewrite->using_index_permalinks() ? 'index.php/' : '/';
147
148 /**
149 * Filter the base URL of the sitemaps.
150 *
151 * @param string $base The string that should be added to home_url() to make the full base URL.
152 */
153 $base = apply_filters( 'wpseo_sitemaps_base_url', $base );
154
155 /*
156 * Get the scheme from the configured home URL instead of letting WordPress
157 * determine the scheme based on the requested URI.
158 */
159 return home_url( $base . $page, wp_parse_url( get_option( 'home' ), PHP_URL_SCHEME ) );
160 }
161 }
162