| 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 |
'trash', |
| 44 |
'untrash', |
| 45 |
'setStatus', |
| 46 |
], |
| 47 |
], |
| 48 |
'ids' => [ |
| 49 |
'type' => 'string', |
| 50 |
'required' => true, |
| 51 |
'validate_callback' => function ($ids) { |
| 52 |
foreach ($this->splitString($ids) as $id) { |
| 53 |
if (! $this->validateInt($id)) { |
| 54 |
return false; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return true; |
| 59 |
}, |
| 60 |
], |
| 61 |
'status' => [ |
| 62 |
'type' => 'string', |
| 63 |
'required' => false, |
| 64 |
'enum' => array_values(SubscriptionStatus::toArray()), |
| 65 |
], |
| 66 |
], |
| 67 |
] |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @since 2.25.2 |
| 73 |
* |
| 74 |
* @inheritDoc |
| 75 |
*/ |
| 76 |
public function permissionsCheck() |
| 77 |
{ |
| 78 |
if (! current_user_can('edit_give_payments')) { |
| 79 |
return new WP_Error( |
| 80 |
'rest_forbidden', |
| 81 |
__('You don\'t have permission to edit Subscriptions', 'give'), |
| 82 |
['status' => $this->authorizationStatusCode()] |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* @since 4.3.1 add permissions check for delete |
| 91 |
* @since 2.24.0 |
| 92 |
* |
| 93 |
* @param WP_REST_Request $request |
| 94 |
* |
| 95 |
* @return WP_Error |
| 96 |
*/ |
| 97 |
public function handleRequest(WP_REST_Request $request) |
| 98 |
{ |
| 99 |
$ids = $this->splitString($request->get_param('ids')); |
| 100 |
$errors = $successes = []; |
| 101 |
|
| 102 |
switch ($request->get_param('action')) { |
| 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 |
|
| 112 |
foreach ($ids as $id) { |
| 113 |
$subscription = Subscription::find($id); |
| 114 |
|
| 115 |
if (! $subscription) { |
| 116 |
$errors[] = $id; |
| 117 |
continue; |
| 118 |
} |
| 119 |
|
| 120 |
try { |
| 121 |
$subscription->delete(); |
| 122 |
$successes[] = $id; |
| 123 |
} catch (Exception $e) { |
| 124 |
$errors[] = $id; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
break; |
| 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 |
|
| 168 |
case 'setStatus': |
| 169 |
foreach ($ids as $id) { |
| 170 |
$subscription = Subscription::find($id); |
| 171 |
|
| 172 |
if (! $subscription) { |
| 173 |
$errors[] = $id; |
| 174 |
continue; |
| 175 |
} |
| 176 |
|
| 177 |
try { |
| 178 |
$subscription->status = new SubscriptionStatus($request->get_param('status')); |
| 179 |
$subscription->save(); |
| 180 |
$successes[] = $id; |
| 181 |
} catch (Exception $e) { |
| 182 |
$errors[] = $id; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
break; |
| 187 |
} |
| 188 |
|
| 189 |
return new WP_REST_Response([ |
| 190 |
'errors' => $errors, |
| 191 |
'successes' => $successes |
| 192 |
]); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
/** |
| 197 |
* Split string |
| 198 |
* |
| 199 |
* @since 2.24.0 |
| 200 |
* |
| 201 |
* @param string $ids |
| 202 |
* |
| 203 |
* @return string[] |
| 204 |
*/ |
| 205 |
protected function splitString($ids) |
| 206 |
{ |
| 207 |
if (strpos($ids, ',')) { |
| 208 |
return array_map('trim', explode(',', $ids)); |
| 209 |
} |
| 210 |
|
| 211 |
return [trim($ids)]; |
| 212 |
} |
| 213 |
} |
| 214 |
|