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 / appointments / api-appointment-taxonomy.php

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

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