PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.64
Timetics – Appointment Booking Calendar & Scheduling v1.0.64
1.0.64 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 All 65 releases
← All changes | core/appointments/api-appointment-taxonomy.php +81 -2 1.0.13 → 1.0.64 View file →
@@ -5,8 +5,10 @@
5 5 * @package Timetics
6 6 */
7 7 namespace Timetics\Core\Appointments;
8 8
9 +defined( 'ABSPATH' ) || exit;
10 +
9 11 use Timetics\Base\Api;
10 12 use Timetics\Utils\Singleton;
11 13 use WP_Error;
12 14
@@ -79,8 +81,15 @@
79 81 * Register route
80 82 */
81 83 register_rest_route( $this->namespace, $this->rest_base . '/categories' . '/(?P<category_id>[\d]+)', [
82 84 [
85 + 'methods' => \WP_REST_Server::READABLE,
86 + 'callback' => [$this, 'get_item'],
87 + 'permission_callback' => function () {
88 + return true;
89 + },
90 + ],
91 + [
83 92 'methods' => \WP_REST_Server::EDITABLE,
84 93 'callback' => [$this, 'update'],
85 94 'permission_callback' => function () {
86 95 return current_user_can( 'manage_timetics' );
@@ -120,8 +129,9 @@
120 129
121 130 $terms = array_map( function ( $term ) {
122 131 $posts_ids = $this->get_post_ids( $term->term_id );
123 132 $term->meeting_ids = $posts_ids;
133 + $term->permalink = get_term_link( $term );
124 134 return $term;
125 135 }, $terms );
126 136
127 137 $data = [
@@ -137,8 +147,39 @@
137 147 return rest_ensure_response( $data );
138 148 }
139 149
140 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 + /**
141 182 * Create category
142 183 *
143 184 * @param WP_Rest_Request $request
144 185 *
@@ -164,12 +205,14 @@
164 205 $category = get_term( $category['term_id'] );
165 206
166 207 $posts = $this->get_post_ids( $category->term_id );
167 208 $category->meeting_ids = $posts;
209 + $category->permalink = get_term_link($category->term_id );
168 210
169 211 $response = [
170 212 'success' => 1,
171 213 'status_code' => 200,
214 + 'message' => __( 'Category successfully created', 'timetics' ),
172 215 'data' => $category,
173 216 ];
174 217
175 218 return rest_ensure_response( $response );
@@ -205,12 +248,14 @@
205 248 $category = get_term( $category['term_id'] );
206 249
207 250 $posts = $this->get_post_ids( $category->term_id );
208 251 $category->meeting_ids = $posts;
252 + $category->permalink = get_term_link( $category->term_id );
209 253
210 254 $response = [
211 255 'success' => 1,
212 256 'status_code' => 200,
257 + 'message' => __( 'Category successfully updated', 'timetics' ),
213 258 'data' => $category,
214 259 ];
215 260
216 261 return rest_ensure_response( $response );
@@ -272,9 +317,10 @@
272 317
273 318 $response = [
274 319 'success' => 1,
275 320 'status_code' => 200,
276 - 'message' => sprintf( __( 'Successfully deleted %d out of %d categories', 'timetics' ), $counter, count( $ids ) ),
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 ) ),
277 323 ];
278 324
279 325 return rest_ensure_response( $response );
280 326 }
@@ -287,10 +333,25 @@
287 333 *
288 334 * @return void
289 335 */
290 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 +
291 345 foreach ( $meeting_ids as $meeting_id ) {
292 - wp_set_post_terms( $meeting_id, [$category_id], $this->taxonomy );
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 );
293 354 }
294 355 }
295 356
296 357 /**
@@ -301,8 +362,9 @@
301 362 private function get_post_ids( $term_id ) {
302 363 $args = [
303 364 'post_type' => 'timetics-appointment',
304 365 'fields' => 'ids',
366 + // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Tax query is necessary for filtering appointments by category
305 367 'tax_query' => [
306 368 [
307 369 'taxonomy' => 'timetics-meeting-category',
308 370 'terms' => $term_id,
@@ -312,6 +374,23 @@
312 374
313 375 $posts = get_posts( $args );
314 376
315 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;
316 395 }
317 396 }