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-post-builder.php +31 -27 18.528.5 View file →
@@ -1,12 +1,13 @@
1 1 <?php
2 2
3 3 namespace Yoast\WP\SEO\Builders;
4 4
5 -use WP_Error;
6 5 use WP_Post;
6 +use Yoast\WP\SEO\Exceptions\Indexable\Post_Not_Built_Exception;
7 7 use Yoast\WP\SEO\Exceptions\Indexable\Post_Not_Found_Exception;
8 8 use Yoast\WP\SEO\Helpers\Meta_Helper;
9 +use Yoast\WP\SEO\Helpers\Permalink_Helper;
9 10 use Yoast\WP\SEO\Helpers\Post_Helper;
10 11 use Yoast\WP\SEO\Helpers\Post_Type_Helper;
11 12 use Yoast\WP\SEO\Models\Indexable;
12 13 use Yoast\WP\SEO\Repositories\Indexable_Repository;
@@ -42,8 +43,15 @@
42 43 */
43 44 protected $post_type_helper;
44 45
45 46 /**
47 + * The permalink helper.
48 + *
49 + * @var Permalink_Helper
50 + */
51 + protected $permalink_helper;
52 +
53 + /**
46 54 * Knows the latest version of the Indexable post builder type.
47 55 *
48 56 * @var int
49 57 */
@@ -62,19 +70,22 @@
62 70 * @param Post_Helper $post_helper The post helper.
63 71 * @param Post_Type_Helper $post_type_helper The post type helper.
64 72 * @param Indexable_Builder_Versions $versions The indexable builder versions.
65 73 * @param Meta_Helper $meta The meta helper.
74 + * @param Permalink_Helper $permalink_helper The permalink helper.
66 75 */
67 76 public function __construct(
68 77 Post_Helper $post_helper,
69 78 Post_Type_Helper $post_type_helper,
70 79 Indexable_Builder_Versions $versions,
71 - Meta_Helper $meta
80 + Meta_Helper $meta,
81 + Permalink_Helper $permalink_helper
72 82 ) {
73 83 $this->post_helper = $post_helper;
74 84 $this->post_type_helper = $post_type_helper;
75 85 $this->version = $versions->get_latest_version_for_type( 'post' );
76 86 $this->meta = $meta;
87 + $this->permalink_helper = $permalink_helper;
77 88 }
78 89
79 90 /**
80 91 * Sets the indexable repository. Done to avoid circular dependencies.
@@ -81,8 +92,10 @@
81 92 *
82 93 * @required
83 94 *
84 95 * @param Indexable_Repository $indexable_repository The indexable repository.
96 + *
97 + * @return void
85 98 */
86 99 public function set_indexable_repository( Indexable_Repository $indexable_repository ) {
87 100 $this->indexable_repository = $indexable_repository;
88 101 }
@@ -95,12 +108,13 @@
95 108 *
96 109 * @return bool|Indexable The extended indexable. False when unable to build.
97 110 *
98 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.
99 113 */
100 114 public function build( $post_id, $indexable ) {
101 115 if ( ! $this->post_helper->is_post_indexable( $post_id ) ) {
102 - return false;
116 + throw Post_Not_Built_Exception::because_not_indexable( $post_id );
103 117 }
104 118
105 119 $post = $this->post_helper->get_post( $post_id );
106 120
@@ -108,26 +122,32 @@
108 122 throw new Post_Not_Found_Exception();
109 123 }
110 124
111 125 if ( $this->should_exclude_post( $post ) ) {
112 - return false;
126 + throw Post_Not_Built_Exception::because_post_type_excluded( $post_id );
113 127 }
114 128
115 129 $indexable->object_id = $post_id;
116 130 $indexable->object_type = 'post';
117 131 $indexable->object_sub_type = $post->post_type;
118 - $indexable->permalink = $this->get_permalink( $post->post_type, $post_id );
132 + $indexable->permalink = $this->permalink_helper->get_permalink_for_post( $post->post_type, $post_id );
119 133
120 134 $indexable->primary_focus_keyword_score = $this->get_keyword_score(
121 135 $this->meta->get_value( 'focuskw', $post_id ),
122 - (int) $this->meta->get_value( 'linkdex', $post_id )
136 + (int) $this->meta->get_value( 'linkdex', $post_id ),
123 137 );
124 138
125 139 $indexable->readability_score = (int) $this->meta->get_value( 'content_score', $post_id );
126 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 +
127 147 $indexable->is_cornerstone = ( $this->meta->get_value( 'is_cornerstone', $post_id ) === '1' );
128 148 $indexable->is_robots_noindex = $this->get_robots_noindex(
129 - (int) $this->meta->get_value( 'meta-robots-noindex', $post_id )
149 + (int) $this->meta->get_value( 'meta-robots-noindex', $post_id ),
130 150 );
131 151
132 152 // Set additional meta-robots values.
133 153 $indexable->is_robots_nofollow = ( $this->meta->get_value( 'meta-robots-nofollow', $post_id ) === '1' );
@@ -171,24 +191,8 @@
171 191 return $indexable;
172 192 }
173 193
174 194 /**
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 195 * Determines the value of is_public.
192 196 *
193 197 * @param Indexable $indexable The indexable.
194 198 *
@@ -412,16 +416,16 @@
412 416
413 417 /**
414 418 * Transforms an empty string into null. Leaves non-empty strings intact.
415 419 *
416 - * @param string $string The string.
420 + * @param string $text The string.
417 421 *
418 422 * @return string|null The input string or null.
419 423 */
420 - protected function empty_string_to_null( $string ) {
421 - if ( ! is_string( $string ) || $string === '' ) {
424 + protected function empty_string_to_null( $text ) {
425 + if ( ! \is_string( $text ) || $text === '' ) {
422 426 return null;
423 427 }
424 428
425 - return $string;
429 + return $text;
426 430 }
427 431 }