class-sitemaps-router.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\XML_Sitemaps
  6. */
  7. /**
  8. * Rewrite setup and handling for sitemaps functionality.
  9. */
  10. class WPSEO_Sitemaps_Router {
  11. /**
  12. * Sets up init logic.
  13. */
  14. public function __construct() {
  15. add_action( 'init', [ $this, 'init' ], 1 );
  16. add_filter( 'redirect_canonical', [ $this, 'redirect_canonical' ] );
  17. add_action( 'template_redirect', [ $this, 'template_redirect' ], 0 );
  18. }
  19. /**
  20. * Sets up rewrite rules.
  21. */
  22. public function init() {
  23. global $wp;
  24. $wp->add_query_var( 'sitemap' );
  25. $wp->add_query_var( 'sitemap_n' );
  26. $wp->add_query_var( 'yoast-sitemap-xsl' );
  27. add_rewrite_rule( 'sitemap_index\.xml$', 'index.php?sitemap=1', 'top' );
  28. add_rewrite_rule( '([^/]+?)-sitemap([0-9]+)?\.xml$', 'index.php?sitemap=$matches[1]&sitemap_n=$matches[2]', 'top' );
  29. add_rewrite_rule( '([a-z]+)?-?sitemap\.xsl$', 'index.php?yoast-sitemap-xsl=$matches[1]', 'top' );
  30. }
  31. /**
  32. * Stop trailing slashes on sitemap.xml URLs.
  33. *
  34. * @param string $redirect The redirect URL currently determined.
  35. *
  36. * @return bool|string $redirect
  37. */
  38. public function redirect_canonical( $redirect ) {
  39. if ( get_query_var( 'sitemap' ) || get_query_var( 'yoast-sitemap-xsl' ) ) {
  40. return false;
  41. }
  42. return $redirect;
  43. }
  44. /**
  45. * Redirects sitemap.xml to sitemap_index.xml.
  46. */
  47. public function template_redirect() {
  48. if ( ! $this->needs_sitemap_index_redirect() ) {
  49. return;
  50. }
  51. header( 'X-Redirect-By: Yoast SEO' );
  52. wp_redirect( home_url( '/sitemap_index.xml' ), 301, 'Yoast SEO' );
  53. exit;
  54. }
  55. /**
  56. * Checks whether the current request needs to be redirected to sitemap_index.xml.
  57. *
  58. * @global WP_Query $wp_query Current query.
  59. *
  60. * @return bool True if redirect is needed, false otherwise.
  61. */
  62. public function needs_sitemap_index_redirect() {
  63. global $wp_query;
  64. $protocol = 'http://';
  65. if ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on' ) {
  66. $protocol = 'https://';
  67. }
  68. $domain = '';
  69. if ( isset( $_SERVER['SERVER_NAME'] ) ) {
  70. $domain = sanitize_text_field( wp_unslash( $_SERVER['SERVER_NAME'] ) );
  71. }
  72. $path = '';
  73. if ( isset( $_SERVER['REQUEST_URI'] ) ) {
  74. $path = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) );
  75. }
  76. // Due to different environment configurations, we need to check both SERVER_NAME and HTTP_HOST.
  77. $check_urls = [ $protocol . $domain . $path ];
  78. if ( ! empty( $_SERVER['HTTP_HOST'] ) ) {
  79. $check_urls[] = $protocol . sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) . $path;
  80. }
  81. return $wp_query->is_404 && in_array( home_url( '/sitemap.xml' ), $check_urls, true );
  82. }
  83. /**
  84. * Create base URL for the sitemap.
  85. *
  86. * @param string $page Page to append to the base URL.
  87. *
  88. * @return string base URL (incl page)
  89. */
  90. public static function get_base_url( $page ) {
  91. global $wp_rewrite;
  92. $base = $wp_rewrite->using_index_permalinks() ? 'index.php/' : '/';
  93. /**
  94. * Filter the base URL of the sitemaps.
  95. *
  96. * @param string $base The string that should be added to home_url() to make the full base URL.
  97. */
  98. $base = apply_filters( 'wpseo_sitemaps_base_url', $base );
  99. /*
  100. * Get the scheme from the configured home URL instead of letting WordPress
  101. * determine the scheme based on the requested URI.
  102. */
  103. return home_url( $base . $page, wp_parse_url( get_option( 'home' ), PHP_URL_SCHEME ) );
  104. }
  105. }