| @@ -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 | } |
| @@ -296,9 +342,16 @@ | ||
| 296 | 342 | } |
| 297 | 343 | |
| 298 | 344 | |
| 299 | 345 | foreach ( $meeting_ids as $meeting_id ) { |
| 300 | - 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 ); | |
| 301 | 354 | } |
| 302 | 355 | } |
| 303 | 356 | |
| 304 | 357 | /** |
| @@ -309,8 +362,9 @@ | ||
| 309 | 362 | private function get_post_ids( $term_id ) { |
| 310 | 363 | $args = [ |
| 311 | 364 | 'post_type' => 'timetics-appointment', |
| 312 | 365 | 'fields' => 'ids', |
| 366 | + // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Tax query is necessary for filtering appointments by category | |
| 313 | 367 | 'tax_query' => [ |
| 314 | 368 | [ |
| 315 | 369 | 'taxonomy' => 'timetics-meeting-category', |
| 316 | 370 | 'terms' => $term_id, |
| @@ -320,6 +374,23 @@ | ||
| 320 | 374 | |
| 321 | 375 | $posts = get_posts( $args ); |
| 322 | 376 | |
| 323 | 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; | |
| 324 | 395 | } |
| 325 | 396 | } |