| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation — deactivation feedback REST route. |
| 4 |
* |
| 5 |
* `POST /desktop-mode/v1/feedback/deactivation` builds the payload, |
| 6 |
* forwards it and answers `{ sent: bool }`. It never errors on a |
| 7 |
* failed forward: the browser deactivates on any answer, and a red |
| 8 |
* response would only delay someone who is already leaving. |
| 9 |
* |
| 10 |
* @package OpenStation |
| 11 |
*/ |
| 12 |
|
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
/** |
| 16 |
* Register the route. |
| 17 |
*/ |
| 18 |
function openstation_register_deactivation_feedback_route() { |
| 19 |
register_rest_route( |
| 20 |
'desktop-mode/v1', |
| 21 |
'/feedback/deactivation', |
| 22 |
array( |
| 23 |
'methods' => WP_REST_Server::CREATABLE, |
| 24 |
'callback' => 'openstation_rest_deactivation_feedback', |
| 25 |
'permission_callback' => 'openstation_rest_deactivation_feedback_permission', |
| 26 |
'args' => array( |
| 27 |
'reasons' => array( |
| 28 |
'required' => true, |
| 29 |
'type' => 'array', |
| 30 |
'minItems' => 1, |
| 31 |
'items' => array( |
| 32 |
'type' => 'string', |
| 33 |
'enum' => OPENSTATION_FEEDBACK_REASONS, |
| 34 |
), |
| 35 |
), |
| 36 |
'details' => array( |
| 37 |
'type' => 'string', |
| 38 |
'default' => '', |
| 39 |
), |
| 40 |
'context' => array( |
| 41 |
'type' => 'string', |
| 42 |
'enum' => OPENSTATION_FEEDBACK_CONTEXTS, |
| 43 |
'default' => 'classic', |
| 44 |
), |
| 45 |
), |
| 46 |
) |
| 47 |
); |
| 48 |
} |
| 49 |
add_action( 'rest_api_init', 'openstation_register_deactivation_feedback_route' ); |
| 50 |
|
| 51 |
/** |
| 52 |
* Permission gate: `activate_plugins` plus the feature flag. |
| 53 |
* |
| 54 |
* Deliberately NOT {@see openstation_rest_require_enabled()}: the |
| 55 |
* person deactivating usually does not have OpenStation on, and that |
| 56 |
* is the whole point of asking. There is no object-level check |
| 57 |
* because the route stores nothing on the site. |
| 58 |
* |
| 59 |
* @return true|WP_Error |
| 60 |
*/ |
| 61 |
function openstation_rest_deactivation_feedback_permission() { |
| 62 |
if ( ! is_user_logged_in() ) { |
| 63 |
return new WP_Error( |
| 64 |
'rest_forbidden', |
| 65 |
__( 'Authentication required.', 'desktop-mode' ), |
| 66 |
array( 'status' => 401 ) |
| 67 |
); |
| 68 |
} |
| 69 |
if ( ! current_user_can( 'activate_plugins' ) || ! openstation_deactivation_feedback_enabled() ) { |
| 70 |
return new WP_Error( |
| 71 |
'rest_forbidden', |
| 72 |
__( 'You are not allowed to do that.', 'desktop-mode' ), |
| 73 |
array( 'status' => 403 ) |
| 74 |
); |
| 75 |
} |
| 76 |
return true; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Handler: build, filter, forward. |
| 81 |
* |
| 82 |
* @param WP_REST_Request $request REST request. |
| 83 |
* @return WP_REST_Response |
| 84 |
*/ |
| 85 |
function openstation_rest_deactivation_feedback( WP_REST_Request $request ) { |
| 86 |
$payload = openstation_deactivation_feedback_payload( |
| 87 |
(array) $request->get_param( 'reasons' ), |
| 88 |
(string) $request->get_param( 'details' ), |
| 89 |
(string) $request->get_param( 'context' ) |
| 90 |
); |
| 91 |
|
| 92 |
/** |
| 93 |
* Filters the payload before it is forwarded. Return an empty |
| 94 |
* array to suppress the send entirely. |
| 95 |
* |
| 96 |
* @param array $payload The anonymous submission. |
| 97 |
*/ |
| 98 |
$payload = (array) apply_filters( 'openstation_deactivation_feedback_payload', $payload ); |
| 99 |
|
| 100 |
$sent = ! empty( $payload ) && openstation_deactivation_feedback_forward( $payload ); |
| 101 |
|
| 102 |
return rest_ensure_response( array( 'sent' => (bool) $sent ) ); |
| 103 |
} |
| 104 |
|