Nenhuma Descrição

class-wp-sitemaps-posts.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * Sitemaps: WP_Sitemaps_Posts class
  4. *
  5. * Builds the sitemaps for the 'post' object type.
  6. *
  7. * @package WordPress
  8. * @subpackage Sitemaps
  9. * @since 5.5.0
  10. */
  11. /**
  12. * Posts XML sitemap provider.
  13. *
  14. * @since 5.5.0
  15. */
  16. class WP_Sitemaps_Posts extends WP_Sitemaps_Provider {
  17. /**
  18. * WP_Sitemaps_Posts constructor.
  19. *
  20. * @since 5.5.0
  21. */
  22. public function __construct() {
  23. $this->name = 'posts';
  24. $this->object_type = 'post';
  25. }
  26. /**
  27. * Returns the public post types, which excludes nav_items and similar types.
  28. * Attachments are also excluded. This includes custom post types with public = true.
  29. *
  30. * @since 5.5.0
  31. *
  32. * @return WP_Post_Type[] Array of registered post type objects keyed by their name.
  33. */
  34. public function get_object_subtypes() {
  35. $post_types = get_post_types( array( 'public' => true ), 'objects' );
  36. unset( $post_types['attachment'] );
  37. $post_types = array_filter( $post_types, 'is_post_type_viewable' );
  38. /**
  39. * Filters the list of post object sub types available within the sitemap.
  40. *
  41. * @since 5.5.0
  42. *
  43. * @param WP_Post_Type[] $post_types Array of registered post type objects keyed by their name.
  44. */
  45. return apply_filters( 'wp_sitemaps_post_types', $post_types );
  46. }
  47. /**
  48. * Gets a URL list for a post type sitemap.
  49. *
  50. * @since 5.5.0
  51. *
  52. * @param int $page_num Page of results.
  53. * @param string $post_type Optional. Post type name. Default empty.
  54. * @return array Array of URLs for a sitemap.
  55. */
  56. public function get_url_list( $page_num, $post_type = '' ) {
  57. // Bail early if the queried post type is not supported.
  58. $supported_types = $this->get_object_subtypes();
  59. if ( ! isset( $supported_types[ $post_type ] ) ) {
  60. return array();
  61. }
  62. /**
  63. * Filters the posts URL list before it is generated.
  64. *
  65. * Passing a non-null value will effectively short-circuit the generation,
  66. * returning that value instead.
  67. *
  68. * @since 5.5.0
  69. *
  70. * @param array $url_list The URL list. Default null.
  71. * @param string $post_type Post type name.
  72. * @param int $page_num Page of results.
  73. */
  74. $url_list = apply_filters(
  75. 'wp_sitemaps_posts_pre_url_list',
  76. null,
  77. $post_type,
  78. $page_num
  79. );
  80. if ( null !== $url_list ) {
  81. return $url_list;
  82. }
  83. $args = $this->get_posts_query_args( $post_type );
  84. $args['paged'] = $page_num;
  85. $query = new WP_Query( $args );
  86. $url_list = array();
  87. /*
  88. * Add a URL for the homepage in the pages sitemap.
  89. * Shows only on the first page if the reading settings are set to display latest posts.
  90. */
  91. if ( 'page' === $post_type && 1 === $page_num && 'posts' === get_option( 'show_on_front' ) ) {
  92. // Extract the data needed for home URL to add to the array.
  93. $sitemap_entry = array(
  94. 'loc' => home_url( '/' ),
  95. );
  96. /**
  97. * Filters the sitemap entry for the home page when the 'show_on_front' option equals 'posts'.
  98. *
  99. * @since 5.5.0
  100. *
  101. * @param array $sitemap_entry Sitemap entry for the home page.
  102. */
  103. $sitemap_entry = apply_filters( 'wp_sitemaps_posts_show_on_front_entry', $sitemap_entry );
  104. $url_list[] = $sitemap_entry;
  105. }
  106. foreach ( $query->posts as $post ) {
  107. $sitemap_entry = array(
  108. 'loc' => get_permalink( $post ),
  109. );
  110. /**
  111. * Filters the sitemap entry for an individual post.
  112. *
  113. * @since 5.5.0
  114. *
  115. * @param array $sitemap_entry Sitemap entry for the post.
  116. * @param WP_Post $post Post object.
  117. * @param string $post_type Name of the post_type.
  118. */
  119. $sitemap_entry = apply_filters( 'wp_sitemaps_posts_entry', $sitemap_entry, $post, $post_type );
  120. $url_list[] = $sitemap_entry;
  121. }
  122. return $url_list;
  123. }
  124. /**
  125. * Gets the max number of pages available for the object type.
  126. *
  127. * @since 5.5.0
  128. *
  129. * @param string $post_type Optional. Post type name. Default empty.
  130. * @return int Total number of pages.
  131. */
  132. public function get_max_num_pages( $post_type = '' ) {
  133. if ( empty( $post_type ) ) {
  134. return 0;
  135. }
  136. /**
  137. * Filters the max number of pages before it is generated.
  138. *
  139. * Passing a non-null value will short-circuit the generation,
  140. * returning that value instead.
  141. *
  142. * @since 5.5.0
  143. *
  144. * @param int|null $max_num_pages The maximum number of pages. Default null.
  145. * @param string $post_type Post type name.
  146. */
  147. $max_num_pages = apply_filters( 'wp_sitemaps_posts_pre_max_num_pages', null, $post_type );
  148. if ( null !== $max_num_pages ) {
  149. return $max_num_pages;
  150. }
  151. $args = $this->get_posts_query_args( $post_type );
  152. $args['fields'] = 'ids';
  153. $args['no_found_rows'] = false;
  154. $query = new WP_Query( $args );
  155. $min_num_pages = ( 'page' === $post_type && 'posts' === get_option( 'show_on_front' ) ) ? 1 : 0;
  156. return isset( $query->max_num_pages ) ? max( $min_num_pages, $query->max_num_pages ) : 1;
  157. }
  158. /**
  159. * Returns the query args for retrieving posts to list in the sitemap.
  160. *
  161. * @since 5.5.0
  162. *
  163. * @param string $post_type Post type name.
  164. * @return array Array of WP_Query arguments.
  165. */
  166. protected function get_posts_query_args( $post_type ) {
  167. /**
  168. * Filters the query arguments for post type sitemap queries.
  169. *
  170. * @see WP_Query for a full list of arguments.
  171. *
  172. * @since 5.5.0
  173. *
  174. * @param array $args Array of WP_Query arguments.
  175. * @param string $post_type Post type name.
  176. */
  177. $args = apply_filters(
  178. 'wp_sitemaps_posts_query_args',
  179. array(
  180. 'orderby' => 'ID',
  181. 'order' => 'ASC',
  182. 'post_type' => $post_type,
  183. 'posts_per_page' => wp_sitemaps_get_max_urls( $this->object_type ),
  184. 'post_status' => array( 'publish' ),
  185. 'no_found_rows' => true,
  186. 'update_post_term_cache' => false,
  187. 'update_post_meta_cache' => false,
  188. ),
  189. $post_type
  190. );
  191. return $args;
  192. }
  193. }