PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 19.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v19.0
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 / builders / indexable-term-builder.php

indexable-term-builder.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 19.0, at src/builders/indexable-term-builder.php

272 lines 7.7 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\Builders;
4
5 use wpdb;
6 use Yoast\WP\SEO\Exceptions\Indexable\Invalid_Term_Exception;
7 use Yoast\WP\SEO\Exceptions\Indexable\Term_Not_Found_Exception;
8 use Yoast\WP\SEO\Helpers\Post_Helper;
9 use Yoast\WP\SEO\Helpers\Taxonomy_Helper;
10 use Yoast\WP\SEO\Models\Indexable;
11 use Yoast\WP\SEO\Values\Indexables\Indexable_Builder_Versions;
12
13 /**
14 * Term Builder for the indexables.
15 *
16 * Formats the term meta to indexable format.
17 */
18 class Indexable_Term_Builder {
19
20 use Indexable_Social_Image_Trait;
21
22 /**
23 * Holds the taxonomy helper instance.
24 *
25 * @var Taxonomy_Helper
26 */
27 protected $taxonomy_helper;
28
29 /**
30 * The latest version of the Indexable_Term_Builder.
31 *
32 * @var int
33 */
34 protected $version;
35
36 /**
37 * Holds the taxonomy helper instance.
38 *
39 * @var Post_Helper
40 */
41 protected $post_helper;
42
43 /**
44 * The WPDB instance.
45 *
46 * @var wpdb
47 */
48 protected $wpdb;
49
50 /**
51 * Indexable_Term_Builder constructor.
52 *
53 * @param Taxonomy_Helper $taxonomy_helper The taxonomy helper.
54 * @param Indexable_Builder_Versions $versions The latest version of each Indexable Builder.
55 * @param Post_Helper $post_helper The post helper.
56 * @param wpdb $wpdb The WPDB instance.
57 */
58 public function __construct(
59 Taxonomy_Helper $taxonomy_helper,
60 Indexable_Builder_Versions $versions,
61 Post_Helper $post_helper,
62 wpdb $wpdb
63 ) {
64 $this->taxonomy_helper = $taxonomy_helper;
65 $this->version = $versions->get_latest_version_for_type( 'term' );
66 $this->post_helper = $post_helper;
67 $this->wpdb = $wpdb;
68 }
69
70 /**
71 * Formats the data.
72 *
73 * @param int $term_id ID of the term to save data for.
74 * @param Indexable $indexable The indexable to format.
75 *
76 * @return bool|Indexable The extended indexable. False when unable to build.
77 *
78 * @throws Invalid_Term_Exception When the term is invalid.
79 * @throws Term_Not_Found_Exception When the term is not found.
80 */
81 public function build( $term_id, $indexable ) {
82 $term = \get_term( $term_id );
83
84 if ( $term === null ) {
85 throw new Term_Not_Found_Exception();
86 }
87
88 if ( \is_wp_error( $term ) ) {
89 throw new Invalid_Term_Exception( $term->get_error_message() );
90 }
91
92 $term_link = \get_term_link( $term, $term->taxonomy );
93
94 if ( \is_wp_error( $term_link ) ) {
95 throw new Invalid_Term_Exception( $term_link->get_error_message() );
96 }
97
98 $term_meta = $this->taxonomy_helper->get_term_meta( $term );
99
100 $indexable->object_id = $term_id;
101 $indexable->object_type = 'term';
102 $indexable->object_sub_type = $term->taxonomy;
103 $indexable->permalink = $term_link;
104 $indexable->blog_id = \get_current_blog_id();
105
106 $indexable->primary_focus_keyword_score = $this->get_keyword_score(
107 $this->get_meta_value( 'wpseo_focuskw', $term_meta ),
108 $this->get_meta_value( 'wpseo_linkdex', $term_meta )
109 );
110
111 $indexable->is_robots_noindex = $this->get_noindex_value( $this->get_meta_value( 'wpseo_noindex', $term_meta ) );
112 $indexable->is_public = ( $indexable->is_robots_noindex === null ) ? null : ! $indexable->is_robots_noindex;
113
114 $this->reset_social_images( $indexable );
115
116 foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) {
117 $indexable->{$indexable_key} = $this->get_meta_value( $meta_key, $term_meta );
118 }
119
120 if ( empty( $indexable->breadcrumb_title ) ) {
121 $indexable->breadcrumb_title = $term->name;
122 }
123
124 $this->handle_social_images( $indexable );
125
126 $indexable->is_cornerstone = $this->get_meta_value( 'wpseo_is_cornerstone', $term_meta );
127
128 // Not implemented yet.
129 $indexable->is_robots_nofollow = null;
130 $indexable->is_robots_noarchive = null;
131 $indexable->is_robots_noimageindex = null;
132 $indexable->is_robots_nosnippet = null;
133
134 $timestamps = $this->get_object_timestamps( $term_id, $term->taxonomy );
135 $indexable->object_published_at = $timestamps->published_at;
136 $indexable->object_last_modified = $timestamps->last_modified;
137
138 $indexable->version = $this->version;
139
140 return $indexable;
141 }
142
143 /**
144 * Converts the meta noindex value to the indexable value.
145 *
146 * @param string $meta_value Term meta to base the value on.
147 *
148 * @return bool|null
149 */
150 protected function get_noindex_value( $meta_value ) {
151 if ( $meta_value === 'noindex' ) {
152 return true;
153 }
154
155 if ( $meta_value === 'index' ) {
156 return false;
157 }
158
159 return null;
160 }
161
162 /**
163 * Determines the focus keyword score.
164 *
165 * @param string $keyword The focus keyword that is set.
166 * @param int $score The score saved on the meta data.
167 *
168 * @return int|null Score to use.
169 */
170 protected function get_keyword_score( $keyword, $score ) {
171 if ( empty( $keyword ) ) {
172 return null;
173 }
174
175 return $score;
176 }
177
178 /**
179 * Retrieves the lookup table.
180 *
181 * @return array Lookup table for the indexable fields.
182 */
183 protected function get_indexable_lookup() {
184 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',
199 ];
200 }
201
202 /**
203 * Retrieves a meta value from the given meta data.
204 *
205 * @param string $meta_key The key to extract.
206 * @param array $term_meta The meta data.
207 *
208 * @return string|null The meta value.
209 */
210 protected function get_meta_value( $meta_key, $term_meta ) {
211 if ( ! $term_meta || ! \array_key_exists( $meta_key, $term_meta ) ) {
212 return null;
213 }
214
215 $value = $term_meta[ $meta_key ];
216 if ( \is_string( $value ) && $value === '' ) {
217 return null;
218 }
219
220 return $value;
221 }
222
223 /**
224 * Finds an alternative image for the social image.
225 *
226 * @param Indexable $indexable The indexable.
227 *
228 * @return array|bool False when not found, array with data when found.
229 */
230 protected function find_alternative_image( Indexable $indexable ) {
231 $content_image = $this->image->get_term_content_image( $indexable->object_id );
232 if ( $content_image ) {
233 return [
234 'image' => $content_image,
235 'source' => 'first-content-image',
236 ];
237 }
238
239 return false;
240 }
241
242 /**
243 * Returns the timestamps for a given term.
244 *
245 * @param int $term_id The term ID.
246 * @param string $taxonomy The taxonomy.
247 *
248 * @return object An object with last_modified and published_at timestamps.
249 */
250 protected function get_object_timestamps( $term_id, $taxonomy ) {
251 $post_statuses = $this->post_helper->get_public_post_statuses();
252
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 ";
265
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 ) );
270 }
271 }
272