← All changes
|
jetpack_vendor/automattic/jetpack-search/src/class-rest-controller.php
+60
-0
16.2
→
16.3-beta
View file →
| @@ -182,8 +182,24 @@ | ||
| 182 | 182 | ) |
| 183 | 183 | ); |
| 184 | 184 | register_rest_route( |
| 185 | 185 | static::$namespace, |
| 186 | + '/search/plan/activate-free', | |
| 187 | + array( | |
| 188 | + 'methods' => WP_REST_Server::EDITABLE, | |
| 189 | + 'callback' => array( $this, 'activate_free_plan' ), | |
| 190 | + 'permission_callback' => array( $this, 'require_admin_privilege_callback' ), | |
| 191 | + 'args' => array( | |
| 192 | + 'source' => array( | |
| 193 | + 'type' => 'string', | |
| 194 | + 'required' => false, | |
| 195 | + 'sanitize_callback' => 'sanitize_key', | |
| 196 | + ), | |
| 197 | + ), | |
| 198 | + ) | |
| 199 | + ); | |
| 200 | + register_rest_route( | |
| 201 | + static::$namespace, | |
| 186 | 202 | '/search/plan/deactivate', |
| 187 | 203 | array( |
| 188 | 204 | 'methods' => WP_REST_Server::EDITABLE, |
| 189 | 205 | 'callback' => array( $this, 'deactivate_plan' ), |
| @@ -424,8 +440,16 @@ | ||
| 424 | 440 | } |
| 425 | 441 | return true; |
| 426 | 442 | } |
| 427 | 443 | |
| 444 | + if ( true === $reader_chat && ( ! $this->plan->supports_search() || $this->plan->is_free_plan() ) ) { | |
| 445 | + return new WP_Error( | |
| 446 | + 'rest_forbidden', | |
| 447 | + esc_html__( 'Site Chat requires a paid Jetpack Search plan.', 'jetpack-search-pkg' ), | |
| 448 | + array( 'status' => 403 ) | |
| 449 | + ); | |
| 450 | + } | |
| 451 | + | |
| 428 | 452 | // AI Answers requires a paid Search plan; reject the write outright. |
| 429 | 453 | if ( true === $ai_answers_enabled && ! Search_Blocks::supports_paid_search() ) { |
| 430 | 454 | return new WP_Error( |
| 431 | 455 | 'rest_forbidden', |
| @@ -604,8 +628,44 @@ | ||
| 604 | 628 | array( |
| 605 | 629 | 'code' => 'success', |
| 606 | 630 | ) |
| 607 | 631 | ); |
| 632 | + } | |
| 633 | + | |
| 634 | + /** | |
| 635 | + * Grant the free Search product to this site instead of sending the user to a $0 checkout. | |
| 636 | + * | |
| 637 | + * POST `jetpack/v4/search/plan/activate-free` | |
| 638 | + * | |
| 639 | + * @since 8.3.0 | |
| 640 | + * | |
| 641 | + * @param WP_REST_Request $request - REST request. | |
| 642 | + * @return WP_REST_Response|WP_Error Errors carry `checkout_fallback`, which the dashboard | |
| 643 | + * branches on to decide whether to fall back to checkout. | |
| 644 | + */ | |
| 645 | + public function activate_free_plan( $request ) { | |
| 646 | + /* | |
| 647 | + * Another plugin can load an older My Jetpack before this package's copy registers, and | |
| 648 | + * that copy has no such method. Degrade to the checkout the dashboard still has. | |
| 649 | + */ | |
| 650 | + if ( ! method_exists( Search_Product::class, 'activate_free_product' ) ) { | |
| 651 | + return new WP_Error( | |
| 652 | + 'jetpack_search_free_activation_unavailable', | |
| 653 | + __( 'Jetpack Search Free could not be activated for this site.', 'jetpack-search-pkg' ), | |
| 654 | + array( | |
| 655 | + 'status' => 501, | |
| 656 | + 'checkout_fallback' => true, | |
| 657 | + ) | |
| 658 | + ); | |
| 659 | + } | |
| 660 | + | |
| 661 | + $source = $request->get_param( 'source' ); | |
| 662 | + $result = Search_Product::activate_free_product( $source ? $source : 'search-dashboard' ); | |
| 663 | + if ( is_wp_error( $result ) ) { | |
| 664 | + return $result; | |
| 665 | + } | |
| 666 | + | |
| 667 | + return rest_ensure_response( $result ); | |
| 608 | 668 | } |
| 609 | 669 | |
| 610 | 670 | /** |
| 611 | 671 | * Deactivate plan: turn off search module and instant search. |