| @@ -84,8 +84,20 @@ | ||
| 84 | 84 | ); |
| 85 | 85 | |
| 86 | 86 | register_rest_route( |
| 87 | 87 | self::NS, |
| 88 | + '/plans/groups/(?P<id>\d+)/order', | |
| 89 | + array( | |
| 90 | + array( | |
| 91 | + 'methods' => WP_REST_Server::EDITABLE, | |
| 92 | + 'callback' => array( $this, 'update_group_order' ), | |
| 93 | + 'permission_callback' => $perm, | |
| 94 | + ), | |
| 95 | + ) | |
| 96 | + ); | |
| 97 | + | |
| 98 | + register_rest_route( | |
| 99 | + self::NS, | |
| 88 | 100 | '/plans/terms', |
| 89 | 101 | array( |
| 90 | 102 | array( |
| 91 | 103 | 'methods' => WP_REST_Server::CREATABLE, |
| @@ -373,8 +385,44 @@ | ||
| 373 | 385 | |
| 374 | 386 | PlanRepository::update_group( $id, $params ); |
| 375 | 387 | |
| 376 | 388 | return rest_ensure_response( PlanRepository::get_group_tree( $id ) ); |
| 389 | + } | |
| 390 | + | |
| 391 | + /** | |
| 392 | + * PUT /plans/groups/{id}/order - save the admin display order of the | |
| 393 | + * group's durations and/or products (drag-and-drop on the detail page). | |
| 394 | + * | |
| 395 | + * Stored in the group's `data` JSON as `term_order` / `product_order` | |
| 396 | + * (lists of ids), merged so the rest of `data` is left as it was. | |
| 397 | + * | |
| 398 | + * @param WP_REST_Request $request Request. | |
| 399 | + * | |
| 400 | + * @return \WP_REST_Response|WP_Error | |
| 401 | + */ | |
| 402 | + public function update_group_order( WP_REST_Request $request ) { | |
| 403 | + $id = (int) $request['id']; | |
| 404 | + $group = PlanRepository::get_group( $id ); | |
| 405 | + | |
| 406 | + if ( ! $group ) { | |
| 407 | + return $this->not_found(); | |
| 408 | + } | |
| 409 | + | |
| 410 | + $params = $this->read_params( $request ); | |
| 411 | + $data = is_array( $group['data'] ) ? $group['data'] : array(); | |
| 412 | + | |
| 413 | + foreach ( array( | |
| 414 | + 'terms' => 'term_order', | |
| 415 | + 'products' => 'product_order', | |
| 416 | + ) as $param => $key ) { | |
| 417 | + if ( isset( $params[ $param ] ) && is_array( $params[ $param ] ) ) { | |
| 418 | + $data[ $key ] = array_values( array_unique( array_filter( array_map( 'absint', $params[ $param ] ) ) ) ); | |
| 419 | + } | |
| 420 | + } | |
| 421 | + | |
| 422 | + PlanRepository::update_group( $id, array( 'data' => $data ) ); | |
| 423 | + | |
| 424 | + return rest_ensure_response( array( 'success' => true ) ); | |
| 377 | 425 | } |
| 378 | 426 | |
| 379 | 427 | /** |
| 380 | 428 | * DELETE /plans/groups/{id} - delete group + cascade. |