abstract-customer-ability.php
4 months ago
connect-customer-to-wp-user.php
2 weeks ago
create-customer.php
4 months ago
delete-customer.php
2 weeks ago
get-customer-bookings.php
2 weeks ago
get-customer-by-email.php
2 weeks ago
get-customer-orders.php
2 weeks ago
get-customer.php
2 weeks ago
get-total-customers-count.php
2 weeks ago
list-customers.php
2 weeks ago
search-customers.php
2 weeks ago
update-customer.php
2 weeks ago
connect-customer-to-wp-user.php
78 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | class LatePointAbilityConnectCustomerToWpUser extends LatePointAbstractCustomerAbility { |
| 7 | |
| 8 | protected function configure(): void { |
| 9 | $this->id = 'latepoint/connect-customer-to-wp-user'; |
| 10 | $this->label = __( 'Connect customer to WP user', 'latepoint' ); |
| 11 | $this->description = __( 'Links a LatePoint customer record to an existing WordPress user account by their user ID.', 'latepoint' ); |
| 12 | $this->permission = 'customer__edit'; |
| 13 | $this->read_only = false; |
| 14 | $this->destructive = false; |
| 15 | $this->idempotent = true; |
| 16 | } |
| 17 | |
| 18 | public function get_input_schema(): array { |
| 19 | return [ |
| 20 | 'type' => 'object', |
| 21 | 'properties' => [ |
| 22 | 'customer_id' => [ |
| 23 | 'type' => 'integer', |
| 24 | 'description' => __( 'LatePoint customer ID.', 'latepoint' ), |
| 25 | ], |
| 26 | 'wp_user_id' => [ |
| 27 | 'type' => 'integer', |
| 28 | 'description' => __( 'WordPress user ID.', 'latepoint' ), |
| 29 | ], |
| 30 | ], |
| 31 | 'required' => [ 'customer_id', 'wp_user_id' ], |
| 32 | ]; |
| 33 | } |
| 34 | |
| 35 | public function get_output_schema(): array { |
| 36 | return $this->customer_output_schema(); |
| 37 | } |
| 38 | |
| 39 | public function execute( array $args ) { |
| 40 | $customer = new OsCustomerModel( (int) $args['customer_id'] ); |
| 41 | if ( $customer->is_new_record() ) { |
| 42 | return new WP_Error( 'not_found', __( 'Customer not found.', 'latepoint' ), [ 'status' => 404 ] ); |
| 43 | } |
| 44 | $auth = $this->authorize_record( $customer, 'edit' ); |
| 45 | if ( is_wp_error( $auth ) ) { |
| 46 | return $auth; |
| 47 | } |
| 48 | |
| 49 | $wp_user_id = (int) $args['wp_user_id']; |
| 50 | $target_user = get_userdata( $wp_user_id ); |
| 51 | if ( ! $target_user ) { |
| 52 | return new WP_Error( 'wp_user_not_found', __( 'WordPress user not found.', 'latepoint' ), [ 'status' => 404 ] ); |
| 53 | } |
| 54 | |
| 55 | // Only allow linking to non-privileged WP accounts using an allowlist of roles. |
| 56 | $allowed_roles = [ LATEPOINT_WP_CUSTOMER_ROLE, 'subscriber', 'customer' ]; |
| 57 | $user_roles = (array) $target_user->roles; |
| 58 | if ( empty( $user_roles ) || ! empty( array_diff( $user_roles, $allowed_roles ) ) ) { |
| 59 | return new WP_Error( |
| 60 | 'privileged_user', |
| 61 | __( 'Cannot link a customer to a privileged WordPress account.', 'latepoint' ), |
| 62 | [ 'status' => 403 ] |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | $customer->wordpress_user_id = $wp_user_id; |
| 67 | if ( ! $customer->save() ) { |
| 68 | return new WP_Error( |
| 69 | 'save_failed', |
| 70 | __( 'Failed to link customer to WordPress user.', 'latepoint' ), |
| 71 | WP_DEBUG ? [ 'errors' => $customer->get_error_messages() ] : [ 'status' => 422 ] |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | return $this->serialize_customer( new OsCustomerModel( $customer->id ) ); |
| 76 | } |
| 77 | } |
| 78 |