| @@ -4,11 +4,8 @@ | ||
| 4 | 4 | * |
| 5 | 5 | * @package WPSEO\XML_Sitemaps |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | -use Yoast\WP\SEO\Helpers\Author_Archive_Helper; | |
| 9 | -use Yoast\WP\SEO\Helpers\Wordpress_Helper; | |
| 10 | - | |
| 11 | 8 | /** |
| 12 | 9 | * Sitemap provider for author archives. |
| 13 | 10 | */ |
| 14 | 11 | class WPSEO_Author_Sitemap_Provider implements WPSEO_Sitemap_Provider { |
| @@ -64,25 +61,20 @@ | ||
| 64 | 61 | return []; |
| 65 | 62 | } |
| 66 | 63 | |
| 67 | 64 | $index = []; |
| 68 | - $page = 1; | |
| 69 | 65 | $user_pages = array_chunk( $users, $max_entries ); |
| 70 | 66 | |
| 71 | - if ( count( $user_pages ) === 1 ) { | |
| 72 | - $page = ''; | |
| 73 | - } | |
| 67 | + foreach ( $user_pages as $page_counter => $users_page ) { | |
| 74 | 68 | |
| 75 | - foreach ( $user_pages as $users_page ) { | |
| 69 | + $current_page = ( $page_counter === 0 ) ? '' : ( $page_counter + 1 ); | |
| 76 | 70 | |
| 77 | 71 | $user_id = array_shift( $users_page ); // Time descending, first user on page is most recently updated. |
| 78 | 72 | $user = get_user_by( 'id', $user_id ); |
| 79 | 73 | $index[] = [ |
| 80 | - 'loc' => WPSEO_Sitemaps_Router::get_base_url( 'author-sitemap' . $page . '.xml' ), | |
| 74 | + 'loc' => WPSEO_Sitemaps_Router::get_base_url( 'author-sitemap' . $current_page . '.xml' ), | |
| 81 | 75 | 'lastmod' => ( $user->_yoast_wpseo_profile_updated ) ? YoastSEO()->helpers->date->format_timestamp( $user->_yoast_wpseo_profile_updated ) : null, |
| 82 | 76 | ]; |
| 83 | - | |
| 84 | - ++$page; | |
| 85 | 77 | } |
| 86 | 78 | |
| 87 | 79 | return $index; |
| 88 | 80 | } |
| @@ -95,53 +87,51 @@ | ||
| 95 | 87 | * @return array |
| 96 | 88 | */ |
| 97 | 89 | protected function get_users( $arguments = [] ) { |
| 98 | 90 | |
| 99 | - global $wpdb; | |
| 100 | - | |
| 101 | 91 | $defaults = [ |
| 102 | - 'capability' => [ 'edit_posts' ], | |
| 103 | 92 | 'meta_key' => '_yoast_wpseo_profile_updated', |
| 104 | 93 | 'orderby' => 'meta_value_num', |
| 105 | 94 | 'order' => 'DESC', |
| 106 | 95 | 'meta_query' => [ |
| 107 | - 'relation' => 'AND', | |
| 96 | + 'relation' => 'OR', | |
| 108 | 97 | [ |
| 109 | - 'key' => $wpdb->get_blog_prefix() . 'user_level', | |
| 110 | - 'value' => '0', | |
| 98 | + 'key' => 'wpseo_noindex_author', | |
| 99 | + 'value' => 'on', | |
| 111 | 100 | 'compare' => '!=', |
| 112 | 101 | ], |
| 113 | 102 | [ |
| 114 | - 'relation' => 'OR', | |
| 115 | - [ | |
| 116 | - 'key' => 'wpseo_noindex_author', | |
| 117 | - 'value' => 'on', | |
| 118 | - 'compare' => '!=', | |
| 119 | - ], | |
| 120 | - [ | |
| 121 | - 'key' => 'wpseo_noindex_author', | |
| 122 | - 'compare' => 'NOT EXISTS', | |
| 123 | - ], | |
| 103 | + 'key' => 'wpseo_noindex_author', | |
| 104 | + 'compare' => 'NOT EXISTS', | |
| 124 | 105 | ], |
| 125 | 106 | ], |
| 126 | 107 | ]; |
| 127 | 108 | |
| 128 | - $wordpress_helper = new Wordpress_Helper(); | |
| 129 | - $wordpress_version = $wordpress_helper->get_wordpress_version(); | |
| 109 | + $defaults = $this->apply_author_eligibility_filter( $defaults ); | |
| 130 | 110 | |
| 131 | - // Capability queries were only introduced in WP 5.9. | |
| 132 | - if ( version_compare( $wordpress_version, '5.8.99', '<' ) ) { | |
| 133 | - $defaults['who'] = 'authors'; | |
| 134 | - unset( $defaults['capability'] ); | |
| 135 | - } | |
| 111 | + return get_users( array_merge( $defaults, $arguments ) ); | |
| 112 | + } | |
| 136 | 113 | |
| 114 | + /** | |
| 115 | + * Applies the author-eligibility clause (capability or has_published_posts) to a get_users() criteria array. | |
| 116 | + * | |
| 117 | + * Centralises the `noindex-author-noposts-wpseo` branching so the sitemap query and its | |
| 118 | + * backfill counterpart always agree on which users are considered eligible. | |
| 119 | + * | |
| 120 | + * @param array<string, array<array<string, string>>> $criteria The get_users() criteria array to extend. | |
| 121 | + * | |
| 122 | + * @return array<string, array<array<string, string>>> The criteria array with the eligibility clause applied. | |
| 123 | + */ | |
| 124 | + protected function apply_author_eligibility_filter( array $criteria ) { | |
| 137 | 125 | if ( WPSEO_Options::get( 'noindex-author-noposts-wpseo', true ) ) { |
| 138 | - unset( $defaults['who'], $defaults['capability'] ); // Otherwise it cancels out next argument. | |
| 139 | - $author_archive = new Author_Archive_Helper(); | |
| 140 | - $defaults['has_published_posts'] = $author_archive->get_author_archive_post_types(); | |
| 126 | + $criteria['has_published_posts'] = YoastSEO()->helpers->author_archive->get_author_archive_post_types(); | |
| 127 | + | |
| 128 | + return $criteria; | |
| 141 | 129 | } |
| 142 | 130 | |
| 143 | - return get_users( array_merge( $defaults, $arguments ) ); | |
| 131 | + $criteria['capability'] = [ 'edit_posts' ]; | |
| 132 | + | |
| 133 | + return $criteria; | |
| 144 | 134 | } |
| 145 | 135 | |
| 146 | 136 | /** |
| 147 | 137 | * Get set of sitemap link data. |
| @@ -222,9 +212,8 @@ | ||
| 222 | 212 | */ |
| 223 | 213 | protected function update_user_meta() { |
| 224 | 214 | |
| 225 | 215 | $user_criteria = [ |
| 226 | - 'capability' => [ 'edit_posts' ], | |
| 227 | 216 | 'meta_query' => [ |
| 228 | 217 | [ |
| 229 | 218 | 'key' => '_yoast_wpseo_profile_updated', |
| 230 | 219 | 'compare' => 'NOT EXISTS', |
| @@ -231,16 +220,9 @@ | ||
| 231 | 220 | ], |
| 232 | 221 | ], |
| 233 | 222 | ]; |
| 234 | 223 | |
| 235 | - $wordpress_helper = new Wordpress_Helper(); | |
| 236 | - $wordpress_version = $wordpress_helper->get_wordpress_version(); | |
| 237 | - | |
| 238 | - // Capability queries were only introduced in WP 5.9. | |
| 239 | - if ( version_compare( $wordpress_version, '5.8.99', '<' ) ) { | |
| 240 | - $user_criteria['who'] = 'authors'; | |
| 241 | - unset( $user_criteria['capability'] ); | |
| 242 | - } | |
| 224 | + $user_criteria = $this->apply_author_eligibility_filter( $user_criteria ); | |
| 243 | 225 | |
| 244 | 226 | $users = get_users( $user_criteria ); |
| 245 | 227 | |
| 246 | 228 | $time = time(); |