| 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->is_cornerstone = ( $this->meta->get_value( 'is_cornerstone', $post_id ) === '1' ); |
| 144 |
$indexable->is_robots_noindex = $this->get_robots_noindex( |
| 145 |
(int) $this->meta->get_value( 'meta-robots-noindex', $post_id ), |
| 146 |
); |
| 147 |
|
| 148 |
// Set additional meta-robots values. |
| 149 |
$indexable->is_robots_nofollow = ( $this->meta->get_value( 'meta-robots-nofollow', $post_id ) === '1' ); |
| 150 |
$noindex_advanced = $this->meta->get_value( 'meta-robots-adv', $post_id ); |
| 151 |
$meta_robots = \explode( ',', $noindex_advanced ); |
| 152 |
|
| 153 |
foreach ( $this->get_robots_options() as $meta_robots_option ) { |
| 154 |
$indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; |
| 155 |
} |
| 156 |
|
| 157 |
$this->reset_social_images( $indexable ); |
| 158 |
|
| 159 |
foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { |
| 160 |
$indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); |
| 161 |
} |
| 162 |
|
| 163 |
if ( empty( $indexable->breadcrumb_title ) ) { |
| 164 |
$indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); |
| 165 |
} |
| 166 |
|
| 167 |
$this->handle_social_images( $indexable ); |
| 168 |
|
| 169 |
$indexable->author_id = $post->post_author; |
| 170 |
$indexable->post_parent = $post->post_parent; |
| 171 |
|
| 172 |
$indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); |
| 173 |
$indexable->post_status = $post->post_status; |
| 174 |
$indexable->is_protected = $post->post_password !== ''; |
| 175 |
$indexable->is_public = $this->is_public( $indexable ); |
| 176 |
$indexable->has_public_posts = $this->has_public_posts( $indexable ); |
| 177 |
$indexable->blog_id = \get_current_blog_id(); |
| 178 |
|
| 179 |
$indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); |
| 180 |
$indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); |
| 181 |
|
| 182 |
$indexable->object_last_modified = $post->post_modified_gmt; |
| 183 |
$indexable->object_published_at = $post->post_date_gmt; |
| 184 |
|
| 185 |
$indexable->version = $this->version; |
| 186 |
|
| 187 |
return $indexable; |
| 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 $text The string. |
| 417 |
* |
| 418 |
* @return string|null The input string or null. |
| 419 |
*/ |
| 420 |
protected function empty_string_to_null( $text ) { |
| 421 |
if ( ! \is_string( $text ) || $text === '' ) { |
| 422 |
return null; |
| 423 |
} |
| 424 |
|
| 425 |
return $text; |
| 426 |
} |
| 427 |
} |
| 428 |
|