← All changes
|
src/Subscriptions/Endpoints/SubscriptionActions.php
+55
-6
2.30.0
→
4.16.9
View file →
| @@ -39,8 +39,10 @@ | ||
| 39 | 39 | 'type' => 'string', |
| 40 | 40 | 'required' => true, |
| 41 | 41 | 'enum' => [ |
| 42 | 42 | 'delete', |
| 43 | + 'trash', | |
| 44 | + 'untrash', | |
| 43 | 45 | 'setStatus', |
| 44 | 46 | ], |
| 45 | 47 | ], |
| 46 | 48 | 'ids' => [ |
| @@ -47,9 +49,9 @@ | ||
| 47 | 49 | 'type' => 'string', |
| 48 | 50 | 'required' => true, |
| 49 | 51 | 'validate_callback' => function ($ids) { |
| 50 | 52 | foreach ($this->splitString($ids) as $id) { |
| 51 | - if ( ! $this->validateInt($id)) { | |
| 53 | + if (! $this->validateInt($id)) { | |
| 52 | 54 | return false; |
| 53 | 55 | } |
| 54 | 56 | } |
| 55 | 57 | |
| @@ -72,12 +74,12 @@ | ||
| 72 | 74 | * @inheritDoc |
| 73 | 75 | */ |
| 74 | 76 | public function permissionsCheck() |
| 75 | 77 | { |
| 76 | - if ( ! current_user_can('edit_give_payments')) { | |
| 78 | + if (! current_user_can('edit_give_payments')) { | |
| 77 | 79 | return new WP_Error( |
| 78 | 80 | 'rest_forbidden', |
| 79 | - esc_html__('You don\'t have permission to edit Subscriptions', 'give'), | |
| 81 | + __('You don\'t have permission to edit Subscriptions', 'give'), | |
| 80 | 82 | ['status' => $this->authorizationStatusCode()] |
| 81 | 83 | ); |
| 82 | 84 | } |
| 83 | 85 | |
| @@ -84,13 +86,14 @@ | ||
| 84 | 86 | return true; |
| 85 | 87 | } |
| 86 | 88 | |
| 87 | 89 | /** |
| 90 | + * @since 4.3.1 add permissions check for delete | |
| 88 | 91 | * @since 2.24.0 |
| 89 | 92 | * |
| 90 | 93 | * @param WP_REST_Request $request |
| 91 | 94 | * |
| 92 | - * @return WP_REST_Response | |
| 95 | + * @return WP_Error | |
| 93 | 96 | */ |
| 94 | 97 | public function handleRequest(WP_REST_Request $request) |
| 95 | 98 | { |
| 96 | 99 | $ids = $this->splitString($request->get_param('ids')); |
| @@ -97,12 +100,20 @@ | ||
| 97 | 100 | $errors = $successes = []; |
| 98 | 101 | |
| 99 | 102 | switch ($request->get_param('action')) { |
| 100 | 103 | case 'delete': |
| 104 | + if (! current_user_can('delete_give_payments')) { | |
| 105 | + return new WP_Error( | |
| 106 | + 'rest_forbidden', | |
| 107 | + __('You don\'t have permission to delete Subscription', 'give'), | |
| 108 | + ['status' => $this->authorizationStatusCode()] | |
| 109 | + ); | |
| 110 | + } | |
| 111 | + | |
| 101 | 112 | foreach ($ids as $id) { |
| 102 | 113 | $subscription = Subscription::find($id); |
| 103 | 114 | |
| 104 | - if ( ! $subscription) { | |
| 115 | + if (! $subscription) { | |
| 105 | 116 | $errors[] = $id; |
| 106 | 117 | continue; |
| 107 | 118 | } |
| 108 | 119 | |
| @@ -115,13 +126,51 @@ | ||
| 115 | 126 | } |
| 116 | 127 | |
| 117 | 128 | break; |
| 118 | 129 | |
| 130 | + case 'trash': | |
| 131 | + foreach ($ids as $id) { | |
| 132 | + $subscription = Subscription::find($id); | |
| 133 | + | |
| 134 | + if (! $subscription) { | |
| 135 | + $errors[] = $id; | |
| 136 | + continue; | |
| 137 | + } | |
| 138 | + | |
| 139 | + try { | |
| 140 | + $subscription->trash(); | |
| 141 | + $successes[] = $id; | |
| 142 | + } catch (Exception $e) { | |
| 143 | + $errors[] = $id; | |
| 144 | + } | |
| 145 | + } | |
| 146 | + | |
| 147 | + break; | |
| 148 | + | |
| 149 | + case 'untrash': | |
| 150 | + foreach ($ids as $id) { | |
| 151 | + $subscription = Subscription::find($id); | |
| 152 | + | |
| 153 | + if (! $subscription) { | |
| 154 | + $errors[] = $id; | |
| 155 | + continue; | |
| 156 | + } | |
| 157 | + | |
| 158 | + try { | |
| 159 | + $subscription->unTrash(); | |
| 160 | + $successes[] = $id; | |
| 161 | + } catch (Exception $e) { | |
| 162 | + $errors[] = $id; | |
| 163 | + } | |
| 164 | + } | |
| 165 | + | |
| 166 | + break; | |
| 167 | + | |
| 119 | 168 | case 'setStatus': |
| 120 | 169 | foreach ($ids as $id) { |
| 121 | 170 | $subscription = Subscription::find($id); |
| 122 | 171 | |
| 123 | - if ( ! $subscription) { | |
| 172 | + if (! $subscription) { | |
| 124 | 173 | $errors[] = $id; |
| 125 | 174 | continue; |
| 126 | 175 | } |
| 127 | 176 | |