From 2f6aaa85bb2e83bcd223d850b50b8175c5188901 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 8 Aug 2017 13:04:27 +0100 Subject: [PATCH] Avoid using meta directly --- includes/class-wc-customer.php | 22 ++++++++++++++++++++++ includes/wc-account-functions.php | 30 +++++++++--------------------- templates/myaccount/my-address.php | 8 +++----- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/includes/class-wc-customer.php b/includes/class-wc-customer.php index e12916886a6..ae3e368cb3a 100644 --- a/includes/class-wc-customer.php +++ b/includes/class-wc-customer.php @@ -439,6 +439,17 @@ class WC_Customer extends WC_Legacy_Customer { return $value; } + /** + * Get billing. + * + * @since 3.2.0 + * @param string $context + * @return array + */ + public function get_billing( $context = 'view' ) { + return $this->get_prop( 'billing', $context ); + } + /** * Get billing_first_name. * @@ -559,6 +570,17 @@ class WC_Customer extends WC_Legacy_Customer { return $this->get_address_prop( 'phone', 'billing', $context ); } + /** + * Get shipping. + * + * @since 3.2.0 + * @param string $context + * @return array + */ + public function get_shipping( $context = 'view' ) { + return $this->get_prop( 'shipping', $context ); + } + /** * Get shipping_first_name. * diff --git a/includes/wc-account-functions.php b/includes/wc-account-functions.php index 20a35a3adbb..aabb09b45ba 100644 --- a/includes/wc-account-functions.php +++ b/includes/wc-account-functions.php @@ -244,30 +244,18 @@ function wc_get_account_payment_methods_types() { * Get account formatted address. * * @since 3.2.0 - * @param string $name + * @param string $address_type billing or shipping. * @return string */ -function wc_get_account_formatted_address( $name ) { - $customer_id = get_current_user_id(); - $meta_keys = [ - 'first_name', - 'last_name', - 'company', - 'address_1', - 'address_2', - 'city', - 'postcode', - 'country', - ]; - - $meta = []; - foreach ( $meta_keys as $key ) { - $meta[ $key ] = get_user_meta( $customer_id, $name . '_' . $key, true ); +function wc_get_account_formatted_address( $address_type ) { + $getter = "get_{$address_type}"; + $customer = new WC_Customer( get_current_user_id() ); + + if ( is_callable( array( $customer, $getter ) ) ) { + $address = $customer->$getter(); + unset( $address['email'], $address['tel'] ); } - - $address = apply_filters( 'woocommerce_my_account_my_address_formatted_address', $meta, $customer_id, $name ); - - return WC()->countries->get_formatted_address( $address ); + return WC()->countries->get_formatted_address( apply_filters( 'woocommerce_my_account_my_address_formatted_address', $address, $customer->get_id(), $address_type ) ); } /** diff --git a/templates/myaccount/my-address.php b/templates/myaccount/my-address.php index 420c414ceca..6ef25a84e39 100644 --- a/templates/myaccount/my-address.php +++ b/templates/myaccount/my-address.php @@ -52,12 +52,10 @@ $col = 1;

-
- -
+ echo $address ? wp_kses_post( $address ) : esc_html_e( 'You have not set up this type of address yet.', 'woocommerce' ); + ?>