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