ensure_permissions(); check_ajax_referer( 'adbear_delete_nonce', 'nonce' ); $attachment_id = isset( $_POST['attachment_id'] ) ? absint( wp_unslash( $_POST['attachment_id'] ) ) : 0; $status = isset( $_POST['status'] ) ? sanitize_key( wp_unslash( $_POST['status'] ) ) : ''; if ( ! $attachment_id ) { wp_send_json_error( array( 'message' => __( 'Invalid attachment ID.', 'adbear-image-cleaner' ), ), 400 ); } if ( ! in_array( $status, Adbear_Image_Cleaner_Helpers::get_deletable_statuses(), true ) ) { wp_send_json_error( array( 'message' => __( 'Only unused or broken images can be trashed.', 'adbear-image-cleaner' ), ), 400 ); } $result = $this->trash_attachment( $attachment_id, $status ); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message(), ), 500 ); } wp_send_json_success( array( 'message' => __( 'Image moved to trash.', 'adbear-image-cleaner' ), 'id' => $attachment_id, ) ); } /** * Bulk move attachments to trash safely. * * @return void */ public function ajax_bulk_safe_delete_attachments() { $this->ensure_permissions(); check_ajax_referer( 'adbear_delete_nonce', 'nonce' ); $raw_items = filter_input( INPUT_POST, 'items', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); $raw_items = is_array( $raw_items ) ? wp_unslash( $raw_items ) : array(); if ( ! is_array( $raw_items ) || empty( $raw_items ) ) { wp_send_json_error( array( 'message' => __( 'No items selected.', 'adbear-image-cleaner' ), ), 400 ); } $deleted_ids = array(); $failed = array(); $items = array(); foreach ( $raw_items as $raw_item ) { if ( ! is_array( $raw_item ) ) { continue; } $items[] = array( 'id' => isset( $raw_item['id'] ) ? absint( $raw_item['id'] ) : 0, 'status' => isset( $raw_item['status'] ) ? sanitize_key( $raw_item['status'] ) : '', ); } foreach ( $items as $item ) { if ( ! is_array( $item ) ) { continue; } $attachment_id = isset( $item['id'] ) ? absint( $item['id'] ) : 0; $status = isset( $item['status'] ) ? sanitize_key( $item['status'] ) : ''; if ( ! $attachment_id || ! in_array( $status, Adbear_Image_Cleaner_Helpers::get_deletable_statuses(), true ) ) { continue; } $result = $this->trash_attachment( $attachment_id, $status ); if ( is_wp_error( $result ) ) { $failed[] = array( 'id' => $attachment_id, 'message' => $result->get_error_message(), ); continue; } $deleted_ids[] = $attachment_id; } wp_send_json_success( array( 'deleted_ids' => $deleted_ids, 'failed' => $failed, 'message' => sprintf( /* translators: %d: number of deleted images */ __( '%d images moved to trash.', 'adbear-image-cleaner' ), count( $deleted_ids ) ), ) ); } /** * Safely move one attachment to trash. * * @param int $attachment_id Attachment ID. * @param string $status Current scan status. * @return true|WP_Error */ private function trash_attachment( $attachment_id, $status ) { if ( ! in_array( $status, Adbear_Image_Cleaner_Helpers::get_deletable_statuses(), true ) ) { return new WP_Error( 'adbear_invalid_status', __( 'Only unused or broken images can be trashed.', 'adbear-image-cleaner' ) ); } $post = get_post( $attachment_id ); if ( ! $post || 'attachment' !== $post->post_type ) { return new WP_Error( 'adbear_attachment_missing', __( 'Attachment not found.', 'adbear-image-cleaner' ) ); } $trashed = wp_trash_post( $attachment_id ); if ( ! $trashed ) { return new WP_Error( 'adbear_trash_failed', __( 'Image could not be moved to trash.', 'adbear-image-cleaner' ) ); } return true; } /** * Ensure user permissions. * * @return void */ private function ensure_permissions() { if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'You are not allowed to perform this action.', 'adbear-image-cleaner' ), ), 403 ); } } }