PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.3
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.3
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 18.3, at src/builders/indexable-post-builder.php

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