PageRenderTime 60ms CodeModel.GetById 36ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 1ms

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

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