CreateEvent.php
1 year ago
CreateEventTicketType.php
1 year ago
DeleteEventTicketType.php
1 year ago
DeleteEventsListTable.php
1 year ago
GetEventForms.php
1 year ago
GetEventTicketTypeTickets.php
1 year ago
GetEventTicketTypes.php
1 year ago
GetEventTickets.php
1 year ago
GetEvents.php
1 year ago
GetEventsListTable.php
1 year ago
UpdateEvent.php
1 year ago
UpdateEventTicketType.php
1 year ago
DeleteEventsListTable.php
117 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\EventTickets\Routes; |
| 4 | |
| 5 | use Give\EventTickets\ListTable\EventTicketsListTable; |
| 6 | use Give\EventTickets\Repositories\EventRepository; |
| 7 | use Give\Framework\Exceptions\Primitives\Exception; |
| 8 | use WP_REST_Request; |
| 9 | use WP_REST_Response; |
| 10 | use WP_REST_Server; |
| 11 | |
| 12 | /** |
| 13 | * @since 3.6.0 |
| 14 | */ |
| 15 | class DeleteEventsListTable |
| 16 | { |
| 17 | /** |
| 18 | * @var string |
| 19 | */ |
| 20 | protected $endpoint = 'events-tickets/events/list-table'; |
| 21 | |
| 22 | /** |
| 23 | * @var WP_REST_Request |
| 24 | */ |
| 25 | protected $request; |
| 26 | |
| 27 | /** |
| 28 | * @var EventTicketsListTable |
| 29 | */ |
| 30 | protected $listTable; |
| 31 | |
| 32 | /** |
| 33 | * @inheritDoc |
| 34 | * |
| 35 | * @since 3.20.0 Set the permission callback to "delete_give_payments". |
| 36 | * @since 3.6.0 |
| 37 | */ |
| 38 | public function registerRoute() |
| 39 | { |
| 40 | register_rest_route( |
| 41 | 'give-api/v2', |
| 42 | $this->endpoint, |
| 43 | [ |
| 44 | [ |
| 45 | 'methods' => WP_REST_Server::DELETABLE, |
| 46 | 'callback' => [$this, 'handleRequest'], |
| 47 | 'permission_callback' => function () { |
| 48 | return current_user_can('edit_give_forms'); |
| 49 | }, |
| 50 | ], |
| 51 | 'args' => [ |
| 52 | 'ids' => [ |
| 53 | 'type' => 'string', |
| 54 | 'required' => true, |
| 55 | 'validate_callback' => function ($ids) { |
| 56 | foreach ($this->splitString($ids) as $id) { |
| 57 | if ( ! filter_var($id, FILTER_VALIDATE_INT)) { |
| 58 | return false; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return true; |
| 63 | }, |
| 64 | ], |
| 65 | ], |
| 66 | ] |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @since 3.6.0 |
| 72 | * @throws Exception |
| 73 | */ |
| 74 | public function handleRequest(WP_REST_Request $request): WP_Rest_Response |
| 75 | { |
| 76 | $ids = $this->splitString($request->get_param('ids')); |
| 77 | $errors = []; |
| 78 | $successes = []; |
| 79 | |
| 80 | foreach ($ids as $id) { |
| 81 | $event = give(EventRepository::class)->getById($id); |
| 82 | if ( ! $event) { |
| 83 | $errors[] = $id; |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | $soldTicketsCount = $event->eventTickets()->count() ?? 0; |
| 88 | if ($soldTicketsCount > 0) { |
| 89 | $errors[] = $id; |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | $eventDeleted = give(EventRepository::class)->getById($id)->delete(); |
| 94 | $eventDeleted ? $successes[] = $id : $errors[] = $id; |
| 95 | } |
| 96 | |
| 97 | return new WP_REST_Response(array('errors' => $errors, 'successes' => $successes)); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | /** |
| 102 | * Split string |
| 103 | * |
| 104 | * @since 3.6.0 |
| 105 | * |
| 106 | * @return string[] |
| 107 | */ |
| 108 | protected function splitString(string $ids): array |
| 109 | { |
| 110 | if (strpos($ids, ',')) { |
| 111 | return array_map('trim', explode(',', $ids)); |
| 112 | } |
| 113 | |
| 114 | return [trim($ids)]; |
| 115 | } |
| 116 | } |
| 117 |