abstract-customer-ability.php
3 months ago
connect-customer-to-wp-user.php
2 months ago
create-customer.php
3 months ago
delete-customer.php
3 months ago
get-customer-bookings.php
3 months ago
get-customer-by-email.php
3 months ago
get-customer-orders.php
3 months ago
get-customer.php
3 months ago
get-total-customers-count.php
3 months ago
list-customers.php
3 months ago
search-customers.php
3 months ago
update-customer.php
3 months ago
list-customers.php
89 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | class LatePointAbilityListCustomers extends LatePointAbstractCustomerAbility { |
| 7 | |
| 8 | protected function configure(): void { |
| 9 | $this->id = 'latepoint/list-customers'; |
| 10 | $this->label = __( 'List customers', 'latepoint' ); |
| 11 | $this->description = __( 'Returns a paginated list of customers with optional search/filter.', '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 | 'search' => [ |
| 23 | 'type' => 'string', |
| 24 | 'description' => __( 'Search by name, email, or phone.', 'latepoint' ), |
| 25 | ], |
| 26 | 'status' => [ |
| 27 | 'type' => 'string', |
| 28 | 'description' => __( 'Filter by customer status.', 'latepoint' ), |
| 29 | ], |
| 30 | ] |
| 31 | ), |
| 32 | ]; |
| 33 | } |
| 34 | |
| 35 | public function get_output_schema(): array { |
| 36 | return [ |
| 37 | 'type' => 'object', |
| 38 | 'properties' => [ |
| 39 | 'customers' => [ |
| 40 | 'type' => 'array', |
| 41 | 'items' => $this->customer_output_schema(), |
| 42 | ], |
| 43 | 'total' => [ 'type' => 'integer' ], |
| 44 | 'page' => [ 'type' => 'integer' ], |
| 45 | 'per_page' => [ 'type' => 'integer' ], |
| 46 | ], |
| 47 | ]; |
| 48 | } |
| 49 | |
| 50 | public function execute( array $args ) { |
| 51 | $page = max( 1, (int) ( $args['page'] ?? 1 ) ); |
| 52 | $per_page = min( 100, max( 1, (int) ( $args['per_page'] ?? 20 ) ) ); |
| 53 | $offset = ( $page - 1 ) * $per_page; |
| 54 | |
| 55 | $query = new OsCustomerModel(); |
| 56 | |
| 57 | if ( ! empty( $args['search'] ) ) { |
| 58 | $s = '%' . sanitize_text_field( $args['search'] ) . '%'; |
| 59 | $query->where( |
| 60 | [ |
| 61 | 'OR' => [ |
| 62 | 'first_name LIKE' => $s, |
| 63 | 'last_name LIKE' => $s, |
| 64 | 'email LIKE' => $s, |
| 65 | 'phone LIKE' => $s, |
| 66 | ], |
| 67 | ] |
| 68 | ); |
| 69 | } |
| 70 | if ( ! empty( $args['status'] ) ) { |
| 71 | $query->where( [ 'status' => sanitize_text_field( $args['status'] ) ] ); |
| 72 | } |
| 73 | |
| 74 | $customers = ( clone $query ) |
| 75 | ->order_by( 'last_name ASC, first_name ASC' ) |
| 76 | ->set_limit( $per_page ) |
| 77 | ->set_offset( $offset ) |
| 78 | ->get_results_as_models(); |
| 79 | $total = $query->count(); |
| 80 | |
| 81 | return [ |
| 82 | 'customers' => array_map( [ $this, 'serialize_customer' ], $customers ), |
| 83 | 'total' => (int) $total, |
| 84 | 'page' => $page, |
| 85 | 'per_page' => $per_page, |
| 86 | ]; |
| 87 | } |
| 88 | } |
| 89 |