PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.0
Booktics – Appointment Booking Calendar for Service Businesses v1.0.0
1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 All 27 releases
booktics / core / order / order-validator.php

order-validator.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.0, at core/order/order-validator.php

154 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Booktics\Order;
4
5 use Booktics\Models\Service_Model;
6 use Booktics\Models\Team_Member_Model;
7 use WP_Error;
8
9 class Order_Validator {
10 /**
11 * Validate order creation request
12 *
13 * @param array $data Order data
14 *
15 * @return true|WP_Error Returns true if valid, WP_Error otherwise
16 */
17 public function validate( array $data ) {
18 $error = new WP_Error();
19
20 // Validate basic required fields
21 $required_fields = array(
22 'items',
23 );
24
25 foreach ( $required_fields as $field ) {
26 if ( empty( $data[ $field ] ) ) {
27 /* translators: %s: Field name */
28 $error->add( 'missing_field', sprintf( __( '%s is required', 'booktics' ), $field ) );
29 }
30 }
31
32 if ( empty( $data['customer_id'] ) && empty( $data['customer_email'] ) ) {
33 $error->add( 'missing_field', __( 'Customer email is required', 'booktics' ) );
34 }
35
36 if ( empty( $data['items'] ) || ! is_array( $data['items'] ) ) {
37 $error->add( 'invalid_items', __( 'Order must contain at least one item', 'booktics' ) );
38
39 return $error;
40 }
41
42 foreach ( $data['items'] as $item ) {
43 $item_validation = $this->validate_order_item( $item );
44 if ( is_wp_error( $item_validation ) ) {
45 return $item_validation;
46 }
47 }
48
49 return $error->has_errors() ? $error : true;
50 }
51
52 /**
53 * Validate individual order item
54 *
55 * @param array $item Order item data
56 *
57 * @return true|WP_Error Returns true if valid, WP_Error otherwise
58 */
59 private function validate_order_item( array $item ) {
60 $error = new WP_Error();
61
62 $service = ( new Service_Model() )->find( $item['service_id'] );
63 if ( ! $service ) {
64 $error->add( 'invalid_service', __( 'Invalid service selected', 'booktics' ) );
65 }
66
67 $team_member = ( new Team_Member_Model() )->find( $item['team_member_id'] );
68 if ( ! $team_member ) {
69 $error->add( 'invalid_team_member', __( 'Invalid team member selected', 'booktics' ) );
70 }
71 $error = apply_filters( 'booktics_extra_service_validate', $error, $item );
72
73 return $error->has_errors() ? $error : true;
74 }
75
76 /**
77 * Validate time slot availability
78 *
79 * @param array $item Order item data
80 *
81 * @return true|WP_Error Returns true if valid, WP_Error otherwise
82 */
83 private function validate_time_slot( array $item ) {
84 $error = new WP_Error();
85
86 $args = array(
87 'post_type' => 'booktics-appointment',
88 'post_status' => 'any',
89 'posts_per_page' => 1,
90 'meta_query' => array(
91 'relation' => 'AND',
92 array(
93 'key' => 'date',
94 'value' => $item['date'],
95 'compare' => '=',
96 ),
97 array(
98 'key' => 'team_member_id',
99 'value' => $item['team_member_id'],
100 'compare' => '=',
101 ),
102 array(
103 'relation' => 'OR',
104 // Appointment starts during requested time slot
105 array(
106 'relation' => 'AND',
107 array(
108 'key' => 'start_time',
109 'value' => $item['start_time'],
110 'compare' => '<=',
111 ),
112 array(
113 'key' => 'end_time',
114 'value' => $item['start_time'],
115 'compare' => '>',
116 ),
117 ),
118 // Appointment ends during requested time slot
119 array(
120 'relation' => 'AND',
121 array(
122 'key' => 'start_time',
123 'value' => $item['end_time'],
124 'compare' => '<',
125 ),
126 array(
127 'key' => 'end_time',
128 'value' => $item['end_time'],
129 'compare' => '>=',
130 ),
131 ),
132 ),
133 ),
134 );
135
136 $query = new \WP_Query( $args );
137
138 if ( $query->have_posts() ) {
139 $error->add(
140 'time_slot_unavailable',
141 /* translators: 1: Start time, 2: End time, 3: Date */
142 sprintf(
143 __( 'Time slot %1$s - %2$s on %3$s is not available', 'booktics' ),
144 $item['start_time'],
145 $item['end_time'],
146 $item['date']
147 )
148 );
149 }
150
151 return $error->has_errors() ? $error : true;
152 }
153 }
154