| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
use Yoast\WP\Lib\Model; |
| 6 |
|
| 7 |
/** |
| 8 |
* A helper object for the cleanup of attachments. |
| 9 |
*/ |
| 10 |
class Attachment_Cleanup_Helper { |
| 11 |
|
| 12 |
/** |
| 13 |
* Removes all indexables for attachments. |
| 14 |
* |
| 15 |
* @param bool $suppress_errors Whether to suppress db errors when running the cleanup query. |
| 16 |
* |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
public function remove_attachment_indexables( $suppress_errors ) { |
| 20 |
global $wpdb; |
| 21 |
|
| 22 |
if ( $suppress_errors ) { |
| 23 |
// If migrations haven't been completed successfully the following may give false errors. So suppress them. |
| 24 |
$show_errors = $wpdb->show_errors; |
| 25 |
$wpdb->show_errors = false; |
| 26 |
} |
| 27 |
|
| 28 |
$indexable_table = Model::get_table_name( 'Indexable' ); |
| 29 |
|
| 30 |
$delete_query = "DELETE FROM $indexable_table WHERE object_type = 'post' AND object_sub_type = 'attachment'"; |
| 31 |
|
| 32 |
// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: No relevant caches. |
| 33 |
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery -- Reason: Most performant way. |
| 34 |
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared -- Reason: Is it prepared already. |
| 35 |
$wpdb->query( $delete_query ); |
| 36 |
// phpcs:enable |
| 37 |
|
| 38 |
if ( $suppress_errors ) { |
| 39 |
$wpdb->show_errors = $show_errors; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Cleans all attachment links in the links table from target indexable ids. |
| 45 |
* |
| 46 |
* @param bool $suppress_errors Whether to suppress db errors when running the cleanup query. |
| 47 |
* |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function clean_attachment_links_from_target_indexable_ids( $suppress_errors ) { |
| 51 |
global $wpdb; |
| 52 |
|
| 53 |
if ( $suppress_errors ) { |
| 54 |
// If migrations haven't been completed successfully the following may give false errors. So suppress them. |
| 55 |
$show_errors = $wpdb->show_errors; |
| 56 |
$wpdb->show_errors = false; |
| 57 |
} |
| 58 |
|
| 59 |
$links_table = Model::get_table_name( 'SEO_Links' ); |
| 60 |
|
| 61 |
$query = "UPDATE $links_table SET target_indexable_id = NULL WHERE type = 'image-in'"; |
| 62 |
|
| 63 |
// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: No relevant caches. |
| 64 |
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery -- Reason: Most performant way. |
| 65 |
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared -- Reason: Is it prepared already. |
| 66 |
$wpdb->query( $query ); |
| 67 |
// phpcs:enable |
| 68 |
|
| 69 |
if ( $suppress_errors ) { |
| 70 |
$wpdb->show_errors = $show_errors; |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|