| 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::EDITABLE, |
| 84 |
'callback' => [$this, 'update'], |
| 85 |
'permission_callback' => function () { |
| 86 |
return current_user_can( 'manage_timetics' ); |
| 87 |
}, |
| 88 |
], |
| 89 |
[ |
| 90 |
'methods' => \WP_REST_Server::DELETABLE, |
| 91 |
'callback' => [$this, 'delete'], |
| 92 |
'permission_callback' => function () { |
| 93 |
return current_user_can( 'manage_timetics' ); |
| 94 |
}, |
| 95 |
], |
| 96 |
] ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get categories |
| 101 |
* |
| 102 |
* @param WP_Rest_Request $request |
| 103 |
* |
| 104 |
* @return JSON |
| 105 |
*/ |
| 106 |
public function get_items( $request ) { |
| 107 |
$per_page = ! empty( $request['per_page'] ) ? intval( $request['per_page'] ) : 20; |
| 108 |
$paged = ! empty( $request['paged'] ) ? intval( $request['paged'] ) : 1; |
| 109 |
$offset = ( $paged - 1 ) * $per_page; |
| 110 |
$taxonomy = $this->taxonomy; |
| 111 |
|
| 112 |
$total_terms = wp_count_terms( $taxonomy ); |
| 113 |
|
| 114 |
$terms = get_terms( [ |
| 115 |
'taxonomy' => $taxonomy, |
| 116 |
'offset' => $offset, |
| 117 |
'number' => $per_page, |
| 118 |
'hide_empty' => false, |
| 119 |
] ); |
| 120 |
|
| 121 |
$terms = array_map( function ( $term ) { |
| 122 |
$posts_ids = $this->get_post_ids( $term->term_id ); |
| 123 |
$term->meeting_ids = $posts_ids; |
| 124 |
return $term; |
| 125 |
}, $terms ); |
| 126 |
|
| 127 |
$data = [ |
| 128 |
'success' => 1, |
| 129 |
'status_code' => 200, |
| 130 |
'message' => __( 'Category list', 'timetics' ), |
| 131 |
'data' => [ |
| 132 |
'total' => $total_terms, |
| 133 |
'category' => $terms, |
| 134 |
], |
| 135 |
]; |
| 136 |
|
| 137 |
return rest_ensure_response( $data ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Create category |
| 142 |
* |
| 143 |
* @param WP_Rest_Request $request |
| 144 |
* |
| 145 |
* @return void |
| 146 |
*/ |
| 147 |
public function create_item( $request ) { |
| 148 |
$data = json_decode( $request->get_body(), true ); |
| 149 |
|
| 150 |
$category_name = ! empty( $data['category_name'] ) ? sanitize_text_field( $data['category_name'] ) : ''; |
| 151 |
$parent = ! empty( $data['parent'] ) ? intval( $data['parent'] ) : 0; |
| 152 |
$meeting_ids = ! empty( $data['meeting_ids'] ) ? array_map( 'intval', $data['meeting_ids'] ) : []; |
| 153 |
|
| 154 |
$category = wp_insert_term( $category_name, $this->taxonomy, [ |
| 155 |
'parent' => $parent, |
| 156 |
] ); |
| 157 |
|
| 158 |
if ( is_wp_error( $category ) ) { |
| 159 |
return new WP_Error( $category->get_error_code(), $category->get_error_message() ); |
| 160 |
} |
| 161 |
|
| 162 |
$this->assign_meetings( $category['term_id'], $meeting_ids ); |
| 163 |
|
| 164 |
$category = get_term( $category['term_id'] ); |
| 165 |
|
| 166 |
$posts = $this->get_post_ids( $category->term_id ); |
| 167 |
$category->meeting_ids = $posts; |
| 168 |
|
| 169 |
$response = [ |
| 170 |
'success' => 1, |
| 171 |
'status_code' => 200, |
| 172 |
'data' => $category, |
| 173 |
]; |
| 174 |
|
| 175 |
return rest_ensure_response( $response ); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Update category |
| 180 |
* |
| 181 |
* @param WP_Rest_Request $request |
| 182 |
* |
| 183 |
* @return JSON |
| 184 |
*/ |
| 185 |
public function update( $request ) { |
| 186 |
$id = intval( $request['category_id'] ); |
| 187 |
$data = json_decode( $request->get_body(), true ); |
| 188 |
$category_name = ! empty( $data['category_name'] ) ? sanitize_text_field( $data['category_name'] ) : ''; |
| 189 |
$parent = ! empty( $data['parent'] ) ? intval( $data['parent'] ) : 0; |
| 190 |
$meeting_ids = ! empty( $data['meeting_ids'] ) ? array_map( 'intval', $data['meeting_ids'] ) : []; |
| 191 |
|
| 192 |
$args = [ |
| 193 |
'name' => $category_name, |
| 194 |
'parent' => $parent, |
| 195 |
]; |
| 196 |
|
| 197 |
$category = wp_update_term( $id, $this->taxonomy, $args ); |
| 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 |
|
| 210 |
$response = [ |
| 211 |
'success' => 1, |
| 212 |
'status_code' => 200, |
| 213 |
'data' => $category, |
| 214 |
]; |
| 215 |
|
| 216 |
return rest_ensure_response( $response ); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Delete category |
| 221 |
* |
| 222 |
* @param WP_Rest $request |
| 223 |
* |
| 224 |
* @return JSON |
| 225 |
*/ |
| 226 |
public function delete( $request ) { |
| 227 |
$id = intval( $request['category_id'] ); |
| 228 |
|
| 229 |
$category = wp_delete_term( $id, $this->taxonomy ); |
| 230 |
|
| 231 |
if ( ! $category ) { |
| 232 |
return new WP_Error( 'category_not_exist', __( 'Category doesn\'t exist', 'timetics' ) ); |
| 233 |
} |
| 234 |
|
| 235 |
$response = [ |
| 236 |
'success' => 1, |
| 237 |
'status_code' => 200, |
| 238 |
'message' => __( 'Successfully deleted, appointment category', 'timetics' ), |
| 239 |
]; |
| 240 |
|
| 241 |
return rest_ensure_response( $response ); |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Delete multiple category |
| 246 |
* |
| 247 |
* @param WP_Rest_Request $request |
| 248 |
* |
| 249 |
* @return JSON |
| 250 |
*/ |
| 251 |
public function bulk_delete( $request ) { |
| 252 |
$data = json_decode( $request->get_body(), true ); |
| 253 |
|
| 254 |
$ids = ! empty( $data['ids'] ) ? array_map( 'intval', $data['ids'] ) : []; |
| 255 |
$counter = 0; |
| 256 |
|
| 257 |
if ( ! $ids ) { |
| 258 |
return new WP_Error( 'cagetory_ids_empty', __( 'Category id required', 'timetics' ) ); |
| 259 |
} |
| 260 |
|
| 261 |
foreach ( $ids as $id ) { |
| 262 |
$delete = wp_delete_term( $id, $this->taxonomy ); |
| 263 |
|
| 264 |
if ( $delete ) { |
| 265 |
$counter++; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
if ( $counter < 1 ) { |
| 270 |
return new WP_Error( 'cagetory_not_exist', __( 'No category exists.', 'timetics' ) ); |
| 271 |
} |
| 272 |
|
| 273 |
$response = [ |
| 274 |
'success' => 1, |
| 275 |
'status_code' => 200, |
| 276 |
'message' => sprintf( __( 'Successfully deleted %d out of %d categories', 'timetics' ), $counter, count( $ids ) ), |
| 277 |
]; |
| 278 |
|
| 279 |
return rest_ensure_response( $response ); |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Assign post to the selected category |
| 284 |
* |
| 285 |
* @param integer $category_id Category id that need to be assign |
| 286 |
* @param array $meeting_ids Meeting ids that will be assign |
| 287 |
* |
| 288 |
* @return void |
| 289 |
*/ |
| 290 |
private function assign_meetings( $category_id, $meeting_ids ) { |
| 291 |
foreach ( $meeting_ids as $meeting_id ) { |
| 292 |
wp_set_post_terms( $meeting_id, [$category_id], $this->taxonomy ); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Get posts ids |
| 298 |
* |
| 299 |
* @return array |
| 300 |
*/ |
| 301 |
private function get_post_ids( $term_id ) { |
| 302 |
$args = [ |
| 303 |
'post_type' => 'timetics-appointment', |
| 304 |
'fields' => 'ids', |
| 305 |
'tax_query' => [ |
| 306 |
[ |
| 307 |
'taxonomy' => 'timetics-meeting-category', |
| 308 |
'terms' => $term_id, |
| 309 |
], |
| 310 |
], |
| 311 |
]; |
| 312 |
|
| 313 |
$posts = get_posts( $args ); |
| 314 |
|
| 315 |
return $posts; |
| 316 |
} |
| 317 |
} |
| 318 |
|