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
wordpress-seo / src / builders / indexable-post-builder.php

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

432 lines 12.3 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 WP_Post;
6 use Yoast\WP\SEO\Exceptions\Indexable\Post_Not_Built_Exception;
7 use Yoast\WP\SEO\Exceptions\Indexable\Post_Not_Found_Exception;
8 use Yoast\WP\SEO\Helpers\Meta_Helper;
9 use Yoast\WP\SEO\Helpers\Permalink_Helper;
10 use Yoast\WP\SEO\Helpers\Post_Helper;
11 use Yoast\WP\SEO\Helpers\Post_Type_Helper;
12 use Yoast\WP\SEO\Models\Indexable;
13 use Yoast\WP\SEO\Repositories\Indexable_Repository;
14 use Yoast\WP\SEO\Values\Indexables\Indexable_Builder_Versions;
15
16 /**
17 * Post Builder for the indexables.
18 *
19 * Formats the post meta to indexable format.
20 */
21 class Indexable_Post_Builder {
22
23 use Indexable_Social_Image_Trait;
24
25 /**
26 * The indexable repository.
27 *
28 * @var Indexable_Repository
29 */
30 protected $indexable_repository;
31
32 /**
33 * Holds the Post_Helper instance.
34 *
35 * @var Post_Helper
36 */
37 protected $post_helper;
38
39 /**
40 * The post type helper.
41 *
42 * @var Post_Type_Helper
43 */
44 protected $post_type_helper;
45
46 /**
47 * The permalink helper.
48 *
49 * @var Permalink_Helper
50 */
51 protected $permalink_helper;
52
53 /**
54 * Knows the latest version of the Indexable post builder type.
55 *
56 * @var int
57 */
58 protected $version;
59
60 /**
61 * The meta helper.
62 *
63 * @var Meta_Helper
64 */
65 protected $meta;
66
67 /**
68 * Indexable_Post_Builder constructor.
69 *
70 * @param Post_Helper $post_helper The post helper.
71 * @param Post_Type_Helper $post_type_helper The post type helper.
72 * @param Indexable_Builder_Versions $versions The indexable builder versions.
73 * @param Meta_Helper $meta The meta helper.
74 * @param Permalink_Helper $permalink_helper The permalink helper.
75 */
76 public function __construct(
77 Post_Helper $post_helper,
78 Post_Type_Helper $post_type_helper,
79 Indexable_Builder_Versions $versions,
80 Meta_Helper $meta,
81 Permalink_Helper $permalink_helper
82 ) {
83 $this->post_helper = $post_helper;
84 $this->post_type_helper = $post_type_helper;
85 $this->version = $versions->get_latest_version_for_type( 'post' );
86 $this->meta = $meta;
87 $this->permalink_helper = $permalink_helper;
88 }
89
90 /**
91 * Sets the indexable repository. Done to avoid circular dependencies.
92 *
93 * @required
94 *
95 * @param Indexable_Repository $indexable_repository The indexable repository.
96 *
97 * @return void
98 */
99 public function set_indexable_repository( Indexable_Repository $indexable_repository ) {
100 $this->indexable_repository = $indexable_repository;
101 }
102
103 /**
104 * Formats the data.
105 *
106 * @param int $post_id The post ID to use.
107 * @param Indexable $indexable The indexable to format.
108 *
109 * @return bool|Indexable The extended indexable. False when unable to build.
110 *
111 * @throws Post_Not_Found_Exception When the post could not be found.
112 * @throws Post_Not_Built_Exception When the post should not be indexed.
113 */
114 public function build( $post_id, $indexable ) {
115 if ( ! $this->post_helper->is_post_indexable( $post_id ) ) {
116 throw Post_Not_Built_Exception::because_not_indexable( $post_id );
117 }
118
119 $post = $this->post_helper->get_post( $post_id );
120
121 if ( $post === null ) {
122 throw new Post_Not_Found_Exception();
123 }
124
125 if ( $this->should_exclude_post( $post ) ) {
126 throw Post_Not_Built_Exception::because_post_type_excluded( $post_id );
127 }
128
129 $indexable->object_id = $post_id;
130 $indexable->object_type = 'post';
131 $indexable->object_sub_type = $post->post_type;
132 $indexable->permalink = $this->permalink_helper->get_permalink_for_post( $post->post_type, $post_id );
133
134 $indexable->primary_focus_keyword_score = $this->get_keyword_score(
135 $this->meta->get_value( 'focuskw', $post_id ),
136 (int) $this->meta->get_value( 'linkdex', $post_id ),
137 );
138
139 $indexable->readability_score = (int) $this->meta->get_value( 'content_score', $post_id );
140
141 $indexable->inclusive_language_score = (int) $this->meta->get_value( 'inclusive_language_score', $post_id );
142
143 $indexable->seo_title_score = (int) $this->meta->get_value( 'seo_title_score', $post_id );
144
145 $indexable->meta_description_score = (int) $this->meta->get_value( 'meta_description_score', $post_id );
146
147 $indexable->is_cornerstone = ( $this->meta->get_value( 'is_cornerstone', $post_id ) === '1' );
148 $indexable->is_robots_noindex = $this->get_robots_noindex(
149 (int) $this->meta->get_value( 'meta-robots-noindex', $post_id ),
150 );
151
152 // Set additional meta-robots values.
153 $indexable->is_robots_nofollow = ( $this->meta->get_value( 'meta-robots-nofollow', $post_id ) === '1' );
154 $noindex_advanced = $this->meta->get_value( 'meta-robots-adv', $post_id );
155 $meta_robots = \explode( ',', $noindex_advanced );
156
157 foreach ( $this->get_robots_options() as $meta_robots_option ) {
158 $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null;
159 }
160
161 $this->reset_social_images( $indexable );
162
163 foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) {
164 $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) );
165 }
166
167 if ( empty( $indexable->breadcrumb_title ) ) {
168 $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true );
169 }
170
171 $this->handle_social_images( $indexable );
172
173 $indexable->author_id = $post->post_author;
174 $indexable->post_parent = $post->post_parent;
175
176 $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post );
177 $indexable->post_status = $post->post_status;
178 $indexable->is_protected = $post->post_password !== '';
179 $indexable->is_public = $this->is_public( $indexable );
180 $indexable->has_public_posts = $this->has_public_posts( $indexable );
181 $indexable->blog_id = \get_current_blog_id();
182
183 $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) );
184 $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) );
185
186 $indexable->object_last_modified = $post->post_modified_gmt;
187 $indexable->object_published_at = $post->post_date_gmt;
188
189 $indexable->version = $this->version;
190
191 return $indexable;
192 }
193
194 /**
195 * Determines the value of is_public.
196 *
197 * @param Indexable $indexable The indexable.
198 *
199 * @return bool|null Whether or not the post type is public. Null if no override is set.
200 */
201 protected function is_public( $indexable ) {
202 if ( $indexable->is_protected === true ) {
203 return false;
204 }
205
206 if ( $indexable->is_robots_noindex === true ) {
207 return false;
208 }
209
210 // Attachments behave differently than the other post types, since they inherit from their parent.
211 if ( $indexable->object_sub_type === 'attachment' ) {
212 return $this->is_public_attachment( $indexable );
213 }
214
215 if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) {
216 return false;
217 }
218
219 if ( $indexable->is_robots_noindex === false ) {
220 return true;
221 }
222
223 return null;
224 }
225
226 /**
227 * Determines the value of is_public for attachments.
228 *
229 * @param Indexable $indexable The indexable.
230 *
231 * @return bool|null False when it has no parent. Null when it has a parent.
232 */
233 protected function is_public_attachment( $indexable ) {
234 // If the attachment has no parent, it should not be public.
235 if ( empty( $indexable->post_parent ) ) {
236 return false;
237 }
238
239 // If the attachment has a parent, the is_public should be NULL.
240 return null;
241 }
242
243 /**
244 * Determines the value of has_public_posts.
245 *
246 * @param Indexable $indexable The indexable.
247 *
248 * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment.
249 */
250 protected function has_public_posts( $indexable ) {
251 // Only attachments (and authors) have this value.
252 if ( $indexable->object_sub_type !== 'attachment' ) {
253 return null;
254 }
255
256 // The attachment should have a post parent.
257 if ( empty( $indexable->post_parent ) ) {
258 return false;
259 }
260
261 // The attachment should inherit the post status.
262 if ( $indexable->post_status !== 'inherit' ) {
263 return false;
264 }
265
266 // The post parent should be public.
267 $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' );
268 if ( $post_parent_indexable !== false ) {
269 return $post_parent_indexable->is_public;
270 }
271
272 return false;
273 }
274
275 /**
276 * Converts the meta robots noindex value to the indexable value.
277 *
278 * @param int $value Meta value to convert.
279 *
280 * @return bool|null True for noindex, false for index, null for default of parent/type.
281 */
282 protected function get_robots_noindex( $value ) {
283 $value = (int) $value;
284
285 switch ( $value ) {
286 case 1:
287 return true;
288 case 2:
289 return false;
290 }
291
292 return null;
293 }
294
295 /**
296 * Retrieves the robot options to search for.
297 *
298 * @return array List of robots values.
299 */
300 protected function get_robots_options() {
301 return [ 'noimageindex', 'noarchive', 'nosnippet' ];
302 }
303
304 /**
305 * Determines the focus keyword score.
306 *
307 * @param string $keyword The focus keyword that is set.
308 * @param int $score The score saved on the meta data.
309 *
310 * @return int|null Score to use.
311 */
312 protected function get_keyword_score( $keyword, $score ) {
313 if ( empty( $keyword ) ) {
314 return null;
315 }
316
317 return $score;
318 }
319
320 /**
321 * Retrieves the lookup table.
322 *
323 * @return array Lookup table for the indexable fields.
324 */
325 protected function get_indexable_lookup() {
326 return [
327 'focuskw' => 'primary_focus_keyword',
328 'canonical' => 'canonical',
329 'title' => 'title',
330 'metadesc' => 'description',
331 'bctitle' => 'breadcrumb_title',
332 'opengraph-title' => 'open_graph_title',
333 'opengraph-image' => 'open_graph_image',
334 'opengraph-image-id' => 'open_graph_image_id',
335 'opengraph-description' => 'open_graph_description',
336 'twitter-title' => 'twitter_title',
337 'twitter-image' => 'twitter_image',
338 'twitter-image-id' => 'twitter_image_id',
339 'twitter-description' => 'twitter_description',
340 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes',
341 ];
342 }
343
344 /**
345 * Finds an alternative image for the social image.
346 *
347 * @param Indexable $indexable The indexable.
348 *
349 * @return array|bool False when not found, array with data when found.
350 */
351 protected function find_alternative_image( Indexable $indexable ) {
352 if (
353 $indexable->object_sub_type === 'attachment'
354 && $this->image->is_valid_attachment( $indexable->object_id )
355 ) {
356 return [
357 'image_id' => $indexable->object_id,
358 'source' => 'attachment-image',
359 ];
360 }
361
362 $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id );
363 if ( $featured_image_id ) {
364 return [
365 'image_id' => $featured_image_id,
366 'source' => 'featured-image',
367 ];
368 }
369
370 $gallery_image = $this->image->get_gallery_image( $indexable->object_id );
371 if ( $gallery_image ) {
372 return [
373 'image' => $gallery_image,
374 'source' => 'gallery-image',
375 ];
376 }
377
378 $content_image = $this->image->get_post_content_image( $indexable->object_id );
379 if ( $content_image ) {
380 return [
381 'image' => $content_image,
382 'source' => 'first-content-image',
383 ];
384 }
385
386 return false;
387 }
388
389 /**
390 * Gets the number of pages for a post.
391 *
392 * @param object $post The post object.
393 *
394 * @return int|null The number of pages or null if the post isn't paginated.
395 */
396 protected function get_number_of_pages_for_post( $post ) {
397 $number_of_pages = ( \substr_count( $post->post_content, '<!--nextpage-->' ) + 1 );
398
399 if ( $number_of_pages <= 1 ) {
400 return null;
401 }
402
403 return $number_of_pages;
404 }
405
406 /**
407 * Checks whether an indexable should be built for this post.
408 *
409 * @param WP_Post $post The post for which an indexable should be built.
410 *
411 * @return bool `true` if the post should be excluded from building, `false` if not.
412 */
413 protected function should_exclude_post( $post ) {
414 return $this->post_type_helper->is_excluded( $post->post_type );
415 }
416
417 /**
418 * Transforms an empty string into null. Leaves non-empty strings intact.
419 *
420 * @param string $text The string.
421 *
422 * @return string|null The input string or null.
423 */
424 protected function empty_string_to_null( $text ) {
425 if ( ! \is_string( $text ) || $text === '' ) {
426 return null;
427 }
428
429 return $text;
430 }
431 }
432