Skip to content

Commit

Permalink
Merge pull request #49 from xylusthemes/image_query_optimized
Browse files Browse the repository at this point in the history
Post image delete query optimized
  • Loading branch information
Rajat1192 authored Apr 30, 2024
2 parents 64835d1 + e373944 commit c7d5753
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 27 deletions.
60 changes: 50 additions & 10 deletions includes/class-delete-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ){
Expand Down
34 changes: 17 additions & 17 deletions languages/wp-bulk-delete.pot
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 ""

Expand Down

0 comments on commit c7d5753

Please sign in to comment.