namespace, '/profile', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_profile' ), 'permission_callback' => array( $this, 'check_permission' ), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_profile' ), 'permission_callback' => array( $this, 'check_permission' ), 'args' => $this->get_profile_params(), ), ) ); // Update password. register_rest_route( $this->namespace, '/profile/password', array( array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'update_password' ), 'permission_callback' => array( $this, 'check_permission' ), 'args' => array( 'current_password' => array( 'description' => __( 'Current password.', 'advanced-customer-account' ), 'type' => 'string', 'required' => true, 'sanitize_callback' => 'sanitize_text_field', ), 'new_password' => array( 'description' => __( 'New password.', 'advanced-customer-account' ), 'type' => 'string', 'required' => true, 'sanitize_callback' => 'sanitize_text_field', ), 'confirm_password' => array( 'description' => __( 'Confirm new password.', 'advanced-customer-account' ), 'type' => 'string', 'required' => true, 'sanitize_callback' => 'sanitize_text_field', ), ), ), ) ); } /** * Get profile params. * * @return array */ private function get_profile_params() { return 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', ), 'display_name' => array( 'description' => __( 'Display name.', 'advanced-customer-account' ), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'email' => array( 'description' => __( 'Email address.', 'advanced-customer-account' ), 'type' => 'string', 'format' => 'email', 'sanitize_callback' => 'sanitize_email', ), ); } /** * Get profile data. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function get_profile( $request ) { $customer_id = $this->get_customer_id(); $customer = new WC_Customer( $customer_id ); $user = get_userdata( $customer_id ); // Get first name - try WC customer, then user meta, then user data $first_name = $customer->get_first_name(); if ( empty( $first_name ) ) { $first_name = get_user_meta( $customer_id, 'first_name', true ); } // Get last name - try WC customer, then user meta, then user data $last_name = $customer->get_last_name(); if ( empty( $last_name ) ) { $last_name = get_user_meta( $customer_id, 'last_name', true ); } // Get display name $display_name = $user->display_name; // Get email - prefer customer email, fallback to user email $email = $customer->get_email(); if ( empty( $email ) ) { $email = $user->user_email; } $data = array( 'id' => $customer_id, 'username' => $user->user_login, 'email' => $email, 'first_name' => $first_name, 'last_name' => $last_name, 'display_name' => $display_name, 'avatar_url' => get_avatar_url( $customer_id, array( 'size' => 150 ) ), 'date_created' => $this->format_date( $user->user_registered ), 'billing_phone' => $customer->get_billing_phone(), 'is_paying' => $customer->get_is_paying_customer(), ); return rest_ensure_response( $data ); } /** * Update profile data. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response|WP_Error */ public function update_profile( $request ) { $customer_id = $this->get_customer_id(); $customer = new WC_Customer( $customer_id ); $params = $request->get_params(); // Update first name - save to both WC customer and WP user meta. if ( isset( $params['first_name'] ) ) { $first_name = sanitize_text_field( $params['first_name'] ); $customer->set_first_name( $first_name ); $customer->set_billing_first_name( $first_name ); update_user_meta( $customer_id, 'first_name', $first_name ); } // Update last name - save to both WC customer and WP user meta. if ( isset( $params['last_name'] ) ) { $last_name = sanitize_text_field( $params['last_name'] ); $customer->set_last_name( $last_name ); $customer->set_billing_last_name( $last_name ); update_user_meta( $customer_id, 'last_name', $last_name ); } // Update display name - save to WP user table. if ( isset( $params['display_name'] ) ) { $display_name = sanitize_text_field( $params['display_name'] ); wp_update_user( array( 'ID' => $customer_id, 'display_name' => $display_name, ) ); } // Update email. if ( isset( $params['email'] ) ) { $new_email = sanitize_email( $params['email'] ); // Check if email is already in use. if ( email_exists( $new_email ) && email_exists( $new_email ) !== $customer_id ) { return new WP_Error( 'rest_email_exists', esc_html__( 'This email address is already in use.', 'advanced-customer-account' ), array( 'status' => 400 ) ); } $customer->set_email( $new_email ); $customer->set_billing_email( $new_email ); // Also update WP user email. wp_update_user( array( 'ID' => $customer_id, 'user_email' => $new_email, ) ); } try { $customer->save(); // Log activity. $this->log_activity( $customer_id, 'profile_updated', array( 'fields' => array_keys( $params ) ) ); return rest_ensure_response( array( 'success' => true, 'message' => __( 'Profile updated successfully.', 'advanced-customer-account' ), ) ); } catch ( Exception $e ) { return new WP_Error( 'rest_update_failed', esc_html( $e->getMessage() ), array( 'status' => 500 ) ); } } /** * Update password. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response|WP_Error */ public function update_password( $request ) { $customer_id = $this->get_customer_id(); $current_password = $request->get_param( 'current_password' ); $new_password = $request->get_param( 'new_password' ); $confirm_password = $request->get_param( 'confirm_password' ); // Get user. $user = get_userdata( $customer_id ); // Verify current password. if ( ! wp_check_password( $current_password, $user->user_pass, $customer_id ) ) { return new WP_Error( 'rest_invalid_password', esc_html__( 'Current password is incorrect.', 'advanced-customer-account' ), array( 'status' => 400 ) ); } // Check if passwords match. if ( $new_password !== $confirm_password ) { return new WP_Error( 'rest_password_mismatch', esc_html__( 'New passwords do not match.', 'advanced-customer-account' ), array( 'status' => 400 ) ); } // Validate password strength. if ( strlen( $new_password ) < 8 ) { return new WP_Error( 'rest_weak_password', esc_html__( 'Password must be at least 8 characters long.', 'advanced-customer-account' ), array( 'status' => 400 ) ); } // Update password. wp_set_password( $new_password, $customer_id ); // Log activity. $this->log_activity( $customer_id, 'password_changed', array() ); // Re-authenticate user. wp_set_current_user( $customer_id ); wp_set_auth_cookie( $customer_id, true ); return rest_ensure_response( array( 'success' => true, 'message' => __( 'Password updated successfully.', 'advanced-customer-account' ), ) ); } /** * Log activity. * * @param int $user_id User ID. * @param string $activity_type Activity type. * @param array $activity_data Activity data. */ private function log_activity( $user_id, $activity_type, $activity_data ) { global $wpdb; $table_name = $wpdb->prefix . 'aca_activity_log'; $wpdb->insert( $table_name, array( 'user_id' => $user_id, 'activity_type' => $activity_type, 'activity_data' => wp_json_encode( $activity_data ), 'created_at' => current_time( 'mysql' ), ), array( '%d', '%s', '%s', '%s' ) ); } }