PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.62
Timetics – Appointment Booking Calendar & Scheduling v1.0.62
1.0.62 1.0.63 1.0.61 1.0.60 1.0.59 1.0.58 1.0.57 1.0.56 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.19 1.0.2 1.0.20 1.0.21 1.0.22 All 64 releases
timetics / core / dummy-data / api-faker.php

api-faker.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.62, at core/dummy-data/api-faker.php

409 lines 10.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Faker api
4 *
5 * @package Timetics
6 */
7 namespace Timetics\Core\DummyData;
8
9 defined( 'ABSPATH' ) || exit;
10
11 use Timetics\Base\Api;
12 use Timetics\Core\Appointments\Appointment;
13 use Timetics\Core\Bookings\Booking;
14 use Timetics\Core\Customers\Customer;
15 use Timetics\Core\DummyData\Factory;
16 use Timetics\Core\Staffs\Staff;
17 use Timetics\Utils\Singleton;
18
19 class Api_Faker extends Api {
20 use Singleton;
21
22 /**
23 * Store api namespace
24 *
25 * @var string
26 */
27 protected $namespace = 'timetics/v1';
28
29 /**
30 * Store rest base
31 *
32 * @var string
33 */
34 protected $rest_base = 'faker';
35
36 /**
37 * Register rest routes
38 *
39 * @return void
40 */
41 public function register_routes() {
42 /**
43 * Register route
44 *
45 * @var void
46 */
47 register_rest_route(
48 $this->namespace, $this->rest_base, [
49 [
50 'methods' => \WP_REST_Server::READABLE,
51 'callback' => [$this, 'generate_faker'],
52 'permission_callback' => function () {
53 return current_user_can( 'manage_timetics' );
54 },
55 ],
56 ]
57 );
58 }
59
60 /**
61 * Get all bookings
62 *
63 * @param WP_Rest_Request $request
64 *
65 * @return JSON
66 */
67 public function generate_faker( $request ) {
68 $this->generate_staff();
69 $this->generate_customer();
70 $this->genere_meeting();
71 $this->generate_booking();
72
73 $data = [
74 'status' => 1,
75 'status_code' => 200,
76 'message' => __( 'Successfully generated dummy data', 'timetics' ),
77 ];
78
79 return rest_ensure_response( $data );
80 }
81
82 /**
83 * Generate meetings
84 *
85 * @return void
86 */
87 private function genere_meeting() {
88 $items = 1;
89 $factory = Factory::create();
90 $visibility = ['enabled', 'disabled'];
91 $staff = $this->get_staff();
92
93 for ( $i = 1; $i <= $items; $i++ ) {
94 $meeting = new Appointment();
95
96 $appointment_data = [
97 'name' => $factory->sentence,
98 'description' => $factory->paragraph( 7 ),
99 'type' => 'One-to-One',
100 'locations' => $this->get_locations( $factory->address ),
101 'staff' => [$staff],
102 'duration' => $this->get_duration(),
103 'price' => $this->get_price(),
104 'capacity' => "1",
105 'visibility' => $visibility[array_rand( $visibility )],
106 'schedule' => $this->get_schedule( $staff ),
107 'timezone' => 'Asia/Dhaka',
108 'availability' => $this->get_availability(),
109 ];
110
111 $meeting->set_props( $appointment_data );
112 $meeting->save();
113 }
114 }
115
116 /**
117 * Generate staff
118 *
119 * @return void
120 */
121 private function generate_staff() {
122 $items = 1;
123 $factory = Factory::create();
124
125 for ( $i = 1; $i <= $items; $i++ ) {
126 $staff = new Staff();
127 $args = [
128 'first_name' => $factory->first_name,
129 'last_name' => $factory->last_name,
130 'user_email' => $factory->email,
131 'user_login' => $factory->username,
132 'phone' => $factory->phone_number,
133 'schedule' => timetics_get_option( 'availability' ),
134 'user_pass' => $factory->password,
135 ];
136
137 $staff->create( $args );
138 }
139 }
140
141 /**
142 * Generate customer
143 *
144 * @return void
145 */
146 private function generate_customer() {
147 $items = 1;
148 $factory = Factory::create();
149
150 for ( $i = 1; $i <= $items; $i++ ) {
151 $customer = new Customer();
152 $args = [
153 'first_name' => $factory->first_name,
154 'last_name' => $factory->last_name,
155 'email' => $factory->email,
156 'user_name' => $factory->username,
157 'phone' => $factory->phone_number,
158 'password' => $factory->password,
159 ];
160
161 $customer->set_props( $args );
162 $customer->save();
163 }
164 }
165
166 private function generate_booking() {
167 $item = 1;
168 $factory = Factory::create();
169
170 for ( $i = 1; $i <= $item; $i++ ) {
171 $booking = new Booking();
172 $customer = new Customer( $this->get_customer() );
173 $staff = new Staff( $this->get_staff() );
174 $meeting = new Appointment( $this->get_meeting() );
175
176 $args = [
177 'customer' => $customer->get_id(),
178 'appointment' => $meeting->get_id(),
179 'staff' => $staff->get_id(),
180 'customer_fname' => $customer->get_first_name(),
181 'customer_lname' => $customer->get_last_name(),
182 'customer_email' => $customer->get_email(),
183 'customer_phone' => $customer->get_phone(),
184 'staff_fname' => $staff->get_first_name(),
185 'staff_lname' => $staff->get_last_name(),
186 'staff_email' => $staff->get_email(),
187 'meeting_name' => $meeting->get_name(),
188 'meeting_description' => $meeting->get_description(),
189 'meeting_type' => $meeting->get_type(),
190 'description' => $meeting->get_description(),
191 'start_date' => $factory->date,
192 'date' => $factory->date,
193 'end_date' => $factory->date,
194 'start_time' => $factory->time,
195 'end_time' => $factory->time,
196 'order_total' => $meeting->get_price(),
197 'post_status' => 'completed',
198 'location' => $factory->address,
199 'location_type' => 'in-person',
200 'timezone' => 'Asia/Dhaka',
201 ];
202
203 $booking->set_props( $args );
204
205 $booking->save();
206 }
207 }
208
209 /**
210 * Get meeting
211 *
212 * @return integer
213 */
214 private function get_meeting() {
215 $meetings = $this->get_meeting_ids();
216
217 return $meetings[array_rand( $meetings )];
218 }
219
220 /**
221 * Get staff id
222 *
223 * @return integer
224 */
225 private function get_staff() {
226 $staff = $this->get_staff_ids();
227
228 return $staff[array_rand( $staff )];
229 }
230
231 /**
232 * Get customer id
233 *
234 * @return integer
235 */
236 private function get_customer() {
237 $customers = $this->get_customer_ids();
238
239 return $customers[array_rand( $customers )];
240 }
241
242 /**
243 * Get staff ids
244 *
245 * @return array
246 */
247 private function get_staff_ids() {
248 $staff = new Staff();
249
250 $users = $staff->all();
251
252 $ids = [];
253
254 foreach ( $users['items'] as $user ) {
255 $ids[] = $user->ID;
256 }
257
258 return $ids;
259 }
260
261 /**
262 * Get all meeting ids
263 *
264 * @return array
265 */
266 private function get_meeting_ids() {
267 $ids = [];
268 $meeting = new Appointment();
269 $meetings = $meeting->all();
270
271 foreach ( $meetings['items'] as $meeting_ob ) {
272 $ids[] = $meeting_ob->ID;
273 }
274
275 return $ids;
276 }
277
278 /**
279 * Get customer ids
280 *
281 * @return array
282 */
283 private function get_customer_ids() {
284 $customer = new Customer();
285
286 $users = $customer->all();
287
288 $ids = [];
289
290 foreach ( $users['items'] as $user ) {
291 $ids[] = $user->ID;
292 }
293
294 return $ids;
295 }
296
297 /**
298 * Generate meeting schedule
299 *
300 * @return array
301 */
302 private function get_schedule( $staff ) {
303 return [
304 $staff => [
305 'Sun' => [
306 [
307 'start' => '9:00am',
308 'end' => '5:00pm',
309 ],
310 ],
311 'Mon' => [
312 [
313 'start' => '9:00am',
314 'end' => '5:00pm',
315 ],
316 ],
317 'Tue' => [
318 [
319 'start' => '9:00am',
320 'end' => '5:00pm',
321 ],
322 ],
323 'Wed' => [
324 [
325 'start' => '9:00am',
326 'end' => '5:00pm',
327 ],
328 ],
329 'Thu' => [
330 [
331 'start' => '9:00am',
332 'end' => '5:00pm',
333 ],
334 ],
335 'Fri' => [
336 [
337 'start' => '9:00am',
338 'end' => '5:00pm',
339 ],
340 ],
341 'Sat' => [
342 [
343 'start' => '9:00am',
344 'end' => '5:00pm',
345 ],
346 ],
347 ],
348 ];
349 }
350
351 /**
352 * Generate locations
353 *
354 * @param string $address
355 *
356 * @return string
357 */
358 private function get_locations( $address ) {
359 return [
360 [
361 'location' => $address,
362 'location_type' => 'in-person-meeting',
363 ],
364 ];
365 }
366
367 /**
368 * Generate availability
369 *
370 * @return array
371 */
372 private function get_availability() {
373 return [
374 'start' => gmdate( 'Y-m-d' ),
375 'end' => gmdate( 'Y-m-d', strtotime( '+1 month', time() ) ),
376 ];
377 }
378
379 /**
380 * Get generate price
381 *
382 * @return array
383 */
384 private function get_price() {
385 return [
386 [
387 'ticket_name' => 'default',
388 'ticket_price' => random_int( 30, 100 ),
389 'ticket_quantity' => '1',
390 ],
391 ];
392 }
393
394 /**
395 * Get meeting duration
396 *
397 * @return string
398 */
399 private function get_duration() {
400 $numbers = [15, 30, 60];
401 $number = $numbers[array_rand( $numbers )];
402 $unit = 'minute';
403
404 $duration = "$number $unit";
405
406 return $duration;
407 }
408 }
409