summaryrefslogtreecommitdiffstats
path: root/wp-includes/sitemaps/providers
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 07:56:49 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 07:56:49 +0000
commita415c29efee45520ae252d2aa28f1083a521cd7b (patch)
treef4ade4b6668ecc0765de7e1424f7c1427ad433ff /wp-includes/sitemaps/providers
parentInitial commit. (diff)
downloadwordpress-a415c29efee45520ae252d2aa28f1083a521cd7b.tar.xz
wordpress-a415c29efee45520ae252d2aa28f1083a521cd7b.zip
Adding upstream version 6.4.3+dfsg1.upstream/6.4.3+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php231
-rw-r--r--wp-includes/sitemaps/providers/class-wp-sitemaps-taxonomies.php213
-rw-r--r--wp-includes/sitemaps/providers/class-wp-sitemaps-users.php164
3 files changed, 608 insertions, 0 deletions
diff --git a/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php b/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php
new file mode 100644
index 0000000..dff85a7
--- /dev/null
+++ b/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php
@@ -0,0 +1,231 @@
+<?php
+/**
+ * Sitemaps: WP_Sitemaps_Posts class
+ *
+ * Builds the sitemaps for the 'post' object type.
+ *
+ * @package WordPress
+ * @subpackage Sitemaps
+ * @since 5.5.0
+ */
+
+/**
+ * Posts XML sitemap provider.
+ *
+ * @since 5.5.0
+ */
+class WP_Sitemaps_Posts extends WP_Sitemaps_Provider {
+ /**
+ * WP_Sitemaps_Posts constructor.
+ *
+ * @since 5.5.0
+ */
+ public function __construct() {
+ $this->name = 'posts';
+ $this->object_type = 'post';
+ }
+
+ /**
+ * Returns the public post types, which excludes nav_items and similar types.
+ * Attachments are also excluded. This includes custom post types with public = true.
+ *
+ * @since 5.5.0
+ *
+ * @return WP_Post_Type[] Array of registered post type objects keyed by their name.
+ */
+ public function get_object_subtypes() {
+ $post_types = get_post_types( array( 'public' => true ), 'objects' );
+ unset( $post_types['attachment'] );
+
+ $post_types = array_filter( $post_types, 'is_post_type_viewable' );
+
+ /**
+ * Filters the list of post object sub types available within the sitemap.
+ *
+ * @since 5.5.0
+ *
+ * @param WP_Post_Type[] $post_types Array of registered post type objects keyed by their name.
+ */
+ return apply_filters( 'wp_sitemaps_post_types', $post_types );
+ }
+
+ /**
+ * Gets a URL list for a post type sitemap.
+ *
+ * @since 5.5.0
+ * @since 5.9.0 Renamed `$post_type` to `$object_subtype` to match parent class
+ * for PHP 8 named parameter support.
+ *
+ * @param int $page_num Page of results.
+ * @param string $object_subtype Optional. Post type name. Default empty.
+ *
+ * @return array[] Array of URL information for a sitemap.
+ */
+ public function get_url_list( $page_num, $object_subtype = '' ) {
+ // Restores the more descriptive, specific name for use within this method.
+ $post_type = $object_subtype;
+
+ // Bail early if the queried post type is not supported.
+ $supported_types = $this->get_object_subtypes();
+
+ if ( ! isset( $supported_types[ $post_type ] ) ) {
+ return array();
+ }
+
+ /**
+ * Filters the posts URL list before it is generated.
+ *
+ * Returning a non-null value will effectively short-circuit the generation,
+ * returning that value instead.
+ *
+ * @since 5.5.0
+ *
+ * @param array[]|null $url_list The URL list. Default null.
+ * @param string $post_type Post type name.
+ * @param int $page_num Page of results.
+ */
+ $url_list = apply_filters(
+ 'wp_sitemaps_posts_pre_url_list',
+ null,
+ $post_type,
+ $page_num
+ );
+
+ if ( null !== $url_list ) {
+ return $url_list;
+ }
+
+ $args = $this->get_posts_query_args( $post_type );
+ $args['paged'] = $page_num;
+
+ $query = new WP_Query( $args );
+
+ $url_list = array();
+
+ /*
+ * Add a URL for the homepage in the pages sitemap.
+ * Shows only on the first page if the reading settings are set to display latest posts.
+ */
+ if ( 'page' === $post_type && 1 === $page_num && 'posts' === get_option( 'show_on_front' ) ) {
+ // Extract the data needed for home URL to add to the array.
+ $sitemap_entry = array(
+ 'loc' => home_url( '/' ),
+ );
+
+ /**
+ * Filters the sitemap entry for the home page when the 'show_on_front' option equals 'posts'.
+ *
+ * @since 5.5.0
+ *
+ * @param array $sitemap_entry Sitemap entry for the home page.
+ */
+ $sitemap_entry = apply_filters( 'wp_sitemaps_posts_show_on_front_entry', $sitemap_entry );
+ $url_list[] = $sitemap_entry;
+ }
+
+ foreach ( $query->posts as $post ) {
+ $sitemap_entry = array(
+ 'loc' => get_permalink( $post ),
+ );
+
+ /**
+ * Filters the sitemap entry for an individual post.
+ *
+ * @since 5.5.0
+ *
+ * @param array $sitemap_entry Sitemap entry for the post.
+ * @param WP_Post $post Post object.
+ * @param string $post_type Name of the post_type.
+ */
+ $sitemap_entry = apply_filters( 'wp_sitemaps_posts_entry', $sitemap_entry, $post, $post_type );
+ $url_list[] = $sitemap_entry;
+ }
+
+ return $url_list;
+ }
+
+ /**
+ * Gets the max number of pages available for the object type.
+ *
+ * @since 5.5.0
+ * @since 5.9.0 Renamed `$post_type` to `$object_subtype` to match parent class
+ * for PHP 8 named parameter support.
+ *
+ * @param string $object_subtype Optional. Post type name. Default empty.
+ * @return int Total number of pages.
+ */
+ public function get_max_num_pages( $object_subtype = '' ) {
+ if ( empty( $object_subtype ) ) {
+ return 0;
+ }
+
+ // Restores the more descriptive, specific name for use within this method.
+ $post_type = $object_subtype;
+
+ /**
+ * Filters the max number of pages before it is generated.
+ *
+ * Passing a non-null value will short-circuit the generation,
+ * returning that value instead.
+ *
+ * @since 5.5.0
+ *
+ * @param int|null $max_num_pages The maximum number of pages. Default null.
+ * @param string $post_type Post type name.
+ */
+ $max_num_pages = apply_filters( 'wp_sitemaps_posts_pre_max_num_pages', null, $post_type );
+
+ if ( null !== $max_num_pages ) {
+ return $max_num_pages;
+ }
+
+ $args = $this->get_posts_query_args( $post_type );
+ $args['fields'] = 'ids';
+ $args['no_found_rows'] = false;
+
+ $query = new WP_Query( $args );
+
+ $min_num_pages = ( 'page' === $post_type && 'posts' === get_option( 'show_on_front' ) ) ? 1 : 0;
+ return isset( $query->max_num_pages ) ? max( $min_num_pages, $query->max_num_pages ) : 1;
+ }
+
+ /**
+ * Returns the query args for retrieving posts to list in the sitemap.
+ *
+ * @since 5.5.0
+ * @since 6.1.0 Added `ignore_sticky_posts` default parameter.
+ *
+ * @param string $post_type Post type name.
+ * @return array Array of WP_Query arguments.
+ */
+ protected function get_posts_query_args( $post_type ) {
+ /**
+ * Filters the query arguments for post type sitemap queries.
+ *
+ * @see WP_Query for a full list of arguments.
+ *
+ * @since 5.5.0
+ * @since 6.1.0 Added `ignore_sticky_posts` default parameter.
+ *
+ * @param array $args Array of WP_Query arguments.
+ * @param string $post_type Post type name.
+ */
+ $args = apply_filters(
+ 'wp_sitemaps_posts_query_args',
+ array(
+ 'orderby' => 'ID',
+ 'order' => 'ASC',
+ 'post_type' => $post_type,
+ 'posts_per_page' => wp_sitemaps_get_max_urls( $this->object_type ),
+ 'post_status' => array( 'publish' ),
+ 'no_found_rows' => true,
+ 'update_post_term_cache' => false,
+ 'update_post_meta_cache' => false,
+ 'ignore_sticky_posts' => true, // Sticky posts will still appear, but they won't be moved to the front.
+ ),
+ $post_type
+ );
+
+ return $args;
+ }
+}
diff --git a/wp-includes/sitemaps/providers/class-wp-sitemaps-taxonomies.php b/wp-includes/sitemaps/providers/class-wp-sitemaps-taxonomies.php
new file mode 100644
index 0000000..5571ff4
--- /dev/null
+++ b/wp-includes/sitemaps/providers/class-wp-sitemaps-taxonomies.php
@@ -0,0 +1,213 @@
+<?php
+/**
+ * Sitemaps: WP_Sitemaps_Taxonomies class
+ *
+ * Builds the sitemaps for the 'taxonomy' object type.
+ *
+ * @package WordPress
+ * @subpackage Sitemaps
+ * @since 5.5.0
+ */
+
+/**
+ * Taxonomies XML sitemap provider.
+ *
+ * @since 5.5.0
+ */
+class WP_Sitemaps_Taxonomies extends WP_Sitemaps_Provider {
+ /**
+ * WP_Sitemaps_Taxonomies constructor.
+ *
+ * @since 5.5.0
+ */
+ public function __construct() {
+ $this->name = 'taxonomies';
+ $this->object_type = 'term';
+ }
+
+ /**
+ * Returns all public, registered taxonomies.
+ *
+ * @since 5.5.0
+ *
+ * @return WP_Taxonomy[] Array of registered taxonomy objects keyed by their name.
+ */
+ public function get_object_subtypes() {
+ $taxonomies = get_taxonomies( array( 'public' => true ), 'objects' );
+
+ $taxonomies = array_filter( $taxonomies, 'is_taxonomy_viewable' );
+
+ /**
+ * Filters the list of taxonomy object subtypes available within the sitemap.
+ *
+ * @since 5.5.0
+ *
+ * @param WP_Taxonomy[] $taxonomies Array of registered taxonomy objects keyed by their name.
+ */
+ return apply_filters( 'wp_sitemaps_taxonomies', $taxonomies );
+ }
+
+ /**
+ * Gets a URL list for a taxonomy sitemap.
+ *
+ * @since 5.5.0
+ * @since 5.9.0 Renamed `$taxonomy` to `$object_subtype` to match parent class
+ * for PHP 8 named parameter support.
+ *
+ * @param int $page_num Page of results.
+ * @param string $object_subtype Optional. Taxonomy name. Default empty.
+ * @return array[] Array of URL information for a sitemap.
+ */
+ public function get_url_list( $page_num, $object_subtype = '' ) {
+ // Restores the more descriptive, specific name for use within this method.
+ $taxonomy = $object_subtype;
+
+ $supported_types = $this->get_object_subtypes();
+
+ // Bail early if the queried taxonomy is not supported.
+ if ( ! isset( $supported_types[ $taxonomy ] ) ) {
+ return array();
+ }
+
+ /**
+ * Filters the taxonomies URL list before it is generated.
+ *
+ * Returning a non-null value will effectively short-circuit the generation,
+ * returning that value instead.
+ *
+ * @since 5.5.0
+ *
+ * @param array[]|null $url_list The URL list. Default null.
+ * @param string $taxonomy Taxonomy name.
+ * @param int $page_num Page of results.
+ */
+ $url_list = apply_filters(
+ 'wp_sitemaps_taxonomies_pre_url_list',
+ null,
+ $taxonomy,
+ $page_num
+ );
+
+ if ( null !== $url_list ) {
+ return $url_list;
+ }
+
+ $url_list = array();
+
+ // Offset by how many terms should be included in previous pages.
+ $offset = ( $page_num - 1 ) * wp_sitemaps_get_max_urls( $this->object_type );
+
+ $args = $this->get_taxonomies_query_args( $taxonomy );
+ $args['fields'] = 'all';
+ $args['offset'] = $offset;
+
+ $taxonomy_terms = new WP_Term_Query( $args );
+
+ if ( ! empty( $taxonomy_terms->terms ) ) {
+ foreach ( $taxonomy_terms->terms as $term ) {
+ $term_link = get_term_link( $term, $taxonomy );
+
+ if ( is_wp_error( $term_link ) ) {
+ continue;
+ }
+
+ $sitemap_entry = array(
+ 'loc' => $term_link,
+ );
+
+ /**
+ * Filters the sitemap entry for an individual term.
+ *
+ * @since 5.5.0
+ * @since 6.0.0 Added `$term` argument containing the term object.
+ *
+ * @param array $sitemap_entry Sitemap entry for the term.
+ * @param int $term_id Term ID.
+ * @param string $taxonomy Taxonomy name.
+ * @param WP_Term $term Term object.
+ */
+ $sitemap_entry = apply_filters( 'wp_sitemaps_taxonomies_entry', $sitemap_entry, $term->term_id, $taxonomy, $term );
+ $url_list[] = $sitemap_entry;
+ }
+ }
+
+ return $url_list;
+ }
+
+ /**
+ * Gets the max number of pages available for the object type.
+ *
+ * @since 5.5.0
+ * @since 5.9.0 Renamed `$taxonomy` to `$object_subtype` to match parent class
+ * for PHP 8 named parameter support.
+ *
+ * @param string $object_subtype Optional. Taxonomy name. Default empty.
+ * @return int Total number of pages.
+ */
+ public function get_max_num_pages( $object_subtype = '' ) {
+ if ( empty( $object_subtype ) ) {
+ return 0;
+ }
+
+ // Restores the more descriptive, specific name for use within this method.
+ $taxonomy = $object_subtype;
+
+ /**
+ * Filters the max number of pages for a taxonomy sitemap before it is generated.
+ *
+ * Passing a non-null value will short-circuit the generation,
+ * returning that value instead.
+ *
+ * @since 5.5.0
+ *
+ * @param int|null $max_num_pages The maximum number of pages. Default null.
+ * @param string $taxonomy Taxonomy name.
+ */
+ $max_num_pages = apply_filters( 'wp_sitemaps_taxonomies_pre_max_num_pages', null, $taxonomy );
+
+ if ( null !== $max_num_pages ) {
+ return $max_num_pages;
+ }
+
+ $term_count = wp_count_terms( $this->get_taxonomies_query_args( $taxonomy ) );
+
+ return (int) ceil( $term_count / wp_sitemaps_get_max_urls( $this->object_type ) );
+ }
+
+ /**
+ * Returns the query args for retrieving taxonomy terms to list in the sitemap.
+ *
+ * @since 5.5.0
+ *
+ * @param string $taxonomy Taxonomy name.
+ * @return array Array of WP_Term_Query arguments.
+ */
+ protected function get_taxonomies_query_args( $taxonomy ) {
+ /**
+ * Filters the taxonomy terms query arguments.
+ *
+ * Allows modification of the taxonomy query arguments before querying.
+ *
+ * @see WP_Term_Query for a full list of arguments
+ *
+ * @since 5.5.0
+ *
+ * @param array $args Array of WP_Term_Query arguments.
+ * @param string $taxonomy Taxonomy name.
+ */
+ $args = apply_filters(
+ 'wp_sitemaps_taxonomies_query_args',
+ array(
+ 'taxonomy' => $taxonomy,
+ 'orderby' => 'term_order',
+ 'number' => wp_sitemaps_get_max_urls( $this->object_type ),
+ 'hide_empty' => true,
+ 'hierarchical' => false,
+ 'update_term_meta_cache' => false,
+ ),
+ $taxonomy
+ );
+
+ return $args;
+ }
+}
diff --git a/wp-includes/sitemaps/providers/class-wp-sitemaps-users.php b/wp-includes/sitemaps/providers/class-wp-sitemaps-users.php
new file mode 100644
index 0000000..8e9f57d
--- /dev/null
+++ b/wp-includes/sitemaps/providers/class-wp-sitemaps-users.php
@@ -0,0 +1,164 @@
+<?php
+/**
+ * Sitemaps: WP_Sitemaps_Users class
+ *
+ * Builds the sitemaps for the 'user' object type.
+ *
+ * @package WordPress
+ * @subpackage Sitemaps
+ * @since 5.5.0
+ */
+
+/**
+ * Users XML sitemap provider.
+ *
+ * @since 5.5.0
+ */
+class WP_Sitemaps_Users extends WP_Sitemaps_Provider {
+ /**
+ * WP_Sitemaps_Users constructor.
+ *
+ * @since 5.5.0
+ */
+ public function __construct() {
+ $this->name = 'users';
+ $this->object_type = 'user';
+ }
+
+ /**
+ * Gets a URL list for a user sitemap.
+ *
+ * @since 5.5.0
+ *
+ * @param int $page_num Page of results.
+ * @param string $object_subtype Optional. Not applicable for Users but
+ * required for compatibility with the parent
+ * provider class. Default empty.
+ * @return array[] Array of URL information for a sitemap.
+ */
+ public function get_url_list( $page_num, $object_subtype = '' ) {
+ /**
+ * Filters the users URL list before it is generated.
+ *
+ * Returning a non-null value will effectively short-circuit the generation,
+ * returning that value instead.
+ *
+ * @since 5.5.0
+ *
+ * @param array[]|null $url_list The URL list. Default null.
+ * @param int $page_num Page of results.
+ */
+ $url_list = apply_filters(
+ 'wp_sitemaps_users_pre_url_list',
+ null,
+ $page_num
+ );
+
+ if ( null !== $url_list ) {
+ return $url_list;
+ }
+
+ $args = $this->get_users_query_args();
+ $args['paged'] = $page_num;
+
+ $query = new WP_User_Query( $args );
+ $users = $query->get_results();
+ $url_list = array();
+
+ foreach ( $users as $user ) {
+ $sitemap_entry = array(
+ 'loc' => get_author_posts_url( $user->ID ),
+ );
+
+ /**
+ * Filters the sitemap entry for an individual user.
+ *
+ * @since 5.5.0
+ *
+ * @param array $sitemap_entry Sitemap entry for the user.
+ * @param WP_User $user User object.
+ */
+ $sitemap_entry = apply_filters( 'wp_sitemaps_users_entry', $sitemap_entry, $user );
+ $url_list[] = $sitemap_entry;
+ }
+
+ return $url_list;
+ }
+
+ /**
+ * Gets the max number of pages available for the object type.
+ *
+ * @since 5.5.0
+ *
+ * @see WP_Sitemaps_Provider::max_num_pages
+ *
+ * @param string $object_subtype Optional. Not applicable for Users but
+ * required for compatibility with the parent
+ * provider class. Default empty.
+ * @return int Total page count.
+ */
+ public function get_max_num_pages( $object_subtype = '' ) {
+ /**
+ * Filters the max number of pages for a user sitemap before it is generated.
+ *
+ * Returning a non-null value will effectively short-circuit the generation,
+ * returning that value instead.
+ *
+ * @since 5.5.0
+ *
+ * @param int|null $max_num_pages The maximum number of pages. Default null.
+ */
+ $max_num_pages = apply_filters( 'wp_sitemaps_users_pre_max_num_pages', null );
+
+ if ( null !== $max_num_pages ) {
+ return $max_num_pages;
+ }
+
+ $args = $this->get_users_query_args();
+ $query = new WP_User_Query( $args );
+
+ $total_users = $query->get_total();
+
+ return (int) ceil( $total_users / wp_sitemaps_get_max_urls( $this->object_type ) );
+ }
+
+ /**
+ * Returns the query args for retrieving users to list in the sitemap.
+ *
+ * @since 5.5.0
+ *
+ * @return array Array of WP_User_Query arguments.
+ */
+ protected function get_users_query_args() {
+ $public_post_types = get_post_types(
+ array(
+ 'public' => true,
+ )
+ );
+
+ // We're not supporting sitemaps for author pages for attachments and pages.
+ unset( $public_post_types['attachment'] );
+ unset( $public_post_types['page'] );
+
+ /**
+ * Filters the query arguments for authors with public posts.
+ *
+ * Allows modification of the authors query arguments before querying.
+ *
+ * @see WP_User_Query for a full list of arguments
+ *
+ * @since 5.5.0
+ *
+ * @param array $args Array of WP_User_Query arguments.
+ */
+ $args = apply_filters(
+ 'wp_sitemaps_users_query_args',
+ array(
+ 'has_published_posts' => array_keys( $public_post_types ),
+ 'number' => wp_sitemaps_get_max_urls( $this->object_type ),
+ )
+ );
+
+ return $args;
+ }
+}