暫無描述

sitemap-finder.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * The functions in this class provide an API for handling
  4. * sitemap related URIs.
  5. *
  6. * @package automattic/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. $url = jetpack_sitemap_uri( $filename );
  31. if ( pathinfo( $filename, PATHINFO_EXTENSION ) === 'xsl' ) {
  32. // strip scheme for sites where sitemap could be access via http or https
  33. $url = preg_replace( '/^https?:/', '', $url );
  34. }
  35. return $url;
  36. }
  37. /**
  38. * Path and query prefix of sitemap files. Depends on permalink
  39. * settings.
  40. *
  41. * @access public
  42. * @since 4.8.0
  43. *
  44. * @return string The path+query prefix.
  45. */
  46. public function the_jetpack_sitemap_path_and_query_prefix() {
  47. global $wp_rewrite;
  48. // Get path fragment from home_url().
  49. $home = wp_parse_url( home_url() );
  50. if ( isset( $home['path'] ) ) {
  51. $home_path = $home['path'];
  52. } else {
  53. $home_path = '';
  54. }
  55. // Get additional path fragment from filter.
  56. $location = Jetpack_Options::get_option_and_ensure_autoload(
  57. 'jetpack_sitemap_location',
  58. ''
  59. );
  60. if ( $wp_rewrite->using_index_permalinks() ) {
  61. return $home_path . '/index.php' . $location . '/';
  62. } elseif ( $wp_rewrite->using_permalinks() ) {
  63. return $home_path . $location . '/';
  64. } else {
  65. return $home_path . $location . '/?jetpack-sitemap=';
  66. }
  67. }
  68. /**
  69. * Examine a path+query URI fragment looking for a sitemap request.
  70. *
  71. * @access public
  72. * @since 4.8.0
  73. *
  74. * @param string $raw_uri A URI (path+query only) to test for sitemap-ness.
  75. *
  76. * @return array @args {
  77. * @type string $sitemap_name The recognized sitemap name (or null).
  78. * }
  79. */
  80. public function recognize_sitemap_uri( $raw_uri ) {
  81. // The path+query where sitemaps are served.
  82. $sitemap_path = $this->the_jetpack_sitemap_path_and_query_prefix();
  83. // A regex which detects $sitemap_path at the beginning of a string.
  84. $path_regex = '/^' . preg_quote( $sitemap_path, '/' ) . '/';
  85. // Check that the request URI begins with the sitemap path.
  86. if ( preg_match( $path_regex, $raw_uri ) ) {
  87. // Strip off the $sitemap_path and any trailing slash.
  88. $stripped_uri = preg_replace( $path_regex, '', rtrim( $raw_uri, '/' ) );
  89. } else {
  90. $stripped_uri = '';
  91. }
  92. // Check that the stripped uri begins with one of the sitemap prefixes.
  93. if ( preg_match( '/^sitemap|^image-s|^news-s|^video-s/', $stripped_uri ) ) {
  94. $filename = $stripped_uri;
  95. } else {
  96. $filename = null;
  97. }
  98. return array(
  99. 'sitemap_name' => $filename,
  100. );
  101. }
  102. }