PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 4.10.1
GiveWP – Donation Plugin and Fundraising Platform v4.10.1
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 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / src / API / REST / V3 / Routes / Campaigns / RegisterCampaignRoutes.php
give / src / API / REST / V3 / Routes / Campaigns Last commit date
Permissions 9 months ago ValueObjects 1 year ago GetCampaignComments.php 10 months ago GetCampaignRevenue.php 9 months ago GetCampaignStatistics.php 1 year ago RegisterCampaignRoutes.php 9 months ago
RegisterCampaignRoutes.php
534 lines
1 <?php
2
3 namespace Give\API\REST\V3\Routes\Campaigns;
4
5 use DateTime;
6 use Give\API\REST\V3\Routes\Campaigns\Permissions\CampaignPermissions;
7 use Give\API\REST\V3\Routes\Campaigns\ValueObjects\CampaignRoute;
8 use Give\Campaigns\Controllers\CampaignRequestController;
9 use WP_REST_Request;
10 use WP_REST_Server;
11
12 /**
13 * @since 4.0.0
14 */
15 class RegisterCampaignRoutes
16 {
17 /**
18 * @var CampaignRequestController
19 */
20 protected $campaignRequestController;
21
22 /**
23 * @since 4.0.0
24 */
25 public function __construct(CampaignRequestController $campaignRequestController)
26 {
27 $this->campaignRequestController = $campaignRequestController;
28 }
29
30 /**
31 * @since 4.0.0
32 */
33 public function __invoke()
34 {
35 $this->registerGetCampaign();
36 $this->registerUpdateCampaign();
37 $this->registerGetCampaigns();
38 $this->registerMergeCampaigns();
39 $this->registerCreateCampaign();
40 $this->registerCreateCampaignPage();
41 $this->registerDuplicateCampaign();
42 }
43
44 /**
45 * Get Campaign route
46 *
47 * @since 4.10.1 Changed permission callback to use validationForGetItem method
48 * @since 4.9.0 Add missing schema key to the route level
49 * @since 4.0.0
50 */
51 public function registerGetCampaign()
52 {
53 register_rest_route(
54 CampaignRoute::NAMESPACE,
55 CampaignRoute::CAMPAIGN,
56 [
57 [
58 'methods' => WP_REST_Server::READABLE,
59 'callback' => function (WP_REST_Request $request) {
60 return $this->campaignRequestController->getCampaign($request);
61 },
62 'permission_callback' => function (WP_REST_Request $request) {
63 return CampaignPermissions::validationForGetItem($request);
64 },
65 ],
66 'args' => [
67 'id' => [
68 'type' => 'integer',
69 'required' => true,
70 ],
71 ],
72 'schema' => [$this, 'getSchema'],
73 ]
74 );
75 }
76
77 /**
78 * Get Campaigns route
79 *
80 * @since 4.10.1 Changed permission callback to use validationForGetItems method
81 * @since 4.0.0
82 */
83 public function registerGetCampaigns()
84 {
85 register_rest_route(
86 CampaignRoute::NAMESPACE,
87 CampaignRoute::CAMPAIGNS,
88 [
89 [
90 'methods' => WP_REST_Server::READABLE,
91 'callback' => function (WP_REST_Request $request) {
92 return $this->campaignRequestController->getCampaigns($request);
93 },
94 'permission_callback' => function (WP_REST_Request $request) {
95 return CampaignPermissions::validationForGetItems($request);
96 },
97 ],
98 'args' => [
99 'status' => [
100 'type' => 'array',
101 'items' => [
102 'type' => 'string',
103 'enum' => ['active', 'draft', 'archived'],
104 ],
105 'default' => ['active'],
106 ],
107 'ids' => [
108 'type' => 'array',
109 'default' => [],
110 ],
111 'page' => [
112 'type' => 'integer',
113 'default' => 1,
114 'minimum' => 1,
115 ],
116 'per_page' => [
117 'type' => 'integer',
118 'default' => 30,
119 'minimum' => 1,
120 'maximum' => 100,
121 ],
122 'sortBy' => [
123 'type' => 'string',
124 'enum' => [
125 'date',
126 'amount',
127 'donors',
128 'donations',
129 ],
130 'default' => 'date',
131 ],
132 'orderBy' => [
133 'type' => 'string',
134 'enum' => [
135 'asc',
136 'desc',
137 ],
138 'default' => 'desc',
139 ],
140 'search' => [
141 'type' => 'string',
142 'default' => '',
143 ],
144 ],
145 ]
146 );
147 }
148
149 /**
150 * Update Campaign route
151 *
152 * @since 4.0.0
153 */
154 public function registerUpdateCampaign()
155 {
156 register_rest_route(
157 CampaignRoute::NAMESPACE,
158 CampaignRoute::CAMPAIGN,
159 [
160 [
161 'methods' => WP_REST_Server::EDITABLE,
162 'callback' => function (WP_REST_Request $request) {
163 return $this->campaignRequestController->updateCampaign($request);
164 },
165 'permission_callback' => function () {
166 return current_user_can('manage_options');
167 },
168 ],
169 'args' => rest_get_endpoint_args_for_schema($this->getSchema(), WP_REST_Server::EDITABLE),
170 'schema' => [$this, 'getSchema'],
171 ]
172 );
173 }
174
175 /**
176 * Update Campaign route
177 *
178 * @since 4.9.0 Add missing schema key to the route level
179 * @since 4.0.0
180 */
181 public function registerMergeCampaigns()
182 {
183 register_rest_route(
184 CampaignRoute::NAMESPACE,
185 CampaignRoute::CAMPAIGN . '/merge',
186 [
187 [
188 'methods' => WP_REST_Server::EDITABLE,
189 'callback' => function (WP_REST_Request $request) {
190 return $this->campaignRequestController->mergeCampaigns($request);
191 },
192 'permission_callback' => function () {
193 return current_user_can('manage_options');
194 },
195 ],
196 'args' => [
197 'id' => [
198 'type' => 'integer',
199 'required' => true,
200 ],
201 'campaignsToMergeIds' => [
202 'type' => 'array',
203 'required' => true,
204 'items' => [
205 'type' => 'integer',
206 ],
207 ],
208 ],
209 'schema' => [$this, 'getSchema'],
210 ]
211 );
212 }
213
214 /**
215 * Create Campaign route
216 *
217 * @since 4.9.0 Add missing schema key to the route level
218 * @since 4.0.0
219 */
220 public function registerCreateCampaign()
221 {
222 register_rest_route(
223 CampaignRoute::NAMESPACE,
224 CampaignRoute::CAMPAIGNS,
225 [
226 [
227 'methods' => WP_REST_Server::CREATABLE,
228 'callback' => function (WP_REST_Request $request) {
229 return $this->campaignRequestController->createCampaign($request);
230 },
231 'permission_callback' => function () {
232 return current_user_can('manage_options');
233 },
234 ],
235 'args' => array_merge(rest_get_endpoint_args_for_schema($this->getSchema(), WP_REST_Server::CREATABLE), [
236 'logo' => [
237 'type' => 'string',
238 'format' => 'uri',
239 'required' => false,
240 ],
241 'image' => [
242 'type' => 'string',
243 'format' => 'uri',
244 'required' => false,
245 ],
246 'startDateTime' => [
247 'type' => 'string',
248 'format' => 'date-time', // @link https://datatracker.ietf.org/doc/html/rfc3339#section-5.8
249 'required' => false,
250 'validate_callback' => 'rest_parse_date',
251 'sanitize_callback' => function ($value) {
252 return new DateTime($value);
253 },
254 ],
255 'endDateTime' => [
256 'type' => 'string',
257 'format' => 'date-time', // @link https://datatracker.ietf.org/doc/html/rfc3339#section-5.8
258 'required' => false,
259 'validate_callback' => 'rest_parse_date',
260 'sanitize_callback' => function ($value) {
261 return new DateTime($value);
262 },
263 ],
264 ] ),
265 'schema' => [$this, 'getSchema'],
266 ]
267 );
268 }
269
270 /**
271 * @since 4.9.0 Add missing schema key to the route level
272 * @since 4.2.0
273 */
274 public function registerDuplicateCampaign()
275 {
276 register_rest_route(
277 CampaignRoute::NAMESPACE,
278 CampaignRoute::CAMPAIGN . '/duplicate',
279 [
280 [
281 'methods' => WP_REST_Server::CREATABLE,
282 'callback' => function (WP_REST_Request $request) {
283 return $this->campaignRequestController->duplicateCampaign($request);
284 },
285 'permission_callback' => function () {
286 return current_user_can('manage_options');
287 },
288 ],
289 'args' => [
290 'id' => [
291 'type' => 'integer',
292 'required' => true,
293 ],
294 ],
295 'schema' => [$this, 'getSchema'],
296 ]
297 );
298 }
299
300 /**
301 * @since 4.9.0 Set proper JSON Schema version
302 * @since 4.0.0
303 */
304 public function getSchema(): array
305 {
306 return [
307 '$schema' => 'http://json-schema.org/draft-04/schema#',
308 'title' => 'givewp/campaign',
309 'type' => 'object',
310 'properties' => [
311 'id' => [
312 'type' => 'integer',
313 'description' => esc_html__('Campaign ID', 'give'),
314 ],
315 'title' => [
316 'type' => 'string',
317 'description' => esc_html__('Campaign title', 'give'),
318 'minLength' => 3,
319 'maxLength' => 128,
320 'errorMessage' => esc_html__('Campaign title is required', 'give'),
321 ],
322 'status' => [
323 'enum' => ['active', 'inactive', 'draft', 'pending', 'processing', 'failed', 'archived'],
324 'description' => esc_html__('Campaign status', 'give'),
325 ],
326 'shortDescription' => [
327 'type' => 'string',
328 'description' => esc_html__('Campaign short description', 'give'),
329 'maxLength' => 120,
330 ],
331 'primaryColor' => [
332 'type' => 'string',
333 'description' => esc_html__('Primary color for the campaign', 'give'),
334 ],
335 'secondaryColor' => [
336 'type' => 'string',
337 'description' => esc_html__('Secondary color for the campaign', 'give'),
338 ],
339 'goal' => [
340 'type' => 'number',
341 'description' => esc_html__('Campaign goal', 'give'),
342 'errorMessage' => esc_html__('Must be a number', 'give'),
343 ],
344 'goalProgress' => [
345 'type' => 'number',
346 'description' => esc_html__('Campaign goal progress', 'give'),
347 ],
348 'goalType' => [
349 'enum' => [
350 'amount',
351 'donations',
352 'donors',
353 'amountFromSubscriptions',
354 'subscriptions',
355 'donorsFromSubscriptions',
356 ],
357 'description' => esc_html__('Campaign goal type', 'give'),
358 ],
359 'defaultFormId' => [
360 'type' => 'integer',
361 'description' => esc_html__('Default campaign form ID', 'give'),
362 ],
363 'pageId' => [
364 'type' => 'integer',
365 'description' => esc_html__('Campaign page ID', 'give'),
366 ],
367 ],
368 'required' => ['id', 'title', 'goal', 'goalType'],
369 'allOf' => [
370 [
371 'if' => [
372 'properties' => [
373 'goalType' => [
374 'const' => 'amount',
375 ],
376 ],
377 ],
378 'then' => [
379 'properties' => [
380 'goal' => [
381 //'minimum' => 1,
382 'type' => 'number',
383 ],
384 ],
385 'errorMessage' => [
386 'properties' => [
387 'goal' => esc_html__('Goal amount must be greater than 0', 'give'),
388 ],
389 ],
390 ],
391 ],
392 [
393 'if' => [
394 'properties' => [
395 'goalType' => [
396 'const' => 'donations',
397 ],
398 ],
399 ],
400 'then' => [
401 'properties' => [
402 'goal' => [
403 'minimum' => 1,
404 'type' => 'number',
405 ],
406 ],
407 'errorMessage' => [
408 'properties' => [
409 'goal' => esc_html__('Number of donations must be greater than 0', 'give'),
410 ],
411 ],
412 ],
413 ],
414 [
415 'if' => [
416 'properties' => [
417 'goalType' => [
418 'const' => 'donors',
419 ],
420 ],
421 ],
422 'then' => [
423 'properties' => [
424 'goal' => [
425 'minimum' => 1,
426 'type' => 'number',
427 ],
428 ],
429 'errorMessage' => [
430 'properties' => [
431 'goal' => esc_html__('Number of donors must be greater than 0', 'give'),
432 ],
433 ],
434 ],
435 ],
436 [
437 'if' => [
438 'properties' => [
439 'goalType' => [
440 'const' => 'amountFromSubscriptions',
441 ],
442 ],
443 ],
444 'then' => [
445 'properties' => [
446 'goal' => [
447 'minimum' => 1,
448 'type' => 'number',
449 ],
450 ],
451 'errorMessage' => [
452 'properties' => [
453 'goal' => esc_html__('Goal recurring amount must be greater than 0', 'give'),
454 ],
455 ],
456 ],
457 ],
458 [
459 'if' => [
460 'properties' => [
461 'goalType' => [
462 'const' => 'subscriptions',
463 ],
464 ],
465 ],
466 'then' => [
467 'properties' => [
468 'goal' => [
469 'minimum' => 1,
470 'type' => 'number',
471 ],
472 ],
473 'errorMessage' => [
474 'properties' => [
475 'goal' => esc_html__('Number of recurring donations must be greater than 0', 'give'),
476 ],
477 ],
478 ],
479 ],
480 [
481 'if' => [
482 'properties' => [
483 'goalType' => [
484 'const' => 'donorsFromSubscriptions',
485 ],
486 ],
487 ],
488 'then' => [
489 'properties' => [
490 'goal' => [
491 'minimum' => 1,
492 'type' => 'number',
493 ],
494 ],
495 'errorMessage' => [
496 'properties' => [
497 'goal' => esc_html__('Number of recurring donors must be greater than 0', 'give'),
498 ],
499 ],
500 ],
501 ],
502 ],
503 ];
504 }
505
506 /**
507 * @since 4.0.0
508 */
509 public function registerCreateCampaignPage(): void
510 {
511 register_rest_route(
512 CampaignRoute::NAMESPACE,
513 CampaignRoute::CAMPAIGN . '/page',
514 [
515 [
516 'methods' => WP_REST_Server::CREATABLE,
517 'callback' => function (WP_REST_Request $request) {
518 return $this->campaignRequestController->createCampaignPage($request);
519 },
520 'permission_callback' => function () {
521 return current_user_can('manage_options');
522 },
523 ],
524 'args' => [
525 'id' => [
526 'type' => 'integer',
527 'required' => true,
528 ],
529 ],
530 ]
531 );
532 }
533 }
534