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