ProductRecommendationsRoute.php
100 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Promotions\InPluginUpsells\Endpoints; |
| 4 | |
| 5 | use Give\API\RestRoute; |
| 6 | use WP_Error; |
| 7 | use WP_REST_Request; |
| 8 | use WP_REST_Response; |
| 9 | |
| 10 | |
| 11 | class ProductRecommendationsRoute implements RestRoute |
| 12 | { |
| 13 | /** |
| 14 | * @var string |
| 15 | */ |
| 16 | protected $endpoint = 'admin/recommended-options'; |
| 17 | |
| 18 | /** |
| 19 | * @inheritDoc |
| 20 | * |
| 21 | * @since 2.27.1 |
| 22 | */ |
| 23 | public function registerRoute() |
| 24 | { |
| 25 | register_rest_route( |
| 26 | 'give-api/v2', |
| 27 | $this->endpoint, |
| 28 | [ |
| 29 | [ |
| 30 | 'methods' => ['POST'], |
| 31 | 'callback' => [$this, 'handleRequest'], |
| 32 | 'permission_callback' => [$this, 'permissionsCheck'], |
| 33 | 'args' => [ |
| 34 | 'option' => [ |
| 35 | 'type' => 'string', |
| 36 | 'required' => true, |
| 37 | 'enum' => [ |
| 38 | 'givewp_donations_recurring_recommendation_dismissed', |
| 39 | 'givewp_donations_fee_recovery_recommendation_dismissed', |
| 40 | 'givewp_donations_designated_funds_recommendation_dismissed', |
| 41 | 'givewp_reports_recurring_recommendation_dismissed', |
| 42 | 'givewp_reports_fee_recovery_recommendation_dismissed', |
| 43 | 'givewp_donors_fee_recovery_recommendation_dismissed', |
| 44 | 'givewp_form_editor_donation_options_recurring_recommendation', |
| 45 | 'givewp_payment_gateway_fee_recovery_recommendation', |
| 46 | ], |
| 47 | ], |
| 48 | ], |
| 49 | ], |
| 50 | ] |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @since 2.27.1 |
| 56 | */ |
| 57 | public function permissionsCheck() |
| 58 | { |
| 59 | if ( ! current_user_can('manage_options')) { |
| 60 | return new WP_Error( |
| 61 | 'rest_forbidden', |
| 62 | esc_html__('You don\'t have permission to dismiss options. Only users with the "manage_options" capability can perform this action.', |
| 63 | 'give'), |
| 64 | ['status' => $this->authorizationStatusCode()] |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Sets up the proper HTTP status code for authorization. |
| 73 | * @since 2.27.1 |
| 74 | * |
| 75 | * @return int |
| 76 | */ |
| 77 | public function authorizationStatusCode() |
| 78 | { |
| 79 | if (is_user_logged_in()) { |
| 80 | return 403; |
| 81 | } |
| 82 | |
| 83 | return 401; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @since 2.27.1 |
| 88 | * |
| 89 | * @param WP_REST_Request $request |
| 90 | * |
| 91 | * @return WP_REST_Response |
| 92 | */ |
| 93 | public function handleRequest(WP_REST_Request $request) |
| 94 | { |
| 95 | update_option($request->get_param('option'), time()); |
| 96 | |
| 97 | return new WP_REST_Response(['option_updated' => $request->get_param('option')]); |
| 98 | } |
| 99 | } |
| 100 |