| @@ -20,8 +20,9 @@ | ||
| 20 | 20 | * This class handles customers. |
| 21 | 21 | * |
| 22 | 22 | * @since 1.0 |
| 23 | 23 | */ |
| 24 | +#[\AllowDynamicProperties] | |
| 24 | 25 | class Give_Donor { |
| 25 | 26 | |
| 26 | 27 | /** |
| 27 | 28 | * The donor ID |
| @@ -120,9 +121,9 @@ | ||
| 120 | 121 | * @access public |
| 121 | 122 | * |
| 122 | 123 | * @var array |
| 123 | 124 | */ |
| 124 | - public $notes; | |
| 125 | + protected $notes = null; | |
| 125 | 126 | |
| 126 | 127 | /** |
| 127 | 128 | * Donor address. |
| 128 | 129 | * |
| @@ -130,9 +131,9 @@ | ||
| 130 | 131 | * @access public |
| 131 | 132 | * |
| 132 | 133 | * @var array |
| 133 | 134 | */ |
| 134 | - public $address = array(); | |
| 135 | + public $address = []; | |
| 135 | 136 | |
| 136 | 137 | /** |
| 137 | 138 | * The Database Abstraction |
| 138 | 139 | * |
| @@ -200,10 +201,11 @@ | ||
| 200 | 201 | foreach ( $donor as $key => $value ) { |
| 201 | 202 | |
| 202 | 203 | switch ( $key ) { |
| 203 | 204 | |
| 205 | + // @todo We will remove this statement when we will remove notes column from donor table | |
| 206 | + // https://github.com/impress-org/give/issues/3632 | |
| 204 | 207 | case 'notes': |
| 205 | - $this->$key = $this->get_notes(); | |
| 206 | 208 | break; |
| 207 | 209 | |
| 208 | 210 | default: |
| 209 | 211 | $this->$key = $value; |
| @@ -213,9 +215,9 @@ | ||
| 213 | 215 | } |
| 214 | 216 | |
| 215 | 217 | // Get donor's all email including primary email. |
| 216 | 218 | $this->emails = (array) $this->get_meta( 'additional_email', false ); |
| 217 | - $this->emails = array( 'primary' => $this->email ) + $this->emails; | |
| 219 | + $this->emails = [ 'primary' => $this->email ] + $this->emails; | |
| 218 | 220 | |
| 219 | 221 | $this->setup_address(); |
| 220 | 222 | |
| 221 | 223 | Give_Cache::set_group( $donor->id, get_object_vars( $this ), 'give-donors' ); |
| @@ -242,16 +244,28 @@ | ||
| 242 | 244 | * @access public |
| 243 | 245 | */ |
| 244 | 246 | public function setup_address() { |
| 245 | 247 | global $wpdb; |
| 246 | - $meta_type = Give()->donor_meta->meta_type; | |
| 248 | + $meta_type = Give()->donor_meta->meta_type; | |
| 249 | + $meta_table_name = Give()->donor_meta->table_name; | |
| 247 | 250 | |
| 248 | - $addresses = $wpdb->get_results( $wpdb->prepare( " | |
| 249 | - SELECT meta_key, meta_value FROM {$wpdb->donormeta} | |
| 251 | + $addresses = $this->get_addresses_from_meta_cache(); | |
| 252 | + | |
| 253 | + $addresses = ! empty( $addresses ) | |
| 254 | + ? $addresses | |
| 255 | + : $wpdb->get_results( | |
| 256 | + $wpdb->prepare( | |
| 257 | + " | |
| 258 | + SELECT meta_key, meta_value FROM {$meta_table_name} | |
| 250 | 259 | WHERE meta_key |
| 251 | - LIKE '%%%s%%' | |
| 260 | + LIKE '%s' | |
| 252 | 261 | AND {$meta_type}_id=%d |
| 253 | - ", 'give_donor_address', $this->id ), ARRAY_N ); | |
| 262 | + ", | |
| 263 | + '%give_donor_address%', | |
| 264 | + $this->id | |
| 265 | + ), | |
| 266 | + ARRAY_N | |
| 267 | + ); | |
| 254 | 268 | |
| 255 | 269 | if ( empty( $addresses ) ) { |
| 256 | 270 | return $this->address; |
| 257 | 271 | } |
| @@ -267,9 +281,33 @@ | ||
| 267 | 281 | } |
| 268 | 282 | } |
| 269 | 283 | } |
| 270 | 284 | |
| 285 | + | |
| 271 | 286 | /** |
| 287 | + * Get addresses from meta cache | |
| 288 | + * | |
| 289 | + * @since 2.5.0 | |
| 290 | + * @return array | |
| 291 | + */ | |
| 292 | + private function get_addresses_from_meta_cache() { | |
| 293 | + $meta = wp_cache_get( $this->id, 'donor_meta' ); | |
| 294 | + $addresses = []; | |
| 295 | + | |
| 296 | + if ( ! empty( $meta ) ) { | |
| 297 | + foreach ( $meta as $meta_key => $meta_value ) { | |
| 298 | + if ( false === strpos( $meta_key, 'give_donor_address' ) ) { | |
| 299 | + continue; | |
| 300 | + } | |
| 301 | + | |
| 302 | + $addresses[] = [ $meta_key, current( $meta_value ) ]; | |
| 303 | + } | |
| 304 | + } | |
| 305 | + | |
| 306 | + return $addresses; | |
| 307 | + } | |
| 308 | + | |
| 309 | + /** | |
| 272 | 310 | * Returns the saved address for a donor |
| 273 | 311 | * |
| 274 | 312 | * @access public |
| 275 | 313 | * |
| @@ -278,17 +316,17 @@ | ||
| 278 | 316 | * @param array $args donor address. |
| 279 | 317 | * |
| 280 | 318 | * @return array The donor's address, if any |
| 281 | 319 | */ |
| 282 | - public function get_donor_address( $args = array() ) { | |
| 320 | + public function get_donor_address( $args = [] ) { | |
| 283 | 321 | $args = wp_parse_args( |
| 284 | 322 | $args, |
| 285 | - array( | |
| 323 | + [ | |
| 286 | 324 | 'address_type' => 'billing', |
| 287 | - ) | |
| 325 | + ] | |
| 288 | 326 | ); |
| 289 | 327 | |
| 290 | - $default_address = array( | |
| 328 | + $default_address = [ | |
| 291 | 329 | 'line1' => '', |
| 292 | 330 | 'line2' => '', |
| 293 | 331 | 'city' => '', |
| 294 | 332 | 'state' => '', |
| @@ -293,9 +331,9 @@ | ||
| 293 | 331 | 'city' => '', |
| 294 | 332 | 'state' => '', |
| 295 | 333 | 'country' => '', |
| 296 | 334 | 'zip' => '', |
| 297 | - ); | |
| 335 | + ]; | |
| 298 | 336 | |
| 299 | 337 | // Backward compatibility. |
| 300 | 338 | if ( ! give_has_upgrade_completed( 'v20_upgrades_user_address' ) ) { |
| 301 | 339 | |
| @@ -334,9 +372,9 @@ | ||
| 334 | 372 | public function __get( $key ) { |
| 335 | 373 | |
| 336 | 374 | if ( method_exists( $this, 'get_' . $key ) ) { |
| 337 | 375 | |
| 338 | - return call_user_func( array( $this, 'get_' . $key ) ); | |
| 376 | + return call_user_func( [ $this, 'get_' . $key ] ); | |
| 339 | 377 | |
| 340 | 378 | } else { |
| 341 | 379 | |
| 342 | 380 | /* translators: %s: property key */ |
| @@ -355,17 +393,17 @@ | ||
| 355 | 393 | * @param array $data Array of attributes for a donor. |
| 356 | 394 | * |
| 357 | 395 | * @return bool|int False if not a valid creation, donor ID if user is found or valid creation. |
| 358 | 396 | */ |
| 359 | - public function create( $data = array() ) { | |
| 397 | + public function create( $data = [] ) { | |
| 360 | 398 | |
| 361 | 399 | if ( $this->id != 0 || empty( $data ) ) { |
| 362 | 400 | return false; |
| 363 | 401 | } |
| 364 | 402 | |
| 365 | - $defaults = array( | |
| 403 | + $defaults = [ | |
| 366 | 404 | 'payment_ids' => '', |
| 367 | - ); | |
| 405 | + ]; | |
| 368 | 406 | |
| 369 | 407 | $args = wp_parse_args( $data, $defaults ); |
| 370 | 408 | $args = $this->sanitize_columns( $args ); |
| 371 | 409 | |
| @@ -423,9 +461,9 @@ | ||
| 423 | 461 | * @param array $data Array of data attributes for a donor (checked via whitelist). |
| 424 | 462 | * |
| 425 | 463 | * @return bool If the update was successful or not. |
| 426 | 464 | */ |
| 427 | - public function update( $data = array() ) { | |
| 465 | + public function update( $data = [] ) { | |
| 428 | 466 | |
| 429 | 467 | if ( empty( $data ) ) { |
| 430 | 468 | return false; |
| 431 | 469 | } |
| @@ -513,9 +551,9 @@ | ||
| 513 | 551 | * @param int $donor_id Donor id. |
| 514 | 552 | */ |
| 515 | 553 | do_action( 'give_donor_pre_attach_payment', $payment_id, $this->id ); |
| 516 | 554 | |
| 517 | - $payment_added = $this->update( array( 'payment_ids' => $new_payment_ids ) ); | |
| 555 | + $payment_added = $this->update( [ 'payment_ids' => $new_payment_ids ] ); | |
| 518 | 556 | |
| 519 | 557 | if ( $payment_added ) { |
| 520 | 558 | |
| 521 | 559 | $this->payment_ids = $new_payment_ids; |
| @@ -521,9 +559,9 @@ | ||
| 521 | 559 | $this->payment_ids = $new_payment_ids; |
| 522 | 560 | |
| 523 | 561 | // We added this payment successfully, increment the stats |
| 524 | 562 | if ( $update_stats ) { |
| 525 | - $payment_amount = give_donation_amount( $payment_id, array( 'type' => 'stats' ) ); | |
| 563 | + $payment_amount = give_donation_amount( $payment_id, [ 'type' => 'stats' ] ); | |
| 526 | 564 | |
| 527 | 565 | if ( ! empty( $payment_amount ) ) { |
| 528 | 566 | $this->increase_value( $payment_amount ); |
| 529 | 567 | } |
| @@ -598,9 +636,9 @@ | ||
| 598 | 636 | * @param int $donor_id Customer id. |
| 599 | 637 | */ |
| 600 | 638 | do_action( 'give_donor_pre_remove_payment', $payment_id, $this->id ); |
| 601 | 639 | |
| 602 | - $payment_removed = $this->update( array( 'payment_ids' => $new_payment_ids ) ); | |
| 640 | + $payment_removed = $this->update( [ 'payment_ids' => $new_payment_ids ] ); | |
| 603 | 641 | |
| 604 | 642 | if ( $payment_removed ) { |
| 605 | 643 | |
| 606 | 644 | $this->payment_ids = $new_payment_ids; |
| @@ -660,9 +698,9 @@ | ||
| 660 | 698 | * @param int $donor_id Donor id. |
| 661 | 699 | */ |
| 662 | 700 | do_action( 'give_donor_pre_increase_donation_count', $count, $this->id ); |
| 663 | 701 | |
| 664 | - if ( $this->update( array( 'purchase_count' => $new_total ) ) ) { | |
| 702 | + if ( $this->update( [ 'purchase_count' => $new_total ] ) ) { | |
| 665 | 703 | $this->purchase_count = $new_total; |
| 666 | 704 | } |
| 667 | 705 | |
| 668 | 706 | /** |
| @@ -711,9 +749,9 @@ | ||
| 711 | 749 | * @param int $donor_id Customer id. |
| 712 | 750 | */ |
| 713 | 751 | do_action( 'give_donor_pre_decrease_donation_count', $count, $this->id ); |
| 714 | 752 | |
| 715 | - if ( $this->update( array( 'purchase_count' => $new_total ) ) ) { | |
| 753 | + if ( $this->update( [ 'purchase_count' => $new_total ] ) ) { | |
| 716 | 754 | $this->purchase_count = $new_total; |
| 717 | 755 | } |
| 718 | 756 | |
| 719 | 757 | /** |
| @@ -753,9 +791,9 @@ | ||
| 753 | 791 | * @param int $donor_id Customer id. |
| 754 | 792 | */ |
| 755 | 793 | do_action( 'give_donor_pre_increase_value', $value, $this->id ); |
| 756 | 794 | |
| 757 | - if ( $this->update( array( 'purchase_value' => $new_value ) ) ) { | |
| 795 | + if ( $this->update( [ 'purchase_value' => $new_value ] ) ) { | |
| 758 | 796 | $this->purchase_value = $new_value; |
| 759 | 797 | } |
| 760 | 798 | |
| 761 | 799 | /** |
| @@ -799,9 +837,9 @@ | ||
| 799 | 837 | * @param int $donor_id Donor id. |
| 800 | 838 | */ |
| 801 | 839 | do_action( 'give_donor_pre_decrease_value', $value, $this->id ); |
| 802 | 840 | |
| 803 | - if ( $this->update( array( 'purchase_value' => $new_value ) ) ) { | |
| 841 | + if ( $this->update( [ 'purchase_value' => $new_value ] ) ) { | |
| 804 | 842 | $this->purchase_value = $new_value; |
| 805 | 843 | } |
| 806 | 844 | |
| 807 | 845 | /** |
| @@ -907,9 +945,9 @@ | ||
| 907 | 945 | * @param array $args Pass any additional data. |
| 908 | 946 | * |
| 909 | 947 | * @return string|float |
| 910 | 948 | */ |
| 911 | - public function get_total_donation_amount( $args = array() ) { | |
| 949 | + public function get_total_donation_amount( $args = [] ) { | |
| 912 | 950 | |
| 913 | 951 | /** |
| 914 | 952 | * Filter total donation amount. |
| 915 | 953 | * |
| @@ -947,9 +985,9 @@ | ||
| 947 | 985 | |
| 948 | 986 | // Backward compatibility. |
| 949 | 987 | $note_string = date_i18n( 'F j, Y H:i:s', current_time( 'timestamp' ) ) . ' - ' . $note; |
| 950 | 988 | $formatted_new_note = apply_filters( 'give_customer_add_note_string', $note_string ); |
| 951 | - $notes .= "\n\n" . $formatted_new_note; | |
| 989 | + $notes .= "\n\n" . $formatted_new_note; | |
| 952 | 990 | |
| 953 | 991 | /** |
| 954 | 992 | * Fires before donor note is added. |
| 955 | 993 | * |
| @@ -961,17 +999,17 @@ | ||
| 961 | 999 | do_action( 'give_donor_pre_add_note', $formatted_new_note, $this->id ); |
| 962 | 1000 | |
| 963 | 1001 | if ( ! give_has_upgrade_completed( 'v230_move_donor_note' ) ) { |
| 964 | 1002 | // Backward compatibility. |
| 965 | - $updated = $this->update( array( 'notes' => $notes ) ); | |
| 1003 | + $updated = $this->update( [ 'notes' => $notes ] ); | |
| 966 | 1004 | } else { |
| 967 | 1005 | $updated = Give()->comment->db->add( |
| 968 | - array( | |
| 1006 | + [ | |
| 969 | 1007 | 'comment_content' => $note, |
| 970 | 1008 | 'user_id' => get_current_user_id(), |
| 971 | 1009 | 'comment_parent' => $this->id, |
| 972 | 1010 | 'comment_type' => 'donor', |
| 973 | - ) | |
| 1011 | + ] | |
| 974 | 1012 | ); |
| 975 | 1013 | } |
| 976 | 1014 | |
| 977 | 1015 | if ( $updated ) { |
| @@ -1002,12 +1040,12 @@ | ||
| 1002 | 1040 | * @return string The Notes for the donor, non-parsed. |
| 1003 | 1041 | */ |
| 1004 | 1042 | private function get_raw_notes() { |
| 1005 | 1043 | $all_notes = ''; |
| 1006 | - $comments = Give()->comment->db->get_results_by( array( 'comment_parent' => $this->id ) ); | |
| 1044 | + $comments = Give()->comment->db->get_results_by( [ 'comment_parent' => $this->id ] ); | |
| 1007 | 1045 | |
| 1008 | 1046 | // Generate notes output as we are doing before 2.3.0. |
| 1009 | - if( ! empty( $comments ) ) { | |
| 1047 | + if ( ! empty( $comments ) ) { | |
| 1010 | 1048 | /* @var stdClass $comment */ |
| 1011 | 1049 | foreach ( $comments as $comment ) { |
| 1012 | 1050 | $all_notes .= date_i18n( 'F j, Y H:i:s', strtotime( $comment->comment_date ) ) . " - {$comment->comment_content}\n\n"; |
| 1013 | 1051 | } |
| @@ -1013,9 +1051,9 @@ | ||
| 1013 | 1051 | } |
| 1014 | 1052 | } |
| 1015 | 1053 | |
| 1016 | 1054 | // Backward compatibility. |
| 1017 | - if( ! give_has_upgrade_completed('v230_move_donor_note') ) { | |
| 1055 | + if ( ! give_has_upgrade_completed( 'v230_move_donor_note' ) ) { | |
| 1018 | 1056 | $all_notes = $this->db->get_column( 'notes', $this->id ); |
| 1019 | 1057 | } |
| 1020 | 1058 | |
| 1021 | 1059 | return $all_notes; |
| @@ -1049,9 +1087,9 @@ | ||
| 1049 | 1087 | * @param bool $unique Optional. Whether the same key should not be added. Default is false. |
| 1050 | 1088 | * |
| 1051 | 1089 | * @return bool False for failure. True for success. |
| 1052 | 1090 | */ |
| 1053 | - public function add_meta( $meta_key = '', $meta_value, $unique = false ) { | |
| 1091 | + public function add_meta( $meta_key, $meta_value, $unique = false ) { | |
| 1054 | 1092 | return Give()->donor_meta->add_meta( $this->id, $meta_key, $meta_value, $unique ); |
| 1055 | 1093 | } |
| 1056 | 1094 | |
| 1057 | 1095 | /** |
| @@ -1065,9 +1103,9 @@ | ||
| 1065 | 1103 | * @param mixed $prev_value Optional. Previous value to check before removing. Default is empty. |
| 1066 | 1104 | * |
| 1067 | 1105 | * @return bool False on failure, true if success. |
| 1068 | 1106 | */ |
| 1069 | - public function update_meta( $meta_key = '', $meta_value, $prev_value = '' ) { | |
| 1107 | + public function update_meta( $meta_key, $meta_value, $prev_value = '' ) { | |
| 1070 | 1108 | return Give()->donor_meta->update_meta( $this->id, $meta_key, $meta_value, $prev_value ); |
| 1071 | 1109 | } |
| 1072 | 1110 | |
| 1073 | 1111 | /** |
| @@ -1201,8 +1239,9 @@ | ||
| 1201 | 1239 | * |
| 1202 | 1240 | * @return bool If the email was removed successfully. |
| 1203 | 1241 | */ |
| 1204 | 1242 | public function remove_email( $email = '' ) { |
| 1243 | + | |
| 1205 | 1244 | if ( ! is_email( $email ) ) { |
| 1206 | 1245 | return false; |
| 1207 | 1246 | } |
| 1208 | 1247 | |
| @@ -1243,9 +1282,9 @@ | ||
| 1243 | 1282 | |
| 1244 | 1283 | $old_email = $this->email; |
| 1245 | 1284 | |
| 1246 | 1285 | // Update donor record with new email. |
| 1247 | - $update = $this->update( array( 'email' => $new_primary_email ) ); | |
| 1286 | + $update = $this->update( [ 'email' => $new_primary_email ] ); | |
| 1248 | 1287 | |
| 1249 | 1288 | // Remove new primary from list of additional emails. |
| 1250 | 1289 | $remove = $this->remove_email( $new_primary_email ); |
| 1251 | 1290 | |
| @@ -1278,9 +1317,9 @@ | ||
| 1278 | 1317 | |
| 1279 | 1318 | // Address ready to process even if only one value set. |
| 1280 | 1319 | foreach ( $address as $address_type => $value ) { |
| 1281 | 1320 | // @todo: Handle state field validation on basis of country. |
| 1282 | - if ( in_array( $address_type, array( 'line2', 'state' ) ) ) { | |
| 1321 | + if ( in_array( $address_type, [ 'line2', 'state' ] ) ) { | |
| 1283 | 1322 | continue; |
| 1284 | 1323 | } |
| 1285 | 1324 | |
| 1286 | 1325 | if ( empty( $value ) ) { |
| @@ -1320,44 +1359,55 @@ | ||
| 1320 | 1359 | $multi_address_id = null; |
| 1321 | 1360 | if ( $is_multi_address = ( false !== strpos( $address_type, '[]' ) ) ) { |
| 1322 | 1361 | $address_type = $is_multi_address ? str_replace( '[]', '', $address_type ) : $address_type; |
| 1323 | 1362 | } elseif ( $is_multi_address = ( false !== strpos( $address_type, '_' ) ) ) { |
| 1324 | - $multi_address_id = $is_multi_address ? array_pop( explode( '_', $address_type ) ) : $address_type; | |
| 1363 | + $exploded_address_type = explode( '_', $address_type ); | |
| 1364 | + $multi_address_id = $is_multi_address ? array_pop( $exploded_address_type ) : $address_type; | |
| 1325 | 1365 | |
| 1326 | - $address_type = $is_multi_address ? array_shift( explode( '_', $address_type ) ) : $address_type; | |
| 1366 | + $address_type = $is_multi_address ? array_shift( $exploded_address_type ) : $address_type; | |
| 1327 | 1367 | } |
| 1328 | 1368 | |
| 1329 | 1369 | // Bailout: do not save duplicate orders |
| 1330 | - if ( $this->does_address_exist( $address_type, $address ) ) { | |
| 1370 | + if ( $this->does_address_exist( $address_type, $address ) && $multi_address_id === null ) { | |
| 1331 | 1371 | return false; |
| 1332 | 1372 | } |
| 1333 | 1373 | |
| 1334 | 1374 | // Set default address. |
| 1335 | - $address = wp_parse_args( $address, array( | |
| 1336 | - 'line1' => '', | |
| 1337 | - 'line2' => '', | |
| 1338 | - 'city' => '', | |
| 1339 | - 'state' => '', | |
| 1340 | - 'country' => '', | |
| 1341 | - 'zip' => '', | |
| 1342 | - ) ); | |
| 1375 | + $address = wp_parse_args( | |
| 1376 | + $address, | |
| 1377 | + [ | |
| 1378 | + 'line1' => '', | |
| 1379 | + 'line2' => '', | |
| 1380 | + 'city' => '', | |
| 1381 | + 'state' => '', | |
| 1382 | + 'country' => '', | |
| 1383 | + 'zip' => '', | |
| 1384 | + ] | |
| 1385 | + ); | |
| 1343 | 1386 | |
| 1344 | 1387 | // Set meta key prefix. |
| 1345 | 1388 | global $wpdb; |
| 1346 | 1389 | $meta_key_prefix = "_give_donor_address_{$address_type}_{address_name}"; |
| 1347 | 1390 | $meta_type = Give()->donor_meta->meta_type; |
| 1391 | + $meta_table_name = Give()->donor_meta->table_name; | |
| 1348 | 1392 | |
| 1349 | 1393 | if ( $is_multi_address ) { |
| 1350 | 1394 | if ( is_null( $multi_address_id ) ) { |
| 1351 | 1395 | // Get latest address key to set multi address id. |
| 1352 | - $multi_address_id = $wpdb->get_var( $wpdb->prepare( " | |
| 1353 | - SELECT meta_key FROM {$wpdb->donormeta} | |
| 1396 | + $multi_address_id = $wpdb->get_var( | |
| 1397 | + $wpdb->prepare( | |
| 1398 | + " | |
| 1399 | + SELECT meta_key FROM {$meta_table_name} | |
| 1354 | 1400 | WHERE meta_key |
| 1355 | - LIKE '%%%s%%' | |
| 1401 | + LIKE '%s' | |
| 1356 | 1402 | AND {$meta_type}_id=%d |
| 1357 | 1403 | ORDER BY meta_id DESC |
| 1358 | 1404 | LIMIT 1 |
| 1359 | - ", "_give_donor_address_{$address_type}_line1", $this->id ) ); | |
| 1405 | + ", | |
| 1406 | + "%_give_donor_address_{$address_type}_line1%", | |
| 1407 | + $this->id | |
| 1408 | + ) | |
| 1409 | + ); | |
| 1360 | 1410 | |
| 1361 | 1411 | if ( ! empty( $multi_address_id ) ) { |
| 1362 | 1412 | $multi_address_id = absint( substr( strrchr( $multi_address_id, '_' ), 1 ) ); |
| 1363 | 1413 | $multi_address_id ++; |
| @@ -1396,11 +1446,12 @@ | ||
| 1396 | 1446 | |
| 1397 | 1447 | // Get address type. |
| 1398 | 1448 | $is_multi_address = false !== strpos( $address_id, '_' ) ? true : false; |
| 1399 | 1449 | |
| 1400 | - $address_type = false !== strpos( $address_id, '_' ) ? array_shift( explode( '_', $address_id ) ) : $address_id; | |
| 1450 | + $address_key_arr = explode( '_', $address_id ); | |
| 1401 | 1451 | |
| 1402 | - $address_count = false !== strpos( $address_id, '_' ) ? array_pop( explode( '_', $address_id ) ) : null; | |
| 1452 | + $address_type = false !== strpos( $address_id, '_' ) ? array_shift( $address_key_arr ) : $address_id; | |
| 1453 | + $address_count = false !== strpos( $address_id, '_' ) ? array_pop( $address_key_arr ) : null; | |
| 1403 | 1454 | |
| 1404 | 1455 | // Set meta key prefix. |
| 1405 | 1456 | $meta_key_prefix = "_give_donor_address_{$address_type}_%"; |
| 1406 | 1457 | if ( $is_multi_address && is_numeric( $address_count ) ) { |
| @@ -1406,18 +1457,29 @@ | ||
| 1406 | 1457 | if ( $is_multi_address && is_numeric( $address_count ) ) { |
| 1407 | 1458 | $meta_key_prefix .= "_{$address_count}"; |
| 1408 | 1459 | } |
| 1409 | 1460 | |
| 1410 | - $meta_type = Give()->donor_meta->meta_type; | |
| 1461 | + $meta_type = Give()->donor_meta->meta_type; | |
| 1462 | + $meta_table_name = Give()->donor_meta->table_name; | |
| 1411 | 1463 | |
| 1412 | 1464 | // Process query. |
| 1413 | - $row_affected = $wpdb->query( $wpdb->prepare( " | |
| 1414 | - DELETE FROM {$wpdb->donormeta} | |
| 1465 | + $row_affected = $wpdb->query( | |
| 1466 | + $wpdb->prepare( | |
| 1467 | + " | |
| 1468 | + DELETE FROM {$meta_table_name} | |
| 1415 | 1469 | WHERE meta_key |
| 1416 | 1470 | LIKE '%s' |
| 1417 | 1471 | AND {$meta_type}_id=%d |
| 1418 | - ", $meta_key_prefix, $this->id ) ); | |
| 1472 | + ", | |
| 1473 | + $meta_key_prefix, | |
| 1474 | + $this->id | |
| 1475 | + ) | |
| 1476 | + ); | |
| 1419 | 1477 | |
| 1478 | + // Delete cache. | |
| 1479 | + Give_Cache::delete_group( $this->id, 'give-donors' ); | |
| 1480 | + wp_cache_delete( $this->id, "{$meta_type}_meta" ); | |
| 1481 | + | |
| 1420 | 1482 | $this->setup_address(); |
| 1421 | 1483 | |
| 1422 | 1484 | return (bool) $row_affected; |
| 1423 | 1485 | } |
| @@ -1437,13 +1499,14 @@ | ||
| 1437 | 1499 | public function update_address( $address_id, $address ) { |
| 1438 | 1500 | global $wpdb; |
| 1439 | 1501 | |
| 1440 | 1502 | // Get address type. |
| 1441 | - $is_multi_address = false !== strpos( $address_id, '_' ) ? true : false; | |
| 1503 | + $is_multi_address = false !== strpos( $address_id, '_' ) ? true : false; | |
| 1504 | + $exploded_address_id = explode( '_', $address_id ); | |
| 1442 | 1505 | |
| 1443 | - $address_type = false !== strpos( $address_id, '_' ) ? array_shift( explode( '_', $address_id ) ) : $address_id; | |
| 1506 | + $address_type = false !== strpos( $address_id, '_' ) ? array_shift( $exploded_address_id ) : $address_id; | |
| 1444 | 1507 | |
| 1445 | - $address_count = false !== strpos( $address_id, '_' ) ? array_pop( explode( '_', $address_id ) ) : null; | |
| 1508 | + $address_count = false !== strpos( $address_id, '_' ) ? array_pop( $exploded_address_id ) : null; | |
| 1446 | 1509 | |
| 1447 | 1510 | // Set meta key prefix. |
| 1448 | 1511 | $meta_key_prefix = "_give_donor_address_{$address_type}_%"; |
| 1449 | 1512 | if ( $is_multi_address && is_numeric( $address_count ) ) { |
| @@ -1449,17 +1512,24 @@ | ||
| 1449 | 1512 | if ( $is_multi_address && is_numeric( $address_count ) ) { |
| 1450 | 1513 | $meta_key_prefix .= "_{$address_count}"; |
| 1451 | 1514 | } |
| 1452 | 1515 | |
| 1453 | - $meta_type = Give()->donor_meta->meta_type; | |
| 1516 | + $meta_type = Give()->donor_meta->meta_type; | |
| 1517 | + $meta_table_name = Give()->donor_meta->table_name; | |
| 1454 | 1518 | |
| 1455 | 1519 | // Process query. |
| 1456 | - $row_affected = $wpdb->get_results( $wpdb->prepare( " | |
| 1457 | - SELECT meta_key FROM {$wpdb->donormeta} | |
| 1520 | + $row_affected = $wpdb->get_results( | |
| 1521 | + $wpdb->prepare( | |
| 1522 | + " | |
| 1523 | + SELECT meta_key FROM {$meta_table_name} | |
| 1458 | 1524 | WHERE meta_key |
| 1459 | 1525 | LIKE '%s' |
| 1460 | 1526 | AND {$meta_type}_id=%d |
| 1461 | - ", $meta_key_prefix, $this->id ) ); | |
| 1527 | + ", | |
| 1528 | + $meta_key_prefix, | |
| 1529 | + $this->id | |
| 1530 | + ) | |
| 1531 | + ); | |
| 1462 | 1532 | |
| 1463 | 1533 | // Return result. |
| 1464 | 1534 | if ( ! count( $row_affected ) ) { |
| 1465 | 1535 | return false; |
| @@ -1503,9 +1573,9 @@ | ||
| 1503 | 1573 | |
| 1504 | 1574 | switch ( true ) { |
| 1505 | 1575 | |
| 1506 | 1576 | // Single address. |
| 1507 | - case is_string( end( $address ) ) : | |
| 1577 | + case is_string( end( $address ) ): | |
| 1508 | 1578 | $status = $this->is_address_match( $current_address, $address ); |
| 1509 | 1579 | break; |
| 1510 | 1580 | |
| 1511 | 1581 | // Multi address. |
| @@ -1538,9 +1608,9 @@ | ||
| 1538 | 1608 | * |
| 1539 | 1609 | * @return bool |
| 1540 | 1610 | */ |
| 1541 | 1611 | private function is_address_match( $address_1, $address_2 ) { |
| 1542 | - $result = array_diff( $address_1, $address_2 ); | |
| 1612 | + $result = array_diff_assoc( $address_1, $address_2 ); | |
| 1543 | 1613 | |
| 1544 | 1614 | return empty( $result ); |
| 1545 | 1615 | } |
| 1546 | 1616 | |
| @@ -1567,9 +1637,12 @@ | ||
| 1567 | 1637 | if ( is_array( $split_donor_name ) && ! empty( $split_donor_name[1] ) ) { |
| 1568 | 1638 | $last_name = $split_donor_name[1]; |
| 1569 | 1639 | } |
| 1570 | 1640 | |
| 1571 | - return (object) array( 'first_name' => $first_name, 'last_name' => $last_name ); | |
| 1641 | + return (object) [ | |
| 1642 | + 'first_name' => $first_name, | |
| 1643 | + 'last_name' => $last_name, | |
| 1644 | + ]; | |
| 1572 | 1645 | } |
| 1573 | 1646 | |
| 1574 | 1647 | /** |
| 1575 | 1648 | * Retrieves first name of donor with backward compatibility |
| @@ -1644,9 +1717,9 @@ | ||
| 1644 | 1717 | public function get_last_donation_date( $formatted = false ) { |
| 1645 | 1718 | $completed_data = ''; |
| 1646 | 1719 | |
| 1647 | 1720 | // Return if donation id is invalid. |
| 1648 | - if( ! ( $last_donation = absint( $this->get_last_donation() ) ) ) { | |
| 1721 | + if ( ! ( $last_donation = absint( $this->get_last_donation() ) ) ) { | |
| 1649 | 1722 | return $completed_data; |
| 1650 | 1723 | } |
| 1651 | 1724 | |
| 1652 | 1725 | $completed_data = give_get_payment_completed_date( $last_donation ); |
| @@ -1673,12 +1746,14 @@ | ||
| 1673 | 1746 | * @since 2.1.0 |
| 1674 | 1747 | */ |
| 1675 | 1748 | return apply_filters( |
| 1676 | 1749 | 'get_donor_initals', |
| 1677 | - give_get_name_initial( array( | |
| 1678 | - 'firstname' => $this->get_first_name(), | |
| 1679 | - 'lastname' => $this->get_last_name() | |
| 1680 | - ) ) | |
| 1750 | + give_get_name_initial( | |
| 1751 | + [ | |
| 1752 | + 'firstname' => $this->get_first_name(), | |
| 1753 | + 'lastname' => $this->get_last_name(), | |
| 1754 | + ] | |
| 1755 | + ) | |
| 1681 | 1756 | ); |
| 1682 | 1757 | |
| 1683 | 1758 | } |
| 1684 | 1759 | |