Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 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 | $meta_table_name = $wpdb->prefix . 'give_customermeta'; |
||
| 108 | $sql[] = "DELETE FROM $table_name WHERE id IN ($ids)"; |
||
| 109 | $sql[] = "DELETE FROM $meta_table_name WHERE customer_id IN ($ids)"; |
||
| 110 | break; |
||
| 111 | case 'forms': |
||
| 112 | $sql[] = "UPDATE $wpdb->postmeta SET meta_value = 0 WHERE meta_key = '_give_form_sales' AND post_id IN ($ids)"; |
||
| 113 | $sql[] = "UPDATE $wpdb->postmeta SET meta_value = 0.00 WHERE meta_key = '_give_form_earnings' AND post_id IN ($ids)"; |
||
| 114 | break; |
||
| 115 | case 'other': |
||
| 116 | $sql[] = "DELETE FROM $wpdb->posts WHERE id IN ($ids)"; |
||
| 117 | $sql[] = "DELETE FROM $wpdb->postmeta WHERE post_id IN ($ids)"; |
||
| 118 | $sql[] = "DELETE FROM $wpdb->comments WHERE comment_post_ID IN ($ids)"; |
||
| 119 | $sql[] = "DELETE FROM $wpdb->commentmeta WHERE comment_id NOT IN (SELECT comment_ID FROM $wpdb->comments)"; |
||
| 120 | break; |
||
| 121 | } |
||
| 122 | |||
| 123 | if ( ! in_array( $type, array( 'customers', 'forms', 'other' ) ) ) { |
||
| 124 | // Allows other types of custom post types to filter on their own post_type |
||
| 125 | // and add items to the query list, for the IDs found in their post type. |
||
| 126 | $sql = apply_filters( "give_reset_add_queries_{$type}", $sql, $ids ); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | if ( ! empty( $sql ) ) { |
||
| 131 | foreach ( $sql as $query ) { |
||
| 132 | $wpdb->query( $query ); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | return true; |
||
| 137 | |||
| 138 | }// End if(). |
||
| 139 | |||
| 140 | return false; |
||
| 141 | |||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Return the calculated completion percentage. |
||
| 146 | * |
||
| 147 | * @since 1.5 |
||
| 148 | * @return int |
||
| 149 | */ |
||
| 150 | View Code Duplication | public function get_percentage_complete() { |
|
| 151 | |||
| 152 | $items = $this->get_stored_data( 'give_temp_reset_ids' ); |
||
| 153 | $total = count( $items ); |
||
| 154 | |||
| 155 | $percentage = 100; |
||
| 156 | |||
| 157 | if ( $total > 0 ) { |
||
| 158 | $percentage = ( ( $this->per_step * $this->step ) / $total ) * 100; |
||
| 159 | } |
||
| 160 | |||
| 161 | if ( $percentage > 100 ) { |
||
| 162 | $percentage = 100; |
||
| 163 | } |
||
| 164 | |||
| 165 | return $percentage; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Set the properties specific to the payments export. |
||
| 170 | * |
||
| 171 | * @since 1.5 |
||
| 172 | * |
||
| 173 | * @param array $request The Form Data passed into the batch processing. |
||
| 174 | */ |
||
| 175 | public function set_properties( $request ) { |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Process a step |
||
| 180 | * |
||
| 181 | * @since 1.5 |
||
| 182 | * @return bool |
||
| 183 | */ |
||
| 184 | public function process_step() { |
||
| 185 | |||
| 186 | if ( ! $this->can_export() ) { |
||
| 187 | wp_die( esc_html__( 'You do not have permission to reset data.', 'give' ), esc_html__( 'Error', 'give' ), array( |
||
| 188 | 'response' => 403, |
||
| 189 | ) ); |
||
| 190 | } |
||
| 191 | |||
| 192 | $had_data = $this->get_data(); |
||
| 193 | |||
| 194 | if ( $had_data ) { |
||
| 195 | $this->done = false; |
||
| 196 | |||
| 197 | return true; |
||
| 198 | } else { |
||
| 199 | update_option( 'give_earnings_total', 0 ); |
||
| 200 | Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) ); |
||
| 201 | |||
| 202 | $this->delete_data( 'give_temp_reset_ids' ); |
||
| 203 | |||
| 204 | // Reset the sequential order numbers |
||
| 205 | if ( give_get_option( 'enable_sequential' ) ) { |
||
| 206 | delete_option( 'give_last_payment_number' ); |
||
| 207 | } |
||
| 208 | |||
| 209 | $this->done = true; |
||
| 210 | $this->message = esc_html__( 'Donation forms, income, donations counts, and logs successfully reset.', 'give' ); |
||
| 211 | |||
| 212 | return false; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Headers |
||
| 218 | */ |
||
| 219 | public function headers() { |
||
| 220 | give_ignore_user_abort(); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Perform the export |
||
| 225 | * |
||
| 226 | * @access public |
||
| 227 | * @since 1.5 |
||
| 228 | * @return void |
||
| 229 | */ |
||
| 230 | public function export() { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Pre Fetch |
||
| 240 | */ |
||
| 241 | public function pre_fetch() { |
||
| 242 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * Given a key, get the information from the Database Directly. |
||
| 291 | * |
||
| 292 | * @since 1.5 |
||
| 293 | * |
||
| 294 | * @param string $key The option_name |
||
| 295 | * |
||
| 296 | * @return mixed Returns the data from the database. |
||
| 297 | */ |
||
| 298 | View Code Duplication | private function get_stored_data( $key ) { |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Give a key, store the value. |
||
| 316 | * |
||
| 317 | * @since 1.5 |
||
| 318 | * |
||
| 319 | * @param string $key The option_name. |
||
| 320 | * @param mixed $value The value to store. |
||
| 321 | * |
||
| 322 | * @return void |
||
| 323 | */ |
||
| 324 | View Code Duplication | private function store_data( $key, $value ) { |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Delete an option |
||
| 346 | * |
||
| 347 | * @since 1.5 |
||
| 348 | * |
||
| 349 | * @param string $key The option_name to delete |
||
| 350 | * |
||
| 351 | * @return void |
||
| 352 | */ |
||
| 353 | private function delete_data( $key ) { |
||
| 359 | |||
| 360 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.