class-sitemaps-router.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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', array( $this, 'init' ), 1 );
  16. add_filter( 'redirect_canonical', array( $this, 'redirect_canonical' ) );
  17. add_action( 'template_redirect', array( $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( '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?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( '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 );
  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 = sanitize_text_field( $_SERVER['SERVER_NAME'] );
  69. $path = sanitize_text_field( $_SERVER['REQUEST_URI'] );
  70. // Due to different environment configurations, we need to check both SERVER_NAME and HTTP_HOST.
  71. $check_urls = array( $protocol . $domain . $path );
  72. if ( ! empty( $_SERVER['HTTP_HOST'] ) ) {
  73. $check_urls[] = $protocol . sanitize_text_field( $_SERVER['HTTP_HOST'] ) . $path;
  74. }
  75. return $wp_query->is_404 && in_array( home_url( '/sitemap.xml' ), $check_urls, true );
  76. }
  77. /**
  78. * Create base URL for the sitemap.
  79. *
  80. * @param string $page Page to append to the base URL.
  81. *
  82. * @return string base URL (incl page)
  83. */
  84. public static function get_base_url( $page ) {
  85. global $wp_rewrite;
  86. $base = $wp_rewrite->using_index_permalinks() ? 'index.php/' : '/';
  87. /**
  88. * Filter the base URL of the sitemaps
  89. *
  90. * @param string $base The string that should be added to home_url() to make the full base URL.
  91. */
  92. $base = apply_filters( 'wpseo_sitemaps_base_url', $base );
  93. // Get the scheme from the configured home url instead of letting WordPress determine the scheme based on the requested URI.
  94. return home_url( $base . $page, wp_parse_url( get_option( 'home' ), PHP_URL_SCHEME ) );
  95. }
  96. }