class-taxonomy-sitemap-provider.php 8.6 KB

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