| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Builders; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Exceptions\Indexable\Not_Built_Exception; |
| 6 |
use Yoast\WP\SEO\Exceptions\Indexable\Source_Exception; |
| 7 |
use Yoast\WP\SEO\Helpers\Indexable_Helper; |
| 8 |
use Yoast\WP\SEO\Models\Indexable; |
| 9 |
use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 10 |
use Yoast\WP\SEO\Services\Indexables\Indexable_Version_Manager; |
| 11 |
|
| 12 |
/** |
| 13 |
* Builder for the indexables. |
| 14 |
* |
| 15 |
* Creates all the indexables. |
| 16 |
*/ |
| 17 |
class Indexable_Builder { |
| 18 |
|
| 19 |
/** |
| 20 |
* The author builder. |
| 21 |
* |
| 22 |
* @var Indexable_Author_Builder |
| 23 |
*/ |
| 24 |
private $author_builder; |
| 25 |
|
| 26 |
/** |
| 27 |
* The post builder. |
| 28 |
* |
| 29 |
* @var Indexable_Post_Builder |
| 30 |
*/ |
| 31 |
private $post_builder; |
| 32 |
|
| 33 |
/** |
| 34 |
* The term builder. |
| 35 |
* |
| 36 |
* @var Indexable_Term_Builder |
| 37 |
*/ |
| 38 |
private $term_builder; |
| 39 |
|
| 40 |
/** |
| 41 |
* The home page builder. |
| 42 |
* |
| 43 |
* @var Indexable_Home_Page_Builder |
| 44 |
*/ |
| 45 |
private $home_page_builder; |
| 46 |
|
| 47 |
/** |
| 48 |
* The post type archive builder. |
| 49 |
* |
| 50 |
* @var Indexable_Post_Type_Archive_Builder |
| 51 |
*/ |
| 52 |
private $post_type_archive_builder; |
| 53 |
|
| 54 |
/** |
| 55 |
* The data archive builder. |
| 56 |
* |
| 57 |
* @var Indexable_Date_Archive_Builder |
| 58 |
*/ |
| 59 |
private $date_archive_builder; |
| 60 |
|
| 61 |
/** |
| 62 |
* The system page builder. |
| 63 |
* |
| 64 |
* @var Indexable_System_Page_Builder |
| 65 |
*/ |
| 66 |
private $system_page_builder; |
| 67 |
|
| 68 |
/** |
| 69 |
* The indexable hierarchy builder. |
| 70 |
* |
| 71 |
* @var Indexable_Hierarchy_Builder |
| 72 |
*/ |
| 73 |
private $hierarchy_builder; |
| 74 |
|
| 75 |
/** |
| 76 |
* The primary term builder |
| 77 |
* |
| 78 |
* @var Primary_Term_Builder |
| 79 |
*/ |
| 80 |
private $primary_term_builder; |
| 81 |
|
| 82 |
/** |
| 83 |
* The link builder |
| 84 |
* |
| 85 |
* @var Indexable_Link_Builder |
| 86 |
*/ |
| 87 |
private $link_builder; |
| 88 |
|
| 89 |
/** |
| 90 |
* The indexable repository. |
| 91 |
* |
| 92 |
* @var Indexable_Repository |
| 93 |
*/ |
| 94 |
private $indexable_repository; |
| 95 |
|
| 96 |
/** |
| 97 |
* The indexable helper. |
| 98 |
* |
| 99 |
* @var Indexable_Helper |
| 100 |
*/ |
| 101 |
protected $indexable_helper; |
| 102 |
|
| 103 |
/** |
| 104 |
* The Indexable Version Manager. |
| 105 |
* |
| 106 |
* @var Indexable_Version_Manager |
| 107 |
*/ |
| 108 |
protected $version_manager; |
| 109 |
|
| 110 |
/** |
| 111 |
* Returns the instance of this class constructed through the ORM Wrapper. |
| 112 |
* |
| 113 |
* @param Indexable_Author_Builder $author_builder The author builder for creating missing indexables. |
| 114 |
* @param Indexable_Post_Builder $post_builder The post builder for creating missing indexables. |
| 115 |
* @param Indexable_Term_Builder $term_builder The term builder for creating missing indexables. |
| 116 |
* @param Indexable_Home_Page_Builder $home_page_builder The front page builder for creating missing indexables. |
| 117 |
* @param Indexable_Post_Type_Archive_Builder $post_type_archive_builder The post type archive builder for creating missing indexables. |
| 118 |
* @param Indexable_Date_Archive_Builder $date_archive_builder The date archive builder for creating missing indexables. |
| 119 |
* @param Indexable_System_Page_Builder $system_page_builder The search result builder for creating missing indexables. |
| 120 |
* @param Indexable_Hierarchy_Builder $hierarchy_builder The hierarchy builder for creating the indexable hierarchy. |
| 121 |
* @param Primary_Term_Builder $primary_term_builder The primary term builder for creating primary terms for posts. |
| 122 |
* @param Indexable_Helper $indexable_helper The indexable helper. |
| 123 |
* @param Indexable_Version_Manager $version_manager The indexable version manager. |
| 124 |
* @param Indexable_Link_Builder $link_builder The link builder for creating missing SEO links. |
| 125 |
*/ |
| 126 |
public function __construct( |
| 127 |
Indexable_Author_Builder $author_builder, |
| 128 |
Indexable_Post_Builder $post_builder, |
| 129 |
Indexable_Term_Builder $term_builder, |
| 130 |
Indexable_Home_Page_Builder $home_page_builder, |
| 131 |
Indexable_Post_Type_Archive_Builder $post_type_archive_builder, |
| 132 |
Indexable_Date_Archive_Builder $date_archive_builder, |
| 133 |
Indexable_System_Page_Builder $system_page_builder, |
| 134 |
Indexable_Hierarchy_Builder $hierarchy_builder, |
| 135 |
Primary_Term_Builder $primary_term_builder, |
| 136 |
Indexable_Helper $indexable_helper, |
| 137 |
Indexable_Version_Manager $version_manager, |
| 138 |
Indexable_Link_Builder $link_builder |
| 139 |
) { |
| 140 |
$this->author_builder = $author_builder; |
| 141 |
$this->post_builder = $post_builder; |
| 142 |
$this->term_builder = $term_builder; |
| 143 |
$this->home_page_builder = $home_page_builder; |
| 144 |
$this->post_type_archive_builder = $post_type_archive_builder; |
| 145 |
$this->date_archive_builder = $date_archive_builder; |
| 146 |
$this->system_page_builder = $system_page_builder; |
| 147 |
$this->hierarchy_builder = $hierarchy_builder; |
| 148 |
$this->primary_term_builder = $primary_term_builder; |
| 149 |
$this->indexable_helper = $indexable_helper; |
| 150 |
$this->version_manager = $version_manager; |
| 151 |
$this->link_builder = $link_builder; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Sets the indexable repository. Done to avoid circular dependencies. |
| 156 |
* |
| 157 |
* @required |
| 158 |
* |
| 159 |
* @param Indexable_Repository $indexable_repository The indexable repository. |
| 160 |
* |
| 161 |
* @return void |
| 162 |
*/ |
| 163 |
public function set_indexable_repository( Indexable_Repository $indexable_repository ) { |
| 164 |
$this->indexable_repository = $indexable_repository; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Creates a clean copy of an Indexable to allow for later database operations. |
| 169 |
* |
| 170 |
* @param Indexable $indexable The Indexable to copy. |
| 171 |
* |
| 172 |
* @return bool|Indexable |
| 173 |
*/ |
| 174 |
protected function deep_copy_indexable( $indexable ) { |
| 175 |
return $this->indexable_repository |
| 176 |
->query() |
| 177 |
->create( $indexable->as_array() ); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Creates an indexable by its ID and type. |
| 182 |
* |
| 183 |
* @param int $object_id The indexable object ID. |
| 184 |
* @param string $object_type The indexable object type. |
| 185 |
* @param Indexable|bool $indexable Optional. An existing indexable to overwrite. |
| 186 |
* |
| 187 |
* @return bool|Indexable Instance of indexable. False when unable to build. |
| 188 |
*/ |
| 189 |
public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { |
| 190 |
$defaults = [ |
| 191 |
'object_type' => $object_type, |
| 192 |
'object_id' => $object_id, |
| 193 |
]; |
| 194 |
|
| 195 |
$indexable = $this->build( $indexable, $defaults ); |
| 196 |
|
| 197 |
return $indexable; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Creates an indexable for the homepage. |
| 202 |
* |
| 203 |
* @param Indexable|bool $indexable Optional. An existing indexable to overwrite. |
| 204 |
* |
| 205 |
* @return Indexable The home page indexable. |
| 206 |
*/ |
| 207 |
public function build_for_home_page( $indexable = false ) { |
| 208 |
return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Creates an indexable for the date archive. |
| 213 |
* |
| 214 |
* @param Indexable|bool $indexable Optional. An existing indexable to overwrite. |
| 215 |
* |
| 216 |
* @return Indexable The date archive indexable. |
| 217 |
*/ |
| 218 |
public function build_for_date_archive( $indexable = false ) { |
| 219 |
return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Creates an indexable for a post type archive. |
| 224 |
* |
| 225 |
* @param string $post_type The post type. |
| 226 |
* @param Indexable|bool $indexable Optional. An existing indexable to overwrite. |
| 227 |
* |
| 228 |
* @return Indexable The post type archive indexable. |
| 229 |
*/ |
| 230 |
public function build_for_post_type_archive( $post_type, $indexable = false ) { |
| 231 |
$defaults = [ |
| 232 |
'object_type' => 'post-type-archive', |
| 233 |
'object_sub_type' => $post_type, |
| 234 |
]; |
| 235 |
return $this->build( $indexable, $defaults ); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Creates an indexable for a system page. |
| 240 |
* |
| 241 |
* @param string $page_type The type of system page. |
| 242 |
* @param Indexable|bool $indexable Optional. An existing indexable to overwrite. |
| 243 |
* |
| 244 |
* @return Indexable The search result indexable. |
| 245 |
*/ |
| 246 |
public function build_for_system_page( $page_type, $indexable = false ) { |
| 247 |
$defaults = [ |
| 248 |
'object_type' => 'system-page', |
| 249 |
'object_sub_type' => $page_type, |
| 250 |
]; |
| 251 |
return $this->build( $indexable, $defaults ); |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Ensures we have a valid indexable. Creates one if false is passed. |
| 256 |
* |
| 257 |
* @param Indexable|false $indexable The indexable. |
| 258 |
* @param array $defaults The initial properties of the Indexable. |
| 259 |
* |
| 260 |
* @return Indexable The indexable. |
| 261 |
*/ |
| 262 |
protected function ensure_indexable( $indexable, $defaults = [] ) { |
| 263 |
if ( ! $indexable ) { |
| 264 |
return $this->indexable_repository->query()->create( $defaults ); |
| 265 |
} |
| 266 |
|
| 267 |
return $indexable; |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. |
| 272 |
* |
| 273 |
* @param int $author_id The author id. |
| 274 |
* |
| 275 |
* @return Indexable|false The author indexable if it has been built, `false` if it could not be built. |
| 276 |
*/ |
| 277 |
protected function maybe_build_author_indexable( $author_id ) { |
| 278 |
$author_indexable = $this->indexable_repository->find_by_id_and_type( |
| 279 |
$author_id, |
| 280 |
'user', |
| 281 |
false, |
| 282 |
); |
| 283 |
if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { |
| 284 |
// Try to build the author. |
| 285 |
$author_defaults = [ |
| 286 |
'object_type' => 'user', |
| 287 |
'object_id' => $author_id, |
| 288 |
]; |
| 289 |
$author_indexable = $this->build( $author_indexable, $author_defaults ); |
| 290 |
} |
| 291 |
return $author_indexable; |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Checks if the indexable type is one that is not supposed to have object ID for. |
| 296 |
* |
| 297 |
* @param string $type The type of the indexable. |
| 298 |
* |
| 299 |
* @return bool Whether the indexable type is one that is not supposed to have object ID for. |
| 300 |
*/ |
| 301 |
protected function is_type_with_no_id( $type ) { |
| 302 |
return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); |
| 303 |
} |
| 304 |
|
| 305 |
// phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. |
| 306 |
|
| 307 |
/** |
| 308 |
* Rebuilds an Indexable from scratch. |
| 309 |
* |
| 310 |
* @param Indexable $indexable The Indexable to (re)build. |
| 311 |
* @param array|null $defaults The object type of the Indexable. |
| 312 |
* |
| 313 |
* @return Indexable|false The resulting Indexable. |
| 314 |
*/ |
| 315 |
public function build( $indexable, $defaults = null ) { |
| 316 |
// Backup the previous Indexable, if there was one. |
| 317 |
$indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; |
| 318 |
|
| 319 |
// Make sure we have an Indexable to work with. |
| 320 |
$indexable = $this->ensure_indexable( $indexable, $defaults ); |
| 321 |
|
| 322 |
try { |
| 323 |
if ( $indexable->object_id === 0 ) { |
| 324 |
throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); |
| 325 |
} |
| 326 |
switch ( $indexable->object_type ) { |
| 327 |
|
| 328 |
case 'post': |
| 329 |
$indexable = $this->post_builder->build( $indexable->object_id, $indexable ); |
| 330 |
|
| 331 |
// Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. |
| 332 |
$indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); |
| 333 |
|
| 334 |
// For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. |
| 335 |
if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { |
| 336 |
$this->link_builder->patch_seo_links( $indexable ); |
| 337 |
} |
| 338 |
|
| 339 |
// Always rebuild the primary term. |
| 340 |
$this->primary_term_builder->build( $indexable->object_id ); |
| 341 |
|
| 342 |
// Always rebuild the hierarchy; this needs the primary term to run correctly. |
| 343 |
$this->hierarchy_builder->build( $indexable ); |
| 344 |
|
| 345 |
$this->maybe_build_author_indexable( $indexable->author_id ); |
| 346 |
|
| 347 |
// The indexable is already saved, so return early. |
| 348 |
return $indexable; |
| 349 |
|
| 350 |
case 'user': |
| 351 |
$indexable = $this->author_builder->build( $indexable->object_id, $indexable ); |
| 352 |
break; |
| 353 |
|
| 354 |
case 'term': |
| 355 |
$indexable = $this->term_builder->build( $indexable->object_id, $indexable ); |
| 356 |
|
| 357 |
// Save indexable, to make sure it can be queried when building hierarchy. |
| 358 |
$indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); |
| 359 |
|
| 360 |
$this->hierarchy_builder->build( $indexable ); |
| 361 |
|
| 362 |
// The indexable is already saved, so return early. |
| 363 |
return $indexable; |
| 364 |
|
| 365 |
case 'home-page': |
| 366 |
$indexable = $this->home_page_builder->build( $indexable ); |
| 367 |
break; |
| 368 |
|
| 369 |
case 'date-archive': |
| 370 |
$indexable = $this->date_archive_builder->build( $indexable ); |
| 371 |
break; |
| 372 |
|
| 373 |
case 'post-type-archive': |
| 374 |
$indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); |
| 375 |
break; |
| 376 |
|
| 377 |
case 'system-page': |
| 378 |
$indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); |
| 379 |
break; |
| 380 |
} |
| 381 |
|
| 382 |
return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); |
| 383 |
} catch ( Source_Exception $exception ) { |
| 384 |
if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { |
| 385 |
return false; |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* The current indexable could not be indexed. Create a placeholder indexable, so we can |
| 390 |
* skip this indexable in future indexing runs. |
| 391 |
* |
| 392 |
* @var Indexable $indexable |
| 393 |
*/ |
| 394 |
$indexable = $this->ensure_indexable( |
| 395 |
$indexable, |
| 396 |
[ |
| 397 |
'object_id' => $indexable->object_id, |
| 398 |
'object_type' => $indexable->object_type, |
| 399 |
'post_status' => 'unindexed', |
| 400 |
'version' => 0, |
| 401 |
], |
| 402 |
); |
| 403 |
// If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. |
| 404 |
$indexable->post_status = 'unindexed'; |
| 405 |
// Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. |
| 406 |
$indexable = $this->version_manager->set_latest( $indexable ); |
| 407 |
|
| 408 |
return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); |
| 409 |
} catch ( Not_Built_Exception $exception ) { |
| 410 |
return false; |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
// phpcs:enable |
| 415 |
} |
| 416 |
|