PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
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
← All changes | src/builders/indexable-term-builder.php +64 -44 18.428.5 View file →
@@ -1,10 +1,10 @@
1 1 <?php
2 2
3 3 namespace Yoast\WP\SEO\Builders;
4 4
5 -use wpdb;
6 5 use Yoast\WP\SEO\Exceptions\Indexable\Invalid_Term_Exception;
6 +use Yoast\WP\SEO\Exceptions\Indexable\Term_Not_Built_Exception;
7 7 use Yoast\WP\SEO\Exceptions\Indexable\Term_Not_Found_Exception;
8 8 use Yoast\WP\SEO\Helpers\Post_Helper;
9 9 use Yoast\WP\SEO\Helpers\Taxonomy_Helper;
10 10 use Yoast\WP\SEO\Models\Indexable;
@@ -40,32 +40,22 @@
40 40 */
41 41 protected $post_helper;
42 42
43 43 /**
44 - * The WPDB instance.
45 - *
46 - * @var wpdb
47 - */
48 - protected $wpdb;
49 -
50 - /**
51 44 * Indexable_Term_Builder constructor.
52 45 *
53 46 * @param Taxonomy_Helper $taxonomy_helper The taxonomy helper.
54 47 * @param Indexable_Builder_Versions $versions The latest version of each Indexable Builder.
55 48 * @param Post_Helper $post_helper The post helper.
56 - * @param wpdb $wpdb The WPDB instance.
57 49 */
58 50 public function __construct(
59 51 Taxonomy_Helper $taxonomy_helper,
60 52 Indexable_Builder_Versions $versions,
61 - Post_Helper $post_helper,
62 - wpdb $wpdb
53 + Post_Helper $post_helper
63 54 ) {
64 55 $this->taxonomy_helper = $taxonomy_helper;
65 56 $this->version = $versions->get_latest_version_for_type( 'term' );
66 57 $this->post_helper = $post_helper;
67 - $this->wpdb = $wpdb;
68 58 }
69 59
70 60 /**
71 61 * Formats the data.
@@ -74,9 +64,10 @@
74 64 * @param Indexable $indexable The indexable to format.
75 65 *
76 66 * @return bool|Indexable The extended indexable. False when unable to build.
77 67 *
78 - * @throws Invalid_Term_Exception When the term is invalid.
68 + * @throws Invalid_Term_Exception When the term is invalid.
69 + * @throws Term_Not_Built_Exception When the term is not viewable.
79 70 * @throws Term_Not_Found_Exception When the term is not found.
80 71 */
81 72 public function build( $term_id, $indexable ) {
82 73 $term = \get_term( $term_id );
@@ -88,8 +79,13 @@
88 79 if ( \is_wp_error( $term ) ) {
89 80 throw new Invalid_Term_Exception( $term->get_error_message() );
90 81 }
91 82
83 + $indexable_taxonomies = $this->taxonomy_helper->get_indexable_taxonomies();
84 + if ( ! \in_array( $term->taxonomy, $indexable_taxonomies, true ) ) {
85 + throw Term_Not_Built_Exception::because_not_indexable( $term_id );
86 + }
87 +
92 88 $term_link = \get_term_link( $term, $term->taxonomy );
93 89
94 90 if ( \is_wp_error( $term_link ) ) {
95 91 throw new Invalid_Term_Exception( $term_link->get_error_message() );
@@ -104,9 +100,9 @@
104 100 $indexable->blog_id = \get_current_blog_id();
105 101
106 102 $indexable->primary_focus_keyword_score = $this->get_keyword_score(
107 103 $this->get_meta_value( 'wpseo_focuskw', $term_meta ),
108 - $this->get_meta_value( 'wpseo_linkdex', $term_meta )
104 + $this->get_meta_value( 'wpseo_linkdex', $term_meta ),
109 105 );
110 106
111 107 $indexable->is_robots_noindex = $this->get_noindex_value( $this->get_meta_value( 'wpseo_noindex', $term_meta ) );
112 108 $indexable->is_public = ( $indexable->is_robots_noindex === null ) ? null : ! $indexable->is_robots_noindex;
@@ -181,22 +177,23 @@
181 177 * @return array Lookup table for the indexable fields.
182 178 */
183 179 protected function get_indexable_lookup() {
184 180 return [
185 - 'wpseo_canonical' => 'canonical',
186 - 'wpseo_focuskw' => 'primary_focus_keyword',
187 - 'wpseo_title' => 'title',
188 - 'wpseo_desc' => 'description',
189 - 'wpseo_content_score' => 'readability_score',
190 - 'wpseo_bctitle' => 'breadcrumb_title',
191 - 'wpseo_opengraph-title' => 'open_graph_title',
192 - 'wpseo_opengraph-description' => 'open_graph_description',
193 - 'wpseo_opengraph-image' => 'open_graph_image',
194 - 'wpseo_opengraph-image-id' => 'open_graph_image_id',
195 - 'wpseo_twitter-title' => 'twitter_title',
196 - 'wpseo_twitter-description' => 'twitter_description',
197 - 'wpseo_twitter-image' => 'twitter_image',
198 - 'wpseo_twitter-image-id' => 'twitter_image_id',
181 + 'wpseo_canonical' => 'canonical',
182 + 'wpseo_focuskw' => 'primary_focus_keyword',
183 + 'wpseo_title' => 'title',
184 + 'wpseo_desc' => 'description',
185 + 'wpseo_content_score' => 'readability_score',
186 + 'wpseo_inclusive_language_score' => 'inclusive_language_score',
187 + 'wpseo_bctitle' => 'breadcrumb_title',
188 + 'wpseo_opengraph-title' => 'open_graph_title',
189 + 'wpseo_opengraph-description' => 'open_graph_description',
190 + 'wpseo_opengraph-image' => 'open_graph_image',
191 + 'wpseo_opengraph-image-id' => 'open_graph_image_id',
192 + 'wpseo_twitter-title' => 'twitter_title',
193 + 'wpseo_twitter-description' => 'twitter_description',
194 + 'wpseo_twitter-image' => 'twitter_image',
195 + 'wpseo_twitter-image-id' => 'twitter_image_id',
199 196 ];
200 197 }
201 198
202 199 /**
@@ -247,25 +244,48 @@
247 244 *
248 245 * @return object An object with last_modified and published_at timestamps.
249 246 */
250 247 protected function get_object_timestamps( $term_id, $taxonomy ) {
248 + global $wpdb;
251 249 $post_statuses = $this->post_helper->get_public_post_statuses();
252 250
253 - $sql = "
254 - SELECT MAX(p.post_modified_gmt) AS last_modified, MIN(p.post_date_gmt) AS published_at
255 - FROM {$this->wpdb->posts} AS p
256 - INNER JOIN {$this->wpdb->term_relationships} AS term_rel
257 - ON term_rel.object_id = p.ID
258 - INNER JOIN {$this->wpdb->term_taxonomy} AS term_tax
259 - ON term_tax.term_taxonomy_id = term_rel.term_taxonomy_id
260 - AND term_tax.taxonomy = %s
261 - AND term_tax.term_id = %d
262 - WHERE p.post_status IN (" . \implode( ', ', \array_fill( 0, \count( $post_statuses ), '%s' ) ) . ")
263 - AND p.post_password = ''
264 - ";
251 + $replacements = [];
252 + $replacements[] = 'post_modified_gmt';
253 + $replacements[] = 'post_date_gmt';
254 + $replacements[] = $wpdb->posts;
255 + $replacements[] = $wpdb->term_relationships;
256 + $replacements[] = 'object_id';
257 + $replacements[] = 'ID';
258 + $replacements[] = $wpdb->term_taxonomy;
259 + $replacements[] = 'term_taxonomy_id';
260 + $replacements[] = 'term_taxonomy_id';
261 + $replacements[] = 'taxonomy';
262 + $replacements[] = $taxonomy;
263 + $replacements[] = 'term_id';
264 + $replacements[] = $term_id;
265 + $replacements[] = 'post_status';
266 + $replacements = \array_merge( $replacements, $post_statuses );
267 + $replacements[] = 'post_password';
265 268
266 - $replacements = \array_merge( [ $taxonomy, $term_id ], $post_statuses );
267 -
268 - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- We are using wpdb prepare.
269 - return $this->wpdb->get_row( $this->wpdb->prepare( $sql, $replacements ) );
269 + //phpcs:disable WordPress.DB.PreparedSQLPlaceholders -- %i placeholder is still not recognized.
270 + //phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery -- Reason: Most performant way.
271 + //phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: No relevant caches.
272 + return $wpdb->get_row(
273 + $wpdb->prepare(
274 + '
275 + SELECT MAX(p.%i) AS last_modified, MIN(p.%i) AS published_at
276 + FROM %i AS p
277 + INNER JOIN %i AS term_rel
278 + ON term_rel.%i = p.%i
279 + INNER JOIN %i AS term_tax
280 + ON term_tax.%i = term_rel.%i
281 + AND term_tax.%i = %s
282 + AND term_tax.%i = %d
283 + WHERE p.%i IN (' . \implode( ', ', \array_fill( 0, \count( $post_statuses ), '%s' ) ) . ")
284 + AND p.%i = ''
285 + ",
286 + $replacements,
287 + ),
288 + );
289 + //phpcs:enable
270 290 }
271 291 }