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 / UpdateEventTicketType.php

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

107 lines 3.6 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\EventTickets\DataTransferObjects\EventTicketTypeData;
7 use Give\EventTickets\Models\EventTicketType;
8 use Give\Framework\Support\ValueObjects\Money;
9 use Give\Framework\Permissions\Facades\UserPermissions;
10 use WP_REST_Request;
11 use WP_REST_Response;
12 use WP_REST_Server;
13
14 /**
15 * @since 3.6.0
16 * @since 3.14.0 add permission callback check
17 */
18 class UpdateEventTicketType implements RestRoute
19 {
20 /** @var string */
21 protected $endpoint = 'events-tickets/ticket-type/(?P<ticket_type_id>\d+)';
22
23 /**
24 * @inheritDoc
25 *
26 * @since 4.14.0 update permission capability to use facade
27 * @since 3.20.0 Set the permission callback to "edit_give_payments" and description's sanitize callback to "textarea".
28 * @since 3.6.0
29 */
30 public function registerRoute()
31 {
32 register_rest_route(
33 'give-api/v2',
34 $this->endpoint,
35 [
36 [
37 'methods' => WP_REST_Server::EDITABLE,
38 'callback' => [$this, 'handleRequest'],
39 'permission_callback' => function () {
40 return UserPermissions::events()->canEdit();
41 },
42 ],
43 'args' => [
44 'ticket_type_id' => [
45 'type' => 'integer',
46 'sanitize_callback' => 'absint',
47 'validate_callback' => function ($ticketTypeId) {
48 return EventTicketType::find(
49 $ticketTypeId
50 ) !== null;
51 },
52 'required' => true,
53 ],
54 'title' => [
55 'type' => 'string',
56 'required' => false,
57 'sanitize_callback' => 'sanitize_text_field',
58 ],
59 'description' => [
60 'type' => 'string',
61 'required' => false,
62 'sanitize_callback' => 'sanitize_textarea_field',
63 ],
64 'price' => [
65 'type' => 'integer',
66 'required' => false,
67 'sanitize_callback' => 'absint',
68 'validate_callback' => 'rest_is_integer',
69 'description' => 'This price to purchase a ticket in the minor amount of the currency. For example, 1000 for $10.00.',
70 ],
71 'capacity' => [
72 'type' => 'integer',
73 'required' => false,
74 'sanitize_callback' => 'absint',
75 'validate_callback' => 'rest_is_integer',
76 ],
77 ],
78 ]
79 );
80 }
81
82 /**
83 * @since 3.6.0
84 *
85 * @return WP_REST_Response
86 *
87 */
88 public function handleRequest(WP_REST_Request $request)
89 {
90 $ticketType = EventTicketType::find($request->get_param('ticket_type_id'));
91
92 foreach(['title', 'description', 'capacity'] as $param) {
93 if ($request->has_param($param)) {
94 $ticketType->setAttribute($param, $request->get_param($param));
95 }
96 }
97
98 if ($request->has_param('price')) {
99 $ticketType->setAttribute('price', new Money($request->get_param('price'), give_get_currency()));
100 }
101
102 $ticketType->save();
103
104 return new WP_REST_Response(EventTicketTypeData::make($ticketType)->toArray(), 200);
105 }
106 }
107