/server/wordpress/wp-content/plugins/wordpress-seo/inc/sitemaps/class-sitemaps-router.php

https://gitlab.com/suporte.spturis/carnaval2015.spturis.com.br · PHP · 127 lines · 56 code · 23 blank · 48 comment · 9 complexity · ceb21ee2ad8ed1688af7f3cdb537f655 MD5 · raw file

  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
  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. wp_safe_redirect( home_url( '/sitemap_index.xml' ), 301, 'Yoast SEO' );
  52. exit;
  53. }
  54. /**
  55. * Checks whether the current request needs to be redirected to sitemap_index.xml.
  56. *
  57. * @global WP_Query $wp_query Current query.
  58. *
  59. * @return bool True if redirect is needed, false otherwise.
  60. */
  61. public function needs_sitemap_index_redirect() {
  62. global $wp_query;
  63. $protocol = 'http://';
  64. if ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on' ) {
  65. $protocol = 'https://';
  66. }
  67. $domain = '';
  68. if ( isset( $_SERVER['SERVER_NAME'] ) ) {
  69. $domain = sanitize_text_field( wp_unslash( $_SERVER['SERVER_NAME'] ) );
  70. }
  71. $path = '';
  72. if ( isset( $_SERVER['REQUEST_URI'] ) ) {
  73. $path = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) );
  74. }
  75. // Due to different environment configurations, we need to check both SERVER_NAME and HTTP_HOST.
  76. $check_urls = [ $protocol . $domain . $path ];
  77. if ( ! empty( $_SERVER['HTTP_HOST'] ) ) {
  78. $check_urls[] = $protocol . sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) . $path;
  79. }
  80. return $wp_query->is_404 && in_array( home_url( '/sitemap.xml' ), $check_urls, true );
  81. }
  82. /**
  83. * Create base URL for the sitemap.
  84. *
  85. * @param string $page Page to append to the base URL.
  86. *
  87. * @return string base URL (incl page)
  88. */
  89. public static function get_base_url( $page ) {
  90. global $wp_rewrite;
  91. $base = $wp_rewrite->using_index_permalinks() ? 'index.php/' : '/';
  92. /**
  93. * Filter the base URL of the sitemaps.
  94. *
  95. * @param string $base The string that should be added to home_url() to make the full base URL.
  96. */
  97. $base = apply_filters( 'wpseo_sitemaps_base_url', $base );
  98. /*
  99. * Get the scheme from the configured home URL instead of letting WordPress
  100. * determine the scheme based on the requested URI.
  101. */
  102. return home_url( $base . $page, wp_parse_url( get_option( 'home' ), PHP_URL_SCHEME ) );
  103. }
  104. }