PluginProbe ʕ •ᴥ•ʔ
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress / 5.6.10
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress v5.6.10
5.6.10 5.6.9 5.6.8 5.6.7 5.6.6 5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / lib / abilities / customers / connect-customer-to-wp-user.php
latepoint / lib / abilities / customers Last commit date
abstract-customer-ability.php 5 months ago connect-customer-to-wp-user.php 1 month ago create-customer.php 5 months ago delete-customer.php 1 month ago get-customer-bookings.php 1 month ago get-customer-by-email.php 1 month ago get-customer-orders.php 1 month ago get-customer.php 1 month ago get-total-customers-count.php 1 month ago list-customers.php 1 month ago search-customers.php 1 month ago update-customer.php 1 month 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