PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.22
Timetics – Appointment Booking Calendar & Scheduling v1.0.22
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 1.0.23 1.0.24 All 62 releases
timetics / core / appointments / api-appointment-taxonomy.php

api-appointment-taxonomy.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.22, at core/appointments/api-appointment-taxonomy.php

392 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Appointments taxonomy api
4 *
5 * @package Timetics
6 */
7 namespace Timetics\Core\Appointments;
8
9 use Timetics\Base\Api;
10 use Timetics\Utils\Singleton;
11 use WP_Error;
12
13 class ApiAppointmentTaxonomy extends Api {
14 use Singleton;
15
16 /**
17 * Store api namespace
18 *
19 * @since 1.10.0
20 *
21 * @var string $namespace
22 */
23 protected $namespace = 'timetics/v1';
24
25 /**
26 * Store rest base
27 *
28 * @since 1.10.0
29 *
30 * @var string $rest_base
31 */
32 protected $rest_base = 'appointments';
33
34 /**
35 * Store taxonomy name
36 *
37 * @since 1.10.0
38 *
39 * @var string
40 */
41 protected $taxonomy = 'timetics-meeting-category';
42
43 /**
44 * Register rest routes.
45 *
46 * @since 1.10.0
47 *
48 * @return void
49 */
50 public function register_routes() {
51 /*
52 * Register route
53 */
54 register_rest_route( $this->namespace, $this->rest_base . '/categories', [
55 [
56 'methods' => \WP_REST_Server::READABLE,
57 'callback' => [$this, 'get_items'],
58 'permission_callback' => function () {
59 return true;
60 },
61 ],
62 [
63 'methods' => \WP_REST_Server::CREATABLE,
64 'callback' => [$this, 'create_item'],
65 'permission_callback' => function () {
66 return current_user_can( 'manage_timetics' );
67 },
68 ],
69 [
70 'methods' => \WP_REST_Server::DELETABLE,
71 'callback' => [$this, 'bulk_delete'],
72 'permission_callback' => function () {
73 return current_user_can( 'manage_timetics' );
74 },
75 ],
76 ] );
77
78 /*
79 * Register route
80 */
81 register_rest_route( $this->namespace, $this->rest_base . '/categories' . '/(?P<category_id>[\d]+)', [
82 [
83 'methods' => \WP_REST_Server::READABLE,
84 'callback' => [$this, 'get_item'],
85 'permission_callback' => function () {
86 return true;
87 },
88 ],
89 [
90 'methods' => \WP_REST_Server::EDITABLE,
91 'callback' => [$this, 'update'],
92 'permission_callback' => function () {
93 return current_user_can( 'manage_timetics' );
94 },
95 ],
96 [
97 'methods' => \WP_REST_Server::DELETABLE,
98 'callback' => [$this, 'delete'],
99 'permission_callback' => function () {
100 return current_user_can( 'manage_timetics' );
101 },
102 ],
103 ] );
104 }
105
106 /**
107 * Get categories
108 *
109 * @param WP_Rest_Request $request
110 *
111 * @return JSON
112 */
113 public function get_items( $request ) {
114 $per_page = ! empty( $request['per_page'] ) ? intval( $request['per_page'] ) : 20;
115 $paged = ! empty( $request['paged'] ) ? intval( $request['paged'] ) : 1;
116 $offset = ( $paged - 1 ) * $per_page;
117 $taxonomy = $this->taxonomy;
118
119 $total_terms = wp_count_terms( $taxonomy );
120
121 $terms = get_terms( [
122 'taxonomy' => $taxonomy,
123 'offset' => $offset,
124 'number' => $per_page,
125 'hide_empty' => false,
126 ] );
127
128 $terms = array_map( function ( $term ) {
129 $posts_ids = $this->get_post_ids( $term->term_id );
130 $term->meeting_ids = $posts_ids;
131 $term->permalink = get_term_link( $term );
132 return $term;
133 }, $terms );
134
135 $data = [
136 'success' => 1,
137 'status_code' => 200,
138 'message' => __( 'Category list', 'timetics' ),
139 'data' => [
140 'total' => $total_terms,
141 'category' => $terms,
142 ],
143 ];
144
145 return rest_ensure_response( $data );
146 }
147
148 /**
149 * Get category
150 *
151 * @param WP_Rest_Request $request
152 *
153 * @return JSON
154 */
155 public function get_item( $request ) {
156 $id = intval( $request['category_id'] );
157 $category = get_term( $id );
158
159 $posts = $this->get_post_ids( $category->term_id );
160 $post_data = [];
161 if(!empty($posts)){
162 foreach($posts as $post){
163 $post_data[] = $this->prepare_get_item($post);
164 }
165 }
166 $category->meetings = $post_data;
167 $data = [
168 'success' => 1,
169 'status_code' => 200,
170 'message' => __( 'Category list', 'timetics' ),
171 'data' => [
172 'category' => $category,
173 ],
174 ];
175
176 return rest_ensure_response( $data );
177 }
178
179 /**
180 * Create category
181 *
182 * @param WP_Rest_Request $request
183 *
184 * @return void
185 */
186 public function create_item( $request ) {
187 $data = json_decode( $request->get_body(), true );
188
189 $category_name = ! empty( $data['category_name'] ) ? sanitize_text_field( $data['category_name'] ) : '';
190 $parent = ! empty( $data['parent'] ) ? intval( $data['parent'] ) : 0;
191 $meeting_ids = ! empty( $data['meeting_ids'] ) ? array_map( 'intval', $data['meeting_ids'] ) : [];
192
193 $category = wp_insert_term( $category_name, $this->taxonomy, [
194 'parent' => $parent,
195 ] );
196
197 if ( is_wp_error( $category ) ) {
198 return new WP_Error( $category->get_error_code(), $category->get_error_message() );
199 }
200
201 $this->assign_meetings( $category['term_id'], $meeting_ids );
202
203 $category = get_term( $category['term_id'] );
204
205 $posts = $this->get_post_ids( $category->term_id );
206 $category->meeting_ids = $posts;
207 $category->permalink = get_term_link($category->term_id );
208
209 $response = [
210 'success' => 1,
211 'status_code' => 200,
212 'message' => __( 'Category successfully created', 'timetics' ),
213 'data' => $category,
214 ];
215
216 return rest_ensure_response( $response );
217 }
218
219 /**
220 * Update category
221 *
222 * @param WP_Rest_Request $request
223 *
224 * @return JSON
225 */
226 public function update( $request ) {
227 $id = intval( $request['category_id'] );
228 $data = json_decode( $request->get_body(), true );
229 $category_name = ! empty( $data['category_name'] ) ? sanitize_text_field( $data['category_name'] ) : '';
230 $parent = ! empty( $data['parent'] ) ? intval( $data['parent'] ) : 0;
231 $meeting_ids = ! empty( $data['meeting_ids'] ) ? array_map( 'intval', $data['meeting_ids'] ) : [];
232
233 $args = [
234 'name' => $category_name,
235 'parent' => $parent,
236 ];
237
238 $category = wp_update_term( $id, $this->taxonomy, $args );
239
240 if ( is_wp_error( $category ) ) {
241 return new WP_Error( $category->get_error_code(), $category->get_error_message() );
242 }
243
244 $this->assign_meetings( $category['term_id'], $meeting_ids );
245
246 $category = get_term( $category['term_id'] );
247
248 $posts = $this->get_post_ids( $category->term_id );
249 $category->meeting_ids = $posts;
250 $category->permalink = get_term_link( $category->term_id );
251
252 $response = [
253 'success' => 1,
254 'status_code' => 200,
255 'message' => __( 'Category successfully updated', 'timetics' ),
256 'data' => $category,
257 ];
258
259 return rest_ensure_response( $response );
260 }
261
262 /**
263 * Delete category
264 *
265 * @param WP_Rest $request
266 *
267 * @return JSON
268 */
269 public function delete( $request ) {
270 $id = intval( $request['category_id'] );
271
272 $category = wp_delete_term( $id, $this->taxonomy );
273
274 if ( ! $category ) {
275 return new WP_Error( 'category_not_exist', __( 'Category doesn\'t exist', 'timetics' ) );
276 }
277
278 $response = [
279 'success' => 1,
280 'status_code' => 200,
281 'message' => __( 'Successfully deleted, appointment category', 'timetics' ),
282 ];
283
284 return rest_ensure_response( $response );
285 }
286
287 /**
288 * Delete multiple category
289 *
290 * @param WP_Rest_Request $request
291 *
292 * @return JSON
293 */
294 public function bulk_delete( $request ) {
295 $data = json_decode( $request->get_body(), true );
296
297 $ids = ! empty( $data['ids'] ) ? array_map( 'intval', $data['ids'] ) : [];
298 $counter = 0;
299
300 if ( ! $ids ) {
301 return new WP_Error( 'cagetory_ids_empty', __( 'Category id required', 'timetics' ) );
302 }
303
304 foreach ( $ids as $id ) {
305 $delete = wp_delete_term( $id, $this->taxonomy );
306
307 if ( $delete ) {
308 $counter++;
309 }
310 }
311
312 if ( $counter < 1 ) {
313 return new WP_Error( 'cagetory_not_exist', __( 'No category exists.', 'timetics' ) );
314 }
315
316 $response = [
317 'success' => 1,
318 'status_code' => 200,
319 'message' => sprintf( __( 'Successfully deleted %d out of %d categories', 'timetics' ), $counter, count( $ids ) ),
320 ];
321
322 return rest_ensure_response( $response );
323 }
324
325 /**
326 * Assign post to the selected category
327 *
328 * @param integer $category_id Category id that need to be assign
329 * @param array $meeting_ids Meeting ids that will be assign
330 *
331 * @return void
332 */
333 private function assign_meetings( $category_id, $meeting_ids ) {
334 // first remove objects from existing category.
335 $posts = $this->get_post_ids( $category_id );
336
337 foreach ( $posts as $meeting_id ) {
338 wp_remove_object_terms( $meeting_id, $category_id, $this->taxonomy );
339 }
340
341
342 foreach ( $meeting_ids as $meeting_id ) {
343 $terms = get_the_terms( $meeting_id, $this->taxonomy );
344 $categories = [$category_id];
345
346 if ( $terms && ! is_wp_error( $terms ) ) {
347 $categories = array_merge( $categories, array_column( $terms, 'term_id' ) );
348 }
349
350 wp_set_post_terms( $meeting_id, $categories, $this->taxonomy );
351 }
352 }
353
354 /**
355 * Get posts ids
356 *
357 * @return array
358 */
359 private function get_post_ids( $term_id ) {
360 $args = [
361 'post_type' => 'timetics-appointment',
362 'fields' => 'ids',
363 'tax_query' => [
364 [
365 'taxonomy' => 'timetics-meeting-category',
366 'terms' => $term_id,
367 ],
368 ],
369 ];
370
371 $posts = get_posts( $args );
372
373 return $posts;
374 }
375
376 public function prepare_get_item( $appoint_id, $timezone = '' ) {
377 $appointment = new Appointment( $appoint_id );
378 $data = [
379 'id' => $appointment->get_id(),
380 'name' => $appointment->get_name(),
381 'image' => $appointment->get_image(),
382 'link' => $appointment->get_link(),
383 'description' => $appointment->get_description(),
384 'type' => $appointment->get_type(),
385 'locations' => $appointment->get_locations(),
386 'price' => $appointment->get_price(),
387 ];
388
389 return $data;
390 }
391 }
392