PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.7
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.7
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 / src / initializers / disable-core-sitemaps.php

disable-core-sitemaps.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.7, at src/initializers/disable-core-sitemaps.php

120 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Initializers;
4
5 use Yoast\WP\SEO\Conditionals\No_Conditionals;
6 use Yoast\WP\SEO\Helpers\Options_Helper;
7 use Yoast\WP\SEO\Helpers\Redirect_Helper;
8
9 /**
10 * Disables the WP core sitemaps.
11 */
12 class Disable_Core_Sitemaps implements Initializer_Interface {
13
14 use No_Conditionals;
15
16 /**
17 * The options helper.
18 *
19 * @var Options_Helper
20 */
21 private $options;
22
23 /**
24 * The redirect helper.
25 *
26 * @var Redirect_Helper
27 */
28 private $redirect;
29
30 /**
31 * Sitemaps_Enabled_Conditional constructor.
32 *
33 * @codeCoverageIgnore
34 *
35 * @param Options_Helper $options The options helper.
36 * @param Redirect_Helper $redirect The redirect helper.
37 */
38 public function __construct( Options_Helper $options, Redirect_Helper $redirect ) {
39 $this->options = $options;
40 $this->redirect = $redirect;
41 }
42
43 /**
44 * Disable the WP core XML sitemaps.
45 */
46 public function initialize() {
47 // This needs to be on priority 15 as that is after our options initialize.
48 \add_action( 'plugins_loaded', [ $this, 'maybe_disable_core_sitemaps' ], 15 );
49 }
50
51 /**
52 * Disables the core sitemaps if Yoast SEO sitemaps are enabled.
53 *
54 * @return void
55 */
56 public function maybe_disable_core_sitemaps() {
57 if ( $this->options->get( 'enable_xml_sitemap' ) ) {
58 \add_filter( 'wp_sitemaps_enabled', '__return_false' );
59
60 \add_action( 'template_redirect', [ $this, 'template_redirect' ], 0 );
61 }
62 }
63
64 /**
65 * Redirects requests to the WordPress sitemap to the Yoast sitemap.
66 *
67 * @return void
68 */
69 public function template_redirect() {
70 // If there is no path, nothing to do.
71 if ( empty( $_SERVER['REQUEST_URI'] ) ) {
72 return;
73 }
74 $path = \sanitize_text_field( \wp_unslash( $_SERVER['REQUEST_URI'] ) );
75
76 // If it's not a wp-sitemap request, nothing to do.
77 if ( \substr( $path, 0, 11 ) !== '/wp-sitemap' ) {
78 return;
79 }
80
81 $redirect = $this->get_redirect_url( $path );
82
83 if ( ! $redirect ) {
84 return;
85 }
86
87 $this->redirect->do_safe_redirect( \home_url( $redirect ), 301 );
88 }
89
90 /**
91 * Returns the relative sitemap URL to redirect to.
92 *
93 * @param string $path The original path.
94 *
95 * @return string|false The path to redirct to. False if no redirect should be done.
96 */
97 private function get_redirect_url( $path ) {
98 // Start with the simple string comparison so we avoid doing unnecessary regexes.
99 if ( $path === '/wp-sitemap.xml' ) {
100 return '/sitemap_index.xml';
101 }
102
103 if ( \preg_match( '/^\/wp-sitemap-(posts|taxonomies)-(\w+)-(\d+)\.xml$/', $path, $matches ) ) {
104 $index = ( (int) $matches[3] - 1 );
105 $index = ( $index === 0 ) ? '' : (string) $index;
106
107 return '/' . $matches[2] . '-sitemap' . $index . '.xml';
108 }
109
110 if ( \preg_match( '/^\/wp-sitemap-users-(\d+)\.xml$/', $path, $matches ) ) {
111 $index = ( (int) $matches[1] - 1 );
112 $index = ( $index === 0 ) ? '' : (string) $index;
113
114 return '/author-sitemap' . $index . '.xml';
115 }
116
117 return false;
118 }
119 }
120