DeleteDonor.php
4 years ago
Endpoint.php
4 years ago
ListDonors.php
4 years ago
SwitchDonorView.php
4 years ago
DeleteDonor.php
120 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Donors\Endpoints; |
| 4 | |
| 5 | use Exception; |
| 6 | use Give\Donors\Models\Donor; |
| 7 | use WP_REST_Request; |
| 8 | use WP_REST_Response; |
| 9 | |
| 10 | class DeleteDonor extends Endpoint |
| 11 | { |
| 12 | /** |
| 13 | * @var string |
| 14 | */ |
| 15 | protected $endpoint = 'admin/donors/delete'; |
| 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' => 'DELETE', |
| 28 | 'callback' => [$this, 'handleRequest'], |
| 29 | 'permission_callback' => [$this, 'permissionsCheck'], |
| 30 | ], |
| 31 | 'args' => [ |
| 32 | 'ids' => [ |
| 33 | 'type' => 'string', |
| 34 | 'required' => true, |
| 35 | 'validate_callback' => function ($ids) { |
| 36 | foreach ($this->splitString($ids) as $id) { |
| 37 | if (!$this->validateInt($id)) { |
| 38 | return false; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return true; |
| 43 | }, |
| 44 | ], |
| 45 | 'deleteDonationsAndRecords' => [ |
| 46 | 'type' => 'boolean', |
| 47 | 'required' => 'false', |
| 48 | 'default' => 'false' |
| 49 | ] |
| 50 | ], |
| 51 | ] |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param WP_REST_Request $request |
| 57 | * |
| 58 | * @since 2.20.0 |
| 59 | * |
| 60 | * @return WP_REST_Response |
| 61 | */ |
| 62 | public function handleRequest(WP_REST_Request $request) |
| 63 | { |
| 64 | $ids = $this->splitString($request->get_param('ids')); |
| 65 | $delete_donation = $request->get_param('deleteDonationsAndRecords'); |
| 66 | $errors = $successes = []; |
| 67 | |
| 68 | foreach ($ids as $id) { |
| 69 | try { |
| 70 | /** |
| 71 | * Fires before deleting donor. |
| 72 | * |
| 73 | * @param int $donor_id The ID of the donor. |
| 74 | * @param bool $delete_donor Confirm Donor Deletion. |
| 75 | * @param bool $delete_donation Confirm Donor related donations deletion. |
| 76 | * |
| 77 | * @since 2.20.0 |
| 78 | */ |
| 79 | do_action( 'give_pre_delete_donor', $id, true, $delete_donation ); |
| 80 | $donor = Donor::find($id); |
| 81 | if ($delete_donation) { |
| 82 | foreach( $donor->donations as $donation ) { |
| 83 | $donation->delete(); |
| 84 | } |
| 85 | } |
| 86 | else { |
| 87 | give_update_payment_meta( $id, '_give_payment_donor_id', 0 ); |
| 88 | } |
| 89 | $donor->delete(); |
| 90 | $successes[] = $id; |
| 91 | } catch (Exception $e) { |
| 92 | $errors[] = $id; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return new WP_REST_Response([ |
| 97 | 'errors' => $errors, |
| 98 | 'successes' => $successes |
| 99 | ]); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /** |
| 104 | * Split string |
| 105 | * |
| 106 | * @param string $ids |
| 107 | * @since 2.20.0 |
| 108 | * |
| 109 | * @return string[] |
| 110 | */ |
| 111 | protected function splitString($ids) |
| 112 | { |
| 113 | if (strpos($ids, ',')) { |
| 114 | return array_map('trim', explode(',', $ids)); |
| 115 | } |
| 116 | |
| 117 | return [trim($ids)]; |
| 118 | } |
| 119 | } |
| 120 |