abstract-service-ability.php
3 months ago
create-service.php
3 months ago
delete-service.php
3 months ago
disable-service.php
3 months ago
duplicate-service.php
3 months ago
enable-service.php
3 months ago
get-service-agents.php
3 months ago
get-service-bookings.php
3 months ago
get-service.php
3 months ago
get-services.php
3 months ago
list-service-categories.php
3 months ago
update-service.php
3 months ago
create-service.php
90 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | class LatePointAbilityCreateService extends LatePointAbstractServiceAbility { |
| 7 | |
| 8 | protected function configure(): void { |
| 9 | $this->id = 'latepoint/create-service'; |
| 10 | $this->label = __( 'Create service', 'latepoint' ); |
| 11 | $this->description = __( 'Creates a new bookable service that customers can select when making appointments. Requires a name at minimum.', 'latepoint' ); |
| 12 | $this->permission = 'service__create'; |
| 13 | $this->read_only = false; |
| 14 | $this->destructive = false; |
| 15 | $this->idempotent = false; |
| 16 | } |
| 17 | |
| 18 | public function get_input_schema(): array { |
| 19 | return [ |
| 20 | 'type' => 'object', |
| 21 | 'properties' => [ |
| 22 | 'name' => [ |
| 23 | 'type' => 'string', |
| 24 | 'description' => __( 'Service name.', 'latepoint' ), |
| 25 | ], |
| 26 | 'description' => [ 'type' => 'string' ], |
| 27 | 'duration' => [ |
| 28 | 'type' => 'integer', |
| 29 | 'description' => __( 'Duration in minutes.', 'latepoint' ), |
| 30 | ], |
| 31 | 'price' => [ |
| 32 | 'type' => 'number', |
| 33 | 'description' => __( 'Service price.', 'latepoint' ), |
| 34 | ], |
| 35 | 'category_id' => [ 'type' => 'integer' ], |
| 36 | 'color' => [ 'type' => 'string' ], |
| 37 | 'capacity_min' => [ |
| 38 | 'type' => 'integer', |
| 39 | 'default' => 1, |
| 40 | ], |
| 41 | 'capacity_max' => [ |
| 42 | 'type' => 'integer', |
| 43 | 'default' => 1, |
| 44 | ], |
| 45 | ], |
| 46 | 'required' => [ 'name', 'duration' ], |
| 47 | ]; |
| 48 | } |
| 49 | |
| 50 | public function get_output_schema(): array { |
| 51 | return $this->service_output_schema(); |
| 52 | } |
| 53 | |
| 54 | public function execute( array $args ) { |
| 55 | $service = new OsServiceModel(); |
| 56 | $service->name = sanitize_text_field( $args['name'] ); |
| 57 | $service->duration = (int) $args['duration']; |
| 58 | $service->status = LATEPOINT_SERVICE_STATUS_ACTIVE; |
| 59 | |
| 60 | if ( isset( $args['description'] ) ) { |
| 61 | $service->short_description = sanitize_textarea_field( $args['description'] ); |
| 62 | } |
| 63 | if ( isset( $args['price'] ) ) { |
| 64 | $service->price_min = (float) $args['price']; |
| 65 | } |
| 66 | if ( ! empty( $args['category_id'] ) ) { |
| 67 | $service->category_id = (int) $args['category_id']; |
| 68 | } |
| 69 | if ( ! empty( $args['color'] ) ) { |
| 70 | $service->bg_color = sanitize_hex_color( $args['color'] ) ?? sanitize_text_field( $args['color'] ); |
| 71 | } |
| 72 | if ( isset( $args['capacity_min'] ) ) { |
| 73 | $service->capacity_min = (int) $args['capacity_min']; |
| 74 | } |
| 75 | if ( isset( $args['capacity_max'] ) ) { |
| 76 | $service->capacity_max = (int) $args['capacity_max']; |
| 77 | } |
| 78 | |
| 79 | if ( ! $service->save() ) { |
| 80 | return new WP_Error( |
| 81 | 'save_failed', |
| 82 | __( 'Failed to create service.', 'latepoint' ), |
| 83 | WP_DEBUG ? [ 'errors' => $service->get_error_messages() ] : [ 'status' => 422 ] |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | return $this->serialize_service( new OsServiceModel( $service->id ) ); |
| 88 | } |
| 89 | } |
| 90 |