DonationActions.php
4 years ago
Endpoint.php
4 years ago
ListDonations.php
4 years ago
SwitchDonationView.php
4 years ago
DonationActions.php
144 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Donations\Endpoints; |
| 4 | |
| 5 | use Exception; |
| 6 | use Give\Framework\Database\DB; |
| 7 | use WP_REST_Request; |
| 8 | use WP_REST_Response; |
| 9 | |
| 10 | class DonationActions extends Endpoint |
| 11 | { |
| 12 | /** |
| 13 | * @var string |
| 14 | */ |
| 15 | protected $endpoint = 'admin/donations/(?P<action>[\S]+)'; |
| 16 | |
| 17 | /** |
| 18 | * @inheritDoc |
| 19 | */ |
| 20 | public function registerRoute() |
| 21 | { |
| 22 | register_rest_route( |
| 23 | 'give-api/v2', |
| 24 | $this->endpoint, |
| 25 | [ |
| 26 | [ |
| 27 | 'methods' => ['POST', 'DELETE'], |
| 28 | 'callback' => [$this, 'handleRequest'], |
| 29 | 'permission_callback' => [$this, 'permissionsCheck'], |
| 30 | ], |
| 31 | 'args' => [ |
| 32 | 'action' => [ |
| 33 | 'type' => 'string', |
| 34 | 'required' => true, |
| 35 | 'enum' => [ |
| 36 | 'delete', |
| 37 | 'setStatus', |
| 38 | 'resendEmailReceipt', |
| 39 | ], |
| 40 | ], |
| 41 | 'ids' => [ |
| 42 | 'type' => 'string', |
| 43 | 'required' => true, |
| 44 | 'validate_callback' => function ($ids) { |
| 45 | foreach ($this->splitString($ids) as $id) { |
| 46 | if (!$this->validateInt($id)) { |
| 47 | return false; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | return true; |
| 52 | }, |
| 53 | ], |
| 54 | 'status' => [ |
| 55 | 'type' => 'string', |
| 56 | 'required' => false, |
| 57 | 'enum' => [ |
| 58 | 'publish', // Completed |
| 59 | 'pending', |
| 60 | 'processing', |
| 61 | 'refunded', |
| 62 | 'revoked', |
| 63 | 'failed', |
| 64 | 'cancelled', |
| 65 | 'abandoned', |
| 66 | 'preapproval' |
| 67 | ], |
| 68 | ], |
| 69 | ], |
| 70 | ] |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @param WP_REST_Request $request |
| 76 | * |
| 77 | * @since 2.20.0 |
| 78 | * |
| 79 | * @return WP_REST_Response |
| 80 | */ |
| 81 | public function handleRequest(WP_REST_Request $request) |
| 82 | { |
| 83 | $ids = $this->splitString($request->get_param('ids')); |
| 84 | $errors = $successes = []; |
| 85 | |
| 86 | switch ($request->get_param('action')) { |
| 87 | case 'delete': |
| 88 | foreach ($ids as $id) { |
| 89 | try { |
| 90 | give_delete_donation($id); |
| 91 | $successes[] = $id; |
| 92 | } catch (Exception $e) { |
| 93 | $errors[] = $id; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | break; |
| 98 | |
| 99 | case 'setStatus': |
| 100 | foreach ($ids as $id) { |
| 101 | $updated = give_update_payment_status($id, $request->get_param('status')); |
| 102 | $updated ? $successes[] = $id : $errors[] = $id; |
| 103 | } |
| 104 | |
| 105 | break; |
| 106 | |
| 107 | case 'resendEmailReceipt': |
| 108 | foreach ($ids as $id) { |
| 109 | try { |
| 110 | do_action('give_donation-receipt_email_notification', $id); |
| 111 | $successes[] = $id; |
| 112 | } catch (Exception $e) { |
| 113 | $errors[] = $id; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | return new WP_REST_Response([ |
| 121 | 'errors' => $errors, |
| 122 | 'successes' => $successes |
| 123 | ]); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /** |
| 128 | * Split string |
| 129 | * |
| 130 | * @param string $ids |
| 131 | * @since 2.20.0 |
| 132 | * |
| 133 | * @return string[] |
| 134 | */ |
| 135 | protected function splitString($ids) |
| 136 | { |
| 137 | if (strpos($ids, ',')) { |
| 138 | return array_map('trim', explode(',', $ids)); |
| 139 | } |
| 140 | |
| 141 | return [trim($ids)]; |
| 142 | } |
| 143 | } |
| 144 |