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

Warning: Undefined array key "page_file_edit_line" in /home/sportsfever/public_html/filemanger/edit_text_line.php on line 32
/home/sportsfe.../httpdocs/clone/wp-conte.../plugins/wordpres.../src/reposito...
File: indexable-cleanup-repository.php
* @param int $limit The limit we'll apply to the delete query.
[500] Fix | Delete
*
[501] Fix | Delete
* @return int|bool The number of rows that was deleted or false if the query failed.
[502] Fix | Delete
*/
[503] Fix | Delete
public function clean_indexables_for_orphaned_users( $limit ) {
[504] Fix | Delete
global $wpdb;
[505] Fix | Delete
[506] Fix | Delete
$indexable_table = Model::get_table_name( 'Indexable' );
[507] Fix | Delete
$source_table = $wpdb->users;
[508] Fix | Delete
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Reason: There is no unescaped user input.
[509] Fix | Delete
$query = $wpdb->prepare(
[510] Fix | Delete
"
[511] Fix | Delete
SELECT indexable_table.object_id
[512] Fix | Delete
FROM {$indexable_table} indexable_table
[513] Fix | Delete
LEFT JOIN {$source_table} AS source_table
[514] Fix | Delete
ON indexable_table.object_id = source_table.ID
[515] Fix | Delete
WHERE source_table.ID IS NULL
[516] Fix | Delete
AND indexable_table.object_id IS NOT NULL
[517] Fix | Delete
AND indexable_table.object_type = 'user'
[518] Fix | Delete
LIMIT %d",
[519] Fix | Delete
$limit
[520] Fix | Delete
);
[521] Fix | Delete
// phpcs:enable
[522] Fix | Delete
[523] Fix | Delete
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: Already prepared.
[524] Fix | Delete
$orphans = $wpdb->get_col( $query );
[525] Fix | Delete
[526] Fix | Delete
if ( empty( $orphans ) ) {
[527] Fix | Delete
return 0;
[528] Fix | Delete
}
[529] Fix | Delete
[530] Fix | Delete
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: Already prepared.
[531] Fix | Delete
return $wpdb->query( "DELETE FROM $indexable_table WHERE object_type = 'user' AND object_id IN( " . \implode( ',', $orphans ) . ' )' );
[532] Fix | Delete
}
[533] Fix | Delete
[534] Fix | Delete
/**
[535] Fix | Delete
* Counts indexables for given source table + source identifier + object type.
[536] Fix | Delete
*
[537] Fix | Delete
* @param string $source_table The source table.
[538] Fix | Delete
* @param string $source_identifier The source identifier.
[539] Fix | Delete
* @param string $object_type The object type.
[540] Fix | Delete
*
[541] Fix | Delete
* @return mixed
[542] Fix | Delete
*/
[543] Fix | Delete
public function count_indexables_for_object_type_and_source_table( string $source_table, string $source_identifier, string $object_type ) {
[544] Fix | Delete
global $wpdb;
[545] Fix | Delete
$indexable_table = Model::get_table_name( 'Indexable' );
[546] Fix | Delete
$source_table = $wpdb->prefix . $source_table;
[547] Fix | Delete
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: Already prepared.
[548] Fix | Delete
return $wpdb->get_col(
[549] Fix | Delete
"
[550] Fix | Delete
SELECT count(*)
[551] Fix | Delete
FROM {$indexable_table} indexable_table
[552] Fix | Delete
LEFT JOIN {$source_table} AS source_table
[553] Fix | Delete
ON indexable_table.object_id = source_table.{$source_identifier}
[554] Fix | Delete
WHERE source_table.{$source_identifier} IS NULL
[555] Fix | Delete
AND indexable_table.object_id IS NOT NULL
[556] Fix | Delete
AND indexable_table.object_type = '{$object_type}'"
[557] Fix | Delete
)[0];
[558] Fix | Delete
// phpcs:enable
[559] Fix | Delete
}
[560] Fix | Delete
[561] Fix | Delete
/**
[562] Fix | Delete
* Counts indexables for orphaned users.
[563] Fix | Delete
*
[564] Fix | Delete
* @return mixed
[565] Fix | Delete
*/
[566] Fix | Delete
public function count_indexables_for_orphaned_users() {
[567] Fix | Delete
global $wpdb;
[568] Fix | Delete
$indexable_table = Model::get_table_name( 'Indexable' );
[569] Fix | Delete
$source_table = $wpdb->users;
[570] Fix | Delete
//phpcs:disable WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: Already prepared.
[571] Fix | Delete
return $wpdb->get_col(
[572] Fix | Delete
"
[573] Fix | Delete
SELECT count(*)
[574] Fix | Delete
FROM {$indexable_table} indexable_table
[575] Fix | Delete
LEFT JOIN {$source_table} AS source_table
[576] Fix | Delete
ON indexable_table.object_id = source_table.ID
[577] Fix | Delete
WHERE source_table.ID IS NULL
[578] Fix | Delete
AND indexable_table.object_id IS NOT NULL
[579] Fix | Delete
AND indexable_table.object_type = 'user'"
[580] Fix | Delete
)[0];
[581] Fix | Delete
// phpcs:enable
[582] Fix | Delete
}
[583] Fix | Delete
[584] Fix | Delete
/**
[585] Fix | Delete
* Cleans orphaned rows from a yoast table.
[586] Fix | Delete
*
[587] Fix | Delete
* @param string $table The table to clean up.
[588] Fix | Delete
* @param string $column The table column the cleanup will rely on.
[589] Fix | Delete
* @param int $limit The limit we'll apply to the queries.
[590] Fix | Delete
*
[591] Fix | Delete
* @return int|bool The number of deleted rows, false if the query fails.
[592] Fix | Delete
*/
[593] Fix | Delete
public function cleanup_orphaned_from_table( $table, $column, $limit ) {
[594] Fix | Delete
global $wpdb;
[595] Fix | Delete
[596] Fix | Delete
$table = Model::get_table_name( $table );
[597] Fix | Delete
$indexable_table = Model::get_table_name( 'Indexable' );
[598] Fix | Delete
[599] Fix | Delete
// Warning: If this query is changed, make sure to update the query in cleanup_orphaned_from_table in Premium as well.
[600] Fix | Delete
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Reason: There is no unescaped user input.
[601] Fix | Delete
$query = $wpdb->prepare(
[602] Fix | Delete
"
[603] Fix | Delete
SELECT table_to_clean.{$column}
[604] Fix | Delete
FROM {$table} table_to_clean
[605] Fix | Delete
LEFT JOIN {$indexable_table} AS indexable_table
[606] Fix | Delete
ON table_to_clean.{$column} = indexable_table.id
[607] Fix | Delete
WHERE indexable_table.id IS NULL
[608] Fix | Delete
AND table_to_clean.{$column} IS NOT NULL
[609] Fix | Delete
LIMIT %d",
[610] Fix | Delete
$limit
[611] Fix | Delete
);
[612] Fix | Delete
// phpcs:enable
[613] Fix | Delete
[614] Fix | Delete
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: Already prepared.
[615] Fix | Delete
$orphans = $wpdb->get_col( $query );
[616] Fix | Delete
[617] Fix | Delete
if ( empty( $orphans ) ) {
[618] Fix | Delete
return 0;
[619] Fix | Delete
}
[620] Fix | Delete
[621] Fix | Delete
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: Already prepared.
[622] Fix | Delete
return $wpdb->query( "DELETE FROM $table WHERE {$column} IN( " . \implode( ',', $orphans ) . ' )' );
[623] Fix | Delete
}
[624] Fix | Delete
[625] Fix | Delete
/**
[626] Fix | Delete
* Counts orphaned rows from a yoast table.
[627] Fix | Delete
*
[628] Fix | Delete
* @param string $table The table to clean up.
[629] Fix | Delete
* @param string $column The table column the cleanup will rely on.
[630] Fix | Delete
*
[631] Fix | Delete
* @return int|bool The number of deleted rows, false if the query fails.
[632] Fix | Delete
*/
[633] Fix | Delete
public function count_orphaned_from_table( string $table, string $column ) {
[634] Fix | Delete
global $wpdb;
[635] Fix | Delete
[636] Fix | Delete
$table = Model::get_table_name( $table );
[637] Fix | Delete
$indexable_table = Model::get_table_name( 'Indexable' );
[638] Fix | Delete
[639] Fix | Delete
// Warning: If this query is changed, make sure to update the query in cleanup_orphaned_from_table in Premium as well.
[640] Fix | Delete
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: Already prepared.
[641] Fix | Delete
return $wpdb->get_col(
[642] Fix | Delete
"
[643] Fix | Delete
SELECT count(*)
[644] Fix | Delete
FROM {$table} table_to_clean
[645] Fix | Delete
LEFT JOIN {$indexable_table} AS indexable_table
[646] Fix | Delete
ON table_to_clean.{$column} = indexable_table.id
[647] Fix | Delete
WHERE indexable_table.id IS NULL
[648] Fix | Delete
AND table_to_clean.{$column} IS NOT NULL"
[649] Fix | Delete
)[0];
[650] Fix | Delete
// phpcs:enable
[651] Fix | Delete
}
[652] Fix | Delete
[653] Fix | Delete
/**
[654] Fix | Delete
* Updates the author_id of indexables which author_id is not in the wp_users table with the id of the reassingned
[655] Fix | Delete
* user.
[656] Fix | Delete
*
[657] Fix | Delete
* @param int $limit The limit we'll apply to the queries.
[658] Fix | Delete
*
[659] Fix | Delete
* @return int|bool The number of updated rows, false if query to get data fails.
[660] Fix | Delete
*/
[661] Fix | Delete
public function update_indexables_author_to_reassigned( $limit ) {
[662] Fix | Delete
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: Already prepared.
[663] Fix | Delete
$reassigned_authors_objs = $this->get_reassigned_authors( $limit );
[664] Fix | Delete
[665] Fix | Delete
if ( $reassigned_authors_objs === false ) {
[666] Fix | Delete
return false;
[667] Fix | Delete
}
[668] Fix | Delete
[669] Fix | Delete
return $this->update_indexable_authors( $reassigned_authors_objs, $limit );
[670] Fix | Delete
}
[671] Fix | Delete
[672] Fix | Delete
/**
[673] Fix | Delete
* Fetches pairs of old_id -> new_id indexed by old_id.
[674] Fix | Delete
* By using the old_id (i.e. the id of the user that has been deleted) as key of the associative array, we can
[675] Fix | Delete
* easily compose an array of unique pairs of old_id -> new_id.
[676] Fix | Delete
*
[677] Fix | Delete
* @param int $limit The limit we'll apply to the queries.
[678] Fix | Delete
*
[679] Fix | Delete
* @return int|bool The associative array with shape [ old_id => [ old_id, new_author ] ] or false if query to get
[680] Fix | Delete
* data fails.
[681] Fix | Delete
*/
[682] Fix | Delete
private function get_reassigned_authors( $limit ) {
[683] Fix | Delete
global $wpdb;
[684] Fix | Delete
[685] Fix | Delete
$indexable_table = Model::get_table_name( 'Indexable' );
[686] Fix | Delete
$posts_table = $wpdb->posts;
[687] Fix | Delete
[688] Fix | Delete
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Reason: There is no unescaped user input.
[689] Fix | Delete
$query = $wpdb->prepare(
[690] Fix | Delete
"
[691] Fix | Delete
SELECT {$indexable_table}.author_id, {$posts_table}.post_author
[692] Fix | Delete
FROM {$indexable_table} JOIN {$posts_table} on {$indexable_table}.object_id = {$posts_table}.id
[693] Fix | Delete
WHERE object_type='post'
[694] Fix | Delete
AND {$indexable_table}.author_id <> {$posts_table}.post_author
[695] Fix | Delete
ORDER BY {$indexable_table}.author_id
[696] Fix | Delete
LIMIT %d",
[697] Fix | Delete
$limit
[698] Fix | Delete
);
[699] Fix | Delete
// phpcs:enable
[700] Fix | Delete
[701] Fix | Delete
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: Already prepared.
[702] Fix | Delete
return $wpdb->get_results( $query, \OBJECT_K );
[703] Fix | Delete
}
[704] Fix | Delete
[705] Fix | Delete
/**
[706] Fix | Delete
* Updates the indexable's author_id referring to a deleted author with the id of the reassigned user.
[707] Fix | Delete
*
[708] Fix | Delete
* @param array $reassigned_authors_objs The array of objects with shape [ old_id => [ old_id, new_id ] ].
[709] Fix | Delete
* @param int $limit The limit we'll apply to the queries.
[710] Fix | Delete
*
[711] Fix | Delete
* @return int|bool The associative array with shape [ old_id => [ old_id, new_author ] ] or false if query to get
[712] Fix | Delete
* data fails.
[713] Fix | Delete
*/
[714] Fix | Delete
private function update_indexable_authors( $reassigned_authors_objs, $limit ) {
[715] Fix | Delete
global $wpdb;
[716] Fix | Delete
[717] Fix | Delete
$indexable_table = Model::get_table_name( 'Indexable' );
[718] Fix | Delete
[719] Fix | Delete
// This is a workaround for the fact that the array_column function does not work on objects in PHP 5.6.
[720] Fix | Delete
$reassigned_authors_array = \array_map(
[721] Fix | Delete
static function ( $obj ) {
[722] Fix | Delete
return (array) $obj;
[723] Fix | Delete
},
[724] Fix | Delete
$reassigned_authors_objs
[725] Fix | Delete
);
[726] Fix | Delete
[727] Fix | Delete
$reassigned_authors = \array_combine( \array_column( $reassigned_authors_array, 'author_id' ), \array_column( $reassigned_authors_array, 'post_author' ) );
[728] Fix | Delete
[729] Fix | Delete
foreach ( $reassigned_authors as $old_author_id => $new_author_id ) {
[730] Fix | Delete
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Reason: There is no unescaped user input.
[731] Fix | Delete
$query = $wpdb->prepare(
[732] Fix | Delete
"
[733] Fix | Delete
UPDATE {$indexable_table}
[734] Fix | Delete
SET {$indexable_table}.author_id = {$new_author_id}
[735] Fix | Delete
WHERE {$indexable_table}.author_id = {$old_author_id}
[736] Fix | Delete
AND object_type='post'
[737] Fix | Delete
LIMIT %d",
[738] Fix | Delete
$limit
[739] Fix | Delete
);
[740] Fix | Delete
// phpcs:enable
[741] Fix | Delete
[742] Fix | Delete
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: Already prepared.
[743] Fix | Delete
$wpdb->query( $query );
[744] Fix | Delete
}
[745] Fix | Delete
[746] Fix | Delete
return \count( $reassigned_authors );
[747] Fix | Delete
}
[748] Fix | Delete
}
[749] Fix | Delete
[750] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function