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