PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / inc / sitemaps / class-author-sitemap-provider.php

class-author-sitemap-provider.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.5, at inc/sitemaps/class-author-sitemap-provider.php

245 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\XML_Sitemaps
6 */
7
8 /**
9 * Sitemap provider for author archives.
10 */
11 class WPSEO_Author_Sitemap_Provider implements WPSEO_Sitemap_Provider {
12
13 /**
14 * Check if provider supports given item type.
15 *
16 * @param string $type Type string to check for.
17 *
18 * @return bool
19 */
20 public function handles_type( $type ) {
21 // If the author archives have been disabled, we don't do anything.
22 if ( WPSEO_Options::get( 'disable-author', false ) || WPSEO_Options::get( 'noindex-author-wpseo', false ) ) {
23 return false;
24 }
25
26 return $type === 'author';
27 }
28
29 /**
30 * Get the links for the sitemap index.
31 *
32 * @param int $max_entries Entries per sitemap.
33 *
34 * @return array
35 */
36 public function get_index_links( $max_entries ) {
37
38 if ( ! $this->handles_type( 'author' ) ) {
39 return [];
40 }
41
42 // @todo Consider doing this less often / when necessary. R.
43 $this->update_user_meta();
44
45 $has_exclude_filter = has_filter( 'wpseo_sitemap_exclude_author' );
46
47 $query_arguments = [];
48
49 if ( ! $has_exclude_filter ) { // We only need full users if legacy filter(s) hooked to exclusion logic. R.
50 $query_arguments['fields'] = 'ID';
51 }
52
53 $users = $this->get_users( $query_arguments );
54
55 if ( $has_exclude_filter ) {
56 $users = $this->exclude_users( $users );
57 $users = wp_list_pluck( $users, 'ID' );
58 }
59
60 if ( empty( $users ) ) {
61 return [];
62 }
63
64 $index = [];
65 $user_pages = array_chunk( $users, $max_entries );
66
67 foreach ( $user_pages as $page_counter => $users_page ) {
68
69 $current_page = ( $page_counter === 0 ) ? '' : ( $page_counter + 1 );
70
71 $user_id = array_shift( $users_page ); // Time descending, first user on page is most recently updated.
72 $user = get_user_by( 'id', $user_id );
73 $index[] = [
74 'loc' => WPSEO_Sitemaps_Router::get_base_url( 'author-sitemap' . $current_page . '.xml' ),
75 'lastmod' => ( $user->_yoast_wpseo_profile_updated ) ? YoastSEO()->helpers->date->format_timestamp( $user->_yoast_wpseo_profile_updated ) : null,
76 ];
77 }
78
79 return $index;
80 }
81
82 /**
83 * Retrieve users, taking account of all necessary exclusions.
84 *
85 * @param array $arguments Arguments to add.
86 *
87 * @return array
88 */
89 protected function get_users( $arguments = [] ) {
90
91 global $wpdb;
92
93 $defaults = [
94 'capability' => [ 'edit_posts' ],
95 'meta_key' => '_yoast_wpseo_profile_updated',
96 'orderby' => 'meta_value_num',
97 'order' => 'DESC',
98 'meta_query' => [
99 'relation' => 'AND',
100 [
101 'key' => $wpdb->get_blog_prefix() . 'user_level',
102 'value' => '0',
103 'compare' => '!=',
104 ],
105 [
106 'relation' => 'OR',
107 [
108 'key' => 'wpseo_noindex_author',
109 'value' => 'on',
110 'compare' => '!=',
111 ],
112 [
113 'key' => 'wpseo_noindex_author',
114 'compare' => 'NOT EXISTS',
115 ],
116 ],
117 ],
118 ];
119
120 if ( WPSEO_Options::get( 'noindex-author-noposts-wpseo', true ) ) {
121 unset( $defaults['capability'] ); // Otherwise it cancels out next argument.
122 $defaults['has_published_posts'] = YoastSEO()->helpers->author_archive->get_author_archive_post_types();
123 }
124
125 return get_users( array_merge( $defaults, $arguments ) );
126 }
127
128 /**
129 * Get set of sitemap link data.
130 *
131 * @param string $type Sitemap type.
132 * @param int $max_entries Entries per sitemap.
133 * @param int $current_page Current page of the sitemap.
134 *
135 * @return array
136 *
137 * @throws OutOfBoundsException When an invalid page is requested.
138 */
139 public function get_sitemap_links( $type, $max_entries, $current_page ) {
140
141 $links = [];
142
143 if ( ! $this->handles_type( 'author' ) ) {
144 return $links;
145 }
146
147 $user_criteria = [
148 'offset' => ( ( $current_page - 1 ) * $max_entries ),
149 'number' => $max_entries,
150 ];
151
152 $users = $this->get_users( $user_criteria );
153
154 // Throw an exception when there are no users in the sitemap.
155 if ( count( $users ) === 0 ) {
156 throw new OutOfBoundsException( 'Invalid sitemap page requested' );
157 }
158
159 $users = $this->exclude_users( $users );
160 if ( empty( $users ) ) {
161 $users = [];
162 }
163
164 $time = time();
165
166 foreach ( $users as $user ) {
167
168 $author_link = get_author_posts_url( $user->ID );
169
170 if ( empty( $author_link ) ) {
171 continue;
172 }
173
174 $mod = $time;
175
176 if ( isset( $user->_yoast_wpseo_profile_updated ) ) {
177 $mod = $user->_yoast_wpseo_profile_updated;
178 }
179
180 $url = [
181 'loc' => $author_link,
182 'mod' => date( DATE_W3C, $mod ),
183
184 // Deprecated, kept for backwards data compat. R.
185 'chf' => 'daily',
186 'pri' => 1,
187 ];
188
189 /** This filter is documented at inc/sitemaps/class-post-type-sitemap-provider.php */
190 $url = apply_filters( 'wpseo_sitemap_entry', $url, 'user', $user );
191
192 if ( ! empty( $url ) ) {
193 $links[] = $url;
194 }
195 }
196
197 return $links;
198 }
199
200 /**
201 * Update any users that don't have last profile update timestamp.
202 *
203 * @return int Count of users updated.
204 */
205 protected function update_user_meta() {
206
207 $user_criteria = [
208 'capability' => [ 'edit_posts' ],
209 'meta_query' => [
210 [
211 'key' => '_yoast_wpseo_profile_updated',
212 'compare' => 'NOT EXISTS',
213 ],
214 ],
215 ];
216
217 $users = get_users( $user_criteria );
218
219 $time = time();
220
221 foreach ( $users as $user ) {
222 update_user_meta( $user->ID, '_yoast_wpseo_profile_updated', $time );
223 }
224
225 return count( $users );
226 }
227
228 /**
229 * Wrap legacy filter to deduplicate calls.
230 *
231 * @param array $users Array of user objects to filter.
232 *
233 * @return array
234 */
235 protected function exclude_users( $users ) {
236
237 /**
238 * Filter the authors, included in XML sitemap.
239 *
240 * @param array $users Array of user objects to filter.
241 */
242 return apply_filters( 'wpseo_sitemap_exclude_author', $users );
243 }
244 }
245