From 821ce3ed01f352146106e596985898e0344c519d Mon Sep 17 00:00:00 2001 From: Rajat Patel Date: Fri, 26 Apr 2024 14:56:13 +0530 Subject: [PATCH 1/3] Post image delete query optimized --- includes/class-delete-api.php | 60 +++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/includes/class-delete-api.php b/includes/class-delete-api.php index 843a632..faa0ab7 100644 --- a/includes/class-delete-api.php +++ b/includes/class-delete-api.php @@ -175,23 +175,63 @@ public function do_delete_posts( $post_ids = array(), $force_delete = false, $it if( $custom_query == 'custom_query' ){ foreach( $post_ids as $post_id ){ - $post_attechment_id = get_post_meta( $post_id, '_thumbnail_id', true ); - $attechment_ids = $wpdb->get_col( "SELECT post_id FROM $wpdb->postmeta WHERE meta_value = $post_attechment_id" ); if( isset( $item['post_media'] ) && $item['post_media'] === 'yes' ){ - if( count( $attechment_ids ) <= 1 ){ - wp_delete_attachment( $post_attechment_id, $force_delete ); + $post_attachment_id = get_post_meta( $post_id, '_thumbnail_id', true ); + if( !empty( $post_attachment_id ) ){ + $attachment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_value = %d", $post_attachment_id ) ); + if( !empty( $attachment_ids ) && count( $attachment_ids ) <= 1 ){ + + $attachment_metadata = wp_get_attachment_metadata( $post_attachment_id ); + if ( !empty($attachment_metadata['sizes'] ) ) { + //Getting file path + $upload_dir = wp_upload_dir(); + $file_path = $upload_dir['basedir'] . '/' . dirname( $attachment_metadata['file'] ) . '/'; + + //Removing all image sizes + foreach( $attachment_metadata['sizes'] as $size_info ){ + $file = $file_path . $size_info['file']; + //file check and remove it + if ( file_exists( $file ) ) { + unlink( $file ); + } + } + } + + if ( !empty( $attachment_metadata['file'] ) ) { + $file_path = $upload_dir['basedir'] . '/' . $attachment_metadata['file']; + //file check and remove it + if (file_exists($file_path)) { + unlink($file_path); + } + } + + $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->posts} WHERE ID = %d", $post_attachment_id ) ); + $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE post_id = %d", $post_attachment_id ) ); + } } } } - $all_posts = implode( ",",$post_ids ); - $wpdb->query( "DELETE p,pt,pm FROM " . $wpdb->posts . " p LEFT JOIN " . $wpdb->term_relationships . " pt ON pt.object_id = p.ID LEFT JOIN " . $wpdb->postmeta . " pm ON pm.post_id = p.ID WHERE p.ID IN ({$all_posts})" ); + + $post_ids_sanitized = array_map( 'intval', $post_ids ); + $placeholders = implode( ',', array_fill( 0, count( $post_ids_sanitized ), '%d' ) ); + $query = $wpdb->prepare( + "DELETE p, pt, pm FROM {$wpdb->posts} p + LEFT JOIN {$wpdb->term_relationships} pt ON pt.object_id = p.ID + LEFT JOIN {$wpdb->postmeta} pm ON pm.post_id = p.ID + WHERE p.ID IN ( $placeholders )", + $post_ids_sanitized + ); + $wpdb->query( $query ); + }else{ foreach ($post_ids as $post_id ){ - $post_attechment_id = get_post_meta( $post_id, '_thumbnail_id', true ); - $attechment_ids = $wpdb->get_col( "SELECT post_id FROM $wpdb->postmeta WHERE meta_value = $post_attechment_id" ); if( isset( $item['post_media'] ) && $item['post_media'] === 'yes' ){ - if( count( $attechment_ids ) <= 1 ){ - wp_delete_attachment( $post_attechment_id, $force_delete ); + $post_attachment_id = get_post_meta( $post_id, '_thumbnail_id', true ); + if( !empty( $post_attachment_id ) ){ + $attachment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_value = %d", $post_attachment_id ) ); + if( count( $attachment_ids ) <= 1 ){ + wp_delete_attachment( $post_attachment_id, $force_delete ); + } } } if( $force_delete === false ){ From 83fe81395ccf0ae265f5b668ffd38dbf3b0880c0 Mon Sep 17 00:00:00 2001 From: "WordPress .pot File Generator" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 26 Apr 2024 09:27:53 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=94=84=20Generated=20POT=20File?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- languages/wp-bulk-delete.pot | 41 ++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/languages/wp-bulk-delete.pot b/languages/wp-bulk-delete.pot index f7d3d5c..2ab73c4 100644 --- a/languages/wp-bulk-delete.pot +++ b/languages/wp-bulk-delete.pot @@ -9,30 +9,35 @@ msgstr "" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -"POT-Creation-Date: 2024-01-22T06:17:56+00:00\n" +"POT-Creation-Date: 2024-04-26T09:27:53+00:00\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" -"X-Generator: WP-CLI 2.9.0\n" +"X-Generator: WP-CLI 2.10.0\n" "X-Domain: wp-bulk-delete\n" #. Plugin Name of the plugin +#: wp-bulk-delete.php #: includes/admin/admin-pages.php:23 #: includes/class-wpbd-plugin-deactivation.php:38 msgid "WP Bulk Delete" msgstr "" #. Plugin URI of the plugin +#: wp-bulk-delete.php msgid "http://xylusthemes.com/plugins/wp-bulk-delete/" msgstr "" #. Description of the plugin +#: wp-bulk-delete.php msgid "Bulk delete and cleanup anything like posts, comments, users, meta fields, taxonomy terms. with powerful filter options." msgstr "" #. Author of the plugin +#: wp-bulk-delete.php msgid "Xylus Themes" msgstr "" #. Author URI of the plugin +#: wp-bulk-delete.php msgid "http://xylusthemes.com" msgstr "" @@ -322,67 +327,67 @@ msgstr "" msgid "Please select required fields for proceed delete operation." msgstr "" -#: includes/class-delete-api.php:335 +#: includes/class-delete-api.php:375 msgid "%s Revisions Cleaned up" msgstr "" -#: includes/class-delete-api.php:345 +#: includes/class-delete-api.php:385 msgid "%s Auto Drafts Cleaned up" msgstr "" -#: includes/class-delete-api.php:355 +#: includes/class-delete-api.php:395 msgid "%s Trashed Posts Cleaned up" msgstr "" -#: includes/class-delete-api.php:371 +#: includes/class-delete-api.php:411 msgid "%s Orphaned Post Meta Cleaned up" msgstr "" -#: includes/class-delete-api.php:386 +#: includes/class-delete-api.php:426 msgid "%s Orphaned Comment Meta Cleaned up" msgstr "" -#: includes/class-delete-api.php:401 +#: includes/class-delete-api.php:441 msgid "%s Orphaned User Meta Cleaned up" msgstr "" -#: includes/class-delete-api.php:416 +#: includes/class-delete-api.php:456 msgid "%s Orphaned Term Meta Cleaned up" msgstr "" -#: includes/class-delete-api.php:429 +#: includes/class-delete-api.php:469 msgid "%s Duplicated Post Meta Cleaned up" msgstr "" -#: includes/class-delete-api.php:441 +#: includes/class-delete-api.php:481 msgid "%s Duplicated Comment Meta Cleaned up" msgstr "" -#: includes/class-delete-api.php:453 +#: includes/class-delete-api.php:493 msgid "%s Duplicated User Meta Cleaned up" msgstr "" -#: includes/class-delete-api.php:465 +#: includes/class-delete-api.php:505 msgid "%s Duplicated Term Meta Cleaned up" msgstr "" -#: includes/class-delete-api.php:1316 +#: includes/class-delete-api.php:1356 msgid "WP Event Aggregator" msgstr "" -#: includes/class-delete-api.php:1317 +#: includes/class-delete-api.php:1357 msgid "Import Facebook Events" msgstr "" -#: includes/class-delete-api.php:1318 +#: includes/class-delete-api.php:1358 msgid "Import Eventbrite Events" msgstr "" -#: includes/class-delete-api.php:1319 +#: includes/class-delete-api.php:1359 msgid "Import Meetup Events" msgstr "" -#: includes/class-delete-api.php:1320 +#: includes/class-delete-api.php:1360 msgid "Event Schema / Structured Data" msgstr "" From e373944a079951eb13ce6e3850fe62287bde12ee Mon Sep 17 00:00:00 2001 From: "WordPress .pot File Generator" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 30 Apr 2024 09:17:26 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=94=84=20Generated=20POT=20File?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- languages/wp-bulk-delete.pot | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/languages/wp-bulk-delete.pot b/languages/wp-bulk-delete.pot index 2ab73c4..bc29ee1 100644 --- a/languages/wp-bulk-delete.pot +++ b/languages/wp-bulk-delete.pot @@ -9,7 +9,7 @@ msgstr "" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -"POT-Creation-Date: 2024-04-26T09:27:53+00:00\n" +"POT-Creation-Date: 2024-04-30T09:17:26+00:00\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "X-Generator: WP-CLI 2.10.0\n" "X-Domain: wp-bulk-delete\n"