class-wp-sitemaps-users.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Sitemaps: WP_Sitemaps_Users class
  4. *
  5. * Builds the sitemaps for the 'user' object type.
  6. *
  7. * @package WordPress
  8. * @subpackage Sitemaps
  9. * @since 5.5.0
  10. */
  11. /**
  12. * Users XML sitemap provider.
  13. *
  14. * @since 5.5.0
  15. */
  16. class WP_Sitemaps_Users extends WP_Sitemaps_Provider {
  17. /**
  18. * WP_Sitemaps_Users constructor.
  19. *
  20. * @since 5.5.0
  21. */
  22. public function __construct() {
  23. $this->name = 'users';
  24. $this->object_type = 'user';
  25. }
  26. /**
  27. * Gets a URL list for a user sitemap.
  28. *
  29. * @since 5.5.0
  30. *
  31. * @param int $page_num Page of results.
  32. * @param string $object_subtype Optional. Not applicable for Users but
  33. * required for compatibility with the parent
  34. * provider class. Default empty.
  35. * @return array[] Array of URL information for a sitemap.
  36. */
  37. public function get_url_list( $page_num, $object_subtype = '' ) {
  38. /**
  39. * Filters the users URL list before it is generated.
  40. *
  41. * Returning a non-null value will effectively short-circuit the generation,
  42. * returning that value instead.
  43. *
  44. * @since 5.5.0
  45. *
  46. * @param array[]|null $url_list The URL list. Default null.
  47. * @param int $page_num Page of results.
  48. */
  49. $url_list = apply_filters(
  50. 'wp_sitemaps_users_pre_url_list',
  51. null,
  52. $page_num
  53. );
  54. if ( null !== $url_list ) {
  55. return $url_list;
  56. }
  57. $args = $this->get_users_query_args();
  58. $args['paged'] = $page_num;
  59. $query = new WP_User_Query( $args );
  60. $users = $query->get_results();
  61. $url_list = array();
  62. foreach ( $users as $user ) {
  63. $sitemap_entry = array(
  64. 'loc' => get_author_posts_url( $user->ID ),
  65. );
  66. /**
  67. * Filters the sitemap entry for an individual user.
  68. *
  69. * @since 5.5.0
  70. *
  71. * @param array $sitemap_entry Sitemap entry for the user.
  72. * @param WP_User $user User object.
  73. */
  74. $sitemap_entry = apply_filters( 'wp_sitemaps_users_entry', $sitemap_entry, $user );
  75. $url_list[] = $sitemap_entry;
  76. }
  77. return $url_list;
  78. }
  79. /**
  80. * Gets the max number of pages available for the object type.
  81. *
  82. * @since 5.5.0
  83. *
  84. * @see WP_Sitemaps_Provider::max_num_pages
  85. *
  86. * @param string $object_subtype Optional. Not applicable for Users but
  87. * required for compatibility with the parent
  88. * provider class. Default empty.
  89. * @return int Total page count.
  90. */
  91. public function get_max_num_pages( $object_subtype = '' ) {
  92. /**
  93. * Filters the max number of pages for a user sitemap before it is generated.
  94. *
  95. * Returning a non-null value will effectively short-circuit the generation,
  96. * returning that value instead.
  97. *
  98. * @since 5.5.0
  99. *
  100. * @param int|null $max_num_pages The maximum number of pages. Default null.
  101. */
  102. $max_num_pages = apply_filters( 'wp_sitemaps_users_pre_max_num_pages', null );
  103. if ( null !== $max_num_pages ) {
  104. return $max_num_pages;
  105. }
  106. $args = $this->get_users_query_args();
  107. $query = new WP_User_Query( $args );
  108. $total_users = $query->get_total();
  109. return (int) ceil( $total_users / wp_sitemaps_get_max_urls( $this->object_type ) );
  110. }
  111. /**
  112. * Returns the query args for retrieving users to list in the sitemap.
  113. *
  114. * @since 5.5.0
  115. *
  116. * @return array Array of WP_User_Query arguments.
  117. */
  118. protected function get_users_query_args() {
  119. $public_post_types = get_post_types(
  120. array(
  121. 'public' => true,
  122. )
  123. );
  124. // We're not supporting sitemaps for author pages for attachments.
  125. unset( $public_post_types['attachment'] );
  126. /**
  127. * Filters the query arguments for authors with public posts.
  128. *
  129. * Allows modification of the authors query arguments before querying.
  130. *
  131. * @see WP_User_Query for a full list of arguments
  132. *
  133. * @since 5.5.0
  134. *
  135. * @param array $args Array of WP_User_Query arguments.
  136. */
  137. $args = apply_filters(
  138. 'wp_sitemaps_users_query_args',
  139. array(
  140. 'has_published_posts' => array_keys( $public_post_types ),
  141. 'number' => wp_sitemaps_get_max_urls( $this->object_type ),
  142. )
  143. );
  144. return $args;
  145. }
  146. }