PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.2
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / actions / indexing / term-link-indexing-action.php

term-link-indexing-action.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.2, at src/actions/indexing/term-link-indexing-action.php

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