admin-functions.php
7 months ago
core-functions.php
7 months ago
database-functions.php
7 months ago
database-functions.php
186 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Database functions for Easy Actions Scheduler Cleaner by AyudaWP |
| 4 | * |
| 5 | * @package EasyActionsSchedulerCleanerAyudaWP |
| 6 | * @since 1.0.1 |
| 7 | */ |
| 8 | |
| 9 | // Prevent direct access |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * Execute database cleanup |
| 16 | * |
| 17 | * @since 1.0.1 |
| 18 | */ |
| 19 | function easc_execute_cleanup() { |
| 20 | |
| 21 | // Verify table exists |
| 22 | if ( ! easc_table_exists() ) { |
| 23 | wp_die( esc_html__( 'Actions Scheduler table not found. Please make sure WooCommerce is properly installed.', 'easy-actions-scheduler-cleaner-ayudawp' ) ); |
| 24 | } |
| 25 | |
| 26 | $batch_size = 10000; |
| 27 | |
| 28 | // 1. Delete completed actions |
| 29 | $deleted_complete = easc_delete_actions_by_status( 'complete', $batch_size ); |
| 30 | |
| 31 | // 2. Delete failed actions |
| 32 | $deleted_failed = easc_delete_actions_by_status( 'failed', $batch_size ); |
| 33 | |
| 34 | // 3. Delete canceled actions |
| 35 | $deleted_canceled = easc_delete_actions_by_status( 'canceled', $batch_size ); |
| 36 | |
| 37 | // 4. Delete past-due actions |
| 38 | $deleted_past_due = easc_delete_actions_by_status( 'past-due', $batch_size ); |
| 39 | |
| 40 | // 5. Delete old pending actions (30+ days) |
| 41 | $deleted_old_pending = easc_delete_old_pending_actions( $batch_size ); |
| 42 | |
| 43 | // 6. Delete all action logs |
| 44 | $deleted_logs = easc_delete_all_logs( $batch_size ); |
| 45 | |
| 46 | // Clean orphaned related tables |
| 47 | easc_clean_orphaned_data(); |
| 48 | |
| 49 | // Calculate totals |
| 50 | $total_actions = $deleted_complete + $deleted_failed + $deleted_canceled + $deleted_past_due + $deleted_old_pending; |
| 51 | |
| 52 | // Redirect to results page |
| 53 | wp_safe_redirect( |
| 54 | add_query_arg( |
| 55 | array( |
| 56 | 'page' => 'easy-actions-scheduler-cleaner-ayudawp', |
| 57 | 'results' => '1', |
| 58 | 'complete' => $deleted_complete, |
| 59 | 'failed' => $deleted_failed, |
| 60 | 'canceled' => $deleted_canceled, |
| 61 | 'past_due' => $deleted_past_due, |
| 62 | 'old_pending' => $deleted_old_pending, |
| 63 | 'logs' => $deleted_logs, |
| 64 | '_wpnonce' => wp_create_nonce( 'easc_results_nonce' ), |
| 65 | ), |
| 66 | admin_url( 'tools.php' ) |
| 67 | ) |
| 68 | ); |
| 69 | exit; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Delete actions by status |
| 74 | * |
| 75 | * @since 1.0.0 |
| 76 | * @param string $status Action status |
| 77 | * @param int $batch_size Batch size for deletion |
| 78 | * @return int Number of deleted actions |
| 79 | */ |
| 80 | function easc_delete_actions_by_status( $status, $batch_size ) { |
| 81 | global $wpdb; |
| 82 | |
| 83 | $deleted = 0; |
| 84 | |
| 85 | do { |
| 86 | // Direct database call necessary for bulk deletion operations not available through WP API |
| 87 | $rows_deleted = $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching |
| 88 | $wpdb->prepare( |
| 89 | "DELETE FROM {$wpdb->prefix}actionscheduler_actions WHERE status = %s LIMIT %d", |
| 90 | $status, |
| 91 | $batch_size |
| 92 | ) |
| 93 | ); |
| 94 | $deleted += (int) $rows_deleted; |
| 95 | |
| 96 | // Clear any relevant caches |
| 97 | wp_cache_delete( 'easc_status_counts', 'easy_actions_scheduler_cleaner_ayudawp' ); |
| 98 | |
| 99 | } while ( $rows_deleted > 0 ); |
| 100 | |
| 101 | return $deleted; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Delete old pending actions (30+ days) |
| 106 | * |
| 107 | * @since 1.0.0 |
| 108 | * @param int $batch_size Batch size for deletion |
| 109 | * @return int Number of deleted actions |
| 110 | */ |
| 111 | function easc_delete_old_pending_actions( $batch_size ) { |
| 112 | global $wpdb; |
| 113 | |
| 114 | $thirty_days_ago = gmdate( 'Y-m-d H:i:s', strtotime( '-30 days' ) ); |
| 115 | $deleted = 0; |
| 116 | |
| 117 | do { |
| 118 | // Direct database call necessary for bulk deletion operations not available through WP API |
| 119 | $rows_deleted = $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching |
| 120 | $wpdb->prepare( |
| 121 | "DELETE FROM {$wpdb->prefix}actionscheduler_actions WHERE status = 'pending' AND scheduled_date_gmt < %s LIMIT %d", |
| 122 | $thirty_days_ago, |
| 123 | $batch_size |
| 124 | ) |
| 125 | ); |
| 126 | $deleted += (int) $rows_deleted; |
| 127 | |
| 128 | // Clear any relevant caches |
| 129 | wp_cache_delete( 'easc_status_counts', 'easy_actions_scheduler_cleaner_ayudawp' ); |
| 130 | |
| 131 | } while ( $rows_deleted > 0 ); |
| 132 | |
| 133 | return $deleted; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Delete all action logs |
| 138 | * |
| 139 | * @since 1.0.0 |
| 140 | * @param int $batch_size Batch size for deletion |
| 141 | * @return int Number of deleted logs |
| 142 | */ |
| 143 | function easc_delete_all_logs( $batch_size ) { |
| 144 | global $wpdb; |
| 145 | |
| 146 | $deleted = 0; |
| 147 | |
| 148 | do { |
| 149 | // Direct database call necessary for bulk deletion operations not available through WP API |
| 150 | $rows_deleted = $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching |
| 151 | $wpdb->prepare( |
| 152 | "DELETE FROM {$wpdb->prefix}actionscheduler_logs LIMIT %d", |
| 153 | $batch_size |
| 154 | ) |
| 155 | ); |
| 156 | $deleted += (int) $rows_deleted; |
| 157 | |
| 158 | // Clear any relevant caches |
| 159 | wp_cache_delete( 'easc_logs_count', 'easy_actions_scheduler_cleaner_ayudawp' ); |
| 160 | |
| 161 | } while ( $rows_deleted > 0 ); |
| 162 | |
| 163 | return $deleted; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Clean orphaned data from related tables |
| 168 | * |
| 169 | * @since 1.0.0 |
| 170 | */ |
| 171 | function easc_clean_orphaned_data() { |
| 172 | global $wpdb; |
| 173 | |
| 174 | // Direct database calls necessary for complex JOIN operations not available through WP API |
| 175 | // Clean orphaned groups |
| 176 | $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching |
| 177 | "DELETE g FROM {$wpdb->prefix}actionscheduler_groups g LEFT JOIN {$wpdb->prefix}actionscheduler_actions a ON g.group_id = a.group_id WHERE a.group_id IS NULL" |
| 178 | ); |
| 179 | |
| 180 | // Note: We don't clean orphaned logs here since we're deleting ALL logs in easc_delete_all_logs() |
| 181 | |
| 182 | // Clear caches after cleanup |
| 183 | wp_cache_delete( 'easc_status_counts', 'easy_actions_scheduler_cleaner_ayudawp' ); |
| 184 | wp_cache_delete( 'easc_logs_count', 'easy_actions_scheduler_cleaner_ayudawp' ); |
| 185 | wp_cache_delete( 'easc_table_exists_' . md5( $wpdb->prefix ), 'easy_actions_scheduler_cleaner_ayudawp' ); |
| 186 | } |