abstract-customer-ability.php
4 months ago
connect-customer-to-wp-user.php
1 week ago
create-customer.php
4 months ago
delete-customer.php
1 week ago
get-customer-bookings.php
1 week ago
get-customer-by-email.php
1 week ago
get-customer-orders.php
1 week ago
get-customer.php
1 week ago
get-total-customers-count.php
1 week ago
list-customers.php
1 week ago
search-customers.php
1 week ago
update-customer.php
1 week ago
search-customers.php
74 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | class LatePointAbilitySearchCustomers extends LatePointAbstractCustomerAbility { |
| 7 | |
| 8 | protected function configure(): void { |
| 9 | $this->id = 'latepoint/search-customers'; |
| 10 | $this->label = __( 'Search customers', 'latepoint' ); |
| 11 | $this->description = __( 'Searches customers by name, email, or phone — optimised for autocomplete.', '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' => [ |
| 20 | 'query' => [ |
| 21 | 'type' => 'string', |
| 22 | 'description' => __( 'Search term.', 'latepoint' ), |
| 23 | ], |
| 24 | 'limit' => [ |
| 25 | 'type' => 'integer', |
| 26 | 'default' => 10, |
| 27 | 'minimum' => 1, |
| 28 | 'maximum' => 50, |
| 29 | ], |
| 30 | ], |
| 31 | 'required' => [ 'query' ], |
| 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 | ], |
| 44 | ]; |
| 45 | } |
| 46 | |
| 47 | public function execute( array $args ) { |
| 48 | $search = sanitize_text_field( $args['query'] ); |
| 49 | $limit = min( 50, max( 1, (int) ( $args['limit'] ?? 10 ) ) ); |
| 50 | |
| 51 | $s = '%' . $search . '%'; |
| 52 | $query = ( new OsCustomerModel() ) |
| 53 | ->where( |
| 54 | [ |
| 55 | 'OR' => [ |
| 56 | 'first_name LIKE' => $s, |
| 57 | 'last_name LIKE' => $s, |
| 58 | 'email LIKE' => $s, |
| 59 | 'phone LIKE' => $s, |
| 60 | ], |
| 61 | ] |
| 62 | ); |
| 63 | $query->filter_allowed_records(); |
| 64 | $customers = $query |
| 65 | ->order_by( 'last_name ASC' ) |
| 66 | ->set_limit( $limit ) |
| 67 | ->get_results_as_models(); |
| 68 | |
| 69 | return [ |
| 70 | 'customers' => array_map( [ $this, 'serialize_customer' ], $customers ), |
| 71 | ]; |
| 72 | } |
| 73 | } |
| 74 |