/** * Maybe create billing address from legacy meta. * * @param PaymentInfo $payment The payment to read. * @return void */ private function maybe_create_billing_address_from_legacy_meta( $payment ) { if ( null !== $payment->get_billing_address() ) { // Bail out if there is already a billing address. return; } $id = $payment->get_id(); if ( empty( $id ) ) { return; } $data = array( 'line_1' => $this->get_meta_string( $id, 'address' ), 'postal_code' => $this->get_meta_string( $id, 'zip' ), 'city' => $this->get_meta_string( $id, 'city' ), 'country' => $this->get_meta_string( $id, 'country' ), 'email' => $this->get_meta_string( $id, 'email' ), 'phone' => $this->get_meta_string( $id, 'telephone_number' ), ); $data = array_filter( $data ); $data = array_map( 'trim', $data ); $data = array_filter( $data ); if ( empty( $data ) ) { // Bail out if there is no address data. return; } $address = new Address(); $payment->set_billing_address( $address ); $address->set_name( $this->get_contact_name_from_legacy_meta( $payment ) ); if ( isset( $data['line_1'] ) ) { $address->set_line_1( $data['line_1'] ); } if ( isset( $data['postal_code'] ) ) { $address->set_postal_code( $data['postal_code'] ); } if ( isset( $data['city'] ) ) { $address->set_city( $data['city'] ); } if ( isset( $data['country'] ) ) { if ( 2 === strlen( $data['country'] ) ) { $address->set_country_code( $data['country'] ); } else { $address->set_country_name( $data['country'] ); } } if ( isset( $data['email'] ) ) { $address->set_email( $data['email'] ); } if ( isset( $data['phone'] ) ) { $address->set_phone( $data['phone'] ); } } /** * Maybe create consumer bank details from legacy meta. * * @param PaymentInfo $payment The payment to read. * @return void */ private function maybe_create_consumer_bank_details_from_legacy_meta( $payment ) { if ( null !== $payment->get_consumer_bank_details() ) { // Bail out if there is already a billing consumer_bank_details. return; } $id = $payment->get_id(); if ( empty( $id ) ) { return; } $data = array( 'consumer_name' => $this->get_meta_string( $id, 'consumer_name' ), 'consumer_account_number' => $this->get_meta_string( $id, 'consumer_account_number' ), 'consumer_iban' => $this->get_meta_string( $id, 'consumer_iban' ), 'consumer_bic' => $this->get_meta_string( $id, 'consumer_bic' ), 'consumer_city' => $this->get_meta_string( $id, 'consumer_city' ), ); $data = array_filter( $data ); $data = array_map( 'trim', $data ); $data = array_filter( $data ); if ( empty( $data ) ) { // Bail out if there is no consumer data. return; } $consumer_bank_details = new BankAccountDetails(); $payment->set_consumer_bank_details( $consumer_bank_details ); if ( isset( $data['consumer_name'] ) ) { $consumer_bank_details->set_name( $data['consumer_name'] ); } if ( isset( $data['consumer_account_number'] ) ) { $consumer_bank_details->set_account_number( $data['consumer_account_number'] ); } if ( isset( $data['consumer_iban'] ) ) { $consumer_bank_details->set_iban( $data['consumer_iban'] ); } if ( isset( $data['consumer_bic'] ) ) { $consumer_bank_details->set_bic( $data['consumer_bic'] ); } if ( isset( $data['consumer_city'] ) ) { $consumer_bank_details->set_city( $data['consumer_city'] ); } } /** * Read post meta. * * @link https://github.com/woocommerce/woocommerce/blob/3.2.6/includes/abstracts/abstract-wc-data.php#L462-L507 * @param PaymentInfo $payment The payment to read. * @return void */ protected function read_post_meta( $payment ) { $this->maybe_create_customer_from_legacy_meta( $payment ); $this->maybe_create_billing_address_from_legacy_meta( $payment ); $this->maybe_create_consumer_bank_details_from_legacy_meta( $payment ); } /** * Get update meta. * * @param PaymentInfo $payment The payment to update. * @param array $meta Meta array. * * @return array */ protected function get_update_meta( $payment, $meta = array() ) { // Customer. $customer = $payment->get_customer(); if ( null !== $customer ) { // Deprecated meta values. $meta['language'] = $customer->get_language(); $meta['locale'] = $customer->get_locale(); $meta['user_agent'] = $customer->get_user_agent(); $meta['user_ip'] = $customer->get_ip_address(); $name = $customer->get_name(); if ( null !== $name ) { $meta['customer_name'] = (string) $name; $meta['first_name'] = $name->get_first_name(); $meta['last_name'] = $name->get_last_name(); } } $billing_address = $payment->get_billing_address(); if ( null !== $billing_address ) { // Deprecated meta values. $meta['address'] = $billing_address->get_line_1(); $meta['zip'] = $billing_address->get_postal_code(); $meta['city'] = $billing_address->get_city(); $meta['country'] = $billing_address->get_country_name(); $meta['telephone_number'] = $billing_address->get_phone(); } // Consumer. $consumer_bank_details = $payment->get_consumer_bank_details(); if ( null !== $consumer_bank_details ) { // Deprecated meta values. $meta['consumer_name'] = $consumer_bank_details->get_name(); $meta['consumer_account_number'] = $consumer_bank_details->get_account_number(); $meta['consumer_iban'] = $consumer_bank_details->get_iban(); $meta['consumer_bic'] = $consumer_bank_details->get_bic(); $meta['consumer_city'] = $consumer_bank_details->get_city(); } return $meta; } }