Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/wordpres.../src/builders
File: indexable-hierarchy-builder.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Builders;
[2] Fix | Delete
[3] Fix | Delete
use WP_Post;
[4] Fix | Delete
use WP_Term;
[5] Fix | Delete
use WPSEO_Meta;
[6] Fix | Delete
use Yoast\WP\SEO\Helpers\Options_Helper;
[7] Fix | Delete
use Yoast\WP\SEO\Helpers\Post_Helper;
[8] Fix | Delete
use Yoast\WP\SEO\Models\Indexable;
[9] Fix | Delete
use Yoast\WP\SEO\Repositories\Indexable_Hierarchy_Repository;
[10] Fix | Delete
use Yoast\WP\SEO\Repositories\Indexable_Repository;
[11] Fix | Delete
use Yoast\WP\SEO\Repositories\Primary_Term_Repository;
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* Builder for the indexables hierarchy.
[15] Fix | Delete
*
[16] Fix | Delete
* Builds the indexable hierarchy for indexables.
[17] Fix | Delete
*/
[18] Fix | Delete
class Indexable_Hierarchy_Builder {
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Holds a list of indexables where the ancestors are saved for.
[22] Fix | Delete
*
[23] Fix | Delete
* @var array
[24] Fix | Delete
*/
[25] Fix | Delete
protected $saved_ancestors = [];
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* The indexable repository.
[29] Fix | Delete
*
[30] Fix | Delete
* @var Indexable_Repository
[31] Fix | Delete
*/
[32] Fix | Delete
private $indexable_repository;
[33] Fix | Delete
[34] Fix | Delete
/**
[35] Fix | Delete
* The indexable hierarchy repository.
[36] Fix | Delete
*
[37] Fix | Delete
* @var Indexable_Hierarchy_Repository
[38] Fix | Delete
*/
[39] Fix | Delete
private $indexable_hierarchy_repository;
[40] Fix | Delete
[41] Fix | Delete
/**
[42] Fix | Delete
* The primary term repository.
[43] Fix | Delete
*
[44] Fix | Delete
* @var Primary_Term_Repository
[45] Fix | Delete
*/
[46] Fix | Delete
private $primary_term_repository;
[47] Fix | Delete
[48] Fix | Delete
/**
[49] Fix | Delete
* The options helper.
[50] Fix | Delete
*
[51] Fix | Delete
* @var Options_Helper
[52] Fix | Delete
*/
[53] Fix | Delete
private $options;
[54] Fix | Delete
[55] Fix | Delete
/**
[56] Fix | Delete
* Holds the Post_Helper instance.
[57] Fix | Delete
*
[58] Fix | Delete
* @var Post_Helper
[59] Fix | Delete
*/
[60] Fix | Delete
private $post;
[61] Fix | Delete
[62] Fix | Delete
/**
[63] Fix | Delete
* Indexable_Author_Builder constructor.
[64] Fix | Delete
*
[65] Fix | Delete
* @param Indexable_Hierarchy_Repository $indexable_hierarchy_repository The indexable hierarchy repository.
[66] Fix | Delete
* @param Primary_Term_Repository $primary_term_repository The primary term repository.
[67] Fix | Delete
* @param Options_Helper $options The options helper.
[68] Fix | Delete
* @param Post_Helper $post The post helper.
[69] Fix | Delete
*/
[70] Fix | Delete
public function __construct(
[71] Fix | Delete
Indexable_Hierarchy_Repository $indexable_hierarchy_repository,
[72] Fix | Delete
Primary_Term_Repository $primary_term_repository,
[73] Fix | Delete
Options_Helper $options,
[74] Fix | Delete
Post_Helper $post
[75] Fix | Delete
) {
[76] Fix | Delete
$this->indexable_hierarchy_repository = $indexable_hierarchy_repository;
[77] Fix | Delete
$this->primary_term_repository = $primary_term_repository;
[78] Fix | Delete
$this->options = $options;
[79] Fix | Delete
$this->post = $post;
[80] Fix | Delete
}
[81] Fix | Delete
[82] Fix | Delete
/**
[83] Fix | Delete
* Sets the indexable repository. Done to avoid circular dependencies.
[84] Fix | Delete
*
[85] Fix | Delete
* @required
[86] Fix | Delete
*
[87] Fix | Delete
* @param Indexable_Repository $indexable_repository The indexable repository.
[88] Fix | Delete
*
[89] Fix | Delete
* @return void
[90] Fix | Delete
*/
[91] Fix | Delete
public function set_indexable_repository( Indexable_Repository $indexable_repository ) {
[92] Fix | Delete
$this->indexable_repository = $indexable_repository;
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
/**
[96] Fix | Delete
* Builds the ancestor hierarchy for an indexable.
[97] Fix | Delete
*
[98] Fix | Delete
* @param Indexable $indexable The indexable.
[99] Fix | Delete
*
[100] Fix | Delete
* @return Indexable The indexable.
[101] Fix | Delete
*/
[102] Fix | Delete
public function build( Indexable $indexable ) {
[103] Fix | Delete
if ( $this->hierarchy_is_built( $indexable ) ) {
[104] Fix | Delete
return $indexable;
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
$this->indexable_hierarchy_repository->clear_ancestors( $indexable->id );
[108] Fix | Delete
[109] Fix | Delete
$indexable_id = $this->get_indexable_id( $indexable );
[110] Fix | Delete
$ancestors = [];
[111] Fix | Delete
if ( $indexable->object_type === 'post' ) {
[112] Fix | Delete
$this->add_ancestors_for_post( $indexable_id, $indexable->object_id, $ancestors );
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
if ( $indexable->object_type === 'term' ) {
[116] Fix | Delete
$this->add_ancestors_for_term( $indexable_id, $indexable->object_id, $ancestors );
[117] Fix | Delete
}
[118] Fix | Delete
$indexable->ancestors = \array_reverse( \array_values( $ancestors ) );
[119] Fix | Delete
$indexable->has_ancestors = ! empty( $ancestors );
[120] Fix | Delete
if ( $indexable->id ) {
[121] Fix | Delete
$this->save_ancestors( $indexable );
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
return $indexable;
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
/**
[128] Fix | Delete
* Checks if a hierarchy is built already for the given indexable.
[129] Fix | Delete
*
[130] Fix | Delete
* @param Indexable $indexable The indexable to check.
[131] Fix | Delete
*
[132] Fix | Delete
* @return bool True when indexable has a built hierarchy.
[133] Fix | Delete
*/
[134] Fix | Delete
protected function hierarchy_is_built( Indexable $indexable ) {
[135] Fix | Delete
if ( \in_array( $indexable->id, $this->saved_ancestors, true ) ) {
[136] Fix | Delete
return true;
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
$this->saved_ancestors[] = $indexable->id;
[140] Fix | Delete
[141] Fix | Delete
return false;
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
/**
[145] Fix | Delete
* Saves the ancestors.
[146] Fix | Delete
*
[147] Fix | Delete
* @param Indexable $indexable The indexable.
[148] Fix | Delete
*
[149] Fix | Delete
* @return void
[150] Fix | Delete
*/
[151] Fix | Delete
private function save_ancestors( $indexable ) {
[152] Fix | Delete
if ( empty( $indexable->ancestors ) ) {
[153] Fix | Delete
$this->indexable_hierarchy_repository->add_ancestor( $indexable->id, 0, 0 );
[154] Fix | Delete
return;
[155] Fix | Delete
}
[156] Fix | Delete
$depth = \count( $indexable->ancestors );
[157] Fix | Delete
foreach ( $indexable->ancestors as $ancestor ) {
[158] Fix | Delete
$this->indexable_hierarchy_repository->add_ancestor( $indexable->id, $ancestor->id, $depth );
[159] Fix | Delete
--$depth;
[160] Fix | Delete
}
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
/**
[164] Fix | Delete
* Adds ancestors for a post.
[165] Fix | Delete
*
[166] Fix | Delete
* @param int $indexable_id The indexable id, this is the id of the original indexable.
[167] Fix | Delete
* @param int $post_id The post id, this is the id of the post currently being evaluated.
[168] Fix | Delete
* @param int[] $parents The indexable IDs of all parents.
[169] Fix | Delete
*
[170] Fix | Delete
* @return void
[171] Fix | Delete
*/
[172] Fix | Delete
private function add_ancestors_for_post( $indexable_id, $post_id, &$parents ) {
[173] Fix | Delete
$post = $this->post->get_post( $post_id );
[174] Fix | Delete
[175] Fix | Delete
if ( ! isset( $post->post_parent ) ) {
[176] Fix | Delete
return;
[177] Fix | Delete
}
[178] Fix | Delete
[179] Fix | Delete
if ( $post->post_parent !== 0 && $this->post->get_post( $post->post_parent ) !== null ) {
[180] Fix | Delete
$ancestor = $this->indexable_repository->find_by_id_and_type( $post->post_parent, 'post' );
[181] Fix | Delete
if ( $this->is_invalid_ancestor( $ancestor, $indexable_id, $parents ) ) {
[182] Fix | Delete
return;
[183] Fix | Delete
}
[184] Fix | Delete
[185] Fix | Delete
$parents[ $this->get_indexable_id( $ancestor ) ] = $ancestor;
[186] Fix | Delete
[187] Fix | Delete
$this->add_ancestors_for_post( $indexable_id, $ancestor->object_id, $parents );
[188] Fix | Delete
[189] Fix | Delete
return;
[190] Fix | Delete
}
[191] Fix | Delete
[192] Fix | Delete
$primary_term_id = $this->find_primary_term_id_for_post( $post );
[193] Fix | Delete
[194] Fix | Delete
if ( $primary_term_id === 0 ) {
[195] Fix | Delete
return;
[196] Fix | Delete
}
[197] Fix | Delete
[198] Fix | Delete
$ancestor = $this->indexable_repository->find_by_id_and_type( $primary_term_id, 'term' );
[199] Fix | Delete
if ( $this->is_invalid_ancestor( $ancestor, $indexable_id, $parents ) ) {
[200] Fix | Delete
return;
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
$parents[ $this->get_indexable_id( $ancestor ) ] = $ancestor;
[204] Fix | Delete
[205] Fix | Delete
$this->add_ancestors_for_term( $indexable_id, $ancestor->object_id, $parents );
[206] Fix | Delete
}
[207] Fix | Delete
[208] Fix | Delete
/**
[209] Fix | Delete
* Adds ancestors for a term.
[210] Fix | Delete
*
[211] Fix | Delete
* @param int $indexable_id The indexable id, this is the id of the original indexable.
[212] Fix | Delete
* @param int $term_id The term id, this is the id of the term currently being evaluated.
[213] Fix | Delete
* @param int[] $parents The indexable IDs of all parents.
[214] Fix | Delete
*
[215] Fix | Delete
* @return void
[216] Fix | Delete
*/
[217] Fix | Delete
private function add_ancestors_for_term( $indexable_id, $term_id, &$parents = [] ) {
[218] Fix | Delete
$term = \get_term( $term_id );
[219] Fix | Delete
$term_parents = $this->get_term_parents( $term );
[220] Fix | Delete
[221] Fix | Delete
foreach ( $term_parents as $parent ) {
[222] Fix | Delete
$ancestor = $this->indexable_repository->find_by_id_and_type( $parent->term_id, 'term' );
[223] Fix | Delete
if ( $this->is_invalid_ancestor( $ancestor, $indexable_id, $parents ) ) {
[224] Fix | Delete
continue;
[225] Fix | Delete
}
[226] Fix | Delete
[227] Fix | Delete
$parents[ $this->get_indexable_id( $ancestor ) ] = $ancestor;
[228] Fix | Delete
}
[229] Fix | Delete
}
[230] Fix | Delete
[231] Fix | Delete
/**
[232] Fix | Delete
* Gets the primary term ID for a post.
[233] Fix | Delete
*
[234] Fix | Delete
* @param WP_Post $post The post.
[235] Fix | Delete
*
[236] Fix | Delete
* @return int The primary term ID. 0 if none exists.
[237] Fix | Delete
*/
[238] Fix | Delete
private function find_primary_term_id_for_post( $post ) {
[239] Fix | Delete
$main_taxonomy = $this->options->get( 'post_types-' . $post->post_type . '-maintax' );
[240] Fix | Delete
[241] Fix | Delete
if ( ! $main_taxonomy || $main_taxonomy === '0' ) {
[242] Fix | Delete
return 0;
[243] Fix | Delete
}
[244] Fix | Delete
[245] Fix | Delete
$primary_term_id = $this->get_primary_term_id( $post->ID, $main_taxonomy );
[246] Fix | Delete
[247] Fix | Delete
if ( $primary_term_id ) {
[248] Fix | Delete
$term = \get_term( $primary_term_id );
[249] Fix | Delete
if ( $term !== null && ! \is_wp_error( $term ) ) {
[250] Fix | Delete
return $primary_term_id;
[251] Fix | Delete
}
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
$terms = \get_the_terms( $post->ID, $main_taxonomy );
[255] Fix | Delete
[256] Fix | Delete
if ( ! \is_array( $terms ) || empty( $terms ) ) {
[257] Fix | Delete
return 0;
[258] Fix | Delete
}
[259] Fix | Delete
[260] Fix | Delete
return $this->find_deepest_term_id( $terms );
[261] Fix | Delete
}
[262] Fix | Delete
[263] Fix | Delete
/**
[264] Fix | Delete
* Find the deepest term in an array of term objects.
[265] Fix | Delete
*
[266] Fix | Delete
* @param array $terms Terms set.
[267] Fix | Delete
*
[268] Fix | Delete
* @return int The deepest term ID.
[269] Fix | Delete
*/
[270] Fix | Delete
private function find_deepest_term_id( $terms ) {
[271] Fix | Delete
/*
[272] Fix | Delete
* Let's find the deepest term in this array, by looping through and then
[273] Fix | Delete
* unsetting every term that is used as a parent by another one in the array.
[274] Fix | Delete
*/
[275] Fix | Delete
$terms_by_id = [];
[276] Fix | Delete
foreach ( $terms as $term ) {
[277] Fix | Delete
$terms_by_id[ $term->term_id ] = $term;
[278] Fix | Delete
}
[279] Fix | Delete
foreach ( $terms as $term ) {
[280] Fix | Delete
unset( $terms_by_id[ $term->parent ] );
[281] Fix | Delete
}
[282] Fix | Delete
[283] Fix | Delete
/*
[284] Fix | Delete
* As we could still have two subcategories, from different parent categories,
[285] Fix | Delete
* let's pick the one with the lowest ordered ancestor.
[286] Fix | Delete
*/
[287] Fix | Delete
$parents_count = -1;
[288] Fix | Delete
$term_order = 9999; // Because ASC.
[289] Fix | Delete
$deepest_term = \reset( $terms_by_id );
[290] Fix | Delete
foreach ( $terms_by_id as $term ) {
[291] Fix | Delete
$parents = $this->get_term_parents( $term );
[292] Fix | Delete
[293] Fix | Delete
$new_parents_count = \count( $parents );
[294] Fix | Delete
[295] Fix | Delete
if ( $new_parents_count < $parents_count ) {
[296] Fix | Delete
continue;
[297] Fix | Delete
}
[298] Fix | Delete
[299] Fix | Delete
$parent_order = 9999; // Set default order.
[300] Fix | Delete
foreach ( $parents as $parent ) {
[301] Fix | Delete
if ( $parent->parent === 0 && isset( $parent->term_order ) ) {
[302] Fix | Delete
$parent_order = $parent->term_order;
[303] Fix | Delete
}
[304] Fix | Delete
}
[305] Fix | Delete
[306] Fix | Delete
// Check if parent has lowest order.
[307] Fix | Delete
if ( $new_parents_count > $parents_count || $parent_order < $term_order ) {
[308] Fix | Delete
$term_order = $parent_order;
[309] Fix | Delete
$deepest_term = $term;
[310] Fix | Delete
}
[311] Fix | Delete
[312] Fix | Delete
$parents_count = $new_parents_count;
[313] Fix | Delete
}
[314] Fix | Delete
[315] Fix | Delete
return $deepest_term->term_id;
[316] Fix | Delete
}
[317] Fix | Delete
[318] Fix | Delete
/**
[319] Fix | Delete
* Get a term's parents.
[320] Fix | Delete
*
[321] Fix | Delete
* @param WP_Term $term Term to get the parents for.
[322] Fix | Delete
*
[323] Fix | Delete
* @return WP_Term[] An array of all this term's parents.
[324] Fix | Delete
*/
[325] Fix | Delete
private function get_term_parents( $term ) {
[326] Fix | Delete
$tax = $term->taxonomy;
[327] Fix | Delete
$parents = [];
[328] Fix | Delete
while ( (int) $term->parent !== 0 ) {
[329] Fix | Delete
$term = \get_term( $term->parent, $tax );
[330] Fix | Delete
$parents[] = $term;
[331] Fix | Delete
}
[332] Fix | Delete
[333] Fix | Delete
return $parents;
[334] Fix | Delete
}
[335] Fix | Delete
[336] Fix | Delete
/**
[337] Fix | Delete
* Checks if an ancestor is valid to add.
[338] Fix | Delete
*
[339] Fix | Delete
* @param Indexable $ancestor The ancestor (presumed indexable) to check.
[340] Fix | Delete
* @param int $indexable_id The indexable id we're adding ancestors for.
[341] Fix | Delete
* @param int[] $parents The indexable ids of the parents already added.
[342] Fix | Delete
*
[343] Fix | Delete
* @return bool
[344] Fix | Delete
*/
[345] Fix | Delete
private function is_invalid_ancestor( $ancestor, $indexable_id, $parents ) {
[346] Fix | Delete
// If the ancestor is not an Indexable, it is invalid by default.
[347] Fix | Delete
if ( ! \is_a( $ancestor, 'Yoast\WP\SEO\Models\Indexable' ) ) {
[348] Fix | Delete
return true;
[349] Fix | Delete
}
[350] Fix | Delete
[351] Fix | Delete
// Don't add ancestors if they're unindexed, already added or the same as the main object.
[352] Fix | Delete
if ( $ancestor->post_status === 'unindexed' ) {
[353] Fix | Delete
return true;
[354] Fix | Delete
}
[355] Fix | Delete
[356] Fix | Delete
$ancestor_id = $this->get_indexable_id( $ancestor );
[357] Fix | Delete
if ( \array_key_exists( $ancestor_id, $parents ) ) {
[358] Fix | Delete
return true;
[359] Fix | Delete
}
[360] Fix | Delete
[361] Fix | Delete
if ( $ancestor_id === $indexable_id ) {
[362] Fix | Delete
return true;
[363] Fix | Delete
}
[364] Fix | Delete
[365] Fix | Delete
return false;
[366] Fix | Delete
}
[367] Fix | Delete
[368] Fix | Delete
/**
[369] Fix | Delete
* Returns the ID for an indexable. Catches situations where the id is null due to errors.
[370] Fix | Delete
*
[371] Fix | Delete
* @param Indexable $indexable The indexable.
[372] Fix | Delete
*
[373] Fix | Delete
* @return string|int A unique ID for the indexable.
[374] Fix | Delete
*/
[375] Fix | Delete
private function get_indexable_id( Indexable $indexable ) {
[376] Fix | Delete
if ( $indexable->id === 0 ) {
[377] Fix | Delete
return "{$indexable->object_type}:{$indexable->object_id}";
[378] Fix | Delete
}
[379] Fix | Delete
[380] Fix | Delete
return $indexable->id;
[381] Fix | Delete
}
[382] Fix | Delete
[383] Fix | Delete
/**
[384] Fix | Delete
* Returns the primary term id of a post.
[385] Fix | Delete
*
[386] Fix | Delete
* @param int $post_id The post ID.
[387] Fix | Delete
* @param string $main_taxonomy The main taxonomy.
[388] Fix | Delete
*
[389] Fix | Delete
* @return int The ID of the primary term.
[390] Fix | Delete
*/
[391] Fix | Delete
private function get_primary_term_id( $post_id, $main_taxonomy ) {
[392] Fix | Delete
$primary_term = $this->primary_term_repository->find_by_post_id_and_taxonomy( $post_id, $main_taxonomy, false );
[393] Fix | Delete
[394] Fix | Delete
if ( $primary_term ) {
[395] Fix | Delete
return $primary_term->term_id;
[396] Fix | Delete
}
[397] Fix | Delete
[398] Fix | Delete
return \get_post_meta( $post_id, WPSEO_Meta::$meta_prefix . 'primary_' . $main_taxonomy, true );
[399] Fix | Delete
}
[400] Fix | Delete
}
[401] Fix | Delete
[402] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function