PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/server/wordpress/wp-content/plugins/wordpress-seo/inc/sitemaps/class-taxonomy-sitemap-provider.php

https://gitlab.com/suporte.spturis/carnaval2015.spturis.com.br
PHP | 322 lines | 166 code | 63 blank | 93 comment | 27 complexity | 9ddca41f8509f016631ef5d0754e41e3 MD5 | raw file
  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 $include 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 bool
  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 bool $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 bool $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. * @return array
  143. *
  144. * @throws OutOfBoundsException When an invalid page is requested.
  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(
  160. [
  161. 'taxonomy' => $taxonomy->name,
  162. 'hide_empty' => $hide_empty_tax,
  163. 'update_term_meta_cache' => false,
  164. 'offset' => $offset,
  165. 'number' => $steps,
  166. ]
  167. );
  168. // If there are no terms fetched for this range, we are on an invalid page.
  169. if ( empty( $terms ) ) {
  170. throw new OutOfBoundsException( 'Invalid sitemap page requested' );
  171. }
  172. $post_statuses = array_map( 'esc_sql', WPSEO_Sitemaps::get_post_statuses() );
  173. // Grab last modified date.
  174. $sql = "
  175. SELECT MAX(p.post_modified_gmt) AS lastmod
  176. FROM $wpdb->posts AS p
  177. INNER JOIN $wpdb->term_relationships AS term_rel
  178. ON term_rel.object_id = p.ID
  179. INNER JOIN $wpdb->term_taxonomy AS term_tax
  180. ON term_tax.term_taxonomy_id = term_rel.term_taxonomy_id
  181. AND term_tax.taxonomy = %s
  182. AND term_tax.term_id = %d
  183. WHERE p.post_status IN ('" . implode( "','", $post_statuses ) . "')
  184. AND p.post_password = ''
  185. ";
  186. /**
  187. * Filter: 'wpseo_exclude_from_sitemap_by_term_ids' - Allow excluding terms by ID.
  188. *
  189. * @api array $terms_to_exclude The terms to exclude.
  190. */
  191. $terms_to_exclude = apply_filters( 'wpseo_exclude_from_sitemap_by_term_ids', [] );
  192. foreach ( $terms as $term ) {
  193. if ( in_array( $term->term_id, $terms_to_exclude, true ) ) {
  194. continue;
  195. }
  196. $url = [];
  197. $tax_noindex = WPSEO_Taxonomy_Meta::get_term_meta( $term, $term->taxonomy, 'noindex' );
  198. if ( $tax_noindex === 'noindex' ) {
  199. continue;
  200. }
  201. $url['loc'] = WPSEO_Taxonomy_Meta::get_term_meta( $term, $term->taxonomy, 'canonical' );
  202. if ( ! is_string( $url['loc'] ) || $url['loc'] === '' ) {
  203. $url['loc'] = get_term_link( $term, $term->taxonomy );
  204. }
  205. $url['mod'] = $wpdb->get_var( $wpdb->prepare( $sql, $term->taxonomy, $term->term_id ) );
  206. if ( $this->include_images ) {
  207. $url['images'] = $this->get_image_parser()->get_term_images( $term );
  208. }
  209. // Deprecated, kept for backwards data compat. R.
  210. $url['chf'] = 'daily';
  211. $url['pri'] = 1;
  212. /** This filter is documented at inc/sitemaps/class-post-type-sitemap-provider.php */
  213. $url = apply_filters( 'wpseo_sitemap_entry', $url, 'term', $term );
  214. if ( ! empty( $url ) ) {
  215. $links[] = $url;
  216. }
  217. }
  218. return $links;
  219. }
  220. /**
  221. * Check if taxonomy by name is valid to appear in sitemaps.
  222. *
  223. * @param string $taxonomy_name Taxonomy name to check.
  224. *
  225. * @return bool
  226. */
  227. public function is_valid_taxonomy( $taxonomy_name ) {
  228. if ( WPSEO_Options::get( "noindex-tax-{$taxonomy_name}" ) === true ) {
  229. return false;
  230. }
  231. if ( in_array( $taxonomy_name, [ 'link_category', 'nav_menu' ], true ) ) {
  232. return false;
  233. }
  234. if ( $taxonomy_name === 'post_format' && WPSEO_Options::get( 'disable-post_format', false ) ) {
  235. return false;
  236. }
  237. /**
  238. * Filter to exclude the taxonomy from the XML sitemap.
  239. *
  240. * @param bool $exclude Defaults to false.
  241. * @param string $taxonomy_name Name of the taxonomy to exclude..
  242. */
  243. if ( apply_filters( 'wpseo_sitemap_exclude_taxonomy', false, $taxonomy_name ) ) {
  244. return false;
  245. }
  246. return true;
  247. }
  248. /**
  249. * Get the Image Parser.
  250. *
  251. * @return WPSEO_Sitemap_Image_Parser
  252. */
  253. protected function get_image_parser() {
  254. if ( ! isset( self::$image_parser ) ) {
  255. self::$image_parser = new WPSEO_Sitemap_Image_Parser();
  256. }
  257. return self::$image_parser;
  258. }
  259. }