| 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 |
/* translators: 1: Number of deleted categories, 2: Total number of categories */ |
| 320 |
'message' => sprintf( __( 'Successfully deleted %1$d out of %2$d categories', 'timetics' ), $counter, count( $ids ) ), |
| 321 |
]; |
| 322 |
|
| 323 |
return rest_ensure_response( $response ); |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Assign post to the selected category |
| 328 |
* |
| 329 |
* @param integer $category_id Category id that need to be assign |
| 330 |
* @param array $meeting_ids Meeting ids that will be assign |
| 331 |
* |
| 332 |
* @return void |
| 333 |
*/ |
| 334 |
private function assign_meetings( $category_id, $meeting_ids ) { |
| 335 |
// first remove objects from existing category. |
| 336 |
$posts = $this->get_post_ids( $category_id ); |
| 337 |
|
| 338 |
foreach ( $posts as $meeting_id ) { |
| 339 |
wp_remove_object_terms( $meeting_id, $category_id, $this->taxonomy ); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
foreach ( $meeting_ids as $meeting_id ) { |
| 344 |
$terms = get_the_terms( $meeting_id, $this->taxonomy ); |
| 345 |
$categories = [$category_id]; |
| 346 |
|
| 347 |
if ( $terms && ! is_wp_error( $terms ) ) { |
| 348 |
$categories = array_merge( $categories, array_column( $terms, 'term_id' ) ); |
| 349 |
} |
| 350 |
|
| 351 |
wp_set_post_terms( $meeting_id, $categories, $this->taxonomy ); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* Get posts ids |
| 357 |
* |
| 358 |
* @return array |
| 359 |
*/ |
| 360 |
private function get_post_ids( $term_id ) { |
| 361 |
$args = [ |
| 362 |
'post_type' => 'timetics-appointment', |
| 363 |
'fields' => 'ids', |
| 364 |
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Tax query is necessary for filtering appointments by category |
| 365 |
'tax_query' => [ |
| 366 |
[ |
| 367 |
'taxonomy' => 'timetics-meeting-category', |
| 368 |
'terms' => $term_id, |
| 369 |
], |
| 370 |
], |
| 371 |
]; |
| 372 |
|
| 373 |
$posts = get_posts( $args ); |
| 374 |
|
| 375 |
return $posts; |
| 376 |
} |
| 377 |
|
| 378 |
public function prepare_get_item( $appoint_id, $timezone = '' ) { |
| 379 |
$appointment = new Appointment( $appoint_id ); |
| 380 |
$data = [ |
| 381 |
'id' => $appointment->get_id(), |
| 382 |
'name' => $appointment->get_name(), |
| 383 |
'image' => $appointment->get_image(), |
| 384 |
'link' => $appointment->get_link(), |
| 385 |
'description' => $appointment->get_description(), |
| 386 |
'type' => $appointment->get_type(), |
| 387 |
'locations' => $appointment->get_locations(), |
| 388 |
'price' => $appointment->get_price(), |
| 389 |
'visibility' => $appointment->get_visibility(), |
| 390 |
]; |
| 391 |
|
| 392 |
return $data; |
| 393 |
} |
| 394 |
} |
| 395 |
|