| @@ -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, |
| @@ -183,8 +195,20 @@ | ||
| 183 | 195 | ); |
| 184 | 196 | |
| 185 | 197 | register_rest_route( |
| 186 | 198 | self::NS, |
| 199 | + '/plans/product-user-cancel/(?P<id>\d+)', | |
| 200 | + array( | |
| 201 | + array( | |
| 202 | + 'methods' => WP_REST_Server::EDITABLE, | |
| 203 | + 'callback' => array( $this, 'save_product_user_cancel' ), | |
| 204 | + 'permission_callback' => $perm, | |
| 205 | + ), | |
| 206 | + ) | |
| 207 | + ); | |
| 208 | + | |
| 209 | + register_rest_route( | |
| 210 | + self::NS, | |
| 187 | 211 | '/plans/group-products/(?P<id>\d+)', |
| 188 | 212 | array( |
| 189 | 213 | array( |
| 190 | 214 | 'methods' => WP_REST_Server::READABLE, |
| @@ -376,8 +400,44 @@ | ||
| 376 | 400 | return rest_ensure_response( PlanRepository::get_group_tree( $id ) ); |
| 377 | 401 | } |
| 378 | 402 | |
| 379 | 403 | /** |
| 404 | + * PUT /plans/groups/{id}/order - save the admin display order of the | |
| 405 | + * group's durations and/or products (drag-and-drop on the detail page). | |
| 406 | + * | |
| 407 | + * Stored in the group's `data` JSON as `term_order` / `product_order` | |
| 408 | + * (lists of ids), merged so the rest of `data` is left as it was. | |
| 409 | + * | |
| 410 | + * @param WP_REST_Request $request Request. | |
| 411 | + * | |
| 412 | + * @return \WP_REST_Response|WP_Error | |
| 413 | + */ | |
| 414 | + public function update_group_order( WP_REST_Request $request ) { | |
| 415 | + $id = (int) $request['id']; | |
| 416 | + $group = PlanRepository::get_group( $id ); | |
| 417 | + | |
| 418 | + if ( ! $group ) { | |
| 419 | + return $this->not_found(); | |
| 420 | + } | |
| 421 | + | |
| 422 | + $params = $this->read_params( $request ); | |
| 423 | + $data = is_array( $group['data'] ) ? $group['data'] : array(); | |
| 424 | + | |
| 425 | + foreach ( array( | |
| 426 | + 'terms' => 'term_order', | |
| 427 | + 'products' => 'product_order', | |
| 428 | + ) as $param => $key ) { | |
| 429 | + if ( isset( $params[ $param ] ) && is_array( $params[ $param ] ) ) { | |
| 430 | + $data[ $key ] = array_values( array_unique( array_filter( array_map( 'absint', $params[ $param ] ) ) ) ); | |
| 431 | + } | |
| 432 | + } | |
| 433 | + | |
| 434 | + PlanRepository::update_group( $id, array( 'data' => $data ) ); | |
| 435 | + | |
| 436 | + return rest_ensure_response( array( 'success' => true ) ); | |
| 437 | + } | |
| 438 | + | |
| 439 | + /** | |
| 380 | 440 | * DELETE /plans/groups/{id} - delete group + cascade. |
| 381 | 441 | * |
| 382 | 442 | * @param WP_REST_Request $request Request. |
| 383 | 443 | * |
| @@ -595,8 +655,34 @@ | ||
| 595 | 655 | 'deleted' => true, |
| 596 | 656 | 'id' => $id, |
| 597 | 657 | ) |
| 598 | 658 | ); |
| 659 | + } | |
| 660 | + | |
| 661 | + /** | |
| 662 | + * PUT /plans/product-user-cancel/{id} - save whether customers may cancel. | |
| 663 | + * | |
| 664 | + * Writes the same `_subscrpt_user_cancel` meta the product editor saves, as | |
| 665 | + * an explicit "yes" / "no", which checkout copies onto each new subscription. | |
| 666 | + * | |
| 667 | + * Body: { enabled: bool }. | |
| 668 | + * | |
| 669 | + * @param WP_REST_Request $request Request. | |
| 670 | + * | |
| 671 | + * @return \WP_REST_Response|WP_Error | |
| 672 | + */ | |
| 673 | + public function save_product_user_cancel( WP_REST_Request $request ) { | |
| 674 | + $product = function_exists( 'wc_get_product' ) ? wc_get_product( (int) $request['id'] ) : null; | |
| 675 | + | |
| 676 | + if ( ! $product ) { | |
| 677 | + return $this->not_found(); | |
| 678 | + } | |
| 679 | + | |
| 680 | + $params = $this->read_params( $request ); | |
| 681 | + $product->update_meta_data( '_subscrpt_user_cancel', empty( $params['enabled'] ) ? 'no' : 'yes' ); | |
| 682 | + $product->save(); | |
| 683 | + | |
| 684 | + return rest_ensure_response( array( 'saved' => true ) ); | |
| 599 | 685 | } |
| 600 | 686 | |
| 601 | 687 | /** |
| 602 | 688 | * POST /plans/bulk-price - set one price on many product/duration relations. |