namespace, '/addresses', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_addresses' ), 'permission_callback' => array( $this, 'check_permission' ), ), ) ); // Get/Update billing address. register_rest_route( $this->namespace, '/addresses/billing', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_billing_address' ), 'permission_callback' => array( $this, 'check_permission' ), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_billing_address' ), 'permission_callback' => array( $this, 'check_permission' ), 'args' => $this->get_address_params( 'billing' ), ), ) ); // Get/Update shipping address. register_rest_route( $this->namespace, '/addresses/shipping', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_shipping_address' ), 'permission_callback' => array( $this, 'check_permission' ), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_shipping_address' ), 'permission_callback' => array( $this, 'check_permission' ), 'args' => $this->get_address_params( 'shipping' ), ), ) ); // Get countries and states. register_rest_route( $this->namespace, '/addresses/countries', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_countries' ), 'permission_callback' => '__return_true', ), ) ); register_rest_route( $this->namespace, '/addresses/states/(?P[A-Z]{2})', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_states' ), 'permission_callback' => '__return_true', 'args' => array( 'country' => array( 'description' => __( 'Country code.', 'advanced-customer-account' ), 'type' => 'string', 'required' => true, 'sanitize_callback' => 'sanitize_text_field', ), ), ), ) ); } /** * Get address params. * * @param string $type Address type. * @return array */ private function get_address_params( $type = 'billing' ) { $params = array( 'first_name' => array( 'description' => __( 'First name.', 'advanced-customer-account' ), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'last_name' => array( 'description' => __( 'Last name.', 'advanced-customer-account' ), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'company' => array( 'description' => __( 'Company name.', 'advanced-customer-account' ), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'address_1' => array( 'description' => __( 'Address line 1.', 'advanced-customer-account' ), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'address_2' => array( 'description' => __( 'Address line 2.', 'advanced-customer-account' ), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'city' => array( 'description' => __( 'City.', 'advanced-customer-account' ), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'state' => array( 'description' => __( 'State/Province.', 'advanced-customer-account' ), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'postcode' => array( 'description' => __( 'Postal code.', 'advanced-customer-account' ), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'country' => array( 'description' => __( 'Country code.', 'advanced-customer-account' ), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), ); // Add billing-specific fields. if ( 'billing' === $type ) { $params['phone'] = array( 'description' => __( 'Phone number.', 'advanced-customer-account' ), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ); $params['email'] = array( 'description' => __( 'Email address.', 'advanced-customer-account' ), 'type' => 'string', 'format' => 'email', 'sanitize_callback' => 'sanitize_email', ); } return $params; } /** * Get all addresses. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function get_addresses( $request ) { $customer_id = $this->get_customer_id(); $customer = new WC_Customer( $customer_id ); $data = array( 'billing' => $this->prepare_address_response( $customer, 'billing' ), 'shipping' => $this->prepare_address_response( $customer, 'shipping' ), ); return rest_ensure_response( $data ); } /** * Get billing address. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function get_billing_address( $request ) { $customer_id = $this->get_customer_id(); $customer = new WC_Customer( $customer_id ); return rest_ensure_response( $this->prepare_address_response( $customer, 'billing' ) ); } /** * Get shipping address. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function get_shipping_address( $request ) { $customer_id = $this->get_customer_id(); $customer = new WC_Customer( $customer_id ); return rest_ensure_response( $this->prepare_address_response( $customer, 'shipping' ) ); } /** * Update billing address. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response|WP_Error */ public function update_billing_address( $request ) { return $this->update_address( $request, 'billing' ); } /** * Update shipping address. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response|WP_Error */ public function update_shipping_address( $request ) { return $this->update_address( $request, 'shipping' ); } /** * Update address. * * @param WP_REST_Request $request Request object. * @param string $type Address type. * @return WP_REST_Response|WP_Error */ private function update_address( $request, $type ) { $customer_id = $this->get_customer_id(); $customer = new WC_Customer( $customer_id ); $params = $request->get_params(); $fields = array( 'first_name', 'last_name', 'company', 'address_1', 'address_2', 'city', 'state', 'postcode', 'country', ); if ( 'billing' === $type ) { $fields[] = 'phone'; $fields[] = 'email'; } foreach ( $fields as $field ) { if ( isset( $params[ $field ] ) ) { $setter = "set_{$type}_{$field}"; if ( method_exists( $customer, $setter ) ) { $customer->$setter( $params[ $field ] ); } } } try { $customer->save(); return rest_ensure_response( array( 'success' => true, 'message' => sprintf( /* translators: %s: address type */ __( '%s address updated successfully.', 'advanced-customer-account' ), ucfirst( $type ) ), 'address' => $this->prepare_address_response( $customer, $type ), ) ); } catch ( Exception $e ) { return new WP_Error( 'rest_update_failed', esc_html( $e->getMessage() ), array( 'status' => 500 ) ); } } /** * Prepare address response. * * @param WC_Customer $customer Customer object. * @param string $type Address type. * @return array */ private function prepare_address_response( $customer, $type ) { $address = array( 'first_name' => $customer->{"get_{$type}_first_name"}(), 'last_name' => $customer->{"get_{$type}_last_name"}(), 'company' => $customer->{"get_{$type}_company"}(), 'address_1' => $customer->{"get_{$type}_address_1"}(), 'address_2' => $customer->{"get_{$type}_address_2"}(), 'city' => $customer->{"get_{$type}_city"}(), 'state' => $customer->{"get_{$type}_state"}(), 'postcode' => $customer->{"get_{$type}_postcode"}(), 'country' => $customer->{"get_{$type}_country"}(), ); if ( 'billing' === $type ) { $address['phone'] = $customer->get_billing_phone(); $address['email'] = $customer->get_billing_email(); } // Add formatted address. $formatted = WC()->countries->get_formatted_address( $address ); $address['formatted'] = $formatted ?: __( 'No address set.', 'advanced-customer-account' ); // Add country name. $countries = WC()->countries->get_countries(); $address['country_name'] = isset( $countries[ $address['country'] ] ) ? $countries[ $address['country'] ] : $address['country']; // Add state name. $states = WC()->countries->get_states( $address['country'] ); $address['state_name'] = isset( $states[ $address['state'] ] ) ? $states[ $address['state'] ] : $address['state']; return $address; } /** * Get countries. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function get_countries( $request ) { // Ensure WooCommerce is loaded. if ( ! function_exists( 'WC' ) || ! WC()->countries ) { return rest_ensure_response( array() ); } $countries = WC()->countries->get_countries(); if ( empty( $countries ) ) { return rest_ensure_response( array() ); } $data = array(); foreach ( $countries as $code => $name ) { $data[] = array( 'code' => $code, 'name' => html_entity_decode( $name ), ); } return rest_ensure_response( $data ); } /** * Get states for a country. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function get_states( $request ) { $country = $request->get_param( 'country' ); $states = WC()->countries->get_states( $country ); if ( empty( $states ) ) { return rest_ensure_response( array() ); } $data = array(); foreach ( $states as $code => $name ) { $data[] = array( 'code' => $code, 'name' => html_entity_decode( $name ), ); } return rest_ensure_response( $data ); } }