route( '/admin/customers', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'search' ), 'permission' => array( $this, 'can_enter' ), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'create' ), 'permission' => $desk, ), ) ); $this->route( '/admin/customers/export', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'export' ), 'permission' => $manage, ), ) ); $this->route( '/admin/customers/(?P\d+)', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'show' ), 'permission' => $desk, ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update' ), 'permission' => $desk, ), ) ); foreach ( array( 'merge', 'erase' ) as $action ) { $this->route( '/admin/customers/(?P\d+)/' . $action, array( array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, $action ), 'permission' => $manage, ), ) ); } } /** * Search customers. Staff without the manage capability get names and * contact details only, for booking someone in. * * @param WP_REST_Request $request Request. * @return array */ public function search( WP_REST_Request $request ): array { $result = Services::customers()->search( sanitize_text_field( (string) ( $request['search'] ?? '' ) ), sanitize_text_field( (string) ( $request['tag'] ?? '' ) ), max( 1, (int) ( $request['page'] ?? 1 ) ), (int) ( $request['per_page'] ?? 50 ) ); $full = $this->can_desk(); return array( 'items' => array_map( static fn( array $c ): array => $full ? $c : array_intersect_key( $c, array_flip( array( 'id', 'name', 'first_name', 'last_name', 'email', 'phone' ) ) ), $result['items'] ), 'total' => $result['total'], 'tags' => $full ? Services::customers()->tags() : array(), ); } /** * Add a customer. * * @param WP_REST_Request $request Request. * @return array * @throws InvalidArgumentException When the email is taken or missing. */ public function create( WP_REST_Request $request ): array { $data = $this->fields( self::body( $request ) ); if ( '' === ( $data['first_name'] ?? '' ) ) { throw new InvalidArgumentException( __( 'Enter a name.', 'schedulereed' ) ); } if ( '' !== ( $data['email'] ?? '' ) && null !== Services::customers()->find_by_email( $data['email'] ) ) { throw new InvalidArgumentException( __( 'A customer with that email already exists.', 'schedulereed' ) ); } $id = Services::customers()->upsert( array_diff_key( $data, array_flip( array( 'tags', 'notes' ) ) ) ); if ( isset( $data['tags'] ) || isset( $data['notes'] ) ) { Services::customers()->update( $id, array_intersect_key( $data, array_flip( array( 'tags', 'notes' ) ) ) ); } return $this->profile( $id ); } /** * A customer's profile. * * @param WP_REST_Request $request Request. * @return array */ public function show( WP_REST_Request $request ): array { return $this->profile( (int) $request['id'] ); } /** * Change a customer. * * @param WP_REST_Request $request Request. * @return array * @throws InvalidArgumentException When the email is taken. */ public function update( WP_REST_Request $request ): array { $customer = $this->customer( (int) $request['id'] ); $data = $this->fields( self::body( $request ) ); if ( isset( $data['email'] ) && '' !== $data['email'] ) { $other = Services::customers()->find_by_email( $data['email'] ); if ( null !== $other && $other['id'] !== $customer['id'] ) { throw new InvalidArgumentException( __( 'Another customer has that email. Merge them instead.', 'schedulereed' ) ); } } Services::customers()->update( $customer['id'], $data ); return $this->profile( $customer['id'] ); } /** * Merge another customer into this one. * * @param WP_REST_Request $request Request. * @return array * @throws InvalidArgumentException When merging a customer with themselves. */ public function merge( WP_REST_Request $request ): array { $keep = $this->customer( (int) $request['id'] ); $other = $this->customer( (int) ( self::body( $request )['other'] ?? 0 ) ); if ( $keep['id'] === $other['id'] ) { throw new InvalidArgumentException( __( 'Choose a different customer to merge.', 'schedulereed' ) ); } Services::bookings()->reassign_customer( $other['id'], $keep['id'] ); Services::customers()->update( $keep['id'], array( 'tags' => array_merge( $keep['tags'], $other['tags'] ), 'notes' => trim( $keep['notes'] . "\n" . $other['notes'] ), 'phone' => '' !== $keep['phone'] ? $keep['phone'] : $other['phone'], ) ); Services::customers()->delete( $other['id'] ); Services::activity()->record( $keep['id'], 'merged', array( 'from' => $other['id'] ), 'customer' ); return $this->profile( $keep['id'] ); } /** * Erase a customer's personal details. * * @param WP_REST_Request $request Request. * @return array */ public function erase( WP_REST_Request $request ): array { Services::privacy()->anonymize( $this->customer( (int) $request['id'] )['id'] ); return array( 'erased' => true ); } /** * Customers as CSV. * * @return array */ public function export(): array { $rows = array( array( 'first_name', 'last_name', 'email', 'phone', 'tags', 'marketing', 'created' ) ); for ( $page = 1; $page <= 200; $page++ ) { $items = Services::customers()->search( '', '', $page, 500 )['items']; foreach ( $items as $c ) { $rows[] = array( $c['first_name'], $c['last_name'], $c['email'], $c['phone'], implode( ', ', $c['tags'] ), ! empty( $c['consent']['marketing'] ) ? 'yes' : 'no', wp_date( 'Y-m-d', $c['created_at'] ) ); } if ( count( $items ) < 500 ) { break; } } return array( 'csv' => BookingsController::csv( $rows ) ); } /** * A profile with history. * * @param int $id Customer. * @return array */ private function profile( int $id ): array { $customer = $this->customer( $id ); $rows = new BookingsController(); return $customer + array( 'counts' => Services::bookings()->customer_counts( $id ), 'lifetime' => Services::bookings()->lifetime_value( $id ), 'bookings' => array_map( array( $rows, 'row' ), Services::bookings()->search( array( 'customer_id' => $id, 'per_page' => 100, 'order' => 'desc', ) )['items'] ), ); } /** * Clean submitted fields. * * @param array $raw Submitted. * @return array * @throws InvalidArgumentException When the email is malformed. */ private function fields( array $raw ): array { $out = array(); foreach ( array( 'first_name', 'last_name' ) as $key ) { if ( isset( $raw[ $key ] ) ) { $out[ $key ] = mb_substr( sanitize_text_field( (string) $raw[ $key ] ), 0, 100 ); } } if ( isset( $raw['email'] ) ) { $email = sanitize_email( (string) $raw['email'] ); if ( '' !== trim( (string) $raw['email'] ) && ! is_email( $email ) ) { throw new InvalidArgumentException( __( 'Enter a valid email address.', 'schedulereed' ) ); } $out['email'] = $email; } if ( isset( $raw['phone'] ) ) { $out['phone'] = mb_substr( preg_replace( '/[^0-9+()\-. ]/', '', (string) $raw['phone'] ), 0, 40 ); } if ( isset( $raw['notes'] ) ) { $out['notes'] = mb_substr( sanitize_textarea_field( (string) $raw['notes'] ), 0, 5000 ); } if ( isset( $raw['tags'] ) ) { $out['tags'] = CustomerRepository::clean_tags( (array) $raw['tags'] ); } if ( isset( $raw['timezone'] ) && in_array( $raw['timezone'], timezone_identifiers_list(), true ) ) { $out['timezone'] = (string) $raw['timezone']; } return $out; } /** * A customer or a 404. * * @param int $id ID. * @return array * @throws BookingException When missing. */ private function customer( int $id ): array { $customer = Services::customers()->find( $id ); if ( null === $customer ) { throw new BookingException( 'schedulereed_not_found', __( 'Customer not found.', 'schedulereed' ), 404 ); } return $customer; } }