diff --git a/includes/api/class-wc-rest-customers-controller.php b/includes/api/class-wc-rest-customers-controller.php index 41ec3f612b9..3ba25e92e26 100644 --- a/includes/api/class-wc-rest-customers-controller.php +++ b/includes/api/class-wc-rest-customers-controller.php @@ -320,7 +320,7 @@ class WC_REST_Customers_Controller extends WC_REST_Controller { $this->update_customer_meta_fields( $customer, $request ); $customer->save(); - $user_data = get_user_by( 'id', $customer->get_id() ); + $user_data = get_userdata( $customer->get_id() ); $this->update_additional_fields_for_object( $user_data, $request ); /** @@ -532,7 +532,7 @@ class WC_REST_Customers_Controller extends WC_REST_Controller { if ( isset( $request['meta_data'] ) ) { if ( is_array( $request['meta_data'] ) ) { foreach ( $request['meta_data'] as $meta ) { - $coupon->update_meta_data( $meta['key'], $meta['value'], $meta['id'] ); + $customer->update_meta_data( $meta['key'], $meta['value'], $meta['id'] ); } } } @@ -551,7 +551,7 @@ class WC_REST_Customers_Controller extends WC_REST_Controller { if ( isset( $request['billing'] ) ) { foreach ( array_keys( $schema['properties']['billing']['properties'] ) as $field ) { if ( isset( $request['billing'][ $field ] ) && is_callable( array( $customer, "set_billing_{$field}" ) ) ) { - $customer->{"set_billing_{$field}"}( $request['billing'][ $field ] ); + $customer->{"set_billing_{$field}"}( $request['billing'][ $field ] ); } } } @@ -560,7 +560,7 @@ class WC_REST_Customers_Controller extends WC_REST_Controller { if ( isset( $request['shipping'] ) ) { foreach ( array_keys( $schema['properties']['shipping']['properties'] ) as $field ) { if ( isset( $request['shipping'][ $field ] ) && is_callable( array( $customer, "set_shipping_{$field}" ) ) ) { - $customer->{"set_shipping_{$field}"}( $request['shipping'][ $field ] ); + $customer->{"set_shipping_{$field}"}( $request['shipping'][ $field ] ); } } } diff --git a/includes/api/legacy/v1/class-wc-api-customers.php b/includes/api/legacy/v1/class-wc-api-customers.php index e36d59d8d3f..3e1e3736244 100644 --- a/includes/api/legacy/v1/class-wc-api-customers.php +++ b/includes/api/legacy/v1/class-wc-api-customers.php @@ -124,56 +124,47 @@ class WC_API_Customers extends WC_API_Resource { $id = $this->validate_request( $id, 'customer', 'read' ); - if ( is_wp_error( $id ) ) + if ( is_wp_error( $id ) ) { return $id; + } - $customer = new WP_User( $id ); - - // get info about user's last order - $last_order = $wpdb->get_row( "SELECT id, post_date_gmt - FROM $wpdb->posts AS posts - LEFT JOIN {$wpdb->postmeta} AS meta on posts.ID = meta.post_id - WHERE meta.meta_key = '_customer_user' - AND meta.meta_value = {$customer->ID} - AND posts.post_type = 'shop_order' - AND posts.post_status IN ( '" . implode( "','", array_keys( wc_get_order_statuses() ) ) . "' ) - " ); - + $customer = new WC_Customer( $id ); + $last_order = $customer->get_last_order(); $customer_data = array( - 'id' => $customer->ID, - 'created_at' => $this->server->format_datetime( $customer->user_registered ), - 'email' => $customer->user_email, - 'first_name' => $customer->first_name, - 'last_name' => $customer->last_name, - 'username' => $customer->user_login, + 'id' => $customer->get_id(), + 'created_at' => $this->server->format_datetime( $customer->get_date_created() ), + 'email' => $customer->get_email(), + 'first_name' => $customer->get_first_name(), + 'last_name' => $customer->get_last_name(), + 'username' => $customer->get_username(), 'last_order_id' => is_object( $last_order ) ? $last_order->get_id() : null, - 'last_order_date' => is_object( $last_order ) ? $this->server->format_datetime( $last_order->post_date_gmt ) : null, - 'orders_count' => (int) $customer->_order_count, - 'total_spent' => wc_format_decimal( $customer->_money_spent, 2 ), - 'avatar_url' => $this->get_avatar_url( $customer->customer_email ), + 'last_order_date' => is_object( $last_order ) ? $this->server->format_datetime( $last_order->get_date_created() ) : null, + 'orders_count' => $customer->get_order_count(), + 'total_spent' => wc_format_decimal( $customer->get_total_spent(), 2 ), + 'avatar_url' => $customer->get_avatar_url(), 'billing_address' => array( - 'first_name' => $customer->billing_first_name, - 'last_name' => $customer->billing_last_name, - 'company' => $customer->billing_company, - 'address_1' => $customer->billing_address_1, - 'address_2' => $customer->billing_address_2, - 'city' => $customer->billing_city, - 'state' => $customer->billing_state, - 'postcode' => $customer->billing_postcode, - 'country' => $customer->billing_country, - 'email' => $customer->billing_email, - 'phone' => $customer->billing_phone, + 'first_name' => $customer->get_billing_first_name(), + 'last_name' => $customer->get_billing_last_name(), + 'company' => $customer->get_billing_company(), + 'address_1' => $customer->get_billing_address_1(), + 'address_2' => $customer->get_billing_address_2(), + 'city' => $customer->get_billing_city(), + 'state' => $customer->get_billing_state(), + 'postcode' => $customer->get_billing_postcode(), + 'country' => $customer->get_billing_country(), + 'email' => $customer->get_billing_email(), + 'phone' => $customer->get_billing_phone(), ), 'shipping_address' => array( - 'first_name' => $customer->shipping_first_name, - 'last_name' => $customer->shipping_last_name, - 'company' => $customer->shipping_company, - 'address_1' => $customer->shipping_address_1, - 'address_2' => $customer->shipping_address_2, - 'city' => $customer->shipping_city, - 'state' => $customer->shipping_state, - 'postcode' => $customer->shipping_postcode, - 'country' => $customer->shipping_country, + 'first_name' => $customer->get_shipping_first_name(), + 'last_name' => $customer->get_shipping_last_name(), + 'company' => $customer->get_shipping_company(), + 'address_1' => $customer->get_shipping_address_1(), + 'address_2' => $customer->get_shipping_address_2(), + 'city' => $customer->get_shipping_city(), + 'state' => $customer->get_shipping_state(), + 'postcode' => $customer->get_shipping_postcode(), + 'country' => $customer->get_shipping_country(), ), ); @@ -258,20 +249,21 @@ class WC_API_Customers extends WC_API_Resource { $id = $this->validate_request( $id, 'customer', 'read' ); - if ( is_wp_error( $id ) ) + if ( is_wp_error( $id ) ) { return $id; + } - $order_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id - FROM $wpdb->posts AS posts - LEFT JOIN {$wpdb->postmeta} AS meta on posts.ID = meta.post_id - WHERE meta.meta_key = '_customer_user' - AND meta.meta_value = '%s' - AND posts.post_type = 'shop_order' - AND posts.post_status = IN ( '" . implode( "','", array_keys( wc_get_order_statuses() ) ) . "' ) - ", $id ) ); + $order_ids = wc_get_orders( array( + 'customer' => $id, + 'limit' => -1, + 'orderby' => 'date', + 'order' => 'ASC', + 'return' => 'ids', + ) ); - if ( empty( $order_ids ) ) + if ( empty( $order_ids ) ) { return array( 'orders' => array() ); + } $orders = array(); @@ -413,28 +405,6 @@ class WC_API_Customers extends WC_API_Resource { $query->query_where .= sprintf( " AND user_registered <= STR_TO_DATE( '%s', '%%Y-%%m-%%d %%h:%%i:%%s' )", esc_sql( $this->created_at_max ) ); } - /** - * Wrapper for @see get_avatar() which doesn't simply return - * the URL so we need to pluck it from the HTML img tag - * - * @since 2.1 - * @param string $email the customer's email - * @return string the URL to the customer's avatar - */ - private function get_avatar_url( $email ) { - - $avatar_html = get_avatar( $email ); - - // Get the URL of the avatar from the provided HTML - preg_match( '/src=["|\'](.+)[\&|"|\']/U', $avatar_html, $matches ); - - if ( isset( $matches[1] ) && ! empty( $matches[1] ) ) { - return esc_url_raw( $matches[1] ); - } - - return null; - } - /** * Validate the request by checking: * diff --git a/includes/api/legacy/v2/class-wc-api-customers.php b/includes/api/legacy/v2/class-wc-api-customers.php index 4cddfe56951..b1b3cb34aab 100644 --- a/includes/api/legacy/v2/class-wc-api-customers.php +++ b/includes/api/legacy/v2/class-wc-api-customers.php @@ -146,57 +146,44 @@ class WC_API_Customers extends WC_API_Resource { return $id; } - $customer = new WP_User( $id ); - - // Get info about user's last order - $last_order = $wpdb->get_row( "SELECT id, post_date_gmt - FROM $wpdb->posts AS posts - LEFT JOIN {$wpdb->postmeta} AS meta on posts.ID = meta.post_id - WHERE meta.meta_key = '_customer_user' - AND meta.meta_value = {$customer->ID} - AND posts.post_type = 'shop_order' - AND posts.post_status IN ( '" . implode( "','", array_keys( wc_get_order_statuses() ) ) . "' ) - ORDER BY posts.ID DESC - " ); - - $roles = array_values( $customer->roles ); - + $customer = new WC_Customer( $id ); + $last_order = $customer->get_last_order(); $customer_data = array( - 'id' => $customer->ID, - 'created_at' => $this->server->format_datetime( $customer->user_registered ), - 'email' => $customer->user_email, - 'first_name' => $customer->first_name, - 'last_name' => $customer->last_name, - 'username' => $customer->user_login, - 'role' => $roles[0], + 'id' => $customer->get_id(), + 'created_at' => $this->server->format_datetime( $customer->get_date_created() ), + 'email' => $customer->get_email(), + 'first_name' => $customer->get_first_name(), + 'last_name' => $customer->get_last_name(), + 'username' => $customer->get_username(), + 'role' => $customer->get_role(), 'last_order_id' => is_object( $last_order ) ? $last_order->get_id() : null, - 'last_order_date' => is_object( $last_order ) ? $this->server->format_datetime( $last_order->post_date_gmt ) : null, - 'orders_count' => wc_get_customer_order_count( $customer->ID ), - 'total_spent' => wc_format_decimal( wc_get_customer_total_spent( $customer->ID ), 2 ), - 'avatar_url' => $this->get_avatar_url( $customer->customer_email ), + 'last_order_date' => is_object( $last_order ) ? $this->server->format_datetime( $last_order->get_date_created() ) : null, + 'orders_count' => $customer->get_order_count(), + 'total_spent' => wc_format_decimal( $customer->get_total_spent(), 2 ), + 'avatar_url' => $customer->get_avatar_url(), 'billing_address' => array( - 'first_name' => $customer->billing_first_name, - 'last_name' => $customer->billing_last_name, - 'company' => $customer->billing_company, - 'address_1' => $customer->billing_address_1, - 'address_2' => $customer->billing_address_2, - 'city' => $customer->billing_city, - 'state' => $customer->billing_state, - 'postcode' => $customer->billing_postcode, - 'country' => $customer->billing_country, - 'email' => $customer->billing_email, - 'phone' => $customer->billing_phone, + 'first_name' => $customer->get_billing_first_name(), + 'last_name' => $customer->get_billing_last_name(), + 'company' => $customer->get_billing_company(), + 'address_1' => $customer->get_billing_address_1(), + 'address_2' => $customer->get_billing_address_2(), + 'city' => $customer->get_billing_city(), + 'state' => $customer->get_billing_state(), + 'postcode' => $customer->get_billing_postcode(), + 'country' => $customer->get_billing_country(), + 'email' => $customer->get_billing_email(), + 'phone' => $customer->get_billing_phone(), ), 'shipping_address' => array( - 'first_name' => $customer->shipping_first_name, - 'last_name' => $customer->shipping_last_name, - 'company' => $customer->shipping_company, - 'address_1' => $customer->shipping_address_1, - 'address_2' => $customer->shipping_address_2, - 'city' => $customer->shipping_city, - 'state' => $customer->shipping_state, - 'postcode' => $customer->shipping_postcode, - 'country' => $customer->shipping_country, + 'first_name' => $customer->get_shipping_first_name(), + 'last_name' => $customer->get_shipping_last_name(), + 'company' => $customer->get_shipping_company(), + 'address_1' => $customer->get_shipping_address_1(), + 'address_2' => $customer->get_shipping_address_2(), + 'city' => $customer->get_shipping_city(), + 'state' => $customer->get_shipping_state(), + 'postcode' => $customer->get_shipping_postcode(), + 'country' => $customer->get_shipping_country(), ), ); @@ -216,10 +203,10 @@ class WC_API_Customers extends WC_API_Resource { if ( is_email( $email ) ) { $customer = get_user_by( 'email', $email ); if ( ! is_object( $customer ) ) { - throw new WC_API_Exception( 'woocommerce_api_invalid_customer_email', __( 'Invalid customer Email', 'woocommerce' ), 404 ); + throw new WC_API_Exception( 'woocommerce_api_invalid_customer_email', __( 'Invalid customer email', 'woocommerce' ), 404 ); } } else { - throw new WC_API_Exception( 'woocommerce_api_invalid_customer_email', __( 'Invalid customer Email', 'woocommerce' ), 404 ); + throw new WC_API_Exception( 'woocommerce_api_invalid_customer_email', __( 'Invalid customer email', 'woocommerce' ), 404 ); } return $this->get_customer( $customer->ID, $fields ); @@ -301,37 +288,47 @@ class WC_API_Customers extends WC_API_Resource { * @since 2.2 * @param int $id the customer ID * @param array $data + * @param WC_Customer $customer */ - protected function update_customer_data( $id, $data ) { + protected function update_customer_data( $id, $data, $customer ) { + // Customer first name. if ( isset( $data['first_name'] ) ) { - update_user_meta( $id, 'first_name', wc_clean( $data['first_name'] ) ); + $customer->set_first_name( wc_clean( $data['first_name'] ) ); } // Customer last name. if ( isset( $data['last_name'] ) ) { - update_user_meta( $id, 'last_name', wc_clean( $data['last_name'] ) ); + $customer->set_last_name( wc_clean( $data['last_name'] ) ); } // Customer billing address. if ( isset( $data['billing_address'] ) ) { - foreach ( $this->get_customer_billing_address() as $address ) { - if ( isset( $data['billing_address'][ $address ] ) ) { - update_user_meta( $id, 'billing_' . $address, wc_clean( $data['billing_address'][ $address ] ) ); + foreach ( $this->get_customer_billing_address() as $field ) { + if ( isset( $data['billing_address'][ $field ] ) ) { + if ( is_callable( array( $customer, "set_billing_{$field}" ) ) ) { + $customer->{"set_billing_{$field}"}( $data['billing_address'][ $field ] ); + } else { + $customer->update_meta_data( 'billing_' . $field, wc_clean( $data['billing_address'][ $field ] ), $meta['id'] ); + } } } } // Customer shipping address. if ( isset( $data['shipping_address'] ) ) { - foreach ( $this->get_customer_shipping_address() as $address ) { - if ( isset( $data['shipping_address'][ $address ] ) ) { - update_user_meta( $id, 'shipping_' . $address, wc_clean( $data['shipping_address'][ $address ] ) ); + foreach ( $this->get_customer_shipping_address() as $field ) { + if ( isset( $data['shipping_address'][ $field ] ) ) { + if ( is_callable( array( $customer, "set_shipping_{$field}" ) ) ) { + $customer->{"set_shipping_{$field}"}( $data['shipping_address'][ $field ] ); + } else { + $customer->update_meta_data( 'shipping_' . $field, wc_clean( $data['shipping_address'][ $field ] ), $meta['id'] ); + } } } } - do_action( 'woocommerce_api_update_customer_data', $id, $data ); + do_action( 'woocommerce_api_update_customer_data', $id, $data, $customer ); } /** @@ -361,29 +358,27 @@ class WC_API_Customers extends WC_API_Resource { throw new WC_API_Exception( 'woocommerce_api_missing_customer_email', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'email' ), 400 ); } - // Sets the username. - $data['username'] = ! empty( $data['username'] ) ? $data['username'] : ''; + // Create customer. + $customer = new WC_Customer; + $customer->set_username( ! empty( $data['username'] ) ? $data['username'] : '' ); + $customer->set_password( ! empty( $data['password'] ) ? $data['password'] : '' ); + $customer->set_email( $data['email'] ); + $customer->save(); - // Sets the password. - $data['password'] = ! empty( $data['password'] ) ? $data['password'] : ''; - - // Attempts to create the new customer - $id = wc_create_new_customer( $data['email'], $data['username'], $data['password'] ); - - // Checks for an error in the customer creation. - if ( is_wp_error( $id ) ) { - throw new WC_API_Exception( $id->get_error_code(), $id->get_error_message(), 400 ); + if ( ! $customer->get_id() ) { + throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_customer', __( 'This resource cannot be created.', 'woocommerce' ), 400 ); } // Added customer data. - $this->update_customer_data( $id, $data ); + $this->update_customer_data( $customer->get_id(), $data, $customer ); + $customer->save(); - do_action( 'woocommerce_api_create_customer', $id, $data ); + do_action( 'woocommerce_api_create_customer', $customer->get_id(), $data ); $this->server->send_status( 201 ); - return $this->get_customer( $id ); - } catch ( WC_API_Exception $e ) { + return $this->get_customer( $customer->get_id() ); + } catch ( Exception $e ) { return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); } } @@ -414,23 +409,27 @@ class WC_API_Customers extends WC_API_Resource { $data = apply_filters( 'woocommerce_api_edit_customer_data', $data, $this ); + $customer = new WC_Customer( $id ); + // Customer email. if ( isset( $data['email'] ) ) { - wp_update_user( array( 'ID' => $id, 'user_email' => sanitize_email( $data['email'] ) ) ); + $customer->set_email( $data['email'] ); } // Customer password. if ( isset( $data['password'] ) ) { - wp_update_user( array( 'ID' => $id, 'user_pass' => wc_clean( $data['password'] ) ) ); + $customer->set_password( $data['password'] ); } // Update customer data. - $this->update_customer_data( $id, $data ); + $this->update_customer_data( $customer->get_id(), $data, $customer ); - do_action( 'woocommerce_api_edit_customer', $id, $data ); + $customer->save(); - return $this->get_customer( $id ); - } catch ( WC_API_Exception $e ) { + do_action( 'woocommerce_api_edit_customer', $customer->get_id(), $data ); + + return $this->get_customer( $customer->get_id() ); + } catch ( Exception $e ) { return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); } } @@ -474,14 +473,13 @@ class WC_API_Customers extends WC_API_Resource { return $id; } - $order_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id - FROM $wpdb->posts AS posts - LEFT JOIN {$wpdb->postmeta} AS meta on posts.ID = meta.post_id - WHERE meta.meta_key = '_customer_user' - AND meta.meta_value = '%s' - AND posts.post_type = 'shop_order' - AND posts.post_status IN ( '" . implode( "','", array_keys( wc_get_order_statuses() ) ) . "' ) - ", $id ) ); + $order_ids = wc_get_orders( array( + 'customer' => $id, + 'limit' => -1, + 'orderby' => 'date', + 'order' => 'ASC', + 'return' => 'ids', + ) ); if ( empty( $order_ids ) ) { return array( 'orders' => array() ); @@ -681,29 +679,6 @@ class WC_API_Customers extends WC_API_Resource { } } - /** - * Wrapper for @see get_avatar() which doesn't simply return - * the URL so we need to pluck it from the HTML img tag - * - * Kudos to https://github.com/WP-API/WP-API for offering a better solution - * - * @since 2.1 - * @param string $email the customer's email - * @return string the URL to the customer's avatar - */ - private function get_avatar_url( $email ) { - $avatar_html = get_avatar( $email ); - - // Get the URL of the avatar from the provided HTML - preg_match( '/src=["|\'](.+)[\&|"|\']/U', $avatar_html, $matches ); - - if ( isset( $matches[1] ) && ! empty( $matches[1] ) ) { - return esc_url_raw( $matches[1] ); - } - - return null; - } - /** * Validate the request by checking: * diff --git a/includes/api/legacy/v3/class-wc-api-customers.php b/includes/api/legacy/v3/class-wc-api-customers.php index b754aa21734..667aed2888a 100644 --- a/includes/api/legacy/v3/class-wc-api-customers.php +++ b/includes/api/legacy/v3/class-wc-api-customers.php @@ -146,58 +146,45 @@ class WC_API_Customers extends WC_API_Resource { return $id; } - $customer = new WP_User( $id ); - - // Get info about user's last order - $last_order = $wpdb->get_row( "SELECT id, post_date_gmt - FROM $wpdb->posts AS posts - LEFT JOIN {$wpdb->postmeta} AS meta on posts.ID = meta.post_id - WHERE meta.meta_key = '_customer_user' - AND meta.meta_value = {$customer->ID} - AND posts.post_type = 'shop_order' - AND posts.post_status IN ( '" . implode( "','", array_keys( wc_get_order_statuses() ) ) . "' ) - ORDER BY posts.ID DESC - " ); - - $roles = array_values( $customer->roles ); - + $customer = new WC_Customer( $id ); + $last_order = $customer->get_last_order(); $customer_data = array( - 'id' => $customer->ID, - 'created_at' => $this->server->format_datetime( $customer->user_registered ), - 'last_update' => $this->server->format_datetime( get_user_meta( $customer->ID, 'last_update', true ) ), - 'email' => $customer->user_email, - 'first_name' => $customer->first_name, - 'last_name' => $customer->last_name, - 'username' => $customer->user_login, - 'role' => $roles[0], + 'id' => $customer->get_id(), + 'created_at' => $this->server->format_datetime( $customer->get_date_created() ), + 'last_update' => $this->server->format_datetime( $customer->get_date_modified() ), + 'email' => $customer->get_email(), + 'first_name' => $customer->get_first_name(), + 'last_name' => $customer->get_last_name(), + 'username' => $customer->get_username(), + 'role' => $customer->get_role(), 'last_order_id' => is_object( $last_order ) ? $last_order->get_id() : null, - 'last_order_date' => is_object( $last_order ) ? $this->server->format_datetime( $last_order->post_date_gmt ) : null, - 'orders_count' => wc_get_customer_order_count( $customer->ID ), - 'total_spent' => wc_format_decimal( wc_get_customer_total_spent( $customer->ID ), 2 ), - 'avatar_url' => $this->get_avatar_url( $customer->customer_email ), + 'last_order_date' => is_object( $last_order ) ? $this->server->format_datetime( $last_order->get_date_created() ) : null, + 'orders_count' => $customer->get_order_count(), + 'total_spent' => wc_format_decimal( $customer->get_total_spent(), 2 ), + 'avatar_url' => $customer->get_avatar_url(), 'billing_address' => array( - 'first_name' => $customer->billing_first_name, - 'last_name' => $customer->billing_last_name, - 'company' => $customer->billing_company, - 'address_1' => $customer->billing_address_1, - 'address_2' => $customer->billing_address_2, - 'city' => $customer->billing_city, - 'state' => $customer->billing_state, - 'postcode' => $customer->billing_postcode, - 'country' => $customer->billing_country, - 'email' => $customer->billing_email, - 'phone' => $customer->billing_phone, + 'first_name' => $customer->get_billing_first_name(), + 'last_name' => $customer->get_billing_last_name(), + 'company' => $customer->get_billing_company(), + 'address_1' => $customer->get_billing_address_1(), + 'address_2' => $customer->get_billing_address_2(), + 'city' => $customer->get_billing_city(), + 'state' => $customer->get_billing_state(), + 'postcode' => $customer->get_billing_postcode(), + 'country' => $customer->get_billing_country(), + 'email' => $customer->get_billing_email(), + 'phone' => $customer->get_billing_phone(), ), 'shipping_address' => array( - 'first_name' => $customer->shipping_first_name, - 'last_name' => $customer->shipping_last_name, - 'company' => $customer->shipping_company, - 'address_1' => $customer->shipping_address_1, - 'address_2' => $customer->shipping_address_2, - 'city' => $customer->shipping_city, - 'state' => $customer->shipping_state, - 'postcode' => $customer->shipping_postcode, - 'country' => $customer->shipping_country, + 'first_name' => $customer->get_shipping_first_name(), + 'last_name' => $customer->get_shipping_last_name(), + 'company' => $customer->get_shipping_company(), + 'address_1' => $customer->get_shipping_address_1(), + 'address_2' => $customer->get_shipping_address_2(), + 'city' => $customer->get_shipping_city(), + 'state' => $customer->get_shipping_state(), + 'postcode' => $customer->get_shipping_postcode(), + 'country' => $customer->get_shipping_country(), ), ); @@ -217,10 +204,10 @@ class WC_API_Customers extends WC_API_Resource { if ( is_email( $email ) ) { $customer = get_user_by( 'email', $email ); if ( ! is_object( $customer ) ) { - throw new WC_API_Exception( 'woocommerce_api_invalid_customer_email', __( 'Invalid customer Email', 'woocommerce' ), 404 ); + throw new WC_API_Exception( 'woocommerce_api_invalid_customer_email', __( 'Invalid customer email', 'woocommerce' ), 404 ); } } else { - throw new WC_API_Exception( 'woocommerce_api_invalid_customer_email', __( 'Invalid customer Email', 'woocommerce' ), 404 ); + throw new WC_API_Exception( 'woocommerce_api_invalid_customer_email', __( 'Invalid customer email', 'woocommerce' ), 404 ); } return $this->get_customer( $customer->ID, $fields ); @@ -302,37 +289,47 @@ class WC_API_Customers extends WC_API_Resource { * @since 2.2 * @param int $id the customer ID * @param array $data + * @param WC_Customer $customer */ - protected function update_customer_data( $id, $data ) { + protected function update_customer_data( $id, $data, $customer ) { + // Customer first name. if ( isset( $data['first_name'] ) ) { - update_user_meta( $id, 'first_name', wc_clean( $data['first_name'] ) ); + $customer->set_first_name( wc_clean( $data['first_name'] ) ); } // Customer last name. if ( isset( $data['last_name'] ) ) { - update_user_meta( $id, 'last_name', wc_clean( $data['last_name'] ) ); + $customer->set_last_name( wc_clean( $data['last_name'] ) ); } // Customer billing address. if ( isset( $data['billing_address'] ) ) { - foreach ( $this->get_customer_billing_address() as $address ) { - if ( isset( $data['billing_address'][ $address ] ) ) { - update_user_meta( $id, 'billing_' . $address, wc_clean( $data['billing_address'][ $address ] ) ); + foreach ( $this->get_customer_billing_address() as $field ) { + if ( isset( $data['billing_address'][ $field ] ) ) { + if ( is_callable( array( $customer, "set_billing_{$field}" ) ) ) { + $customer->{"set_billing_{$field}"}( $data['billing_address'][ $field ] ); + } else { + $customer->update_meta_data( 'billing_' . $field, wc_clean( $data['billing_address'][ $field ] ), $meta['id'] ); + } } } } // Customer shipping address. if ( isset( $data['shipping_address'] ) ) { - foreach ( $this->get_customer_shipping_address() as $address ) { - if ( isset( $data['shipping_address'][ $address ] ) ) { - update_user_meta( $id, 'shipping_' . $address, wc_clean( $data['shipping_address'][ $address ] ) ); + foreach ( $this->get_customer_shipping_address() as $field ) { + if ( isset( $data['shipping_address'][ $field ] ) ) { + if ( is_callable( array( $customer, "set_shipping_{$field}" ) ) ) { + $customer->{"set_shipping_{$field}"}( $data['shipping_address'][ $field ] ); + } else { + $customer->update_meta_data( 'shipping_' . $field, wc_clean( $data['shipping_address'][ $field ] ), $meta['id'] ); + } } } } - do_action( 'woocommerce_api_update_customer_data', $id, $data ); + do_action( 'woocommerce_api_update_customer_data', $id, $data, $customer ); } /** @@ -362,29 +359,27 @@ class WC_API_Customers extends WC_API_Resource { throw new WC_API_Exception( 'woocommerce_api_missing_customer_email', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'email' ), 400 ); } - // Sets the username. - $data['username'] = ! empty( $data['username'] ) ? $data['username'] : ''; + // Create customer. + $customer = new WC_Customer; + $customer->set_username( ! empty( $data['username'] ) ? $data['username'] : '' ); + $customer->set_password( ! empty( $data['password'] ) ? $data['password'] : '' ); + $customer->set_email( $data['email'] ); + $customer->save(); - // Sets the password. - $data['password'] = ! empty( $data['password'] ) ? $data['password'] : ''; - - // Attempts to create the new customer - $id = wc_create_new_customer( $data['email'], $data['username'], $data['password'] ); - - // Checks for an error in the customer creation. - if ( is_wp_error( $id ) ) { - throw new WC_API_Exception( $id->get_error_code(), $id->get_error_message(), 400 ); + if ( ! $customer->get_id() ) { + throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_customer', __( 'This resource cannot be created.', 'woocommerce' ), 400 ); } // Added customer data. - $this->update_customer_data( $id, $data ); + $this->update_customer_data( $customer->get_id(), $data, $customer ); + $customer->save(); - do_action( 'woocommerce_api_create_customer', $id, $data ); + do_action( 'woocommerce_api_create_customer', $customer->get_id(), $data ); $this->server->send_status( 201 ); - return $this->get_customer( $id ); - } catch ( WC_API_Exception $e ) { + return $this->get_customer( $customer->get_id() ); + } catch ( Exception $e ) { return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); } } @@ -415,23 +410,27 @@ class WC_API_Customers extends WC_API_Resource { $data = apply_filters( 'woocommerce_api_edit_customer_data', $data, $this ); + $customer = new WC_Customer( $id ); + // Customer email. if ( isset( $data['email'] ) ) { - wp_update_user( array( 'ID' => $id, 'user_email' => sanitize_email( $data['email'] ) ) ); + $customer->set_email( $data['email'] ); } // Customer password. if ( isset( $data['password'] ) ) { - wp_update_user( array( 'ID' => $id, 'user_pass' => wc_clean( $data['password'] ) ) ); + $customer->set_password( $data['password'] ); } // Update customer data. - $this->update_customer_data( $id, $data ); + $this->update_customer_data( $customer->get_id(), $data, $customer ); - do_action( 'woocommerce_api_edit_customer', $id, $data ); + $customer->save(); - return $this->get_customer( $id ); - } catch ( WC_API_Exception $e ) { + do_action( 'woocommerce_api_edit_customer', $customer->get_id(), $data ); + + return $this->get_customer( $customer->get_id() ); + } catch ( Exception $e ) { return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); } } @@ -670,29 +669,6 @@ class WC_API_Customers extends WC_API_Resource { } } - /** - * Wrapper for @see get_avatar() which doesn't simply return - * the URL so we need to pluck it from the HTML img tag - * - * Kudos to https://github.com/WP-API/WP-API for offering a better solution - * - * @since 2.1 - * @param string $email the customer's email - * @return string the URL to the customer's avatar - */ - private function get_avatar_url( $email ) { - $avatar_html = get_avatar( $email ); - - // Get the URL of the avatar from the provided HTML - preg_match( '/src=["|\'](.+)[\&|"|\']/U', $avatar_html, $matches ); - - if ( isset( $matches[1] ) && ! empty( $matches[1] ) ) { - return esc_url_raw( $matches[1] ); - } - - return null; - } - /** * Validate the request by checking: *