sitemap-finder.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * The functions in this class provide an API for handling
  4. * sitemap related URIs.
  5. *
  6. * @package Jetpack
  7. * @since 4.8.0
  8. * @author Automattic
  9. */
  10. /**
  11. * The Jetpack_Sitemap_Finder object deals with constructing
  12. * sitemap URIs.
  13. *
  14. * @since 4.8.0
  15. */
  16. class Jetpack_Sitemap_Finder {
  17. /**
  18. * Construct the complete URL of a sitemap file. Depends on
  19. * permalink settings.
  20. *
  21. * @access public
  22. * @since 4.8.0
  23. * @since 4.8.1 Call jetpack_sitemap_uri()
  24. *
  25. * @param string $filename The filename of the sitemap.
  26. *
  27. * @return string Complete URI of the given sitemap file.
  28. */
  29. public function construct_sitemap_url( $filename ) {
  30. return jetpack_sitemap_uri( $filename );
  31. }
  32. /**
  33. * Path and query prefix of sitemap files. Depends on permalink
  34. * settings.
  35. *
  36. * @access public
  37. * @since 4.8.0
  38. *
  39. * @return string The path+query prefix.
  40. */
  41. public function the_jetpack_sitemap_path_and_query_prefix() {
  42. global $wp_rewrite;
  43. // Get path fragment from home_url().
  44. $home = wp_parse_url( home_url() );
  45. if ( isset( $home['path'] ) ) {
  46. $home_path = $home['path'];
  47. } else {
  48. $home_path = '';
  49. }
  50. // Get additional path fragment from filter.
  51. $location = Jetpack_Options::get_option_and_ensure_autoload(
  52. 'jetpack_sitemap_location',
  53. ''
  54. );
  55. if ( $wp_rewrite->using_index_permalinks() ) {
  56. return $home_path . '/index.php' . $location . '/';
  57. } elseif ( $wp_rewrite->using_permalinks() ) {
  58. return $home_path . $location . '/';
  59. } else {
  60. return $home_path . $location . '/?jetpack-sitemap=';
  61. }
  62. }
  63. /**
  64. * Examine a path+query URI fragment looking for a sitemap request.
  65. *
  66. * @access public
  67. * @since 4.8.0
  68. *
  69. * @param string $raw_uri A URI (path+query only) to test for sitemap-ness.
  70. *
  71. * @return array @args {
  72. * @type string $sitemap_name The recognized sitemap name (or null).
  73. * }
  74. */
  75. public function recognize_sitemap_uri( $raw_uri ) {
  76. // The path+query where sitemaps are served.
  77. $sitemap_path = $this->the_jetpack_sitemap_path_and_query_prefix();
  78. // A regex which detects $sitemap_path at the beginning of a string.
  79. $path_regex = '/^' . preg_quote( $sitemap_path, '/' ) . '/';
  80. // Check that the request URI begins with the sitemap path.
  81. if ( preg_match( $path_regex, $raw_uri ) ) {
  82. // Strip off the $sitemap_path and any trailing slash.
  83. $stripped_uri = preg_replace( $path_regex, '', rtrim( $raw_uri, '/' ) );
  84. } else {
  85. $stripped_uri = '';
  86. }
  87. // Check that the stripped uri begins with one of the sitemap prefixes.
  88. if ( preg_match( '/^sitemap|^image-s|^news-s|^video-s/', $stripped_uri ) ) {
  89. $filename = $stripped_uri;
  90. } else {
  91. $filename = null;
  92. }
  93. return array(
  94. 'sitemap_name' => $filename,
  95. );
  96. }
  97. }