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 ){ diff --git a/languages/wp-bulk-delete.pot b/languages/wp-bulk-delete.pot index 7f4f3f1..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-15T12:46:14+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" @@ -327,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 ""