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

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