← All changes
|
includes/admin/tools/data/class-give-tools-reset-stats.php
+109
-45
2.3.0
→
4.17.0
View file →
| @@ -4,9 +4,9 @@ | ||
| 4 | 4 | * |
| 5 | 5 | * This class handles batch processing of resetting donations and income stats. |
| 6 | 6 | * |
| 7 | 7 | * @subpackage Admin/Tools/Give_Tools_Reset_Stats |
| 8 | - * @copyright Copyright (c) 2016, WordImpress | |
| 8 | + * @copyright Copyright (c) 2016, GiveWP | |
| 9 | 9 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 10 | 10 | * @since 1.5 |
| 11 | 11 | */ |
| 12 | 12 | |
| @@ -46,8 +46,16 @@ | ||
| 46 | 46 | */ |
| 47 | 47 | public $per_step = 30; |
| 48 | 48 | |
| 49 | 49 | /** |
| 50 | + * Success message to display when reset is complete | |
| 51 | + * | |
| 52 | + * @since 4.10.0 | |
| 53 | + * @var string | |
| 54 | + */ | |
| 55 | + public $message = ''; | |
| 56 | + | |
| 57 | + /** | |
| 50 | 58 | * Constructor. |
| 51 | 59 | */ |
| 52 | 60 | public function __construct( $_step = 1 ) { |
| 53 | 61 | parent::__construct( $_step ); |
| @@ -58,8 +66,9 @@ | ||
| 58 | 66 | /** |
| 59 | 67 | * Get the Export Data |
| 60 | 68 | * |
| 61 | 69 | * @access public |
| 70 | + * @since 4.10.0 Added deletion logic for campaigns, campaign pages, subscriptions, events, logs, revenue, usermeta, etc. | |
| 62 | 71 | * @since 1.5 |
| 63 | 72 | * @global object $wpdb Used to query the database using the WordPress |
| 64 | 73 | * Database API |
| 65 | 74 | * @return bool $data The data for the CSV file |
| @@ -77,13 +86,13 @@ | ||
| 77 | 86 | $step_items = array_slice( $items, $offset, $this->per_step ); |
| 78 | 87 | |
| 79 | 88 | if ( $step_items ) { |
| 80 | 89 | |
| 81 | - $step_ids = array( | |
| 82 | - 'customers' => array(), | |
| 83 | - 'forms' => array(), | |
| 84 | - 'other' => array(), | |
| 85 | - ); | |
| 90 | + $step_ids = [ | |
| 91 | + 'customers' => [], | |
| 92 | + 'forms' => [], | |
| 93 | + 'other' => [], | |
| 94 | + ]; | |
| 86 | 95 | |
| 87 | 96 | foreach ( $step_items as $item ) { |
| 88 | 97 | |
| 89 | 98 | switch ( $item['type'] ) { |
| @@ -99,10 +108,11 @@ | ||
| 99 | 108 | break; |
| 100 | 109 | } |
| 101 | 110 | } |
| 102 | 111 | |
| 103 | - $sql = array(); | |
| 104 | - $meta_table = __give_v20_bc_table_details('form' ); | |
| 112 | + $sql = []; | |
| 113 | + $meta_table = give_v20_bc_table_details( 'form' ); | |
| 114 | + $donor_meta_table_name = Give()->donor_meta->table_name; | |
| 105 | 115 | |
| 106 | 116 | foreach ( $step_ids as $type => $ids ) { |
| 107 | 117 | |
| 108 | 118 | if ( empty( $ids ) ) { |
| @@ -112,19 +122,18 @@ | ||
| 112 | 122 | $ids = implode( ',', $ids ); |
| 113 | 123 | |
| 114 | 124 | switch ( $type ) { |
| 115 | 125 | case 'customers': |
| 116 | - | |
| 117 | 126 | // Delete all the Give related donor and its meta. |
| 118 | 127 | $sql[] = "DELETE FROM {$wpdb->donors}"; |
| 119 | - $sql[] = "DELETE FROM {$wpdb->donormeta}"; | |
| 128 | + $sql[] = "DELETE FROM {$donor_meta_table_name}"; | |
| 120 | 129 | break; |
| 121 | 130 | case 'forms': |
| 122 | 131 | $sql[] = "UPDATE {$meta_table['name']} SET meta_value = 0 WHERE meta_key = '_give_form_sales' AND {$meta_table['column']['id']} IN ($ids)"; |
| 123 | 132 | $sql[] = "UPDATE {$meta_table['name']} SET meta_value = 0.00 WHERE meta_key = '_give_form_earnings' AND {$meta_table['column']['id']} IN ($ids)"; |
| 124 | 133 | break; |
| 134 | + | |
| 125 | 135 | case 'other': |
| 126 | - | |
| 127 | 136 | // Delete main entries of forms and donations exists in posts table. |
| 128 | 137 | $sql[] = "DELETE FROM {$wpdb->posts} WHERE id IN ($ids)"; |
| 129 | 138 | |
| 130 | 139 | // Delete all the meta rows of form exists in form meta table. |
| @@ -139,15 +148,57 @@ | ||
| 139 | 148 | // Delete all the Give related comments and its meta. |
| 140 | 149 | $sql[] = "DELETE FROM {$wpdb->give_comments}"; |
| 141 | 150 | $sql[] = "DELETE FROM {$wpdb->give_commentmeta}"; |
| 142 | 151 | |
| 143 | - // Delete all the Give related logs and its meta. | |
| 144 | - $sql[] = "DELETE FROM {$wpdb->prefix}give_logs"; | |
| 145 | - $sql[] = "DELETE FROM {$wpdb->logmeta}"; | |
| 146 | - | |
| 147 | 152 | // Delete all the Give sessions data. |
| 148 | 153 | $sql[] = "DELETE FROM {$wpdb->prefix}give_sessions"; |
| 149 | 154 | |
| 155 | + // Delete all Give logs data. | |
| 156 | + $sql[] = "DELETE FROM {$wpdb->prefix}give_log"; | |
| 157 | + | |
| 158 | + // Delete all Give revenue data. | |
| 159 | + $sql[] = "DELETE FROM {$wpdb->prefix}give_revenue"; | |
| 160 | + | |
| 161 | + // Delete campaigns and related data | |
| 162 | + $sql[] = "DELETE FROM {$wpdb->prefix}give_campaign_forms"; | |
| 163 | + $sql[] = "DELETE FROM {$wpdb->prefix}give_campaigns"; | |
| 164 | + | |
| 165 | + // Delete GiveWP Campaign Pages and their meta data | |
| 166 | + $sql[] = "DELETE FROM {$wpdb->posts} WHERE post_type = 'page' AND id IN (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'give_campaign_id')"; | |
| 167 | + $sql[] = "DELETE FROM {$wpdb->postmeta} WHERE meta_key = 'give_campaign_id'"; | |
| 168 | + | |
| 169 | + // Delete subscriptions and related data | |
| 170 | + $sql[] = "DELETE FROM {$wpdb->prefix}give_subscriptionmeta"; | |
| 171 | + $sql[] = "DELETE FROM {$wpdb->prefix}give_subscriptions"; | |
| 172 | + | |
| 173 | + // Delete events and related data | |
| 174 | + $sql[] = "DELETE FROM {$wpdb->prefix}give_event_tickets"; | |
| 175 | + $sql[] = "DELETE FROM {$wpdb->prefix}give_event_ticket_types"; | |
| 176 | + $sql[] = "DELETE FROM {$wpdb->prefix}give_events"; | |
| 177 | + | |
| 178 | + // Clear all Give user meta data (notices, preferences, etc.) | |
| 179 | + $sql[] = "DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE '%give%'"; | |
| 180 | + | |
| 181 | + // Reset GiveWP options to default values (preserve essential ones) | |
| 182 | + // Essential options that must be preserved to prevent plugin deactivation issues | |
| 183 | + $essential_options = [ | |
| 184 | + 'give_version', // Plugin version - needed for upgrade detection | |
| 185 | + 'give_completed_upgrades', // Completed upgrade routines - prevents re-running | |
| 186 | + 'give_default_api_version', // API version - needed for API functionality | |
| 187 | + '_give_table_check', // Database table check - prevents table recreation | |
| 188 | + 'give_temp_reset_ids', // Temporary reset data used by this class | |
| 189 | + 'give_settings', // Plugin settings - preserve to avoid pages recreation | |
| 190 | + ]; | |
| 191 | + | |
| 192 | + // Create SQL placeholder string for the essential options list | |
| 193 | + $essential_options_placeholder = implode( "','", $essential_options ); | |
| 194 | + // Delete all GiveWP options except the essential ones to prevent deactivation issues | |
| 195 | + $sql[] = "DELETE FROM {$wpdb->options} WHERE option_name LIKE '%give%' AND option_name NOT IN ('{$essential_options_placeholder}')"; | |
| 196 | + | |
| 197 | + // Clear Action Scheduler data related to GiveWP | |
| 198 | + $sql[] = "DELETE FROM {$wpdb->prefix}actionscheduler_actions WHERE hook LIKE '%give%'"; | |
| 199 | + $sql[] = "DELETE FROM {$wpdb->prefix}actionscheduler_groups WHERE slug LIKE '%give%'"; | |
| 200 | + | |
| 150 | 201 | // Delete Give related categories and tags data from taxonomy tables. |
| 151 | 202 | $sql[] = $wpdb->prepare( |
| 152 | 203 | " |
| 153 | 204 | DELETE FROM $wpdb->terms |
| @@ -158,9 +209,9 @@ | ||
| 158 | 209 | WHERE $wpdb->term_taxonomy.taxonomy = %s |
| 159 | 210 | OR $wpdb->term_taxonomy.taxonomy = %s |
| 160 | 211 | ) |
| 161 | 212 | ", |
| 162 | - array( 'give_forms_category', 'give_forms_tag' ) | |
| 213 | + [ 'give_forms_category', 'give_forms_tag' ] | |
| 163 | 214 | ); |
| 164 | 215 | |
| 165 | 216 | $sql[] = $wpdb->prepare( |
| 166 | 217 | " |
| @@ -167,15 +218,15 @@ | ||
| 167 | 218 | DELETE FROM $wpdb->term_taxonomy |
| 168 | 219 | WHERE $wpdb->term_taxonomy.taxonomy = %s |
| 169 | 220 | OR $wpdb->term_taxonomy.taxonomy = %s |
| 170 | 221 | ", |
| 171 | - array( 'give_forms_category', 'give_forms_tag' ) | |
| 222 | + [ 'give_forms_category', 'give_forms_tag' ] | |
| 172 | 223 | ); |
| 173 | 224 | |
| 174 | 225 | break; |
| 175 | 226 | } |
| 176 | 227 | |
| 177 | - if ( ! in_array( $type, array( 'customers', 'forms', 'other' ) ) ) { | |
| 228 | + if ( ! in_array( $type, [ 'customers', 'forms', 'other' ] ) ) { | |
| 178 | 229 | // Allows other types of custom post types to filter on their own post_type |
| 179 | 230 | // and add items to the query list, for the IDs found in their post type. |
| 180 | 231 | $sql = apply_filters( "give_reset_add_queries_{$type}", $sql, $ids ); |
| 181 | 232 | } |
| @@ -197,8 +248,9 @@ | ||
| 197 | 248 | |
| 198 | 249 | /** |
| 199 | 250 | * Return the calculated completion percentage. |
| 200 | 251 | * |
| 252 | + * @since 4.10.0 Check if items are set before counting to prevent fatal errors on PHP 8 | |
| 201 | 253 | * @since 1.5 |
| 202 | 254 | * @return int |
| 203 | 255 | */ |
| 204 | 256 | public function get_percentage_complete() { |
| @@ -203,9 +255,9 @@ | ||
| 203 | 255 | */ |
| 204 | 256 | public function get_percentage_complete() { |
| 205 | 257 | |
| 206 | 258 | $items = $this->get_stored_data( 'give_temp_reset_ids' ); |
| 207 | - $total = count( $items ); | |
| 259 | + $total = $items ? count( $items ) : 0; | |
| 208 | 260 | |
| 209 | 261 | $percentage = 100; |
| 210 | 262 | |
| 211 | 263 | if ( $total > 0 ) { |
| @@ -231,8 +283,9 @@ | ||
| 231 | 283 | |
| 232 | 284 | /** |
| 233 | 285 | * Process a step |
| 234 | 286 | * |
| 287 | + * @since 4.10.0 Updated success message | |
| 235 | 288 | * @since 1.5 |
| 236 | 289 | * @return bool |
| 237 | 290 | */ |
| 238 | 291 | public function process_step() { |
| @@ -237,11 +290,15 @@ | ||
| 237 | 290 | */ |
| 238 | 291 | public function process_step() { |
| 239 | 292 | |
| 240 | 293 | if ( ! $this->can_export() ) { |
| 241 | - wp_die( esc_html__( 'You do not have permission to reset data.', 'give' ), esc_html__( 'Error', 'give' ), array( | |
| 242 | - 'response' => 403, | |
| 243 | - ) ); | |
| 294 | + wp_die( | |
| 295 | + esc_html__( 'You do not have permission to reset data.', 'give' ), | |
| 296 | + esc_html__( 'Error', 'give' ), | |
| 297 | + [ | |
| 298 | + 'response' => 403, | |
| 299 | + ] | |
| 300 | + ); | |
| 244 | 301 | } |
| 245 | 302 | |
| 246 | 303 | $had_data = $this->get_data(); |
| 247 | 304 | |
| @@ -254,10 +311,10 @@ | ||
| 254 | 311 | Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) ); |
| 255 | 312 | |
| 256 | 313 | $this->delete_data( 'give_temp_reset_ids' ); |
| 257 | 314 | |
| 258 | - $this->done = true; | |
| 259 | - $this->message = esc_html__( 'Donation forms, income, donations counts, and logs successfully reset.', 'give' ); | |
| 315 | + $this->done = true; | |
| 316 | + $this->message = esc_html__( 'Successfully reset data for campaigns, campaign pages, donation forms, subscriptions, events, revenue, donation counts, logs, etc.', 'give' ); | |
| 260 | 317 | |
| 261 | 318 | return false; |
| 262 | 319 | } |
| 263 | 320 | } |
| @@ -295,38 +352,42 @@ | ||
| 295 | 352 | |
| 296 | 353 | $items = get_option( 'give_temp_reset_ids', false ); |
| 297 | 354 | |
| 298 | 355 | if ( false === $items ) { |
| 299 | - $items = array(); | |
| 356 | + $items = []; | |
| 300 | 357 | |
| 301 | - $give_types_for_reset = array( 'give_forms', 'give_payment' ); | |
| 358 | + $give_types_for_reset = [ 'give_forms', 'give_payment' ]; | |
| 302 | 359 | $give_types_for_reset = apply_filters( 'give_reset_store_post_types', $give_types_for_reset ); |
| 303 | 360 | |
| 304 | - $args = apply_filters( 'give_tools_reset_stats_total_args', array( | |
| 305 | - 'post_type' => $give_types_for_reset, | |
| 306 | - 'post_status' => 'any', | |
| 307 | - 'posts_per_page' => - 1, | |
| 308 | - ) ); | |
| 361 | + $args = apply_filters( | |
| 362 | + 'give_tools_reset_stats_total_args', | |
| 363 | + [ | |
| 364 | + 'post_type' => $give_types_for_reset, | |
| 365 | + 'post_status' => 'any', | |
| 366 | + 'posts_per_page' => - 1, | |
| 367 | + ] | |
| 368 | + ); | |
| 309 | 369 | |
| 310 | 370 | $posts = get_posts( $args ); |
| 311 | 371 | foreach ( $posts as $post ) { |
| 312 | - $items[] = array( | |
| 372 | + $items[] = [ | |
| 313 | 373 | 'id' => (int) $post->ID, |
| 314 | 374 | 'type' => $post->post_type, |
| 315 | - ); | |
| 375 | + ]; | |
| 316 | 376 | } |
| 317 | 377 | |
| 318 | - $donor_args = array( | |
| 378 | + $donor_args = [ | |
| 319 | 379 | 'number' => - 1, |
| 320 | - ); | |
| 380 | + ]; | |
| 321 | 381 | $donors = Give()->donors->get_donors( $donor_args ); |
| 322 | 382 | foreach ( $donors as $donor ) { |
| 323 | - $items[] = array( | |
| 383 | + $items[] = [ | |
| 324 | 384 | 'id' => (int) $donor->id, |
| 325 | 385 | 'type' => 'customer', |
| 326 | - ); | |
| 386 | + ]; | |
| 327 | 387 | } |
| 328 | 388 | |
| 389 | + | |
| 329 | 390 | // Allow filtering of items to remove with an unassociative array for each item |
| 330 | 391 | // The array contains the unique ID of the item, and a 'type' for you to use in the execution of the get_data method |
| 331 | 392 | $items = apply_filters( 'give_reset_items', $items ); |
| 332 | 393 | |
| @@ -374,19 +435,19 @@ | ||
| 374 | 435 | global $wpdb; |
| 375 | 436 | |
| 376 | 437 | $value = is_array( $value ) ? wp_json_encode( $value ) : esc_attr( $value ); |
| 377 | 438 | |
| 378 | - $data = array( | |
| 439 | + $data = [ | |
| 379 | 440 | 'option_name' => $key, |
| 380 | 441 | 'option_value' => $value, |
| 381 | 442 | 'autoload' => 'no', |
| 382 | - ); | |
| 443 | + ]; | |
| 383 | 444 | |
| 384 | - $formats = array( | |
| 445 | + $formats = [ | |
| 385 | 446 | '%s', |
| 386 | 447 | '%s', |
| 387 | 448 | '%s', |
| 388 | - ); | |
| 449 | + ]; | |
| 389 | 450 | |
| 390 | 451 | $wpdb->replace( $wpdb->options, $data, $formats ); |
| 391 | 452 | } |
| 392 | 453 | |
| @@ -400,11 +461,14 @@ | ||
| 400 | 461 | * @return void |
| 401 | 462 | */ |
| 402 | 463 | private function delete_data( $key ) { |
| 403 | 464 | global $wpdb; |
| 404 | - $wpdb->delete( $wpdb->options, array( | |
| 405 | - 'option_name' => $key, | |
| 406 | - ) ); | |
| 465 | + $wpdb->delete( | |
| 466 | + $wpdb->options, | |
| 467 | + [ | |
| 468 | + 'option_name' => $key, | |
| 469 | + ] | |
| 470 | + ); | |
| 407 | 471 | } |
| 408 | 472 | |
| 409 | 473 | /** |
| 410 | 474 | * Unset the properties specific to the donors export. |
| @@ -410,9 +474,9 @@ | ||
| 410 | 474 | * Unset the properties specific to the donors export. |
| 411 | 475 | * |
| 412 | 476 | * @since 2.3.0 |
| 413 | 477 | * |
| 414 | - * @param array $request | |
| 478 | + * @param array $request | |
| 415 | 479 | * @param Give_Batch_Export $export |
| 416 | 480 | */ |
| 417 | 481 | public function unset_properties( $request, $export ) { |
| 418 | 482 | if ( $export->done ) { |