Aucune description

class-wp-sitemaps-provider.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Sitemaps: WP_Sitemaps_Provider class
  4. *
  5. * This class is a base class for other sitemap providers to extend and contains shared functionality.
  6. *
  7. * @package WordPress
  8. * @subpackage Sitemaps
  9. * @since 5.5.0
  10. */
  11. /**
  12. * Class WP_Sitemaps_Provider.
  13. *
  14. * @since 5.5.0
  15. */
  16. abstract class WP_Sitemaps_Provider {
  17. /**
  18. * Provider name.
  19. *
  20. * This will also be used as the public-facing name in URLs.
  21. *
  22. * @since 5.5.0
  23. *
  24. * @var string
  25. */
  26. protected $name = '';
  27. /**
  28. * Object type name (e.g. 'post', 'term', 'user').
  29. *
  30. * @since 5.5.0
  31. *
  32. * @var string
  33. */
  34. protected $object_type = '';
  35. /**
  36. * Gets a URL list for a sitemap.
  37. *
  38. * @since 5.5.0
  39. *
  40. * @param int $page_num Page of results.
  41. * @param string $object_subtype Optional. Object subtype name. Default empty.
  42. * @return array Array of URLs for a sitemap.
  43. */
  44. abstract public function get_url_list( $page_num, $object_subtype = '' );
  45. /**
  46. * Gets the max number of pages available for the object type.
  47. *
  48. * @since 5.5.0
  49. *
  50. * @param string $object_subtype Optional. Object subtype. Default empty.
  51. * @return int Total number of pages.
  52. */
  53. abstract public function get_max_num_pages( $object_subtype = '' );
  54. /**
  55. * Gets data about each sitemap type.
  56. *
  57. * @since 5.5.0
  58. *
  59. * @return array[] Array of sitemap types including object subtype name and number of pages.
  60. */
  61. public function get_sitemap_type_data() {
  62. $sitemap_data = array();
  63. $object_subtypes = $this->get_object_subtypes();
  64. // If there are no object subtypes, include a single sitemap for the
  65. // entire object type.
  66. if ( empty( $object_subtypes ) ) {
  67. $sitemap_data[] = array(
  68. 'name' => '',
  69. 'pages' => $this->get_max_num_pages(),
  70. );
  71. return $sitemap_data;
  72. }
  73. // Otherwise, include individual sitemaps for every object subtype.
  74. foreach ( $object_subtypes as $object_subtype_name => $data ) {
  75. $object_subtype_name = (string) $object_subtype_name;
  76. $sitemap_data[] = array(
  77. 'name' => $object_subtype_name,
  78. 'pages' => $this->get_max_num_pages( $object_subtype_name ),
  79. );
  80. }
  81. return $sitemap_data;
  82. }
  83. /**
  84. * Lists sitemap pages exposed by this provider.
  85. *
  86. * The returned data is used to populate the sitemap entries of the index.
  87. *
  88. * @since 5.5.0
  89. *
  90. * @return array[] Array of sitemap entries.
  91. */
  92. public function get_sitemap_entries() {
  93. $sitemaps = array();
  94. $sitemap_types = $this->get_sitemap_type_data();
  95. foreach ( $sitemap_types as $type ) {
  96. for ( $page = 1; $page <= $type['pages']; $page ++ ) {
  97. $sitemap_entry = array(
  98. 'loc' => $this->get_sitemap_url( $type['name'], $page ),
  99. );
  100. /**
  101. * Filters the sitemap entry for the sitemap index.
  102. *
  103. * @since 5.5.0
  104. *
  105. * @param array $sitemap_entry Sitemap entry for the post.
  106. * @param string $object_type Object empty name.
  107. * @param string $object_subtype Object subtype name.
  108. * Empty string if the object type does not support subtypes.
  109. * @param int $page Page number of results.
  110. */
  111. $sitemap_entry = apply_filters( 'wp_sitemaps_index_entry', $sitemap_entry, $this->object_type, $type['name'], $page );
  112. $sitemaps[] = $sitemap_entry;
  113. }
  114. }
  115. return $sitemaps;
  116. }
  117. /**
  118. * Gets the URL of a sitemap entry.
  119. *
  120. * @since 5.5.0
  121. *
  122. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  123. *
  124. * @param string $name The name of the sitemap.
  125. * @param int $page The page of the sitemap.
  126. * @return string The composed URL for a sitemap entry.
  127. */
  128. public function get_sitemap_url( $name, $page ) {
  129. global $wp_rewrite;
  130. // Accounts for cases where name is not included, ex: sitemaps-users-1.xml.
  131. $params = array_filter(
  132. array(
  133. 'sitemap' => $this->name,
  134. 'sitemap-subtype' => $name,
  135. 'paged' => $page,
  136. )
  137. );
  138. $basename = sprintf(
  139. '/wp-sitemap-%1$s.xml',
  140. implode( '-', $params )
  141. );
  142. if ( ! $wp_rewrite->using_permalinks() ) {
  143. $basename = '/?' . http_build_query( $params, null, '&' );
  144. }
  145. return home_url( $basename );
  146. }
  147. /**
  148. * Returns the list of supported object subtypes exposed by the provider.
  149. *
  150. * @since 5.5.0
  151. *
  152. * @return array List of object subtypes objects keyed by their name.
  153. */
  154. public function get_object_subtypes() {
  155. return array();
  156. }
  157. }