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

122 lines 2.9 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 * @return void
47 */
48 public function initialize() {
49 // This needs to be on priority 15 as that is after our options initialize.
50 \add_action( 'plugins_loaded', [ $this, 'maybe_disable_core_sitemaps' ], 15 );
51 }
52
53 /**
54 * Disables the core sitemaps if Yoast SEO sitemaps are enabled.
55 *
56 * @return void
57 */
58 public function maybe_disable_core_sitemaps() {
59 if ( $this->options->get( 'enable_xml_sitemap' ) ) {
60 \add_filter( 'wp_sitemaps_enabled', '__return_false' );
61
62 \add_action( 'template_redirect', [ $this, 'template_redirect' ], 0 );
63 }
64 }
65
66 /**
67 * Redirects requests to the WordPress sitemap to the Yoast sitemap.
68 *
69 * @return void
70 */
71 public function template_redirect() {
72 // If there is no path, nothing to do.
73 if ( empty( $_SERVER['REQUEST_URI'] ) ) {
74 return;
75 }
76 $path = \sanitize_text_field( \wp_unslash( $_SERVER['REQUEST_URI'] ) );
77
78 // If it's not a wp-sitemap request, nothing to do.
79 if ( \substr( $path, 0, 11 ) !== '/wp-sitemap' ) {
80 return;
81 }
82
83 $redirect = $this->get_redirect_url( $path );
84
85 if ( ! $redirect ) {
86 return;
87 }
88
89 $this->redirect->do_safe_redirect( \home_url( $redirect ), 301 );
90 }
91
92 /**
93 * Returns the relative sitemap URL to redirect to.
94 *
95 * @param string $path The original path.
96 *
97 * @return string|false The path to redirct to. False if no redirect should be done.
98 */
99 private function get_redirect_url( $path ) {
100 // Start with the simple string comparison so we avoid doing unnecessary regexes.
101 if ( $path === '/wp-sitemap.xml' ) {
102 return '/sitemap_index.xml';
103 }
104
105 if ( \preg_match( '/^\/wp-sitemap-(posts|taxonomies)-(\w+)-(\d+)\.xml$/', $path, $matches ) ) {
106 $index = ( (int) $matches[3] - 1 );
107 $index = ( $index === 0 ) ? '' : (string) $index;
108
109 return '/' . $matches[2] . '-sitemap' . $index . '.xml';
110 }
111
112 if ( \preg_match( '/^\/wp-sitemap-users-(\d+)\.xml$/', $path, $matches ) ) {
113 $index = ( (int) $matches[1] - 1 );
114 $index = ( $index === 0 ) ? '' : (string) $index;
115
116 return '/author-sitemap' . $index . '.xml';
117 }
118
119 return false;
120 }
121 }
122