| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Actions\Indexing; |
| 4 |
|
| 5 |
use Yoast\WP\Lib\Model; |
| 6 |
use Yoast\WP\SEO\Helpers\Taxonomy_Helper; |
| 7 |
|
| 8 |
/** |
| 9 |
* Reindexing action for term link indexables. |
| 10 |
* |
| 11 |
* @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded |
| 12 |
*/ |
| 13 |
class Term_Link_Indexing_Action extends Abstract_Link_Indexing_Action { |
| 14 |
|
| 15 |
/** |
| 16 |
* The transient name. |
| 17 |
* |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
const UNINDEXED_COUNT_TRANSIENT = 'wpseo_unindexed_term_link_count'; |
| 21 |
|
| 22 |
/** |
| 23 |
* The transient cache key for limited counts. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
const UNINDEXED_LIMITED_COUNT_TRANSIENT = self::UNINDEXED_COUNT_TRANSIENT . '_limited'; |
| 28 |
|
| 29 |
/** |
| 30 |
* The post type helper. |
| 31 |
* |
| 32 |
* @var Taxonomy_Helper |
| 33 |
*/ |
| 34 |
protected $taxonomy_helper; |
| 35 |
|
| 36 |
/** |
| 37 |
* Sets the required helper. |
| 38 |
* |
| 39 |
* @required |
| 40 |
* |
| 41 |
* @param Taxonomy_Helper $taxonomy_helper The taxonomy helper. |
| 42 |
* |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
public function set_helper( Taxonomy_Helper $taxonomy_helper ) { |
| 46 |
$this->taxonomy_helper = $taxonomy_helper; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Returns objects to be indexed. |
| 51 |
* |
| 52 |
* @return array Objects to be indexed. |
| 53 |
*/ |
| 54 |
protected function get_objects() { |
| 55 |
$query = $this->get_select_query( $this->get_limit() ); |
| 56 |
|
| 57 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Function get_select_query returns a prepared query. |
| 58 |
$terms = $this->wpdb->get_results( $query ); |
| 59 |
|
| 60 |
return \array_map( |
| 61 |
static function ( $term ) { |
| 62 |
return (object) [ |
| 63 |
'id' => (int) $term->term_id, |
| 64 |
'type' => 'term', |
| 65 |
'content' => $term->description, |
| 66 |
]; |
| 67 |
}, |
| 68 |
$terms |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Builds a query for counting the number of unindexed term links. |
| 74 |
* |
| 75 |
* @return string The prepared query string. |
| 76 |
*/ |
| 77 |
protected function get_count_query() { |
| 78 |
$public_taxonomies = $this->taxonomy_helper->get_public_taxonomies(); |
| 79 |
$placeholders = \implode( ', ', \array_fill( 0, \count( $public_taxonomies ), '%s' ) ); |
| 80 |
$indexable_table = Model::get_table_name( 'Indexable' ); |
| 81 |
|
| 82 |
// Warning: If this query is changed, makes sure to update the query in get_select_query as well. |
| 83 |
return $this->wpdb->prepare( |
| 84 |
" |
| 85 |
SELECT COUNT(T.term_id) |
| 86 |
FROM {$this->wpdb->term_taxonomy} AS T |
| 87 |
LEFT JOIN $indexable_table AS I |
| 88 |
ON T.term_id = I.object_id |
| 89 |
AND I.object_type = 'term' |
| 90 |
AND I.link_count IS NOT NULL |
| 91 |
WHERE I.object_id IS NULL |
| 92 |
AND T.taxonomy IN ($placeholders)", |
| 93 |
$public_taxonomies |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Builds a query for selecting the ID's of unindexed term links. |
| 99 |
* |
| 100 |
* @param int|false $limit The maximum number of term link IDs to return. |
| 101 |
* |
| 102 |
* @return string The prepared query string. |
| 103 |
*/ |
| 104 |
protected function get_select_query( $limit = false ) { |
| 105 |
$public_taxonomies = $this->taxonomy_helper->get_public_taxonomies(); |
| 106 |
$indexable_table = Model::get_table_name( 'Indexable' ); |
| 107 |
$replacements = $public_taxonomies; |
| 108 |
|
| 109 |
$limit_query = ''; |
| 110 |
if ( $limit ) { |
| 111 |
$limit_query = 'LIMIT %d'; |
| 112 |
$replacements[] = $limit; |
| 113 |
} |
| 114 |
|
| 115 |
// Warning: If this query is changed, makes sure to update the query in get_count_query as well. |
| 116 |
return $this->wpdb->prepare( |
| 117 |
" |
| 118 |
SELECT T.term_id, T.description |
| 119 |
FROM {$this->wpdb->term_taxonomy} AS T |
| 120 |
LEFT JOIN $indexable_table AS I |
| 121 |
ON T.term_id = I.object_id |
| 122 |
AND I.object_type = 'term' |
| 123 |
AND I.link_count IS NOT NULL |
| 124 |
WHERE I.object_id IS NULL |
| 125 |
AND T.taxonomy IN (" . \implode( ', ', \array_fill( 0, \count( $public_taxonomies ), '%s' ) ) . ") |
| 126 |
$limit_query", |
| 127 |
$replacements |
| 128 |
); |
| 129 |
} |
| 130 |
} |
| 131 |
|