ravinderk /
Give
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Recount income and stats |
||
| 4 | * |
||
| 5 | * This class handles batch processing of resetting donations and income stats. |
||
| 6 | * |
||
| 7 | * @subpackage Admin/Tools/Give_Tools_Reset_Stats |
||
| 8 | * @copyright Copyright (c) 2016, WordImpress |
||
| 9 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
||
| 10 | * @since 1.5 |
||
| 11 | */ |
||
| 12 | |||
| 13 | // Exit if accessed directly. |
||
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
||
| 15 | exit; |
||
| 16 | } |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Give_Tools_Reset_Stats Class |
||
| 20 | * |
||
| 21 | * @since 1.5 |
||
| 22 | */ |
||
| 23 | class Give_Tools_Reset_Stats extends Give_Batch_Export { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Our export type. Used for export-type specific filters/actions |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | * @since 1.5 |
||
| 30 | */ |
||
| 31 | public $export_type = ''; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Allows for a non-form batch processing to be run. |
||
| 35 | * |
||
| 36 | * @since 1.5 |
||
| 37 | * @var boolean |
||
| 38 | */ |
||
| 39 | public $is_void = true; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Sets the number of items to pull on each step |
||
| 43 | * |
||
| 44 | * @since 1.5 |
||
| 45 | * @var integer |
||
| 46 | */ |
||
| 47 | public $per_step = 30; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Get the Export Data |
||
| 51 | * |
||
| 52 | * @access public |
||
| 53 | * @since 1.5 |
||
| 54 | * @global object $wpdb Used to query the database using the WordPress |
||
| 55 | * Database API |
||
| 56 | * @return bool $data The data for the CSV file |
||
| 57 | */ |
||
| 58 | public function get_data() { |
||
| 59 | global $wpdb; |
||
| 60 | |||
| 61 | $items = $this->get_stored_data( 'give_temp_reset_ids' ); |
||
| 62 | |||
| 63 | if ( ! is_array( $items ) ) { |
||
| 64 | return false; |
||
| 65 | } |
||
| 66 | |||
| 67 | $offset = ( $this->step - 1 ) * $this->per_step; |
||
| 68 | $step_items = array_slice( $items, $offset, $this->per_step ); |
||
| 69 | |||
| 70 | if ( $step_items ) { |
||
| 71 | |||
| 72 | $step_ids = array( |
||
| 73 | 'customers' => array(), |
||
| 74 | 'forms' => array(), |
||
| 75 | 'other' => array(), |
||
| 76 | ); |
||
| 77 | |||
| 78 | foreach ( $step_items as $item ) { |
||
| 79 | |||
| 80 | switch ( $item['type'] ) { |
||
| 81 | case 'customer': |
||
| 82 | $step_ids['customers'][] = $item['id']; |
||
| 83 | break; |
||
| 84 | case 'forms': |
||
| 85 | $step_ids['give_forms'][] = $item['id']; |
||
| 86 | break; |
||
| 87 | default: |
||
| 88 | $item_type = apply_filters( 'give_reset_item_type', 'other', $item ); |
||
| 89 | $step_ids[ $item_type ][] = $item['id']; |
||
| 90 | break; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | $sql = array(); |
||
| 95 | |||
| 96 | foreach ( $step_ids as $type => $ids ) { |
||
| 97 | |||
| 98 | if ( empty( $ids ) ) { |
||
| 99 | continue; |
||
| 100 | } |
||
| 101 | |||
| 102 | $ids = implode( ',', $ids ); |
||
| 103 | |||
| 104 | switch ( $type ) { |
||
| 105 | case 'customers': |
||
| 106 | $table_name = $wpdb->prefix . 'give_customers'; |
||
| 107 | $sql[] = "DELETE FROM $table_name WHERE id IN ($ids)"; |
||
| 108 | break; |
||
| 109 | case 'forms': |
||
| 110 | $sql[] = "UPDATE $wpdb->postmeta SET meta_value = 0 WHERE meta_key = '_give_form_sales' AND post_id IN ($ids)"; |
||
| 111 | $sql[] = "UPDATE $wpdb->postmeta SET meta_value = 0.00 WHERE meta_key = '_give_form_earnings' AND post_id IN ($ids)"; |
||
| 112 | break; |
||
| 113 | case 'other': |
||
| 114 | $sql[] = "DELETE FROM $wpdb->posts WHERE id IN ($ids)"; |
||
| 115 | $sql[] = "DELETE FROM $wpdb->postmeta WHERE post_id IN ($ids)"; |
||
| 116 | $sql[] = "DELETE FROM $wpdb->comments WHERE comment_post_ID IN ($ids)"; |
||
| 117 | $sql[] = "DELETE FROM $wpdb->commentmeta WHERE comment_id NOT IN (SELECT comment_ID FROM $wpdb->comments)"; |
||
| 118 | break; |
||
| 119 | } |
||
| 120 | |||
| 121 | if ( ! in_array( $type, array( 'customers', 'forms', 'other' ) ) ) { |
||
| 122 | // Allows other types of custom post types to filter on their own post_type |
||
| 123 | // and add items to the query list, for the IDs found in their post type. |
||
| 124 | $sql = apply_filters( "give_reset_add_queries_{$type}", $sql, $ids ); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | if ( ! empty( $sql ) ) { |
||
| 129 | foreach ( $sql as $query ) { |
||
| 130 | $wpdb->query( $query ); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | return true; |
||
| 135 | |||
| 136 | }// End if(). |
||
|
0 ignored issues
–
show
|
|||
| 137 | |||
| 138 | return false; |
||
| 139 | |||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Return the calculated completion percentage. |
||
| 144 | * |
||
| 145 | * @since 1.5 |
||
| 146 | * @return int |
||
| 147 | */ |
||
| 148 | public function get_percentage_complete() { |
||
| 149 | |||
| 150 | $items = $this->get_stored_data( 'give_temp_reset_ids', false ); |
||
| 151 | $total = count( $items ); |
||
| 152 | |||
| 153 | $percentage = 100; |
||
| 154 | |||
| 155 | if ( $total > 0 ) { |
||
| 156 | $percentage = ( ( $this->per_step * $this->step ) / $total ) * 100; |
||
| 157 | } |
||
| 158 | |||
| 159 | if ( $percentage > 100 ) { |
||
| 160 | $percentage = 100; |
||
| 161 | } |
||
| 162 | |||
| 163 | return $percentage; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Set the properties specific to the payments export. |
||
| 168 | * |
||
| 169 | * @since 1.5 |
||
| 170 | * |
||
| 171 | * @param array $request The Form Data passed into the batch processing. |
||
| 172 | */ |
||
| 173 | public function set_properties( $request ) { |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Process a step |
||
| 178 | * |
||
| 179 | * @since 1.5 |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | public function process_step() { |
||
| 183 | |||
| 184 | if ( ! $this->can_export() ) { |
||
| 185 | wp_die( esc_html__( 'You do not have permission to reset data.', 'give' ), esc_html__( 'Error', 'give' ), array( |
||
| 186 | 'response' => 403, |
||
| 187 | ) ); |
||
| 188 | } |
||
| 189 | |||
| 190 | $had_data = $this->get_data(); |
||
| 191 | |||
| 192 | if ( $had_data ) { |
||
| 193 | $this->done = false; |
||
| 194 | |||
| 195 | return true; |
||
| 196 | } else { |
||
| 197 | update_option( 'give_earnings_total', 0 ); |
||
| 198 | Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) ); |
||
| 199 | |||
| 200 | $this->delete_data( 'give_temp_reset_ids' ); |
||
| 201 | |||
| 202 | // Reset the sequential order numbers |
||
| 203 | if ( give_get_option( 'enable_sequential' ) ) { |
||
| 204 | delete_option( 'give_last_payment_number' ); |
||
| 205 | } |
||
| 206 | |||
| 207 | $this->done = true; |
||
| 208 | $this->message = esc_html__( 'Donation forms, income, donations counts, and logs successfully reset.', 'give' ); |
||
| 209 | |||
| 210 | return false; |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Headers |
||
| 216 | */ |
||
| 217 | public function headers() { |
||
| 218 | ignore_user_abort( true ); |
||
| 219 | |||
| 220 | if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) { |
||
| 221 | set_time_limit( 0 ); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Perform the export |
||
| 227 | * |
||
| 228 | * @access public |
||
| 229 | * @since 1.5 |
||
| 230 | * @return void |
||
| 231 | */ |
||
| 232 | public function export() { |
||
| 233 | |||
| 234 | // Set headers |
||
| 235 | $this->headers(); |
||
| 236 | |||
| 237 | give_die(); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Pre Fetch |
||
| 242 | */ |
||
| 243 | public function pre_fetch() { |
||
| 244 | |||
| 245 | if ( $this->step == 1 ) { |
||
| 246 | $this->delete_data( 'give_temp_reset_ids' ); |
||
| 247 | } |
||
| 248 | |||
| 249 | $items = get_option( 'give_temp_reset_ids', false ); |
||
| 250 | |||
| 251 | if ( false === $items ) { |
||
| 252 | $items = array(); |
||
| 253 | |||
| 254 | $give_types_for_reset = array( 'give_forms', 'give_log', 'give_payment' ); |
||
| 255 | $give_types_for_reset = apply_filters( 'give_reset_store_post_types', $give_types_for_reset ); |
||
| 256 | |||
| 257 | $args = apply_filters( 'give_tools_reset_stats_total_args', array( |
||
| 258 | 'post_type' => $give_types_for_reset, |
||
| 259 | 'post_status' => 'any', |
||
| 260 | 'posts_per_page' => - 1, |
||
| 261 | ) ); |
||
| 262 | |||
| 263 | $posts = get_posts( $args ); |
||
| 264 | foreach ( $posts as $post ) { |
||
| 265 | $items[] = array( |
||
| 266 | 'id' => (int) $post->ID, |
||
| 267 | 'type' => $post->post_type, |
||
| 268 | ); |
||
| 269 | } |
||
| 270 | |||
| 271 | $donor_args = array( |
||
| 272 | 'number' => - 1, |
||
| 273 | ); |
||
| 274 | $donors = Give()->donors->get_donors( $donor_args ); |
||
| 275 | foreach ( $donors as $donor ) { |
||
|
0 ignored issues
–
show
The expression
$donors of type array|object|null is not guaranteed to be traversable. How about adding an additional type check?
There are different options of fixing this problem.
Loading history...
|
|||
| 276 | $items[] = array( |
||
| 277 | 'id' => (int) $donor->id, |
||
| 278 | 'type' => 'customer', |
||
| 279 | ); |
||
| 280 | } |
||
| 281 | |||
| 282 | // Allow filtering of items to remove with an unassociative array for each item |
||
| 283 | // The array contains the unique ID of the item, and a 'type' for you to use in the execution of the get_data method |
||
| 284 | $items = apply_filters( 'give_reset_items', $items ); |
||
| 285 | |||
| 286 | $this->store_data( 'give_temp_reset_ids', $items ); |
||
| 287 | }// End if(). |
||
|
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
43% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. Loading history...
|
|||
| 288 | |||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Given a key, get the information from the Database Directly. |
||
| 293 | * |
||
| 294 | * @since 1.5 |
||
| 295 | * |
||
| 296 | * @param string $key The option_name |
||
| 297 | * |
||
| 298 | * @return mixed Returns the data from the database. |
||
| 299 | */ |
||
| 300 | private function get_stored_data( $key ) { |
||
| 301 | global $wpdb; |
||
| 302 | $value = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = '%s'", $key ) ); |
||
| 303 | |||
| 304 | return empty( $value ) ? false : maybe_unserialize( $value ); |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Give a key, store the value. |
||
| 309 | * |
||
| 310 | * @since 1.5 |
||
| 311 | * |
||
| 312 | * @param string $key The option_name. |
||
| 313 | * @param mixed $value The value to store. |
||
| 314 | * |
||
| 315 | * @return void |
||
| 316 | */ |
||
| 317 | private function store_data( $key, $value ) { |
||
| 318 | global $wpdb; |
||
| 319 | |||
| 320 | $value = maybe_serialize( $value ); |
||
| 321 | |||
| 322 | $data = array( |
||
| 323 | 'option_name' => $key, |
||
| 324 | 'option_value' => $value, |
||
| 325 | 'autoload' => 'no', |
||
| 326 | ); |
||
| 327 | |||
| 328 | $formats = array( |
||
| 329 | '%s', |
||
| 330 | '%s', |
||
| 331 | '%s', |
||
| 332 | ); |
||
| 333 | |||
| 334 | $wpdb->replace( $wpdb->options, $data, $formats ); |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Delete an option |
||
| 339 | * |
||
| 340 | * @since 1.5 |
||
| 341 | * |
||
| 342 | * @param string $key The option_name to delete |
||
| 343 | * |
||
| 344 | * @return void |
||
| 345 | */ |
||
| 346 | private function delete_data( $key ) { |
||
| 347 | global $wpdb; |
||
| 348 | $wpdb->delete( $wpdb->options, array( |
||
| 349 | 'option_name' => $key, |
||
| 350 | ) ); |
||
| 351 | } |
||
| 352 | |||
| 353 | } |
||
| 354 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.