Endpoint.php
3 years ago
ListSubscriptions.php
3 years ago
SubscriptionActions.php
3 years ago
SwitchSubscriptionView.php
3 years ago
SubscriptionActions.php
165 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Subscriptions\Endpoints; |
| 4 | |
| 5 | use Exception; |
| 6 | use Give\Subscriptions\Models\Subscription; |
| 7 | use Give\Subscriptions\ValueObjects\SubscriptionStatus; |
| 8 | use WP_Error; |
| 9 | use WP_REST_Request; |
| 10 | use WP_REST_Response; |
| 11 | |
| 12 | /** |
| 13 | * @since 2.24.0 |
| 14 | * |
| 15 | */ |
| 16 | class SubscriptionActions extends Endpoint |
| 17 | { |
| 18 | /** |
| 19 | * @var string |
| 20 | */ |
| 21 | protected $endpoint = 'admin/subscriptions/(?P<action>[\S]+)'; |
| 22 | |
| 23 | /** |
| 24 | * @inheritDoc |
| 25 | */ |
| 26 | public function registerRoute() |
| 27 | { |
| 28 | register_rest_route( |
| 29 | 'give-api/v2', |
| 30 | $this->endpoint, |
| 31 | [ |
| 32 | [ |
| 33 | 'methods' => ['POST', 'DELETE'], |
| 34 | 'callback' => [$this, 'handleRequest'], |
| 35 | 'permission_callback' => [$this, 'permissionsCheck'], |
| 36 | ], |
| 37 | 'args' => [ |
| 38 | 'action' => [ |
| 39 | 'type' => 'string', |
| 40 | 'required' => true, |
| 41 | 'enum' => [ |
| 42 | 'delete', |
| 43 | 'setStatus', |
| 44 | ], |
| 45 | ], |
| 46 | 'ids' => [ |
| 47 | 'type' => 'string', |
| 48 | 'required' => true, |
| 49 | 'validate_callback' => function ($ids) { |
| 50 | foreach ($this->splitString($ids) as $id) { |
| 51 | if ( ! $this->validateInt($id)) { |
| 52 | return false; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return true; |
| 57 | }, |
| 58 | ], |
| 59 | 'status' => [ |
| 60 | 'type' => 'string', |
| 61 | 'required' => false, |
| 62 | 'enum' => array_values(SubscriptionStatus::toArray()), |
| 63 | ], |
| 64 | ], |
| 65 | ] |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @since 2.25.2 |
| 71 | * |
| 72 | * @inheritDoc |
| 73 | */ |
| 74 | public function permissionsCheck() |
| 75 | { |
| 76 | if ( ! current_user_can('edit_give_payments')) { |
| 77 | return new WP_Error( |
| 78 | 'rest_forbidden', |
| 79 | esc_html__('You don\'t have permission to edit Subscriptions', 'give'), |
| 80 | ['status' => $this->authorizationStatusCode()] |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @since 2.24.0 |
| 89 | * |
| 90 | * @param WP_REST_Request $request |
| 91 | * |
| 92 | * @return WP_REST_Response |
| 93 | */ |
| 94 | public function handleRequest(WP_REST_Request $request) |
| 95 | { |
| 96 | $ids = $this->splitString($request->get_param('ids')); |
| 97 | $errors = $successes = []; |
| 98 | |
| 99 | switch ($request->get_param('action')) { |
| 100 | case 'delete': |
| 101 | foreach ($ids as $id) { |
| 102 | $subscription = Subscription::find($id); |
| 103 | |
| 104 | if ( ! $subscription) { |
| 105 | $errors[] = $id; |
| 106 | continue; |
| 107 | } |
| 108 | |
| 109 | try { |
| 110 | $subscription->delete(); |
| 111 | $successes[] = $id; |
| 112 | } catch (Exception $e) { |
| 113 | $errors[] = $id; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | break; |
| 118 | |
| 119 | case 'setStatus': |
| 120 | foreach ($ids as $id) { |
| 121 | $subscription = Subscription::find($id); |
| 122 | |
| 123 | if ( ! $subscription) { |
| 124 | $errors[] = $id; |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | try { |
| 129 | $subscription->status = new SubscriptionStatus($request->get_param('status')); |
| 130 | $subscription->save(); |
| 131 | $successes[] = $id; |
| 132 | } catch (Exception $e) { |
| 133 | $errors[] = $id; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | break; |
| 138 | } |
| 139 | |
| 140 | return new WP_REST_Response([ |
| 141 | 'errors' => $errors, |
| 142 | 'successes' => $successes |
| 143 | ]); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /** |
| 148 | * Split string |
| 149 | * |
| 150 | * @since 2.24.0 |
| 151 | * |
| 152 | * @param string $ids |
| 153 | * |
| 154 | * @return string[] |
| 155 | */ |
| 156 | protected function splitString($ids) |
| 157 | { |
| 158 | if (strpos($ids, ',')) { |
| 159 | return array_map('trim', explode(',', $ids)); |
| 160 | } |
| 161 | |
| 162 | return [trim($ids)]; |
| 163 | } |
| 164 | } |
| 165 |