| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Promotions\FreeAddonModal\Controllers; |
| 4 |
|
| 5 |
use WP_REST_Request; |
| 6 |
use WP_REST_Response; |
| 7 |
use WP_REST_Server; |
| 8 |
|
| 9 |
class CompleteRestApiEndpoint |
| 10 |
{ |
| 11 |
public function __invoke() |
| 12 |
{ |
| 13 |
register_rest_route('give/v1', '/promotions/free-addon-modal/complete', [ |
| 14 |
'methods' => WP_REST_Server::EDITABLE, |
| 15 |
'callback' => [$this, 'handleModalCompletion'], |
| 16 |
'permission_callback' => function () { |
| 17 |
return current_user_can('manage_options'); |
| 18 |
}, |
| 19 |
'args' => [ |
| 20 |
'reason' => [ |
| 21 |
'required' => true, |
| 22 |
'type' => 'string', |
| 23 |
'enum' => ['subscribed', 'rejected'], |
| 24 |
], |
| 25 |
], |
| 26 |
|
| 27 |
]); |
| 28 |
} |
| 29 |
|
| 30 |
public function handleModalCompletion(WP_REST_Request $request) |
| 31 |
{ |
| 32 |
$reason = $request['reason']; |
| 33 |
$iteration = 1; |
| 34 |
|
| 35 |
if ( 'rejected' === $reason ) { |
| 36 |
// If the user has rejected the modal before, increase the iteration. |
| 37 |
$status = get_option('give_free_addon_modal_displayed'); |
| 38 |
|
| 39 |
if ( !empty($status) ) { |
| 40 |
// The value will be something like rejected:1:1.18.0. The first number is the number of versions the modal has appeared |
| 41 |
// in, and the second number is the version number of the plugin at the time of last display. |
| 42 |
list($status, $iteration, $version) = explode(':', $status); |
| 43 |
|
| 44 |
$iteration++; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
update_option('give_free_addon_modal_displayed', implode(':', [ |
| 49 |
$request['reason'], |
| 50 |
$iteration, |
| 51 |
GIVE_VERSION, |
| 52 |
])); |
| 53 |
|
| 54 |
return new WP_REST_Response(['success' => true]); |
| 55 |
} |
| 56 |
} |
| 57 |
|