PluginProbe ʕ •ᴥ•ʔ
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress / trunk
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress vtrunk
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 / get-customer-orders.php
latepoint / lib / abilities / customers Last commit date
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
get-customer-orders.php
89 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4 }
5
6 class LatePointAbilityGetCustomerOrders extends LatePointAbstractCustomerAbility {
7
8 protected function configure(): void {
9 $this->id = 'latepoint/get-customer-orders';
10 $this->label = __( 'Get customer orders', 'latepoint' );
11 $this->description = __( 'Returns all orders for a specific customer.', 'latepoint' );
12 $this->permission = 'customer__view';
13 $this->read_only = true;
14 }
15
16 public function get_input_schema(): array {
17 return [
18 'type' => 'object',
19 'properties' => array_merge(
20 self::pagination(),
21 [
22 'customer_id' => [
23 'type' => 'integer',
24 'description' => __( 'Customer ID.', 'latepoint' ),
25 ],
26 ]
27 ),
28 'required' => [ 'customer_id' ],
29 ];
30 }
31
32 public function get_output_schema(): array {
33 return [
34 'type' => 'object',
35 'properties' => [
36 'orders' => [
37 'type' => 'array',
38 'items' => [ 'type' => 'object' ],
39 ],
40 'total' => [ 'type' => 'integer' ],
41 'page' => [ 'type' => 'integer' ],
42 'per_page' => [ 'type' => 'integer' ],
43 ],
44 ];
45 }
46
47 public function execute( array $args ) {
48 $customer = new OsCustomerModel( (int) $args['customer_id'] );
49 if ( $customer->is_new_record() ) {
50 return new WP_Error( 'not_found', __( 'Customer not found.', 'latepoint' ), [ 'status' => 404 ] );
51 }
52 $auth = $this->authorize_record( $customer, 'view' );
53 if ( is_wp_error( $auth ) ) {
54 return $auth;
55 }
56
57 $page = max( 1, (int) ( $args['page'] ?? 1 ) );
58 $per_page = min( 100, max( 1, (int) ( $args['per_page'] ?? 20 ) ) );
59 $offset = ( $page - 1 ) * $per_page;
60
61 $query = ( new OsOrderModel() )->where( [ 'customer_id' => $customer->id ] );
62 $query->filter_allowed_records();
63 $orders = ( clone $query )
64 ->order_by( 'created_at DESC' )
65 ->set_limit( $per_page )
66 ->set_offset( $offset )
67 ->get_results_as_models();
68 $total = $query->count();
69
70 $serialized = array_map(
71 fn( OsOrderModel $o ) => [
72 'id' => (int) $o->id,
73 'status' => $o->status ?? '',
74 'total' => $o->total ?? 0,
75 'customer_id' => (int) $o->customer_id,
76 'created_at' => $o->created_at ?? '',
77 ],
78 $orders
79 );
80
81 return [
82 'orders' => $serialized,
83 'total' => (int) $total,
84 'page' => $page,
85 'per_page' => $per_page,
86 ];
87 }
88 }
89