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