123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- <?php
- /**
- * WPSEO plugin file.
- *
- * @package WPSEO\XML_Sitemaps
- */
- /**
- * Sitemap provider for author archives.
- */
- class WPSEO_Author_Sitemap_Provider implements WPSEO_Sitemap_Provider {
- /**
- * Check if provider supports given item type.
- *
- * @param string $type Type string to check for.
- *
- * @return boolean
- */
- public function handles_type( $type ) {
- // If the author archives have been disabled, we don't do anything.
- if ( WPSEO_Options::get( 'disable-author', false ) || WPSEO_Options::get( 'noindex-author-wpseo', false ) ) {
- return false;
- }
- return $type === 'author';
- }
- /**
- * Get the links for the sitemap index.
- *
- * @param int $max_entries Entries per sitemap.
- *
- * @return array
- */
- public function get_index_links( $max_entries ) {
- if ( ! $this->handles_type( 'author' ) ) {
- return [];
- }
- // @todo Consider doing this less often / when necessary. R.
- $this->update_user_meta();
- $has_exclude_filter = has_filter( 'wpseo_sitemap_exclude_author' );
- $query_arguments = [];
- if ( ! $has_exclude_filter ) { // We only need full users if legacy filter(s) hooked to exclusion logic. R.
- $query_arguments['fields'] = 'ID';
- }
- $users = $this->get_users( $query_arguments );
- if ( $has_exclude_filter ) {
- $users = $this->exclude_users( $users );
- $users = wp_list_pluck( $users, 'ID' );
- }
- if ( empty( $users ) ) {
- return [];
- }
- $index = [];
- $page = 1;
- $user_pages = array_chunk( $users, $max_entries );
- if ( count( $user_pages ) === 1 ) {
- $page = '';
- }
- foreach ( $user_pages as $users_page ) {
- $user_id = array_shift( $users_page ); // Time descending, first user on page is most recently updated.
- $user = get_user_by( 'id', $user_id );
- $index[] = [
- 'loc' => WPSEO_Sitemaps_Router::get_base_url( 'author-sitemap' . $page . '.xml' ),
- 'lastmod' => '@' . $user->_yoast_wpseo_profile_updated, // @ for explicit timestamp format
- ];
- $page++;
- }
- return $index;
- }
- /**
- * Retrieve users, taking account of all necessary exclusions.
- *
- * @param array $arguments Arguments to add.
- *
- * @return array
- */
- protected function get_users( $arguments = [] ) {
- global $wpdb;
- $defaults = [
- // @todo Re-enable after plugin requirements raised to WP 4.6 with the fix.
- // 'who' => 'authors', Breaks meta keys, {@link https://core.trac.wordpress.org/ticket/36724#ticket} R.
- 'meta_key' => '_yoast_wpseo_profile_updated',
- 'orderby' => 'meta_value_num',
- 'order' => 'DESC',
- 'meta_query' => [
- 'relation' => 'AND',
- [
- 'key' => $wpdb->get_blog_prefix() . 'user_level',
- 'value' => '0',
- 'compare' => '!=',
- ],
- [
- 'relation' => 'OR',
- [
- 'key' => 'wpseo_noindex_author',
- 'value' => 'on',
- 'compare' => '!=',
- ],
- [
- 'key' => 'wpseo_noindex_author',
- 'compare' => 'NOT EXISTS',
- ],
- ],
- ],
- ];
- if ( WPSEO_Options::get( 'noindex-author-noposts-wpseo', true ) ) {
- // $defaults['who'] = ''; // Otherwise it cancels out next argument.
- $defaults['has_published_posts'] = true;
- }
- return get_users( array_merge( $defaults, $arguments ) );
- }
- /**
- * Get set of sitemap link data.
- *
- * @param string $type Sitemap type.
- * @param int $max_entries Entries per sitemap.
- * @param int $current_page Current page of the sitemap.
- *
- * @throws OutOfBoundsException When an invalid page is requested.
- *
- * @return array
- */
- public function get_sitemap_links( $type, $max_entries, $current_page ) {
- $links = [];
- if ( ! $this->handles_type( 'author' ) ) {
- return $links;
- }
- $user_criteria = [
- 'offset' => ( ( $current_page - 1 ) * $max_entries ),
- 'number' => $max_entries,
- ];
- $users = $this->get_users( $user_criteria );
- // Throw an exception when there are no users in the sitemap.
- if ( count( $users ) === 0 ) {
- throw new OutOfBoundsException( 'Invalid sitemap page requested' );
- }
- $users = $this->exclude_users( $users );
- if ( empty( $users ) ) {
- $users = [];
- }
- $time = time();
- foreach ( $users as $user ) {
- $author_link = get_author_posts_url( $user->ID );
- if ( empty( $author_link ) ) {
- continue;
- }
- $mod = $time;
- if ( isset( $user->_yoast_wpseo_profile_updated ) ) {
- $mod = $user->_yoast_wpseo_profile_updated;
- }
- $url = [
- 'loc' => $author_link,
- 'mod' => date( DATE_W3C, $mod ),
- // Deprecated, kept for backwards data compat. R.
- 'chf' => 'daily',
- 'pri' => 1,
- ];
- /** This filter is documented at inc/sitemaps/class-post-type-sitemap-provider.php */
- $url = apply_filters( 'wpseo_sitemap_entry', $url, 'user', $user );
- if ( ! empty( $url ) ) {
- $links[] = $url;
- }
- }
- return $links;
- }
- /**
- * Update any users that don't have last profile update timestamp.
- *
- * @return int Count of users updated.
- */
- protected function update_user_meta() {
- $user_criteria = [
- 'who' => 'authors',
- 'meta_query' => [
- [
- 'key' => '_yoast_wpseo_profile_updated',
- 'compare' => 'NOT EXISTS',
- ],
- ],
- ];
- $users = get_users( $user_criteria );
- $time = time();
- foreach ( $users as $user ) {
- update_user_meta( $user->ID, '_yoast_wpseo_profile_updated', $time );
- }
- return count( $users );
- }
- /**
- * Wrap legacy filter to deduplicate calls.
- *
- * @param array $users Array of user objects to filter.
- *
- * @return array
- */
- protected function exclude_users( $users ) {
- /**
- * Filter the authors, included in XML sitemap.
- *
- * @param array $users Array of user objects to filter.
- */
- return apply_filters( 'wpseo_sitemap_exclude_author', $users );
- }
- }
|