class-wp-sitemaps-provider.php 4.3 KB

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