Skip to content

Commit

Permalink
Merge pull request #15 from xylusthemes/added_post_image_delete_support
Browse files Browse the repository at this point in the history
Added Delete Post Featured Image Support
  • Loading branch information
Rajat1192 authored Jan 19, 2024
2 parents 069d57b + 29b6711 commit 9c37eec
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 101 deletions.
12 changes: 11 additions & 1 deletion includes/class-delete-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function get_delete_posts_ids( $data = array() ) {
$delete_end_date = isset( $data['delete_end_date'] ) ? esc_sql( $data['delete_end_date'] ) : '';
$delete_authors = isset( $data['delete_authors'] ) ? array_map( 'intval', $data['delete_authors'] ) : array();
$delete_type = isset( $data['delete_type'] ) ? $data['delete_type'] : 'trash';
$post_media = isset( $data['post_media'] ) ? $data['post_media'] : 'no';
$limit_post = !empty( $data['limit_post'] ) ? absint( $data['limit_post'] ) : '10000';
$date_type = isset( $data['date_type'] ) ? esc_sql( $data['date_type'] ) : 'custom_date';
$input_days = isset( $data['input_days'] ) ? esc_sql( $data['input_days'] ) : '';
Expand Down Expand Up @@ -159,7 +160,7 @@ public function get_delete_posts_ids( $data = array() ) {
* @param array $data Posts Id.
* @return array | deleted posts count.
*/
public function do_delete_posts( $post_ids = array(), $force_delete = false, $custom_query = null ) {
public function do_delete_posts( $post_ids = array(), $force_delete = false, $item = array(), $custom_query = null ) {
global $wpdb;
$post_delete_count = 0;

Expand All @@ -170,6 +171,15 @@ public function do_delete_posts( $post_ids = array(), $force_delete = false, $cu
}

if( ! empty( $post_ids ) && count( $post_ids ) > 0 ) {

$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 );
}
}

if( $custom_query == 'custom_query' ){
$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})" );
Expand Down
31 changes: 29 additions & 2 deletions includes/delele-posts-form-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ function xt_delete_posts_form_process( $data ) {
if ( $data['delete_type'] === 'permenant' ) {
$force_delete = true;
}

$post_count = wpbulkdelete()->api->do_delete_posts( $post_ids, $force_delete, $custom_query );
$post_count = wpbulkdelete()->api->do_delete_posts( $post_ids, $force_delete, $data, $custom_query );
return array(
'status' => 1,
'messages' => array( sprintf( esc_html__( '%d Record deleted successfully.', 'wp-bulk-delete' ), $post_count)
Expand Down Expand Up @@ -762,6 +761,8 @@ function wpbd_render_common_form(){

wpbd_render_form_delete_type();

wpbd_render_form_delete_media();

wpbd_render_limit_post();

wpbd_render_delete_time();
Expand All @@ -784,4 +785,30 @@ function wpbd_get_timezone_string() {
$tz_offset = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins );

return $tz_offset;
}

/**
* Render Delete Post Media.
*
* @since 1.0
* @return void
*/
function wpbd_render_form_delete_media(){
?>
<tr>
<th scope="row">
<?php _e('Delete Post Featured image :','wp-bulk-delete'); ?>
</th>
<?php if( wpbd_is_pro() ){ ?>
<td>
<input type="checkbox" id="post_media" name="post_media" class="post_media" value="yes" />
<?php _e( 'It enables the removal of the featured image of the post, if the image is a featured image of multiple posts, it will not be removed. and If the image is being used in a place other than the featured image, it will be deleted.', 'wp-bulk-delete' ); ?>
</td>
<?php }else{ ?>
<td>
<?php do_action( 'wpbd_display_available_in_pro'); ?>
</td>
<?php } ?>
</tr>
<?php
}
Loading

0 comments on commit 9c37eec

Please sign in to comment.