Home / Admin / Delete Pending Donations
Duplicate Snippet

Embed Snippet on Your Site

Delete Pending Donations

This script properly deletes all pending donations using WordPress functions ensuring all hooks fire and related data is cleaned up.

Code Preview
php
<?php
<?php
/**
 * Delete All Pending Donations in Charitable
 * 
 * IMPORTANT: BACKUP YOUR DATABASE BEFORE RUNNING THIS!
 * 
 * This script properly deletes all pending donations using WordPress functions,
 * ensuring all hooks fire and related data is cleaned up.
 * 
 * Usage:
 * 1. Upload this file to your wp-content directory
 * 2. Access it via: https://yoursite.com/wp-content/delete-pending-donations.php
 * 3. Delete this file immediately after use for security
 * 
 * Or use via WP-CLI:
 * wp eval-file wp-content/delete-pending-donations.php
 */
// Load WordPress
require_once( dirname( __FILE__ ) . '/../wp-load.php' );
// Security check - only allow administrators
if ( ! current_user_can( 'manage_options' ) ) {
    // If running via command line, allow it
    if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
        wp_die( 'You do not have permission to access this page.' );
    }
}
// Check if Charitable is active
if ( ! class_exists( 'Charitable' ) ) {
    wp_die( 'Charitable plugin is not active.' );
}
/**
 * Delete all pending donations
 */
function charitable_delete_all_pending_donations() {
    global $wpdb;
    
    // Get all pending donation IDs
    // Note: Charitable uses 'charitable-pending' status, NOT 'pending'
    $donation_ids = $wpdb->get_col( $wpdb->prepare(
        "SELECT ID FROM {$wpdb->posts} 
         WHERE post_type = %s 
         AND post_status = %s",
        'donation',
        'charitable-pending'
    ) );
    
    if ( empty( $donation_ids ) ) {
        return array(
            'success' => true,
            'count' => 0,
            'message' => 'No pending donations found.'
        );
    }
    
    $deleted = 0;
    $failed = 0;
    $errors = array();
    
    foreach ( $donation_ids as $donation_id ) {
        // wp_delete_post() triggers all WordPress hooks including:
        // - deleted_post hook which calls Charitable_Campaign_Donations_DB::delete_donation_records()
        // - This ensures charitable_campaign_donations table is cleaned up
        $result = wp_delete_post( $donation_id, true );
        
        if ( $result ) {
            $deleted++;
        } else {
            $failed++;
            $errors[] = "Failed to delete donation ID: {$donation_id}";
        }
    }
    
    // Clear any transients/cache
    wp_cache_flush();
    
    return array(
        'success' => true,
        'total' => count( $donation_ids ),
        'deleted' => $deleted,
        'failed' => $failed,
        'errors' => $errors,
        'message' => sprintf(
            'Deleted %d of %d pending donations.',
            $deleted,
            count( $donation_ids )
        )
    );
}
// If running via web browser
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
    // Check for confirmation
    if ( ! isset( $_GET['confirm'] ) || 'yes' !== $_GET['confirm'] ) {
        ?>
        <!DOCTYPE html>
        <html>
        <head>
            <title>Delete Pending Donations</title>
            <style>
                body { font-family: Arial, sans-serif; max-width: 800px; margin: 50px auto; padding: 20px; }
                .warning { background: #fff3cd; border: 1px solid #ffc107; padding: 15px; margin: 20px 0; border-radius: 4px; }
                .error { background: #f8d7da; border: 1px solid #dc3545; padding: 15px; margin: 20px 0; border-radius: 4px; }
                .success { background: #d4edda; border: 1px solid #28a745; padding: 15px; margin: 20px 0; border-radius: 4px; }
                .button { display: inline-block; padding: 10px 20px; background: #dc3545; color: white; text-decoration: none; border-radius: 4px; margin: 10px 5px; }
                .button:hover { background: #c82333; }
                .button-secondary { background: #6c757d; }
                .button-secondary:hover { background: #5a6268; }
            </style>
        </head>
        <body>
            <h1>Delete All Pending Donations</h1>
            
            <div class="warning">
                <strong>⚠️ WARNING:</strong> This will permanently delete ALL pending donations. 
                This action cannot be undone. Please ensure you have backed up your database.
            </div>
            
            <?php
            global $wpdb;
            $count = $wpdb->get_var( $wpdb->prepare(
                "SELECT COUNT(*) FROM {$wpdb->posts} 
                 WHERE post_type = %s 
                 AND post_status = %s",
                'donation',
                'charitable-pending'
            ) );
            
            if ( $count > 0 ) {
                echo '<p><strong>Pending donations found: ' . esc_html( $count ) . '</strong></p>';
                echo '<a href="?confirm=yes" class="button" onclick="return confirm(\'Are you absolutely sure? This cannot be undone!\');">Delete All Pending Donations</a>';
            } else {
                echo '<div class="success"><strong>No pending donations found.</strong></div>';
            }
            ?>
            
            <a href="<?php echo esc_url( admin_url() ); ?>" class="button button-secondary">Cancel - Go to Admin</a>
        </body>
        </html>
        <?php
        exit;
    }
    
    // Execute deletion
    $result = charitable_delete_all_pending_donations();
    
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Delete Pending Donations - Results</title>
        <style>
            body { font-family: Arial, sans-serif; max-width: 800px; margin: 50px auto; padding: 20px; }
            .success { background: #d4edda; border: 1px solid #28a745; padding: 15px; margin: 20px 0; border-radius: 4px; }
            .error { background: #f8d7da; border: 1px solid #dc3545; padding: 15px; margin: 20px 0; border-radius: 4px; }
            .button { display: inline-block; padding: 10px 20px; background: #007cba; color: white; text-decoration: none; border-radius: 4px; margin: 10px 5px; }
        </style>
    </head>
    <body>
        <h1>Deletion Results</h1>
        
        <?php if ( $result['success'] ) : ?>
            <div class="success">
                <strong>✓ Success!</strong><br>
                <?php echo esc_html( $result['message'] ); ?><br>
                Total found: <?php echo esc_html( $result['total'] ); ?><br>
                Successfully deleted: <?php echo esc_html( $result['deleted'] ); ?><br>
                <?php if ( $result['failed'] > 0 ) : ?>
                    Failed: <?php echo esc_html( $result['failed'] ); ?><br>
                <?php endif; ?>
            </div>
            
            <?php if ( ! empty( $result['errors'] ) ) : ?>
                <div class="error">
                    <strong>Errors:</strong><br>
                    <?php foreach ( $result['errors'] as $error ) : ?>
                        <?php echo esc_html( $error ); ?><br>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        <?php else : ?>
            <div class="error">
                <strong>Error:</strong> <?php echo esc_html( $result['message'] ); ?>
            </div>
        <?php endif; ?>
        
        <a href="<?php echo esc_url( admin_url() ); ?>" class="button">Go to Admin</a>
        <p><small><strong>IMPORTANT:</strong> Delete this file (delete-pending-donations.php) from your server for security.</small></p>
    </body>
    </html>
    <?php
    exit;
}
// If running via WP-CLI
if ( defined( 'WP_CLI' ) && WP_CLI ) {
    $result = charitable_delete_all_pending_donations();
    
    if ( $result['success'] ) {
        WP_CLI::success( $result['message'] );
        if ( $result['failed'] > 0 ) {
            WP_CLI::warning( "Failed to delete {$result['failed']} donations." );
        }
    } else {
        WP_CLI::error( $result['message'] );
    }
}

Comments

Add a Comment