PageRenderTime 35ms CodeModel.GetById 17ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/admin/links/class-link-query.php

https://bitbucket.org/carloskikea/helpet
PHP | 160 lines | 80 code | 25 blank | 55 comment | 4 complexity | f4de9c28b5da91634247e978b6d5c412 MD5 | raw file
  1<?php
  2/**
  3 * WPSEO plugin file.
  4 *
  5 * @package WPSEO\Admin\Links
  6 */
  7
  8/**
  9 * Database helper class.
 10 */
 11class WPSEO_Link_Query {
 12
 13	/**
 14	 * Determine if there are any unprocessed public posts.
 15	 *
 16	 * @param array $post_types List of post types to check with.
 17	 *
 18	 * @return bool True if unprocessed posts are found, false if none are found.
 19	 */
 20	public static function has_unprocessed_posts( array $post_types ) {
 21		global $wpdb;
 22
 23		if ( empty( $post_types ) ) {
 24			return false;
 25		}
 26
 27		$post_types  = self::format_post_types( $post_types );
 28		$count_table = self::get_count_table_name();
 29
 30		// Get any object which has not got the processed meta key.
 31		$query = '
 32			SELECT ID
 33			  FROM ' . $wpdb->posts . ' AS posts
 34		 LEFT JOIN ' . $count_table . ' AS yoast_meta
 35				ON yoast_meta.object_id = posts.ID
 36			 WHERE posts.post_status = "publish"
 37			   AND posts.post_type IN ( ' . $post_types . ' )
 38			   AND yoast_meta.internal_link_count IS NULL
 39			 LIMIT 1';
 40
 41		// If anything is found, we have unprocessed posts.
 42		$results = $wpdb->get_var( $query );
 43
 44		return ! empty( $results );
 45	}
 46
 47	/**
 48	 * Filter out posts that have not been processed yet.
 49	 *
 50	 * @param array $post_ids Post IDs to filter.
 51	 *
 52	 * @return array
 53	 */
 54	public static function filter_unprocessed_posts( array $post_ids ) {
 55		global $wpdb;
 56
 57		$post_ids = array_filter( $post_ids );
 58		if ( empty( $post_ids ) || array() === $post_ids ) {
 59			return $post_ids;
 60		}
 61
 62		$count_table = self::get_count_table_name();
 63
 64		$query = '
 65			SELECT object_id
 66			  FROM ' . $count_table . '
 67			 WHERE object_id IN ( ' . implode( ',', $post_ids ) . ' )
 68			';
 69
 70		$results = $wpdb->get_results( $query, ARRAY_A );
 71
 72		return array_map( 'intval', wp_list_pluck( $results, 'object_id' ) );
 73	}
 74
 75	/**
 76	 * Returns a limited set of unindexed posts.
 77	 *
 78	 * @param array $post_types The post type.
 79	 * @param int   $limit      The limit for the resultset.
 80	 *
 81	 * @return array|null|object The set of unindexed posts.
 82	 */
 83	public static function get_unprocessed_posts( array $post_types, $limit = 5 ) {
 84		global $wpdb;
 85
 86		$count_table = self::get_count_table_name();
 87		$post_types  = self::format_post_types( $post_types );
 88
 89		// @codingStandardsIgnoreStart
 90		$query = 'SELECT posts.ID, posts.post_content
 91				  FROM ' . $wpdb->posts . ' AS posts
 92			 LEFT JOIN ' . $count_table . ' AS yoast_meta
 93			 		ON yoast_meta.object_id = posts.ID
 94				 WHERE posts.post_status = "publish"
 95				   AND posts.post_type IN ( ' . $post_types . ' )
 96				   AND yoast_meta.internal_link_count IS NULL
 97				 LIMIT %d
 98		';
 99		// @codingStandardsIgnoreEnd
100
101		return $wpdb->get_results(
102			$wpdb->prepare( $query, $limit )
103		);
104	}
105
106	/**
107	 * Returns the total amount of unindexed posts for given post type.
108	 *
109	 * @param array $post_types The post types.
110	 *
111	 * @return int The total of unindexed posts.
112	 */
113	public static function get_unprocessed_count( array $post_types ) {
114		global $wpdb;
115
116		if ( empty( $post_types ) ) {
117			return 0;
118		}
119
120		$count_table = self::get_count_table_name();
121		$post_types  = self::format_post_types( $post_types );
122
123		// @codingStandardsIgnoreStart
124		$query = '
125			SELECT COUNT( posts.ID )
126			  FROM ' . $wpdb->posts . ' AS posts
127		 LEFT JOIN ' . $count_table . ' AS yoast_meta
128				ON yoast_meta.object_id = posts.ID
129			 WHERE posts.post_status = "publish"
130			   AND posts.post_type IN ( ' . $post_types . ' )
131			   AND yoast_meta.internal_link_count IS NULL';
132		// @codingStandardsIgnoreEnd
133
134		return (int) $wpdb->get_var( $query );
135	}
136
137	/**
138	 * Returns the table name where the counts are stored.
139	 *
140	 * @return string
141	 */
142	protected static function get_count_table_name() {
143		$storage = new WPSEO_Meta_Storage();
144		return $storage->get_table_name();
145	}
146
147	/**
148	 * Formats an array with post types as an SQL string.
149	 *
150	 * @param array $post_types The post types to format.
151	 *
152	 * @return string Post types formatted for use in SQL statement.
153	 */
154	protected static function format_post_types( array $post_types ) {
155		$sanitized_post_types = array_map( 'esc_sql', $post_types );
156		$post_types           = sprintf( '"%s"', implode( '", "', $sanitized_post_types ) );
157
158		return $post_types;
159	}
160}