WPSEO_Author_Sitemap_Provider

Sitemap provider for author archives.

Defined (1)

The class is defined in the following location(s).

/inc/sitemaps/class-author-sitemap-provider.php  
  1. class WPSEO_Author_Sitemap_Provider implements WPSEO_Sitemap_Provider { 
  2.  
  3. /** 
  4. * Check if provider supports given item type. 
  5. * @param string $type Type string to check for. 
  6. * @return boolean 
  7. */ 
  8. public function handles_type( $type ) { 
  9.  
  10. return $type === 'author'; 
  11.  
  12. /** 
  13. * @param int $max_entries Entries per sitemap. 
  14. * @return array 
  15. */ 
  16. public function get_index_links( $max_entries ) { 
  17.  
  18. $options = WPSEO_Options::get_all(); 
  19.  
  20. if ( $options['disable-author'] || $options['disable_author_sitemap'] ) { 
  21. return array(); 
  22.  
  23. // TODO Consider doing this less often / when necessary. R. 
  24. $this->update_user_meta(); 
  25.  
  26. $has_exclude_filter = has_filter( 'wpseo_sitemap_exclude_author' ); 
  27.  
  28. $query_arguments = array(); 
  29.  
  30. if ( ! $has_exclude_filter ) { // We only need full users if legacy filter(s) hooked to exclusion logic. R. 
  31. $query_arguments['fields'] = 'ID'; 
  32.  
  33. $users = $this->get_users( $query_arguments ); 
  34.  
  35. if ( $has_exclude_filter ) { 
  36. $users = $this->exclude_users( $users ); 
  37. $users = wp_list_pluck( $users, 'ID' ); 
  38.  
  39. if ( empty( $users ) ) { 
  40. return array(); 
  41.  
  42. $index = array(); 
  43. $page = 1; 
  44. $user_pages = array_chunk( $users, $max_entries ); 
  45.  
  46. if ( count( $user_pages ) === 1 ) { 
  47. $page = ''; 
  48.  
  49. foreach ( $user_pages as $users_page ) { 
  50.  
  51. $user_id = array_shift( $users_page ); // Time descending, first user on page is most recently updated. 
  52. $user = get_user_by( 'id', $user_id ); 
  53. $index[] = array( 
  54. 'loc' => WPSEO_Sitemaps_Router::get_base_url( 'author-sitemap' . $page . '.xml' ),  
  55. 'lastmod' => '@' . $user->_yoast_wpseo_profile_updated, // @ for explicit timestamp format 
  56. ); 
  57.  
  58. $page++; 
  59.  
  60. return $index; 
  61.  
  62. /** 
  63. * Retrieve users, taking account of all necessary exclusions. 
  64. * @param array $arguments Arguments to add. 
  65. * @return array 
  66. */ 
  67. protected function get_users( $arguments = array() ) { 
  68.  
  69. global $wpdb; 
  70.  
  71. $options = WPSEO_Options::get_all(); 
  72.  
  73. $defaults = array( 
  74. // TODO re-enable after plugin requirements raised to WP 4.6 with the fix. 
  75. // 'who' => 'authors', Breaks meta keys, see https://core.trac.wordpress.org/ticket/36724#ticket R. 
  76. 'meta_key' => '_yoast_wpseo_profile_updated',  
  77. 'orderby' => 'meta_value_num',  
  78. 'order' => 'DESC',  
  79. 'meta_query' => array( 
  80. 'relation' => 'AND',  
  81. array( 
  82. 'key' => $wpdb->get_blog_prefix() . 'user_level',  
  83. 'value' => '0',  
  84. 'compare' => '!=',  
  85. ),  
  86. array( 
  87. 'relation' => 'OR',  
  88. array( 
  89. 'key' => 'wpseo_excludeauthorsitemap',  
  90. 'value' => 'on',  
  91. 'compare' => '!=',  
  92. ),  
  93. array( 
  94. 'key' => 'wpseo_excludeauthorsitemap',  
  95. 'compare' => 'NOT EXISTS',  
  96. ),  
  97. ),  
  98. ),  
  99. ); 
  100.  
  101. if ( $options['disable_author_noposts'] === true ) { 
  102. // $defaults['who'] = ''; // Otherwise it cancels out next argument. 
  103. $defaults['has_published_posts'] = true; 
  104.  
  105. $excluded_roles = $this->get_excluded_roles(); 
  106.  
  107. if ( ! empty( $excluded_roles ) ) { 
  108. // $defaults['who'] = ''; // Otherwise it cancels out next argument. 
  109. $defaults['role__not_in'] = $excluded_roles; 
  110.  
  111. return get_users( array_merge( $defaults, $arguments ) ); 
  112.  
  113. /** 
  114. * Retrieve array of roles, excluded in settings. 
  115. * @return array 
  116. */ 
  117. protected function get_excluded_roles() { 
  118.  
  119. static $excluded_roles; 
  120.  
  121. if ( isset( $excluded_roles ) ) { 
  122. return $excluded_roles; 
  123.  
  124. $options = WPSEO_Options::get_all(); 
  125. $roles = WPSEO_Utils::get_roles(); 
  126.  
  127. foreach ( $roles as $role_slug => $role_name ) { 
  128.  
  129. if ( ! empty( $options[ "user_role-{$role_slug}-not_in_sitemap" ] ) ) { 
  130. $excluded_roles[] = $role_name; 
  131.  
  132. if ( ! empty( $excluded_roles ) ) { // Otherwise it's handled by who=>authors query. 
  133. $excluded_roles[] = 'Subscriber'; 
  134.  
  135. return $excluded_roles; 
  136.  
  137. /** 
  138. * Get set of sitemap link data. 
  139. * @param string $type Sitemap type. 
  140. * @param int $max_entries Entries per sitemap. 
  141. * @param int $current_page Current page of the sitemap. 
  142. * @return array 
  143. */ 
  144. public function get_sitemap_links( $type, $max_entries, $current_page ) { 
  145.  
  146. $options = WPSEO_Options::get_all(); 
  147.  
  148. $links = array(); 
  149.  
  150. if ( $options['disable-author'] === true || $options['disable_author_sitemap'] === true ) { 
  151. return $links; 
  152.  
  153. $users = $this->get_users( array( 
  154. 'offset' => ( $current_page - 1 ) * $max_entries,  
  155. 'number' => $max_entries,  
  156. ) ); 
  157.  
  158. $users = $this->exclude_users( $users ); 
  159.  
  160. if ( empty( $users ) ) { 
  161. $users = array(); 
  162.  
  163. $time = time(); 
  164.  
  165. foreach ( $users as $user ) { 
  166.  
  167. $author_link = get_author_posts_url( $user->ID ); 
  168.  
  169. if ( empty( $author_link ) ) { 
  170. continue; 
  171.  
  172. $mod = $time; 
  173.  
  174. if ( isset( $user->_yoast_wpseo_profile_updated ) ) { 
  175. $mod = $user->_yoast_wpseo_profile_updated; 
  176.  
  177. $url = array( 
  178. 'loc' => $author_link,  
  179. 'mod' => date( DATE_W3C, $mod ),  
  180.  
  181. // Deprecated, kept for backwards data compat. R. 
  182. 'chf' => 'daily',  
  183. 'pri' => 1,  
  184. ); 
  185.  
  186. /** This filter is documented at inc/sitemaps/class-post-type-sitemap-provider.php */ 
  187. $url = apply_filters( 'wpseo_sitemap_entry', $url, 'user', $user ); 
  188.  
  189. if ( ! empty( $url ) ) { 
  190. $links[] = $url; 
  191.  
  192. return $links; 
  193.  
  194. /** 
  195. * Update any users that don't have last profile update timestamp. 
  196. * @return int Count of users updated. 
  197. */ 
  198. protected function update_user_meta() { 
  199.  
  200. $users = get_users( array( 
  201. 'who' => 'authors',  
  202. 'meta_query' => array( 
  203. array( 
  204. 'key' => '_yoast_wpseo_profile_updated',  
  205. 'compare' => 'NOT EXISTS',  
  206. ),  
  207. ),  
  208. ) ); 
  209.  
  210. $time = time(); 
  211.  
  212. foreach ( $users as $user ) { 
  213. update_user_meta( $user->ID, '_yoast_wpseo_profile_updated', $time ); 
  214.  
  215. return count( $users ); 
  216.  
  217. /** 
  218. * Wrap legacy filter to deduplicate calls. 
  219. * @param array $users Array of user objects to filter. 
  220. * @return array 
  221. */ 
  222. protected function exclude_users( $users ) { 
  223.  
  224. /** 
  225. * Filter the authors, included in XML sitemap. 
  226. * @param array $users Array of user objects to filter. 
  227. */ 
  228. return apply_filters( 'wpseo_sitemap_exclude_author', $users ); 
  229.  
  230. /** 
  231. * Sorts an array of WP_User by the _yoast_wpseo_profile_updated meta field. 
  232. * @since 1.6 
  233. * @deprecated 3.3 User meta sort can now be done by queries. 
  234. * @param WP_User $first The first WP user. 
  235. * @param WP_User $second The second WP user. 
  236. * @return int 0 if equal, 1 if $a is larger else or -1; 
  237. */ 
  238. public function user_map_sorter( $first, $second ) { 
  239. _deprecated_function( __METHOD__, 'WPSEO 3.3', __( 'Use queries instead', 'wordpress-seo' ) ); 
  240.  
  241. if ( ! isset( $first->_yoast_wpseo_profile_updated ) ) { 
  242. $first->_yoast_wpseo_profile_updated = time(); 
  243.  
  244. if ( ! isset( $second->_yoast_wpseo_profile_updated ) ) { 
  245. $second->_yoast_wpseo_profile_updated = time(); 
  246.  
  247. if ( $first->_yoast_wpseo_profile_updated === $second->_yoast_wpseo_profile_updated ) { 
  248. return 0; 
  249.  
  250. return ( ( $first->_yoast_wpseo_profile_updated > $second->_yoast_wpseo_profile_updated ) ? 1 : -1 );