WPSEO_Taxonomy_Sitemap_Provider

Sitemap provider for author archives.

Defined (1)

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

/inc/sitemaps/class-taxonomy-sitemap-provider.php  
  1. class WPSEO_Taxonomy_Sitemap_Provider implements WPSEO_Sitemap_Provider { 
  2.  
  3. /** @var array $options All of plugin options. */ 
  4. protected static $options; 
  5.  
  6. /** @var WPSEO_Sitemap_Image_Parser $image_parser Holds image parser instance. */ 
  7. protected static $image_parser; 
  8.  
  9. /** 
  10. * Set up object properties for data reuse. 
  11. */ 
  12. public function __construct() { 
  13.  
  14. /** 
  15. * Get Options 
  16. * @return array 
  17. */ 
  18. protected function get_options() { 
  19. if ( ! isset( self::$options ) ) { 
  20. self::$options = WPSEO_Options::get_all(); 
  21.  
  22. return self::$options; 
  23.  
  24. /** 
  25. * Check if provider supports given item type. 
  26. * @param string $type Type string to check for. 
  27. * @return boolean 
  28. */ 
  29. public function handles_type( $type ) { 
  30.  
  31. return taxonomy_exists( $type ); 
  32.  
  33. /** 
  34. * @param int $max_entries Entries per sitemap. 
  35. * @return array 
  36. */ 
  37. public function get_index_links( $max_entries ) { 
  38.  
  39. $taxonomies = get_taxonomies( array( 'public' => true ), 'objects' ); 
  40.  
  41. if ( empty( $taxonomies ) ) { 
  42. return array(); 
  43.  
  44. $taxonomy_names = array_filter( array_keys( $taxonomies ), array( $this, 'is_valid_taxonomy' ) ); 
  45. $taxonomies = array_intersect_key( $taxonomies, array_flip( $taxonomy_names ) ); 
  46.  
  47. // Retrieve all the taxonomies and their terms so we can do a proper count on them. 
  48. /** 
  49. * Filter the setting of excluding empty terms from the XML sitemap. 
  50. * @param boolean $exclude Defaults to true. 
  51. * @param array $taxonomy_names Array of names for the taxonomies being processed. 
  52. */ 
  53. $hide_empty = apply_filters( 'wpseo_sitemap_exclude_empty_terms', true, $taxonomy_names ); 
  54.  
  55. $all_taxonomies = array(); 
  56.  
  57. foreach ( $taxonomy_names as $taxonomy_name ) { 
  58.  
  59. $taxonomy_terms = get_terms( $taxonomy_name, array( 
  60. 'hide_empty' => $hide_empty,  
  61. 'fields' => 'ids',  
  62. ) ); 
  63.  
  64. if ( count( $taxonomy_terms ) > 0 ) { 
  65. $all_taxonomies[ $taxonomy_name ] = $taxonomy_terms; 
  66.  
  67. $index = array(); 
  68.  
  69. foreach ( $taxonomies as $tax_name => $tax ) { 
  70.  
  71. if ( ! isset( $all_taxonomies[ $tax_name ] ) ) { // No eligible terms found. 
  72. continue; 
  73.  
  74. $total_count = ( isset( $all_taxonomies[ $tax_name ] ) ) ? count( $all_taxonomies[ $tax_name ] ) : 1; 
  75. $max_pages = 1; 
  76.  
  77. if ( $total_count > $max_entries ) { 
  78. $max_pages = (int) ceil( $total_count / $max_entries ); 
  79.  
  80. $last_modified_gmt = WPSEO_Sitemaps::get_last_modified_gmt( $tax->object_type ); 
  81.  
  82. for ( $page_counter = 0; $page_counter < $max_pages; $page_counter++ ) { 
  83.  
  84. $current_page = ( $max_pages > 1 ) ? ( $page_counter + 1 ) : ''; 
  85.  
  86. if ( ! is_array( $tax->object_type ) || count( $tax->object_type ) == 0 ) { 
  87. continue; 
  88.  
  89. $terms = array_splice( $all_taxonomies[ $tax_name ], 0, $max_entries ); 
  90.  
  91. if ( ! $terms ) { 
  92. continue; 
  93.  
  94. $args = array( 
  95. 'post_type' => $tax->object_type,  
  96. 'tax_query' => array( 
  97. array( 
  98. 'taxonomy' => $tax_name,  
  99. 'terms' => $terms,  
  100. ),  
  101. ),  
  102. 'orderby' => 'modified',  
  103. 'order' => 'DESC',  
  104. 'posts_per_page' => 1,  
  105. ); 
  106. $query = new WP_Query( $args ); 
  107.  
  108. if ( $query->have_posts() ) { 
  109. $date = $query->posts[0]->post_modified_gmt; 
  110. else { 
  111. $date = $last_modified_gmt; 
  112.  
  113. $index[] = array( 
  114. 'loc' => WPSEO_Sitemaps_Router::get_base_url( $tax_name . '-sitemap' . $current_page . '.xml' ),  
  115. 'lastmod' => $date,  
  116. ); 
  117.  
  118. return $index; 
  119.  
  120. /** 
  121. * Get set of sitemap link data. 
  122. * @param string $type Sitemap type. 
  123. * @param int $max_entries Entries per sitemap. 
  124. * @param int $current_page Current page of the sitemap. 
  125. * @return array 
  126. */ 
  127. public function get_sitemap_links( $type, $max_entries, $current_page ) { 
  128.  
  129. global $wpdb; 
  130.  
  131. $links = array(); 
  132. $taxonomy = get_taxonomy( $type ); 
  133.  
  134. if ( $taxonomy === false || ! $this->is_valid_taxonomy( $taxonomy->name ) || ! $taxonomy->public ) { 
  135. return $links; 
  136.  
  137. $options = $this->get_options(); 
  138.  
  139. $steps = $max_entries; 
  140. $offset = ( $current_page > 1 ) ? ( ( $current_page - 1 ) * $max_entries ) : 0; 
  141.  
  142. /** This filter is documented in inc/sitemaps/class-taxonomy-sitemap-provider.php */ 
  143. $hide_empty = apply_filters( 'wpseo_sitemap_exclude_empty_terms', true, $taxonomy ); 
  144. $terms = get_terms( $taxonomy->name, array( 'hide_empty' => $hide_empty ) ); 
  145. $terms = array_splice( $terms, $offset, $steps ); 
  146.  
  147. if ( empty( $terms ) ) { 
  148. $terms = array(); 
  149.  
  150. // Grab last modified date. 
  151. $sql = " 
  152. SELECT MAX(p.post_modified_gmt) AS lastmod 
  153. FROM $wpdb->posts AS p 
  154. INNER JOIN $wpdb->term_relationships AS term_rel 
  155. ON term_rel.object_id = p.ID 
  156. INNER JOIN $wpdb->term_taxonomy AS term_tax 
  157. ON term_tax.term_taxonomy_id = term_rel.term_taxonomy_id 
  158. AND term_tax.taxonomy = %s 
  159. AND term_tax.term_id = %d 
  160. WHERE p.post_status IN ('publish', 'inherit') 
  161. AND p.post_password = '' 
  162. "; 
  163.  
  164. foreach ( $terms as $term ) { 
  165.  
  166. $url = array(); 
  167.  
  168. $tax_noindex = WPSEO_Taxonomy_Meta::get_term_meta( $term, $term->taxonomy, 'noindex' ); 
  169. $tax_sitemap_inc = WPSEO_Taxonomy_Meta::get_term_meta( $term, $term->taxonomy, 'sitemap_include' ); 
  170.  
  171. if ( $tax_noindex === 'noindex' && $tax_sitemap_inc !== 'always' ) { 
  172. continue; 
  173.  
  174. if ( $tax_sitemap_inc === 'never' ) { 
  175. continue; 
  176.  
  177. $url['loc'] = WPSEO_Taxonomy_Meta::get_term_meta( $term, $term->taxonomy, 'canonical' ); 
  178.  
  179. if ( ! is_string( $url['loc'] ) || $url['loc'] === '' ) { 
  180.  
  181. $url['loc'] = get_term_link( $term, $term->taxonomy ); 
  182.  
  183. if ( $options['trailingslash'] === true ) { 
  184.  
  185. $url['loc'] = trailingslashit( $url['loc'] ); 
  186.  
  187. $url['mod'] = $wpdb->get_var( $wpdb->prepare( $sql, $term->taxonomy, $term->term_id ) ); 
  188. $url['images'] = $this->get_image_parser()->get_term_images( $term ); 
  189.  
  190. // Deprecated, kept for backwards data compat. R. 
  191. $url['chf'] = 'daily'; 
  192. $url['pri'] = 1; 
  193.  
  194. /** This filter is documented at inc/sitemaps/class-post-type-sitemap-provider.php */ 
  195. $url = apply_filters( 'wpseo_sitemap_entry', $url, 'term', $term ); 
  196.  
  197. if ( ! empty( $url ) ) { 
  198. $links[] = $url; 
  199.  
  200. return $links; 
  201.  
  202. /** 
  203. * Check if taxonomy by name is valid to appear in sitemaps. 
  204. * @param string $taxonomy_name Taxonomy name to check. 
  205. * @return bool 
  206. */ 
  207. public function is_valid_taxonomy( $taxonomy_name ) { 
  208.  
  209. $options = $this->get_options(); 
  210. if ( ! empty( $options[ "taxonomies-{$taxonomy_name}-not_in_sitemap" ] ) ) { 
  211. return false; 
  212.  
  213. if ( in_array( $taxonomy_name, array( 'link_category', 'nav_menu' ) ) ) { 
  214. return false; 
  215.  
  216. if ( 'post_format' === $taxonomy_name && ! empty( $options['disable-post_format'] ) ) { 
  217. return false; 
  218.  
  219. /** 
  220. * Filter to exclude the taxonomy from the XML sitemap. 
  221. * @param boolean $exclude Defaults to false. 
  222. * @param string $taxonomy_name Name of the taxonomy to exclude.. 
  223. */ 
  224. if ( apply_filters( 'wpseo_sitemap_exclude_taxonomy', false, $taxonomy_name ) ) { 
  225. return false; 
  226.  
  227. return true; 
  228.  
  229. /** 
  230. * Get the Image Parser 
  231. * @return WPSEO_Sitemap_Image_Parser 
  232. */ 
  233. protected function get_image_parser() { 
  234. if ( ! isset( self::$image_parser ) ) { 
  235. self::$image_parser = new WPSEO_Sitemap_Image_Parser(); 
  236.  
  237. return self::$image_parser;