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/integrat.../watchers
File: indexable-ancestor-watcher.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Integrations\Watchers;
[2] Fix | Delete
[3] Fix | Delete
use Yoast\WP\SEO\Builders\Indexable_Hierarchy_Builder;
[4] Fix | Delete
use Yoast\WP\SEO\Conditionals\Migrations_Conditional;
[5] Fix | Delete
use Yoast\WP\SEO\Helpers\Indexable_Helper;
[6] Fix | Delete
use Yoast\WP\SEO\Helpers\Permalink_Helper;
[7] Fix | Delete
use Yoast\WP\SEO\Helpers\Post_Type_Helper;
[8] Fix | Delete
use Yoast\WP\SEO\Integrations\Integration_Interface;
[9] Fix | Delete
use Yoast\WP\SEO\Models\Indexable;
[10] Fix | Delete
use Yoast\WP\SEO\Repositories\Indexable_Hierarchy_Repository;
[11] Fix | Delete
use Yoast\WP\SEO\Repositories\Indexable_Repository;
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* Ancestor watcher to update the ancestor's children.
[15] Fix | Delete
*
[16] Fix | Delete
* Updates its children's permalink when the ancestor itself is updated.
[17] Fix | Delete
*/
[18] Fix | Delete
class Indexable_Ancestor_Watcher implements Integration_Interface {
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Represents the indexable repository.
[22] Fix | Delete
*
[23] Fix | Delete
* @var Indexable_Repository
[24] Fix | Delete
*/
[25] Fix | Delete
protected $indexable_repository;
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* Represents the indexable hierarchy builder.
[29] Fix | Delete
*
[30] Fix | Delete
* @var Indexable_Hierarchy_Builder
[31] Fix | Delete
*/
[32] Fix | Delete
protected $indexable_hierarchy_builder;
[33] Fix | Delete
[34] Fix | Delete
/**
[35] Fix | Delete
* Represents the indexable hierarchy repository.
[36] Fix | Delete
*
[37] Fix | Delete
* @var Indexable_Hierarchy_Repository
[38] Fix | Delete
*/
[39] Fix | Delete
protected $indexable_hierarchy_repository;
[40] Fix | Delete
[41] Fix | Delete
/**
[42] Fix | Delete
* The indexable helper.
[43] Fix | Delete
*
[44] Fix | Delete
* @var Indexable_Helper
[45] Fix | Delete
*/
[46] Fix | Delete
private $indexable_helper;
[47] Fix | Delete
[48] Fix | Delete
/**
[49] Fix | Delete
* Represents the permalink helper.
[50] Fix | Delete
*
[51] Fix | Delete
* @var Permalink_Helper
[52] Fix | Delete
*/
[53] Fix | Delete
protected $permalink_helper;
[54] Fix | Delete
[55] Fix | Delete
/**
[56] Fix | Delete
* The post type helper.
[57] Fix | Delete
*
[58] Fix | Delete
* @var Post_Type_Helper
[59] Fix | Delete
*/
[60] Fix | Delete
protected $post_type_helper;
[61] Fix | Delete
[62] Fix | Delete
/**
[63] Fix | Delete
* Sets the needed dependencies.
[64] Fix | Delete
*
[65] Fix | Delete
* @param Indexable_Repository $indexable_repository The indexable repository.
[66] Fix | Delete
* @param Indexable_Hierarchy_Builder $indexable_hierarchy_builder The indexable hierarchy builder.
[67] Fix | Delete
* @param Indexable_Hierarchy_Repository $indexable_hierarchy_repository The indexable hierarchy repository.
[68] Fix | Delete
* @param Indexable_Helper $indexable_helper The indexable helper.
[69] Fix | Delete
* @param Permalink_Helper $permalink_helper The permalink helper.
[70] Fix | Delete
* @param Post_Type_Helper $post_type_helper The post type helper.
[71] Fix | Delete
*/
[72] Fix | Delete
public function __construct(
[73] Fix | Delete
Indexable_Repository $indexable_repository,
[74] Fix | Delete
Indexable_Hierarchy_Builder $indexable_hierarchy_builder,
[75] Fix | Delete
Indexable_Hierarchy_Repository $indexable_hierarchy_repository,
[76] Fix | Delete
Indexable_Helper $indexable_helper,
[77] Fix | Delete
Permalink_Helper $permalink_helper,
[78] Fix | Delete
Post_Type_Helper $post_type_helper
[79] Fix | Delete
) {
[80] Fix | Delete
$this->indexable_repository = $indexable_repository;
[81] Fix | Delete
$this->indexable_hierarchy_builder = $indexable_hierarchy_builder;
[82] Fix | Delete
$this->indexable_hierarchy_repository = $indexable_hierarchy_repository;
[83] Fix | Delete
$this->indexable_helper = $indexable_helper;
[84] Fix | Delete
$this->permalink_helper = $permalink_helper;
[85] Fix | Delete
$this->post_type_helper = $post_type_helper;
[86] Fix | Delete
}
[87] Fix | Delete
[88] Fix | Delete
/**
[89] Fix | Delete
* Registers the appropriate hooks.
[90] Fix | Delete
*
[91] Fix | Delete
* @return void
[92] Fix | Delete
*/
[93] Fix | Delete
public function register_hooks() {
[94] Fix | Delete
\add_action( 'wpseo_save_indexable', [ $this, 'reset_children' ], \PHP_INT_MAX, 2 );
[95] Fix | Delete
}
[96] Fix | Delete
[97] Fix | Delete
/**
[98] Fix | Delete
* Returns the conditionals based on which this loadable should be active.
[99] Fix | Delete
*
[100] Fix | Delete
* @return array<Migrations_Conditional>
[101] Fix | Delete
*/
[102] Fix | Delete
public static function get_conditionals() {
[103] Fix | Delete
return [ Migrations_Conditional::class ];
[104] Fix | Delete
}
[105] Fix | Delete
[106] Fix | Delete
/**
[107] Fix | Delete
* If an indexable's permalink has changed, updates its children in the hierarchy table and resets the children's permalink.
[108] Fix | Delete
*
[109] Fix | Delete
* @param Indexable $indexable The indexable.
[110] Fix | Delete
* @param Indexable $indexable_before The old indexable.
[111] Fix | Delete
*
[112] Fix | Delete
* @return bool True if the children were reset.
[113] Fix | Delete
*/
[114] Fix | Delete
public function reset_children( $indexable, $indexable_before ) {
[115] Fix | Delete
if ( ! \in_array( $indexable->object_type, [ 'post', 'term' ], true ) ) {
[116] Fix | Delete
return false;
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
// If the permalink was null it means it was reset instead of changed.
[120] Fix | Delete
if ( $indexable->permalink === $indexable_before->permalink || \is_null( $indexable_before->permalink ) ) {
[121] Fix | Delete
return false;
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
$child_indexable_ids = $this->indexable_hierarchy_repository->find_children( $indexable );
[125] Fix | Delete
$child_indexables = $this->indexable_repository->find_by_ids( $child_indexable_ids );
[126] Fix | Delete
[127] Fix | Delete
\array_walk( $child_indexables, [ $this, 'update_hierarchy_and_permalink' ] );
[128] Fix | Delete
if ( $indexable->object_type === 'term' ) {
[129] Fix | Delete
$child_indexables_for_term = $this->get_children_for_term( $indexable->object_id, $child_indexables );
[130] Fix | Delete
[131] Fix | Delete
\array_walk( $child_indexables_for_term, [ $this, 'update_hierarchy_and_permalink' ] );
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
return true;
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
/**
[138] Fix | Delete
* Finds all child indexables for the given term.
[139] Fix | Delete
*
[140] Fix | Delete
* @param int $term_id Term to fetch the indexable for.
[141] Fix | Delete
* @param array<Indexable> $child_indexables The already known child indexables.
[142] Fix | Delete
*
[143] Fix | Delete
* @return array<Indexable> The list of additional child indexables for a given term.
[144] Fix | Delete
*/
[145] Fix | Delete
public function get_children_for_term( $term_id, array $child_indexables ) {
[146] Fix | Delete
// Finds object_ids (posts) for the term.
[147] Fix | Delete
$post_object_ids = $this->get_object_ids_for_term( $term_id, $child_indexables );
[148] Fix | Delete
[149] Fix | Delete
// Removes the objects that are already present in the children.
[150] Fix | Delete
$existing_post_indexables = \array_filter(
[151] Fix | Delete
$child_indexables,
[152] Fix | Delete
static function ( $indexable ) {
[153] Fix | Delete
return $indexable->object_type === 'post';
[154] Fix | Delete
}
[155] Fix | Delete
);
[156] Fix | Delete
[157] Fix | Delete
$existing_post_object_ids = \wp_list_pluck( $existing_post_indexables, 'object_id' );
[158] Fix | Delete
$post_object_ids = \array_diff( $post_object_ids, $existing_post_object_ids );
[159] Fix | Delete
[160] Fix | Delete
// Finds the indexables for the fetched post_object_ids.
[161] Fix | Delete
$post_indexables = $this->indexable_repository->find_by_multiple_ids_and_type( $post_object_ids, 'post', false );
[162] Fix | Delete
[163] Fix | Delete
// Finds the indexables for the posts that are attached to the term.
[164] Fix | Delete
$post_indexable_ids = \wp_list_pluck( $post_indexables, 'id' );
[165] Fix | Delete
$additional_indexable_ids = $this->indexable_hierarchy_repository->find_children_by_ancestor_ids( $post_indexable_ids );
[166] Fix | Delete
[167] Fix | Delete
// Makes sure we only have indexable id's that we haven't fetched before.
[168] Fix | Delete
$additional_indexable_ids = \array_diff( $additional_indexable_ids, $post_indexable_ids );
[169] Fix | Delete
[170] Fix | Delete
// Finds the additional indexables.
[171] Fix | Delete
$additional_indexables = $this->indexable_repository->find_by_ids( $additional_indexable_ids );
[172] Fix | Delete
[173] Fix | Delete
// Merges all fetched indexables.
[174] Fix | Delete
return \array_merge( $post_indexables, $additional_indexables );
[175] Fix | Delete
}
[176] Fix | Delete
[177] Fix | Delete
/**
[178] Fix | Delete
* Updates the indexable hierarchy and indexable permalink.
[179] Fix | Delete
*
[180] Fix | Delete
* @param Indexable $indexable The indexable to update the hierarchy and permalink for.
[181] Fix | Delete
*
[182] Fix | Delete
* @return void
[183] Fix | Delete
*/
[184] Fix | Delete
protected function update_hierarchy_and_permalink( $indexable ) {
[185] Fix | Delete
if ( \is_a( $indexable, Indexable::class ) ) {
[186] Fix | Delete
$this->indexable_hierarchy_builder->build( $indexable );
[187] Fix | Delete
[188] Fix | Delete
$indexable->permalink = $this->permalink_helper->get_permalink_for_indexable( $indexable );
[189] Fix | Delete
$this->indexable_helper->save_indexable( $indexable );
[190] Fix | Delete
}
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
/**
[194] Fix | Delete
* Retrieves the object id's for a term based on the term-post relationship.
[195] Fix | Delete
*
[196] Fix | Delete
* @param int $term_id The term to get the object id's for.
[197] Fix | Delete
* @param array<Indexable> $child_indexables The child indexables.
[198] Fix | Delete
*
[199] Fix | Delete
* @return array<int> List with object ids for the term.
[200] Fix | Delete
*/
[201] Fix | Delete
protected function get_object_ids_for_term( $term_id, $child_indexables ) {
[202] Fix | Delete
global $wpdb;
[203] Fix | Delete
[204] Fix | Delete
$filter_terms = static function ( $child ) {
[205] Fix | Delete
return $child->object_type === 'term';
[206] Fix | Delete
};
[207] Fix | Delete
[208] Fix | Delete
$child_terms = \array_filter( $child_indexables, $filter_terms );
[209] Fix | Delete
$child_object_ids = \array_merge( [ $term_id ], \wp_list_pluck( $child_terms, 'object_id' ) );
[210] Fix | Delete
[211] Fix | Delete
// Get the term-taxonomy id's for the term and its children.
[212] Fix | Delete
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
[213] Fix | Delete
$term_taxonomy_ids = $wpdb->get_col(
[214] Fix | Delete
$wpdb->prepare(
[215] Fix | Delete
'SELECT term_taxonomy_id
[216] Fix | Delete
FROM %i
[217] Fix | Delete
WHERE term_id IN( ' . \implode( ', ', \array_fill( 0, ( \count( $child_object_ids ) ), '%s' ) ) . ' )',
[218] Fix | Delete
$wpdb->term_taxonomy,
[219] Fix | Delete
...$child_object_ids
[220] Fix | Delete
)
[221] Fix | Delete
);
[222] Fix | Delete
[223] Fix | Delete
// In the case of faulty data having been saved the above query can return 0 results.
[224] Fix | Delete
if ( empty( $term_taxonomy_ids ) ) {
[225] Fix | Delete
return [];
[226] Fix | Delete
}
[227] Fix | Delete
[228] Fix | Delete
// Get the (post) object id's that are attached to the term.
[229] Fix | Delete
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
[230] Fix | Delete
return $wpdb->get_col(
[231] Fix | Delete
$wpdb->prepare(
[232] Fix | Delete
'SELECT DISTINCT object_id
[233] Fix | Delete
FROM %i
[234] Fix | Delete
WHERE term_taxonomy_id IN( ' . \implode( ', ', \array_fill( 0, \count( $term_taxonomy_ids ), '%s' ) ) . ' )',
[235] Fix | Delete
$wpdb->term_relationships,
[236] Fix | Delete
...$term_taxonomy_ids
[237] Fix | Delete
)
[238] Fix | Delete
);
[239] Fix | Delete
}
[240] Fix | Delete
}
[241] Fix | Delete
[242] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function