| Conditions | 34 |
| Paths | > 20000 |
| Total Lines | 154 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 275 | function give_bc_v20_get_payment_meta( $check, $object_id, $meta_key, $single ) { |
||
| 276 | // Bailout. |
||
| 277 | if ( |
||
| 278 | 'give_payment' !== get_post_type( $object_id ) |
||
| 279 | || '_give_payment_meta' !== $meta_key |
||
| 280 | ) { |
||
| 281 | return $check; |
||
| 282 | } |
||
| 283 | |||
| 284 | $cache_key = "_give_payment_meta_{$object_id}"; |
||
| 285 | |||
| 286 | // Get already calculate payment meta from cache. |
||
| 287 | $payment_meta = Give_Cache::get_db_query( $cache_key ); |
||
| 288 | |||
| 289 | if ( is_null( $payment_meta ) ) { |
||
| 290 | // Remove filter. |
||
| 291 | remove_filter( 'get_post_metadata', 'give_bc_v20_get_payment_meta', 999 ); |
||
| 292 | |||
| 293 | $donation = new Give_Payment( $object_id ); |
||
| 294 | |||
| 295 | // Get all payment meta. |
||
| 296 | $payment_meta = give_get_meta( $object_id ); |
||
| 297 | |||
| 298 | // Set default value to array. |
||
| 299 | if ( empty( $payment_meta ) ) { |
||
| 300 | return $check; |
||
| 301 | } |
||
| 302 | |||
| 303 | // Convert all meta key value to string instead of array |
||
| 304 | array_walk( $payment_meta, function ( &$meta, $key ) { |
||
| 305 | $meta = current( $meta ); |
||
| 306 | } ); |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Add backward compatibility to old meta keys. |
||
| 310 | */ |
||
| 311 | // Donation key. |
||
| 312 | $payment_meta['key'] = ! empty( $payment_meta['_give_payment_purchase_key'] ) ? $payment_meta['_give_payment_purchase_key'] : ''; |
||
| 313 | |||
| 314 | // Donation form. |
||
| 315 | $payment_meta['form_title'] = ! empty( $payment_meta['_give_payment_form_title'] ) ? $payment_meta['_give_payment_form_title'] : ''; |
||
| 316 | |||
| 317 | // Donor email. |
||
| 318 | $payment_meta['email'] = ! empty( $payment_meta['_give_payment_donor_email'] ) ? $payment_meta['_give_payment_donor_email'] : ''; |
||
| 319 | $payment_meta['email'] = ! empty( $payment_meta['email'] ) ? |
||
| 320 | $payment_meta['email'] : |
||
| 321 | Give()->donors->get_column( 'email', $donation->donor_id ); |
||
| 322 | |||
| 323 | // Form id. |
||
| 324 | $payment_meta['form_id'] = ! empty( $payment_meta['_give_payment_form_id'] ) ? $payment_meta['_give_payment_form_id'] : ''; |
||
| 325 | |||
| 326 | // Price id. |
||
| 327 | $payment_meta['price_id'] = ! empty( $payment_meta['_give_payment_price_id'] ) ? $payment_meta['_give_payment_price_id'] : ''; |
||
| 328 | |||
| 329 | // Date. |
||
| 330 | $payment_meta['date'] = ! empty( $payment_meta['_give_payment_date'] ) ? $payment_meta['_give_payment_date'] : ''; |
||
| 331 | $payment_meta['date'] = ! empty( $payment_meta['date'] ) ? |
||
| 332 | $payment_meta['date'] : |
||
| 333 | get_post_field( 'post_date', $object_id ); |
||
| 334 | |||
| 335 | |||
| 336 | // Currency. |
||
| 337 | $payment_meta['currency'] = ! empty( $payment_meta['_give_payment_currency'] ) ? $payment_meta['_give_payment_currency'] : ''; |
||
| 338 | |||
| 339 | // Decode donor data. |
||
| 340 | $donor_id = ! empty( $payment_meta['_give_payment_donor_id'] ) ? $payment_meta['_give_payment_donor_id'] : 0; |
||
| 341 | $donor = new Give_Donor( $donor_id ); |
||
| 342 | |||
| 343 | // Donor first name. |
||
| 344 | $donor_data['first_name'] = ! empty( $payment_meta['_give_donor_billing_first_name'] ) ? $payment_meta['_give_donor_billing_first_name'] : ''; |
||
| 345 | $donor_data['first_name'] = ! empty( $donor_data['first_name'] ) ? |
||
| 346 | $donor_data['first_name'] : |
||
| 347 | $donor->get_first_name(); |
||
| 348 | |||
| 349 | // Donor last name. |
||
| 350 | $donor_data['last_name'] = ! empty( $payment_meta['_give_donor_billing_last_name'] ) ? $payment_meta['_give_donor_billing_last_name'] : ''; |
||
| 351 | $donor_data['last_name'] = ! empty( $donor_data['last_name'] ) ? |
||
| 352 | $donor_data['last_name'] : |
||
| 353 | $donor->get_last_name(); |
||
| 354 | |||
| 355 | // Donor email. |
||
| 356 | $donor_data['email'] = $payment_meta['email']; |
||
| 357 | |||
| 358 | // User ID. |
||
| 359 | $donor_data['id'] = $donation->user_id; |
||
| 360 | |||
| 361 | $donor_data['address'] = false; |
||
| 362 | |||
| 363 | // Address1. |
||
| 364 | $address1 = ! empty( $payment_meta['_give_donor_billing_address1'] ) ? $payment_meta['_give_donor_billing_address1'] : ''; |
||
| 365 | if ( $address1 ) { |
||
| 366 | $donor_data['address']['line1'] = $address1; |
||
| 367 | } |
||
| 368 | |||
| 369 | // Address2. |
||
| 370 | $address2 = ! empty( $payment_meta['_give_donor_billing_address2'] ) ? $payment_meta['_give_donor_billing_address2'] : ''; |
||
| 371 | if ( $address2 ) { |
||
| 372 | $donor_data['address']['line2'] = $address2; |
||
| 373 | } |
||
| 374 | |||
| 375 | // City. |
||
| 376 | $city = ! empty( $payment_meta['_give_donor_billing_city'] ) ? $payment_meta['_give_donor_billing_city'] : ''; |
||
| 377 | if ( $city ) { |
||
| 378 | $donor_data['address']['city'] = $city; |
||
| 379 | } |
||
| 380 | |||
| 381 | // Zip. |
||
| 382 | $zip = ! empty( $payment_meta['_give_donor_billing_zip'] ) ? $payment_meta['_give_donor_billing_zip'] : ''; |
||
| 383 | if ( $zip ) { |
||
| 384 | $donor_data['address']['zip'] = $zip; |
||
| 385 | } |
||
| 386 | |||
| 387 | // State. |
||
| 388 | $state = ! empty( $payment_meta['_give_donor_billing_state'] ) ? $payment_meta['_give_donor_billing_state'] : ''; |
||
| 389 | if ( $state ) { |
||
| 390 | $donor_data['address']['state'] = $state; |
||
| 391 | } |
||
| 392 | |||
| 393 | // Country. |
||
| 394 | $country = ! empty( $payment_meta['_give_donor_billing_country'] ) ? $payment_meta['_give_donor_billing_country'] : ''; |
||
| 395 | if ( $country ) { |
||
| 396 | $donor_data['address']['country'] = $country; |
||
| 397 | } |
||
| 398 | |||
| 399 | $payment_meta['user_info'] = $donor_data; |
||
| 400 | |||
| 401 | // Add filter |
||
| 402 | add_filter( 'get_post_metadata', 'give_bc_v20_get_payment_meta', 999, 4 ); |
||
| 403 | |||
| 404 | // Set custom meta key into payment meta. |
||
| 405 | if ( ! empty( $payment_meta['_give_payment_meta'] ) ) { |
||
| 406 | $payment_meta['_give_payment_meta'] = is_array( $payment_meta['_give_payment_meta'] ) ? $payment_meta['_give_payment_meta'] : array(); |
||
| 407 | |||
| 408 | $payment_meta = array_merge( maybe_unserialize( $payment_meta['_give_payment_meta'] ), $payment_meta ); |
||
| 409 | } |
||
| 410 | |||
| 411 | // Set cache. |
||
| 412 | Give_Cache::set_db_query( $cache_key, $payment_meta ); |
||
| 413 | } |
||
| 414 | |||
| 415 | if ( $single ) { |
||
| 416 | /** |
||
| 417 | * Filter the payment meta |
||
| 418 | * Add custom meta key to payment meta |
||
| 419 | * |
||
| 420 | * @since 2.0 |
||
| 421 | */ |
||
| 422 | $new_payment_meta[0] = apply_filters( 'give_get_payment_meta', $payment_meta, $object_id, $meta_key ); |
||
| 423 | |||
| 424 | $payment_meta = $new_payment_meta; |
||
| 425 | } |
||
| 426 | |||
| 427 | return $payment_meta; |
||
| 428 | } |
||
| 429 | |||
| 460 |