PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.2
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 18.2, at inc/sitemaps/class-sitemaps-router.php

128 lines 3.3 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 /**
9 * Rewrite setup and handling for sitemaps functionality.
10 */
11 class WPSEO_Sitemaps_Router {
12
13 /**
14 * Sets up init logic.
15 */
16 public function __construct() {
17
18 add_action( 'init', [ $this, 'init' ], 1 );
19 add_filter( 'redirect_canonical', [ $this, 'redirect_canonical' ] );
20 add_action( 'template_redirect', [ $this, 'template_redirect' ], 0 );
21 }
22
23 /**
24 * Sets up rewrite rules.
25 */
26 public function init() {
27
28 global $wp;
29
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' );
37 }
38
39 /**
40 * Stop trailing slashes on sitemap.xml URLs.
41 *
42 * @param string $redirect The redirect URL currently determined.
43 *
44 * @return bool|string
45 */
46 public function redirect_canonical( $redirect ) {
47
48 if ( get_query_var( 'sitemap' ) || get_query_var( 'yoast-sitemap-xsl' ) ) {
49 return false;
50 }
51
52 return $redirect;
53 }
54
55 /**
56 * Redirects sitemap.xml to sitemap_index.xml.
57 */
58 public function template_redirect() {
59 if ( ! $this->needs_sitemap_index_redirect() ) {
60 return;
61 }
62
63 wp_safe_redirect( home_url( '/sitemap_index.xml' ), 301, 'Yoast SEO' );
64 exit;
65 }
66
67 /**
68 * Checks whether the current request needs to be redirected to sitemap_index.xml.
69 *
70 * @global WP_Query $wp_query Current query.
71 *
72 * @return bool True if redirect is needed, false otherwise.
73 */
74 public function needs_sitemap_index_redirect() {
75 global $wp_query;
76
77 $protocol = 'http://';
78 if ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on' ) {
79 $protocol = 'https://';
80 }
81
82 $domain = '';
83 if ( isset( $_SERVER['SERVER_NAME'] ) ) {
84 $domain = sanitize_text_field( wp_unslash( $_SERVER['SERVER_NAME'] ) );
85 }
86
87 $path = '';
88 if ( isset( $_SERVER['REQUEST_URI'] ) ) {
89 $path = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) );
90 }
91
92 // Due to different environment configurations, we need to check both SERVER_NAME and HTTP_HOST.
93 $check_urls = [ $protocol . $domain . $path ];
94 if ( ! empty( $_SERVER['HTTP_HOST'] ) ) {
95 $check_urls[] = $protocol . sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) . $path;
96 }
97
98 return $wp_query->is_404 && in_array( home_url( '/sitemap.xml' ), $check_urls, true );
99 }
100
101 /**
102 * Create base URL for the sitemap.
103 *
104 * @param string $page Page to append to the base URL.
105 *
106 * @return string base URL (incl page)
107 */
108 public static function get_base_url( $page ) {
109
110 global $wp_rewrite;
111
112 $base = $wp_rewrite->using_index_permalinks() ? 'index.php/' : '/';
113
114 /**
115 * Filter the base URL of the sitemaps.
116 *
117 * @param string $base The string that should be added to home_url() to make the full base URL.
118 */
119 $base = apply_filters( 'wpseo_sitemaps_base_url', $base );
120
121 /*
122 * Get the scheme from the configured home URL instead of letting WordPress
123 * determine the scheme based on the requested URI.
124 */
125 return home_url( $base . $page, wp_parse_url( get_option( 'home' ), PHP_URL_SCHEME ) );
126 }
127 }
128