PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.16
Timetics – Appointment Booking Calendar & Scheduling v1.0.16
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.16, at core/appointments/api-appointment-taxonomy.php

390 lines 11.4 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 'data' => $category,
213 ];
214
215 return rest_ensure_response( $response );
216 }
217
218 /**
219 * Update category
220 *
221 * @param WP_Rest_Request $request
222 *
223 * @return JSON
224 */
225 public function update( $request ) {
226 $id = intval( $request['category_id'] );
227 $data = json_decode( $request->get_body(), true );
228 $category_name = ! empty( $data['category_name'] ) ? sanitize_text_field( $data['category_name'] ) : '';
229 $parent = ! empty( $data['parent'] ) ? intval( $data['parent'] ) : 0;
230 $meeting_ids = ! empty( $data['meeting_ids'] ) ? array_map( 'intval', $data['meeting_ids'] ) : [];
231
232 $args = [
233 'name' => $category_name,
234 'parent' => $parent,
235 ];
236
237 $category = wp_update_term( $id, $this->taxonomy, $args );
238
239 if ( is_wp_error( $category ) ) {
240 return new WP_Error( $category->get_error_code(), $category->get_error_message() );
241 }
242
243 $this->assign_meetings( $category['term_id'], $meeting_ids );
244
245 $category = get_term( $category['term_id'] );
246
247 $posts = $this->get_post_ids( $category->term_id );
248 $category->meeting_ids = $posts;
249 $category->permalink = get_term_link( $category->term_id );
250
251 $response = [
252 'success' => 1,
253 'status_code' => 200,
254 'data' => $category,
255 ];
256
257 return rest_ensure_response( $response );
258 }
259
260 /**
261 * Delete category
262 *
263 * @param WP_Rest $request
264 *
265 * @return JSON
266 */
267 public function delete( $request ) {
268 $id = intval( $request['category_id'] );
269
270 $category = wp_delete_term( $id, $this->taxonomy );
271
272 if ( ! $category ) {
273 return new WP_Error( 'category_not_exist', __( 'Category doesn\'t exist', 'timetics' ) );
274 }
275
276 $response = [
277 'success' => 1,
278 'status_code' => 200,
279 'message' => __( 'Successfully deleted, appointment category', 'timetics' ),
280 ];
281
282 return rest_ensure_response( $response );
283 }
284
285 /**
286 * Delete multiple category
287 *
288 * @param WP_Rest_Request $request
289 *
290 * @return JSON
291 */
292 public function bulk_delete( $request ) {
293 $data = json_decode( $request->get_body(), true );
294
295 $ids = ! empty( $data['ids'] ) ? array_map( 'intval', $data['ids'] ) : [];
296 $counter = 0;
297
298 if ( ! $ids ) {
299 return new WP_Error( 'cagetory_ids_empty', __( 'Category id required', 'timetics' ) );
300 }
301
302 foreach ( $ids as $id ) {
303 $delete = wp_delete_term( $id, $this->taxonomy );
304
305 if ( $delete ) {
306 $counter++;
307 }
308 }
309
310 if ( $counter < 1 ) {
311 return new WP_Error( 'cagetory_not_exist', __( 'No category exists.', 'timetics' ) );
312 }
313
314 $response = [
315 'success' => 1,
316 'status_code' => 200,
317 'message' => sprintf( __( 'Successfully deleted %d out of %d categories', 'timetics' ), $counter, count( $ids ) ),
318 ];
319
320 return rest_ensure_response( $response );
321 }
322
323 /**
324 * Assign post to the selected category
325 *
326 * @param integer $category_id Category id that need to be assign
327 * @param array $meeting_ids Meeting ids that will be assign
328 *
329 * @return void
330 */
331 private function assign_meetings( $category_id, $meeting_ids ) {
332 // first remove objects from existing category.
333 $posts = $this->get_post_ids( $category_id );
334
335 foreach ( $posts as $meeting_id ) {
336 wp_remove_object_terms( $meeting_id, $category_id, $this->taxonomy );
337 }
338
339
340 foreach ( $meeting_ids as $meeting_id ) {
341 $terms = get_the_terms( $meeting_id, $this->taxonomy );
342 $categories = [$category_id];
343
344 if ( $terms && ! is_wp_error( $terms ) ) {
345 $categories = array_merge( $categories, array_column( $terms, 'term_id' ) );
346 }
347
348 wp_set_post_terms( $meeting_id, $categories, $this->taxonomy );
349 }
350 }
351
352 /**
353 * Get posts ids
354 *
355 * @return array
356 */
357 private function get_post_ids( $term_id ) {
358 $args = [
359 'post_type' => 'timetics-appointment',
360 'fields' => 'ids',
361 'tax_query' => [
362 [
363 'taxonomy' => 'timetics-meeting-category',
364 'terms' => $term_id,
365 ],
366 ],
367 ];
368
369 $posts = get_posts( $args );
370
371 return $posts;
372 }
373
374 public function prepare_get_item( $appoint_id, $timezone = '' ) {
375 $appointment = new Appointment( $appoint_id );
376 $data = [
377 'id' => $appointment->get_id(),
378 'name' => $appointment->get_name(),
379 'image' => $appointment->get_image(),
380 'link' => $appointment->get_link(),
381 'description' => $appointment->get_description(),
382 'type' => $appointment->get_type(),
383 'locations' => $appointment->get_locations(),
384 'price' => $appointment->get_price(),
385 ];
386
387 return $data;
388 }
389 }
390