Aucune description

class-wp-sitemaps.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * Sitemaps: WP_Sitemaps class
  4. *
  5. * This is the main class integrating all other classes.
  6. *
  7. * @package WordPress
  8. * @subpackage Sitemaps
  9. * @since 5.5.0
  10. */
  11. /**
  12. * Class WP_Sitemaps.
  13. *
  14. * @since 5.5.0
  15. */
  16. class WP_Sitemaps {
  17. /**
  18. * The main index of supported sitemaps.
  19. *
  20. * @since 5.5.0
  21. *
  22. * @var WP_Sitemaps_Index
  23. */
  24. public $index;
  25. /**
  26. * The main registry of supported sitemaps.
  27. *
  28. * @since 5.5.0
  29. *
  30. * @var WP_Sitemaps_Registry
  31. */
  32. public $registry;
  33. /**
  34. * An instance of the renderer class.
  35. *
  36. * @since 5.5.0
  37. *
  38. * @var WP_Sitemaps_Renderer
  39. */
  40. public $renderer;
  41. /**
  42. * WP_Sitemaps constructor.
  43. *
  44. * @since 5.5.0
  45. */
  46. public function __construct() {
  47. $this->registry = new WP_Sitemaps_Registry();
  48. $this->renderer = new WP_Sitemaps_Renderer();
  49. $this->index = new WP_Sitemaps_Index( $this->registry );
  50. }
  51. /**
  52. * Initiates all sitemap functionality.
  53. *
  54. * If sitemaps are disabled, only the rewrite rules will be registered
  55. * by this method, in order to properly send 404s.
  56. *
  57. * @since 5.5.0
  58. */
  59. public function init() {
  60. // These will all fire on the init hook.
  61. $this->register_rewrites();
  62. add_action( 'template_redirect', array( $this, 'render_sitemaps' ) );
  63. if ( ! $this->sitemaps_enabled() ) {
  64. return;
  65. }
  66. $this->register_sitemaps();
  67. // Add additional action callbacks.
  68. add_filter( 'pre_handle_404', array( $this, 'redirect_sitemapxml' ), 10, 2 );
  69. add_filter( 'robots_txt', array( $this, 'add_robots' ), 0, 2 );
  70. }
  71. /**
  72. * Determines whether sitemaps are enabled or not.
  73. *
  74. * @since 5.5.0
  75. *
  76. * @return bool Whether sitemaps are enabled.
  77. */
  78. public function sitemaps_enabled() {
  79. $is_enabled = (bool) get_option( 'blog_public' );
  80. /**
  81. * Filters whether XML Sitemaps are enabled or not.
  82. *
  83. * When XML Sitemaps are disabled via this filter, rewrite rules are still
  84. * in place to ensure a 404 is returned.
  85. *
  86. * @see WP_Sitemaps::register_rewrites()
  87. *
  88. * @since 5.5.0
  89. *
  90. * @param bool $is_enabled Whether XML Sitemaps are enabled or not. Defaults
  91. * to true for public sites.
  92. */
  93. return (bool) apply_filters( 'wp_sitemaps_enabled', $is_enabled );
  94. }
  95. /**
  96. * Registers and sets up the functionality for all supported sitemaps.
  97. *
  98. * @since 5.5.0
  99. */
  100. public function register_sitemaps() {
  101. $providers = array(
  102. 'posts' => new WP_Sitemaps_Posts(),
  103. 'taxonomies' => new WP_Sitemaps_Taxonomies(),
  104. 'users' => new WP_Sitemaps_Users(),
  105. );
  106. /* @var WP_Sitemaps_Provider $provider */
  107. foreach ( $providers as $name => $provider ) {
  108. $this->registry->add_provider( $name, $provider );
  109. }
  110. }
  111. /**
  112. * Registers sitemap rewrite tags and routing rules.
  113. *
  114. * @since 5.5.0
  115. */
  116. public function register_rewrites() {
  117. // Add rewrite tags.
  118. add_rewrite_tag( '%sitemap%', '([^?]+)' );
  119. add_rewrite_tag( '%sitemap-subtype%', '([^?]+)' );
  120. // Register index route.
  121. add_rewrite_rule( '^wp-sitemap\.xml$', 'index.php?sitemap=index', 'top' );
  122. // Register rewrites for the XSL stylesheet.
  123. add_rewrite_tag( '%sitemap-stylesheet%', '([^?]+)' );
  124. add_rewrite_rule( '^wp-sitemap\.xsl$', 'index.php?sitemap-stylesheet=sitemap', 'top' );
  125. add_rewrite_rule( '^wp-sitemap-index\.xsl$', 'index.php?sitemap-stylesheet=index', 'top' );
  126. // Register routes for providers.
  127. add_rewrite_rule(
  128. '^wp-sitemap-([a-z]+?)-([a-z\d_-]+?)-(\d+?)\.xml$',
  129. 'index.php?sitemap=$matches[1]&sitemap-subtype=$matches[2]&paged=$matches[3]',
  130. 'top'
  131. );
  132. add_rewrite_rule(
  133. '^wp-sitemap-([a-z]+?)-(\d+?)\.xml$',
  134. 'index.php?sitemap=$matches[1]&paged=$matches[2]',
  135. 'top'
  136. );
  137. }
  138. /**
  139. * Renders sitemap templates based on rewrite rules.
  140. *
  141. * @since 5.5.0
  142. *
  143. * @global WP_Query $wp_query WordPress Query object.
  144. */
  145. public function render_sitemaps() {
  146. global $wp_query;
  147. $sitemap = sanitize_text_field( get_query_var( 'sitemap' ) );
  148. $object_subtype = sanitize_text_field( get_query_var( 'sitemap-subtype' ) );
  149. $stylesheet_type = sanitize_text_field( get_query_var( 'sitemap-stylesheet' ) );
  150. $paged = absint( get_query_var( 'paged' ) );
  151. // Bail early if this isn't a sitemap or stylesheet route.
  152. if ( ! ( $sitemap || $stylesheet_type ) ) {
  153. return;
  154. }
  155. if ( ! $this->sitemaps_enabled() ) {
  156. $wp_query->set_404();
  157. status_header( 404 );
  158. return;
  159. }
  160. // Render stylesheet if this is stylesheet route.
  161. if ( $stylesheet_type ) {
  162. $stylesheet = new WP_Sitemaps_Stylesheet();
  163. $stylesheet->render_stylesheet( $stylesheet_type );
  164. exit;
  165. }
  166. // Render the index.
  167. if ( 'index' === $sitemap ) {
  168. $sitemap_list = $this->index->get_sitemap_list();
  169. $this->renderer->render_index( $sitemap_list );
  170. exit;
  171. }
  172. $provider = $this->registry->get_provider( $sitemap );
  173. if ( ! $provider ) {
  174. return;
  175. }
  176. if ( empty( $paged ) ) {
  177. $paged = 1;
  178. }
  179. $url_list = $provider->get_url_list( $paged, $object_subtype );
  180. // Force a 404 and bail early if no URLs are present.
  181. if ( empty( $url_list ) ) {
  182. $wp_query->set_404();
  183. status_header( 404 );
  184. return;
  185. }
  186. $this->renderer->render_sitemap( $url_list );
  187. exit;
  188. }
  189. /**
  190. * Redirects a URL to the wp-sitemap.xml
  191. *
  192. * @since 5.5.0
  193. *
  194. * @param bool $bypass Pass-through of the pre_handle_404 filter value.
  195. * @param WP_Query $query The WP_Query object.
  196. * @return bool Bypass value.
  197. */
  198. public function redirect_sitemapxml( $bypass, $query ) {
  199. // If a plugin has already utilized the pre_handle_404 function, return without action to avoid conflicts.
  200. if ( $bypass ) {
  201. return $bypass;
  202. }
  203. // 'pagename' is for most permalink types, name is for when the %postname% is used as a top-level field.
  204. if ( 'sitemap-xml' === $query->get( 'pagename' )
  205. || 'sitemap-xml' === $query->get( 'name' )
  206. ) {
  207. wp_safe_redirect( $this->index->get_index_url() );
  208. exit();
  209. }
  210. return $bypass;
  211. }
  212. /**
  213. * Adds the sitemap index to robots.txt.
  214. *
  215. * @since 5.5.0
  216. *
  217. * @param string $output robots.txt output.
  218. * @param bool $public Whether the site is public.
  219. * @return string The robots.txt output.
  220. */
  221. public function add_robots( $output, $public ) {
  222. if ( $public ) {
  223. $output .= "\nSitemap: " . esc_url( $this->index->get_index_url() ) . "\n";
  224. }
  225. return $output;
  226. }
  227. }