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-post-watcher.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Integrations\Watchers;
[2] Fix | Delete
[3] Fix | Delete
use Exception;
[4] Fix | Delete
use WP_Post;
[5] Fix | Delete
use Yoast\WP\SEO\Builders\Indexable_Builder;
[6] Fix | Delete
use Yoast\WP\SEO\Builders\Indexable_Link_Builder;
[7] Fix | Delete
use Yoast\WP\SEO\Conditionals\Migrations_Conditional;
[8] Fix | Delete
use Yoast\WP\SEO\Helpers\Author_Archive_Helper;
[9] Fix | Delete
use Yoast\WP\SEO\Helpers\Indexable_Helper;
[10] Fix | Delete
use Yoast\WP\SEO\Helpers\Post_Helper;
[11] Fix | Delete
use Yoast\WP\SEO\Integrations\Cleanup_Integration;
[12] Fix | Delete
use Yoast\WP\SEO\Integrations\Integration_Interface;
[13] Fix | Delete
use Yoast\WP\SEO\Loggers\Logger;
[14] Fix | Delete
use Yoast\WP\SEO\Models\Indexable;
[15] Fix | Delete
use Yoast\WP\SEO\Repositories\Indexable_Hierarchy_Repository;
[16] Fix | Delete
use Yoast\WP\SEO\Repositories\Indexable_Repository;
[17] Fix | Delete
use YoastSEO_Vendor\Psr\Log\LogLevel;
[18] Fix | Delete
[19] Fix | Delete
/**
[20] Fix | Delete
* WordPress Post watcher.
[21] Fix | Delete
*
[22] Fix | Delete
* Fills the Indexable according to Post data.
[23] Fix | Delete
*/
[24] Fix | Delete
class Indexable_Post_Watcher implements Integration_Interface {
[25] Fix | Delete
[26] Fix | Delete
/**
[27] Fix | Delete
* The indexable repository.
[28] Fix | Delete
*
[29] Fix | Delete
* @var Indexable_Repository
[30] Fix | Delete
*/
[31] Fix | Delete
protected $repository;
[32] Fix | Delete
[33] Fix | Delete
/**
[34] Fix | Delete
* The indexable builder.
[35] Fix | Delete
*
[36] Fix | Delete
* @var Indexable_Builder
[37] Fix | Delete
*/
[38] Fix | Delete
protected $builder;
[39] Fix | Delete
[40] Fix | Delete
/**
[41] Fix | Delete
* The indexable hierarchy repository.
[42] Fix | Delete
*
[43] Fix | Delete
* @var Indexable_Hierarchy_Repository
[44] Fix | Delete
*/
[45] Fix | Delete
private $hierarchy_repository;
[46] Fix | Delete
[47] Fix | Delete
/**
[48] Fix | Delete
* The link builder.
[49] Fix | Delete
*
[50] Fix | Delete
* @var Indexable_Link_Builder
[51] Fix | Delete
*/
[52] Fix | Delete
protected $link_builder;
[53] Fix | Delete
[54] Fix | Delete
/**
[55] Fix | Delete
* The author archive helper.
[56] Fix | Delete
*
[57] Fix | Delete
* @var Author_Archive_Helper
[58] Fix | Delete
*/
[59] Fix | Delete
private $author_archive;
[60] Fix | Delete
[61] Fix | Delete
/**
[62] Fix | Delete
* The indexable helper.
[63] Fix | Delete
*
[64] Fix | Delete
* @var Indexable_Helper
[65] Fix | Delete
*/
[66] Fix | Delete
private $indexable_helper;
[67] Fix | Delete
[68] Fix | Delete
/**
[69] Fix | Delete
* Holds the Post_Helper instance.
[70] Fix | Delete
*
[71] Fix | Delete
* @var Post_Helper
[72] Fix | Delete
*/
[73] Fix | Delete
private $post;
[74] Fix | Delete
[75] Fix | Delete
/**
[76] Fix | Delete
* Holds the logger.
[77] Fix | Delete
*
[78] Fix | Delete
* @var Logger
[79] Fix | Delete
*/
[80] Fix | Delete
protected $logger;
[81] Fix | Delete
[82] Fix | Delete
/**
[83] Fix | Delete
* Returns the conditionals based on which this loadable should be active.
[84] Fix | Delete
*
[85] Fix | Delete
* @return array
[86] Fix | Delete
*/
[87] Fix | Delete
public static function get_conditionals() {
[88] Fix | Delete
return [ Migrations_Conditional::class ];
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
/**
[92] Fix | Delete
* Indexable_Post_Watcher constructor.
[93] Fix | Delete
*
[94] Fix | Delete
* @param Indexable_Repository $repository The repository to use.
[95] Fix | Delete
* @param Indexable_Builder $builder The post builder to use.
[96] Fix | Delete
* @param Indexable_Hierarchy_Repository $hierarchy_repository The hierarchy repository to use.
[97] Fix | Delete
* @param Indexable_Link_Builder $link_builder The link builder.
[98] Fix | Delete
* @param Author_Archive_Helper $author_archive The author archive helper.
[99] Fix | Delete
* @param Indexable_Helper $indexable_helper The indexable helper.
[100] Fix | Delete
* @param Post_Helper $post The post helper.
[101] Fix | Delete
* @param Logger $logger The logger.
[102] Fix | Delete
*/
[103] Fix | Delete
public function __construct(
[104] Fix | Delete
Indexable_Repository $repository,
[105] Fix | Delete
Indexable_Builder $builder,
[106] Fix | Delete
Indexable_Hierarchy_Repository $hierarchy_repository,
[107] Fix | Delete
Indexable_Link_Builder $link_builder,
[108] Fix | Delete
Author_Archive_Helper $author_archive,
[109] Fix | Delete
Indexable_Helper $indexable_helper,
[110] Fix | Delete
Post_Helper $post,
[111] Fix | Delete
Logger $logger
[112] Fix | Delete
) {
[113] Fix | Delete
$this->repository = $repository;
[114] Fix | Delete
$this->builder = $builder;
[115] Fix | Delete
$this->hierarchy_repository = $hierarchy_repository;
[116] Fix | Delete
$this->link_builder = $link_builder;
[117] Fix | Delete
$this->author_archive = $author_archive;
[118] Fix | Delete
$this->indexable_helper = $indexable_helper;
[119] Fix | Delete
$this->post = $post;
[120] Fix | Delete
$this->logger = $logger;
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
/**
[124] Fix | Delete
* Initializes the integration.
[125] Fix | Delete
*
[126] Fix | Delete
* This is the place to register hooks and filters.
[127] Fix | Delete
*
[128] Fix | Delete
* @return void
[129] Fix | Delete
*/
[130] Fix | Delete
public function register_hooks() {
[131] Fix | Delete
\add_action( 'wp_insert_post', [ $this, 'build_indexable' ], \PHP_INT_MAX );
[132] Fix | Delete
\add_action( 'delete_post', [ $this, 'delete_indexable' ] );
[133] Fix | Delete
[134] Fix | Delete
\add_action( 'edit_attachment', [ $this, 'build_indexable' ], \PHP_INT_MAX );
[135] Fix | Delete
\add_action( 'add_attachment', [ $this, 'build_indexable' ], \PHP_INT_MAX );
[136] Fix | Delete
\add_action( 'delete_attachment', [ $this, 'delete_indexable' ] );
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
/**
[140] Fix | Delete
* Deletes the meta when a post is deleted.
[141] Fix | Delete
*
[142] Fix | Delete
* @param int $post_id Post ID.
[143] Fix | Delete
*
[144] Fix | Delete
* @return void
[145] Fix | Delete
*/
[146] Fix | Delete
public function delete_indexable( $post_id ) {
[147] Fix | Delete
$indexable = $this->repository->find_by_id_and_type( $post_id, 'post', false );
[148] Fix | Delete
[149] Fix | Delete
// Only interested in post indexables.
[150] Fix | Delete
if ( ! $indexable || $indexable->object_type !== 'post' ) {
[151] Fix | Delete
return;
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
$this->update_relations( $this->post->get_post( $post_id ) );
[155] Fix | Delete
[156] Fix | Delete
$this->update_has_public_posts( $indexable );
[157] Fix | Delete
[158] Fix | Delete
$this->hierarchy_repository->clear_ancestors( $indexable->id );
[159] Fix | Delete
$this->link_builder->delete( $indexable );
[160] Fix | Delete
$indexable->delete();
[161] Fix | Delete
\do_action( 'wpseo_indexable_deleted', $indexable );
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
/**
[165] Fix | Delete
* Updates the relations when the post indexable is built.
[166] Fix | Delete
*
[167] Fix | Delete
* @param Indexable $indexable The indexable.
[168] Fix | Delete
* @param WP_Post $post The post.
[169] Fix | Delete
*
[170] Fix | Delete
* @return void
[171] Fix | Delete
*/
[172] Fix | Delete
public function updated_indexable( $indexable, $post ) {
[173] Fix | Delete
// Only interested in post indexables.
[174] Fix | Delete
if ( $indexable->object_type !== 'post' ) {
[175] Fix | Delete
return;
[176] Fix | Delete
}
[177] Fix | Delete
[178] Fix | Delete
if ( \is_a( $post, Indexable::class ) ) {
[179] Fix | Delete
\_deprecated_argument( __FUNCTION__, '17.7', 'The $old_indexable argument has been deprecated.' );
[180] Fix | Delete
$post = $this->post->get_post( $indexable->object_id );
[181] Fix | Delete
}
[182] Fix | Delete
[183] Fix | Delete
$this->update_relations( $post );
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
/**
[187] Fix | Delete
* Saves post meta.
[188] Fix | Delete
*
[189] Fix | Delete
* @param int $post_id Post ID.
[190] Fix | Delete
*
[191] Fix | Delete
* @return void
[192] Fix | Delete
*/
[193] Fix | Delete
public function build_indexable( $post_id ) {
[194] Fix | Delete
// Bail if this is a multisite installation and the site has been switched.
[195] Fix | Delete
if ( $this->is_multisite_and_switched() ) {
[196] Fix | Delete
return;
[197] Fix | Delete
}
[198] Fix | Delete
[199] Fix | Delete
try {
[200] Fix | Delete
$indexable = $this->repository->find_by_id_and_type( $post_id, 'post', false );
[201] Fix | Delete
$indexable = $this->builder->build_for_id_and_type( $post_id, 'post', $indexable );
[202] Fix | Delete
[203] Fix | Delete
$post = $this->post->get_post( $post_id );
[204] Fix | Delete
[205] Fix | Delete
/*
[206] Fix | Delete
* Update whether an author has public posts.
[207] Fix | Delete
* For example this post could be set to Draft or Private,
[208] Fix | Delete
* which can influence if its author has any public posts at all.
[209] Fix | Delete
*/
[210] Fix | Delete
if ( $indexable ) {
[211] Fix | Delete
$this->update_has_public_posts( $indexable );
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
// Build links for this post.
[215] Fix | Delete
if ( $post && $indexable && \in_array( $post->post_status, $this->post->get_public_post_statuses(), true ) ) {
[216] Fix | Delete
$this->link_builder->build( $indexable, $post->post_content );
[217] Fix | Delete
// Save indexable to persist the updated link count.
[218] Fix | Delete
$this->indexable_helper->save_indexable( $indexable );
[219] Fix | Delete
$this->updated_indexable( $indexable, $post );
[220] Fix | Delete
}
[221] Fix | Delete
} catch ( Exception $exception ) {
[222] Fix | Delete
$this->logger->log( LogLevel::ERROR, $exception->getMessage() );
[223] Fix | Delete
}
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
/**
[227] Fix | Delete
* Updates the has_public_posts when the post indexable is built.
[228] Fix | Delete
*
[229] Fix | Delete
* @param Indexable $indexable The indexable to check.
[230] Fix | Delete
*
[231] Fix | Delete
* @return void
[232] Fix | Delete
*/
[233] Fix | Delete
protected function update_has_public_posts( $indexable ) {
[234] Fix | Delete
// Update the author indexable's has public posts value.
[235] Fix | Delete
try {
[236] Fix | Delete
$author_indexable = $this->repository->find_by_id_and_type( $indexable->author_id, 'user' );
[237] Fix | Delete
if ( $author_indexable ) {
[238] Fix | Delete
$author_indexable->has_public_posts = $this->author_archive->author_has_public_posts( $author_indexable->object_id );
[239] Fix | Delete
$this->indexable_helper->save_indexable( $author_indexable );
[240] Fix | Delete
[241] Fix | Delete
$this->reschedule_cleanup_if_author_has_no_posts( $author_indexable );
[242] Fix | Delete
}
[243] Fix | Delete
} catch ( Exception $exception ) {
[244] Fix | Delete
$this->logger->log( LogLevel::ERROR, $exception->getMessage() );
[245] Fix | Delete
}
[246] Fix | Delete
[247] Fix | Delete
// Update possible attachment's has public posts value.
[248] Fix | Delete
$this->post->update_has_public_posts_on_attachments( $indexable->object_id, $indexable->is_public );
[249] Fix | Delete
}
[250] Fix | Delete
[251] Fix | Delete
/**
[252] Fix | Delete
* Reschedule indexable cleanup if the author does not have any public posts.
[253] Fix | Delete
* This should remove the author from the indexable table, since we do not
[254] Fix | Delete
* want to store authors without public facing posts in the table.
[255] Fix | Delete
*
[256] Fix | Delete
* @param Indexable $author_indexable The author indexable.
[257] Fix | Delete
*
[258] Fix | Delete
* @return void
[259] Fix | Delete
*/
[260] Fix | Delete
protected function reschedule_cleanup_if_author_has_no_posts( $author_indexable ) {
[261] Fix | Delete
if ( $author_indexable->has_public_posts === false ) {
[262] Fix | Delete
$cleanup_not_yet_scheduled = ! \wp_next_scheduled( Cleanup_Integration::START_HOOK );
[263] Fix | Delete
if ( $cleanup_not_yet_scheduled ) {
[264] Fix | Delete
\wp_schedule_single_event( ( \time() + ( \MINUTE_IN_SECONDS * 5 ) ), Cleanup_Integration::START_HOOK );
[265] Fix | Delete
}
[266] Fix | Delete
}
[267] Fix | Delete
}
[268] Fix | Delete
[269] Fix | Delete
/**
[270] Fix | Delete
* Updates the relations on post save or post status change.
[271] Fix | Delete
*
[272] Fix | Delete
* @param WP_Post $post The post that has been updated.
[273] Fix | Delete
*
[274] Fix | Delete
* @return void
[275] Fix | Delete
*/
[276] Fix | Delete
protected function update_relations( $post ) {
[277] Fix | Delete
$related_indexables = $this->get_related_indexables( $post );
[278] Fix | Delete
[279] Fix | Delete
foreach ( $related_indexables as $indexable ) {
[280] Fix | Delete
// Ignore everything that is not an actual indexable.
[281] Fix | Delete
if ( \is_a( $indexable, Indexable::class ) ) {
[282] Fix | Delete
$indexable->object_last_modified = \max( $indexable->object_last_modified, $post->post_modified_gmt );
[283] Fix | Delete
$this->indexable_helper->save_indexable( $indexable );
[284] Fix | Delete
}
[285] Fix | Delete
}
[286] Fix | Delete
}
[287] Fix | Delete
[288] Fix | Delete
/**
[289] Fix | Delete
* Retrieves the related indexables for given post.
[290] Fix | Delete
*
[291] Fix | Delete
* @param WP_Post $post The post to get the indexables for.
[292] Fix | Delete
*
[293] Fix | Delete
* @return Indexable[] The indexables.
[294] Fix | Delete
*/
[295] Fix | Delete
protected function get_related_indexables( $post ) {
[296] Fix | Delete
/**
[297] Fix | Delete
* The related indexables.
[298] Fix | Delete
*
[299] Fix | Delete
* @var Indexable[] $related_indexables .
[300] Fix | Delete
*/
[301] Fix | Delete
$related_indexables = [];
[302] Fix | Delete
$related_indexables[] = $this->repository->find_by_id_and_type( $post->post_author, 'user', false );
[303] Fix | Delete
$related_indexables[] = $this->repository->find_for_post_type_archive( $post->post_type, false );
[304] Fix | Delete
$related_indexables[] = $this->repository->find_for_home_page( false );
[305] Fix | Delete
[306] Fix | Delete
$taxonomies = \get_post_taxonomies( $post->ID );
[307] Fix | Delete
$taxonomies = \array_filter( $taxonomies, 'is_taxonomy_viewable' );
[308] Fix | Delete
$term_ids = [];
[309] Fix | Delete
foreach ( $taxonomies as $taxonomy ) {
[310] Fix | Delete
$terms = \get_the_terms( $post->ID, $taxonomy );
[311] Fix | Delete
[312] Fix | Delete
if ( empty( $terms ) || \is_wp_error( $terms ) ) {
[313] Fix | Delete
continue;
[314] Fix | Delete
}
[315] Fix | Delete
[316] Fix | Delete
$term_ids = \array_merge( $term_ids, \wp_list_pluck( $terms, 'term_id' ) );
[317] Fix | Delete
}
[318] Fix | Delete
$related_indexables = \array_merge(
[319] Fix | Delete
$related_indexables,
[320] Fix | Delete
$this->repository->find_by_multiple_ids_and_type( $term_ids, 'term', false )
[321] Fix | Delete
);
[322] Fix | Delete
[323] Fix | Delete
return \array_filter( $related_indexables );
[324] Fix | Delete
}
[325] Fix | Delete
[326] Fix | Delete
/**
[327] Fix | Delete
* Tests if the site is multisite and switched.
[328] Fix | Delete
*
[329] Fix | Delete
* @return bool True when the site is multisite and switched
[330] Fix | Delete
*/
[331] Fix | Delete
protected function is_multisite_and_switched() {
[332] Fix | Delete
return \is_multisite() && \ms_is_switched();
[333] Fix | Delete
}
[334] Fix | Delete
}
[335] Fix | Delete
[336] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function