/inc/sitemaps/class-taxonomy-sitemap-provider.php

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