| Conditions | 51 |
| Paths | > 20000 |
| Total Lines | 255 |
| Code Lines | 140 |
| Lines | 21 |
| Ratio | 8.24 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 193 | public function get_data() { |
||
| 194 | |||
| 195 | $data = array(); |
||
| 196 | $i = 0; |
||
| 197 | |||
| 198 | $args = array( |
||
| 199 | 'number' => 30, |
||
| 200 | 'page' => $this->step, |
||
| 201 | 'status' => $this->status, |
||
| 202 | ); |
||
| 203 | |||
| 204 | // Date query. |
||
| 205 | if ( ! empty( $this->start ) || ! empty( $this->end ) ) { |
||
| 206 | |||
| 207 | if ( ! empty( $this->start ) ) { |
||
| 208 | $args['date_query'][0]['after'] = date( 'Y-n-d 00:00:00', strtotime( $this->start ) ); |
||
| 209 | } |
||
| 210 | |||
| 211 | if ( ! empty( $this->end ) ) { |
||
| 212 | $args['date_query'][0]['before'] = date( 'Y-n-d 00:00:00', strtotime( $this->end ) ); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | // Check for price option |
||
| 217 | View Code Duplication | if ( null !== $this->price_id ) { |
|
| 218 | $args['meta_query'] = array( |
||
| 219 | array( |
||
| 220 | 'key' => '_give_payment_price_id', |
||
| 221 | 'value' => (int) $this->price_id, |
||
| 222 | ), |
||
| 223 | ); |
||
| 224 | } |
||
| 225 | |||
| 226 | if ( ! empty( $this->form_id ) ) { |
||
| 227 | $args['give_forms'] = array( $this->form_id ); |
||
| 228 | } |
||
| 229 | |||
| 230 | // Payment query. |
||
| 231 | $payments = give_get_payments( $args ); |
||
| 232 | |||
| 233 | if ( $payments ) { |
||
| 234 | |||
| 235 | foreach ( $payments as $payment ) { |
||
| 236 | |||
| 237 | $columns = $this->csv_cols(); |
||
| 238 | $payment_meta = give_get_payment_meta( $payment->ID ); |
||
| 239 | $payment = new Give_Payment( $payment->ID ); |
||
| 240 | $donor = new Give_Donor( give_get_payment_donor_id( $payment->ID ) ); |
||
| 241 | $address = ''; |
||
| 242 | |||
| 243 | if ( isset( $donor->user_id ) && $donor->user_id > 0 ) { |
||
| 244 | $address = give_get_donor_address( $donor->user_id ); |
||
| 245 | } |
||
| 246 | $name_array = explode( ' ', $donor->name ); |
||
| 247 | |||
| 248 | // Set columns |
||
| 249 | if ( ! empty( $this->cols['donation_id'] ) ) { |
||
| 250 | $data[ $i ]['donation_id'] = $payment->ID; |
||
| 251 | } |
||
| 252 | if ( ! empty( $this->cols['first_name'] ) ) { |
||
| 253 | $data[ $i ]['first_name'] = isset( $name_array[0] ) ? $name_array[0] : ''; |
||
| 254 | } |
||
| 255 | if ( ! empty( $this->cols['last_name'] ) ) { |
||
| 256 | $data[ $i ]['last_name'] = ( isset( $name_array[1] ) ? $name_array[1] : '' ) . ( isset( $name_array[2] ) ? ' ' . $name_array[2] : '' ) . ( isset( $name_array[3] ) ? ' ' . $name_array[3] : '' ); |
||
| 257 | } |
||
| 258 | if ( ! empty( $this->cols['email'] ) ) { |
||
| 259 | $data[ $i ]['email'] = $donor->email; |
||
| 260 | } |
||
| 261 | View Code Duplication | if ( ! empty( $this->cols['address_line1'] ) ) { |
|
| 262 | $data[ $i ]['address_line1'] = isset( $address['line1'] ) ? $address['line1'] : ''; |
||
| 263 | $data[ $i ]['address_line2'] = isset( $address['line2'] ) ? $address['line2'] : ''; |
||
| 264 | $data[ $i ]['address_city'] = isset( $address['city'] ) ? $address['city'] : ''; |
||
| 265 | $data[ $i ]['address_state'] = isset( $address['state'] ) ? $address['state'] : ''; |
||
| 266 | $data[ $i ]['address_zip'] = isset( $address['zip'] ) ? $address['zip'] : ''; |
||
| 267 | $data[ $i ]['address_country'] = isset( $address['country'] ) ? $address['country'] : ''; |
||
| 268 | } |
||
| 269 | |||
| 270 | if ( ! empty( $this->cols['donation_total'] ) ) { |
||
| 271 | $data[ $i ]['donation_total'] = give_format_amount( give_get_payment_amount( $payment->ID ) ); |
||
| 272 | } |
||
| 273 | |||
| 274 | if ( ! empty( $columns['donation_status'] ) ) { |
||
| 275 | $data[ $i ]['donation_status'] = give_get_payment_status( $payment, true ); |
||
| 276 | } |
||
| 277 | |||
| 278 | if ( ! empty( $columns['payment_gateway'] ) ) { |
||
| 279 | $data[ $i ]['payment_gateway'] = $payment->gateway; |
||
| 280 | } |
||
| 281 | |||
| 282 | if ( ! empty( $columns['form_id'] ) ) { |
||
| 283 | $data[ $i ]['form_id'] = $payment->form_id; |
||
| 284 | } |
||
| 285 | |||
| 286 | if ( ! empty( $columns['form_title'] ) ) { |
||
| 287 | $data[ $i ]['form_title'] = get_the_title( $payment->form_id ); |
||
| 288 | } |
||
| 289 | |||
| 290 | if ( ! empty( $columns['form_level_id'] ) ) { |
||
| 291 | $data[ $i ]['form_level_id'] = $payment->price_id; |
||
| 292 | } |
||
| 293 | |||
| 294 | if ( ! empty( $columns['form_level_title'] ) ) { |
||
| 295 | $var_prices = give_has_variable_prices( $payment_meta['form_id'] ); |
||
| 296 | if ( empty( $var_prices ) ) { |
||
| 297 | $data[ $i ]['form_level_title'] = ''; |
||
| 298 | } else { |
||
| 299 | $prices_atts = ''; |
||
| 300 | View Code Duplication | if ( $variable_prices = give_get_variable_prices( $payment_meta['form_id'] ) ) { |
|
| 301 | foreach ( $variable_prices as $variable_price ) { |
||
| 302 | $prices_atts[ $variable_price['_give_id']['level_id'] ] = give_format_amount( $variable_price['_give_amount'] ); |
||
| 303 | } |
||
| 304 | } |
||
| 305 | $data[ $i ]['form_level_title'] = give_get_price_option_name( $payment->form_id, $payment->price_id ); |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | if ( ! empty( $columns['donation_date'] ) ) { |
||
| 310 | $payment_date = strtotime( $payment->date ); |
||
| 311 | $data[ $i ]['donation_date'] = date( give_date_format(), $payment_date ); |
||
| 312 | } |
||
| 313 | |||
| 314 | if ( ! empty( $columns['donation_time'] ) ) { |
||
| 315 | $payment_date = strtotime( $payment->date ); |
||
| 316 | $data[ $i ]['donation_time'] = date_i18n( 'H', $payment_date ) . ':' . date( 'i', $payment_date ); |
||
| 317 | } |
||
| 318 | |||
| 319 | if ( ! empty( $columns['userid'] ) ) { |
||
| 320 | $data[ $i ]['userid'] = $payment->user_id; |
||
| 321 | } |
||
| 322 | |||
| 323 | if ( ! empty( $columns['donorid'] ) ) { |
||
| 324 | $data[ $i ]['donorid'] = $payment->customer_id; |
||
| 325 | } |
||
| 326 | |||
| 327 | if ( ! empty( $columns['donor_ip'] ) ) { |
||
| 328 | $data[ $i ]['donor_ip'] = give_get_payment_user_ip( $payment->ID ); |
||
| 329 | } |
||
| 330 | |||
| 331 | // Add custom field data. |
||
| 332 | // First we remove the standard included keys from above. |
||
| 333 | $remove_keys = array( |
||
| 334 | 'donation_id', |
||
| 335 | 'first_name', |
||
| 336 | 'last_name', |
||
| 337 | 'email', |
||
| 338 | 'address_line1', |
||
| 339 | 'address_line2', |
||
| 340 | 'address_city', |
||
| 341 | 'address_state', |
||
| 342 | 'address_zip', |
||
| 343 | 'address_country', |
||
| 344 | 'donation_total', |
||
| 345 | 'payment_gateway', |
||
| 346 | 'form_id', |
||
| 347 | 'form_title', |
||
| 348 | 'form_level_id', |
||
| 349 | 'form_level_title', |
||
| 350 | 'donation_date', |
||
| 351 | 'donation_time', |
||
| 352 | 'userid', |
||
| 353 | 'donorid', |
||
| 354 | 'donor_ip', |
||
| 355 | ); |
||
| 356 | |||
| 357 | // Removing above keys... |
||
| 358 | foreach ( $remove_keys as $key ) { |
||
| 359 | unset( $columns[ $key ] ); |
||
| 360 | } |
||
| 361 | |||
| 362 | // Is FFM available? Take care of repeater fields. |
||
| 363 | if ( class_exists( 'Give_FFM_Render_Form' ) ) { |
||
| 364 | |||
| 365 | // Get the custom fields for the payment's form. |
||
| 366 | $ffm = new Give_FFM_Render_Form(); |
||
| 367 | list( |
||
| 368 | $post_fields, |
||
| 369 | $taxonomy_fields, |
||
| 370 | $custom_fields |
||
| 371 | ) = $ffm->get_input_fields( $payment->form_id ); |
||
| 372 | $parents = isset( $this->data['give_give_donations_export_parent'] ) ? $this->data['give_give_donations_export_parent'] : array(); |
||
| 373 | |||
| 374 | |||
| 375 | // Loop through the fields. |
||
| 376 | foreach ( $custom_fields as $field ) { |
||
| 377 | |||
| 378 | // Check if this custom field should be exported first. |
||
| 379 | if ( empty( $parents[ $field['name'] ] ) ) { |
||
| 380 | continue; |
||
| 381 | } |
||
| 382 | |||
| 383 | // Check for Repeater Columns |
||
| 384 | if ( isset( $field['multiple'] ) ) { |
||
| 385 | |||
| 386 | $num_columns = count( $field['columns'] ); |
||
| 387 | |||
| 388 | // Loop through columns |
||
| 389 | for ( $count = 0; $count < $num_columns; $count ++ ) { |
||
| 390 | $keyname = 'repeater_' . give_export_donations_create_column_key( $field['columns'][ $count ] ); |
||
| 391 | $items = (array) $ffm->get_meta( $payment->ID, $field['name'], 'post', false ); |
||
| 392 | |||
| 393 | // Reassemble arrays. |
||
| 394 | if ( $items ) { |
||
| 395 | |||
| 396 | $final_vals = array(); |
||
| 397 | |||
| 398 | foreach ( $items as $item_val ) { |
||
| 399 | |||
| 400 | $item_val = explode( $ffm::$separator, $item_val ); |
||
| 401 | |||
| 402 | // Add relevant fields to array. |
||
| 403 | $final_vals[ $count ][] = $item_val[ $count ]; |
||
| 404 | |||
| 405 | } |
||
| 406 | |||
| 407 | $data[ $i ][ $keyname ] = implode( '| ', $final_vals[ $count ] ); |
||
| 408 | |||
| 409 | } else { |
||
| 410 | $data[ $i ][ $keyname ] = ''; |
||
| 411 | } |
||
| 412 | |||
| 413 | $this->cols[ $keyname ] = ''; |
||
| 414 | |||
| 415 | unset( $columns[ $keyname ] ); |
||
| 416 | |||
| 417 | } |
||
| 418 | |||
| 419 | unset( $this->cols[ $field['name'] ] ); |
||
| 420 | // Unset this to prevent field from catchall field loop below. |
||
| 421 | unset( $columns[ $field['name'] ] ); |
||
| 422 | } |
||
| 423 | } |
||
| 424 | } |
||
| 425 | |||
| 426 | // Now loop through remaining meta fields. |
||
| 427 | foreach ( $columns as $col ) { |
||
| 428 | $field_data = get_post_meta( $payment->ID, $col, true ); |
||
| 429 | $data[ $i ][ $col ] = $field_data; |
||
| 430 | unset( $columns[ $col ] ); |
||
| 431 | } |
||
| 432 | |||
| 433 | // Increment iterator. |
||
| 434 | $i ++; |
||
| 435 | |||
| 436 | } |
||
| 437 | |||
| 438 | $data = apply_filters( 'give_export_get_data', $data ); |
||
| 439 | $data = apply_filters( "give_export_get_data_{$this->export_type}", $data ); |
||
| 440 | |||
| 441 | return $data; |
||
| 442 | |||
| 443 | } |
||
| 444 | |||
| 445 | return array(); |
||
| 446 | |||
| 447 | } |
||
| 448 | |||
| 484 |