PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / EventTickets / Routes / GetEvents.php

GetEvents.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/EventTickets/Routes/GetEvents.php

82 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\EventTickets\Routes;
4
5 use Give\API\RestRoute;
6 use Give\Framework\Models\Model;
7 use Give\Framework\Permissions\Facades\UserPermissions;
8 use WP_REST_Request;
9 use WP_REST_Response;
10
11 /**
12 * @since 3.6.0
13 */
14 class GetEvents implements RestRoute
15 {
16 /** @var string */
17 protected $endpoint = 'events-tickets/events';
18
19 /**
20 * @inheritDoc
21 *
22 * @since 4.14.0 update permission capability to use facade
23 * @since 3.20.0 Set the permission callback to "read".
24 * @since 3.6.0
25 */
26 public function registerRoute()
27 {
28 register_rest_route(
29 'give-api/v2',
30 $this->endpoint,
31 [
32 [
33 'methods' => 'GET',
34 'callback' => [$this, 'handleRequest'],
35 'permission_callback' => function () {
36 return UserPermissions::events()->canView();
37 },
38 ],
39 'args' => [
40 'page' => [
41 'validate_callback' => function ($param) {
42 return filter_var($param, FILTER_VALIDATE_INT);
43 },
44 'default' => 1,
45 ],
46 'per_page' => [
47 'validate_callback' => function ($param) {
48 return filter_var($param, FILTER_VALIDATE_INT);
49 },
50 'default' => 10,
51 ],
52 ],
53 ]
54 );
55 }
56
57 /**
58 * @since 3.6.0
59 *
60 * @return WP_REST_Response
61 *
62 */
63 public function handleRequest(WP_REST_Request $request)
64 {
65 $events = give('events')
66 ->prepareQuery()
67 ->paginate(
68 $request->get_param('page'),
69 $request->get_param('per_page')
70 )->getAll();
71
72 return new WP_REST_Response(
73 array_map(
74 function (Model $model) {
75 return $model->toArray();
76 },
77 $events
78 )
79 );
80 }
81 }
82