PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 4.13.0
GiveWP – Donation Plugin and Fundraising Platform v4.13.0
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 8 months ago GetCampaignRevenue.php 8 months ago GetCampaignStatistics.php 8 months ago RegisterCampaignRoutes.php 8 months ago
RegisterCampaignRoutes.php
605 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 Give\Campaigns\ValueObjects\CampaignGoalType;
10 use Give\Campaigns\ValueObjects\CampaignStatus;
11 use Give\Campaigns\ValueObjects\CampaignType;
12 use WP_REST_Request;
13 use WP_REST_Server;
14
15 /**
16 * @since 4.0.0
17 */
18 class RegisterCampaignRoutes
19 {
20 /**
21 * @var CampaignRequestController
22 */
23 protected $campaignRequestController;
24
25 /**
26 * @since 4.0.0
27 */
28 public function __construct(CampaignRequestController $campaignRequestController)
29 {
30 $this->campaignRequestController = $campaignRequestController;
31 }
32
33 /**
34 * @since 4.0.0
35 */
36 public function __invoke()
37 {
38 $this->registerGetCampaign();
39 $this->registerUpdateCampaign();
40 $this->registerGetCampaigns();
41 $this->registerMergeCampaigns();
42 $this->registerCreateCampaign();
43 $this->registerCreateCampaignPage();
44 $this->registerDuplicateCampaign();
45 }
46
47 /**
48 * Get Campaign route
49 *
50 * @since 4.10.1 Changed permission callback to use validationForGetItem method
51 * @since 4.9.0 Add missing schema key to the route level
52 * @since 4.0.0
53 */
54 public function registerGetCampaign()
55 {
56 register_rest_route(
57 CampaignRoute::NAMESPACE,
58 CampaignRoute::CAMPAIGN,
59 [
60 [
61 'methods' => WP_REST_Server::READABLE,
62 'callback' => function (WP_REST_Request $request) {
63 return $this->campaignRequestController->getCampaign($request);
64 },
65 'permission_callback' => function (WP_REST_Request $request) {
66 return CampaignPermissions::validationForGetItem($request);
67 },
68 ],
69 'args' => [
70 'id' => [
71 'type' => 'integer',
72 'required' => true,
73 ],
74 ],
75 'schema' => [$this, 'getSchema'],
76 ]
77 );
78 }
79
80 /**
81 * Get Campaigns route
82 *
83 * @since 4.10.1 Changed permission callback to use validationForGetItems method
84 * @since 4.0.0
85 */
86 public function registerGetCampaigns()
87 {
88 register_rest_route(
89 CampaignRoute::NAMESPACE,
90 CampaignRoute::CAMPAIGNS,
91 [
92 [
93 'methods' => WP_REST_Server::READABLE,
94 'callback' => function (WP_REST_Request $request) {
95 return $this->campaignRequestController->getCampaigns($request);
96 },
97 'permission_callback' => function (WP_REST_Request $request) {
98 return CampaignPermissions::validationForGetItems($request);
99 },
100 ],
101 'args' => [
102 'status' => [
103 'type' => 'array',
104 'items' => [
105 'type' => 'string',
106 'enum' => ['active', 'draft', 'archived'],
107 ],
108 'default' => ['active'],
109 ],
110 'ids' => [
111 'type' => 'array',
112 'default' => [],
113 ],
114 'page' => [
115 'type' => 'integer',
116 'default' => 1,
117 'minimum' => 1,
118 ],
119 'per_page' => [
120 'type' => 'integer',
121 'default' => 30,
122 'minimum' => 1,
123 'maximum' => 100,
124 ],
125 'sortBy' => [
126 'type' => 'string',
127 'enum' => [
128 'date',
129 'amount',
130 'donors',
131 'donations',
132 ],
133 'default' => 'date',
134 ],
135 'orderBy' => [
136 'type' => 'string',
137 'enum' => [
138 'asc',
139 'desc',
140 ],
141 'default' => 'desc',
142 ],
143 'search' => [
144 'type' => 'string',
145 'default' => '',
146 ],
147 ],
148 'schema' => [$this, 'getSchema'],
149 ]
150 );
151 }
152
153 /**
154 * Update Campaign route
155 *
156 * @since 4.0.0
157 */
158 public function registerUpdateCampaign()
159 {
160 register_rest_route(
161 CampaignRoute::NAMESPACE,
162 CampaignRoute::CAMPAIGN,
163 [
164 [
165 'methods' => WP_REST_Server::EDITABLE,
166 'callback' => function (WP_REST_Request $request) {
167 return $this->campaignRequestController->updateCampaign($request);
168 },
169 'permission_callback' => function () {
170 return current_user_can('manage_options');
171 },
172 ],
173 'args' => rest_get_endpoint_args_for_schema($this->getSchema(), WP_REST_Server::EDITABLE),
174 'schema' => [$this, 'getSchema'],
175 ]
176 );
177 }
178
179 /**
180 * Update Campaign route
181 *
182 * @since 4.9.0 Add missing schema key to the route level
183 * @since 4.0.0
184 */
185 public function registerMergeCampaigns()
186 {
187 register_rest_route(
188 CampaignRoute::NAMESPACE,
189 CampaignRoute::CAMPAIGN . '/merge',
190 [
191 [
192 'methods' => WP_REST_Server::EDITABLE,
193 'callback' => function (WP_REST_Request $request) {
194 return $this->campaignRequestController->mergeCampaigns($request);
195 },
196 'permission_callback' => function () {
197 return current_user_can('manage_options');
198 },
199 ],
200 'args' => [
201 'id' => [
202 'type' => 'integer',
203 'required' => true,
204 ],
205 'campaignsToMergeIds' => [
206 'type' => 'array',
207 'required' => true,
208 'items' => [
209 'type' => 'integer',
210 ],
211 ],
212 ],
213 'schema' => [$this, 'getSchema'],
214 ]
215 );
216 }
217
218 /**
219 * Create Campaign route
220 *
221 * @since 4.9.0 Add missing schema key to the route level
222 * @since 4.0.0
223 */
224 public function registerCreateCampaign()
225 {
226 register_rest_route(
227 CampaignRoute::NAMESPACE,
228 CampaignRoute::CAMPAIGNS,
229 [
230 [
231 'methods' => WP_REST_Server::CREATABLE,
232 'callback' => function (WP_REST_Request $request) {
233 return $this->campaignRequestController->createCampaign($request);
234 },
235 'permission_callback' => function () {
236 return current_user_can('manage_options');
237 },
238 ],
239 'args' => array_merge(rest_get_endpoint_args_for_schema($this->getSchema(), WP_REST_Server::CREATABLE), [
240 'logo' => [
241 'type' => 'string',
242 'format' => 'uri',
243 'required' => false,
244 ],
245 'image' => [
246 'type' => 'string',
247 'format' => 'uri',
248 'required' => false,
249 ],
250 'startDateTime' => [
251 'type' => 'string',
252 'format' => 'date-time', // @link https://datatracker.ietf.org/doc/html/rfc3339#section-5.8
253 'required' => false,
254 'validate_callback' => 'rest_parse_date',
255 'sanitize_callback' => function ($value) {
256 return new DateTime($value, wp_timezone());
257 },
258 ],
259 'endDateTime' => [
260 'type' => 'string',
261 'format' => 'date-time', // @link https://datatracker.ietf.org/doc/html/rfc3339#section-5.8
262 'required' => false,
263 'validate_callback' => 'rest_parse_date',
264 'sanitize_callback' => function ($value) {
265 return new DateTime($value, wp_timezone());
266 },
267 ],
268 ] ),
269 'schema' => [$this, 'getSchema'],
270 ]
271 );
272 }
273
274 /**
275 * @since 4.9.0 Add missing schema key to the route level
276 * @since 4.2.0
277 */
278 public function registerDuplicateCampaign()
279 {
280 register_rest_route(
281 CampaignRoute::NAMESPACE,
282 CampaignRoute::CAMPAIGN . '/duplicate',
283 [
284 [
285 'methods' => WP_REST_Server::CREATABLE,
286 'callback' => function (WP_REST_Request $request) {
287 return $this->campaignRequestController->duplicateCampaign($request);
288 },
289 'permission_callback' => function () {
290 return current_user_can('manage_options');
291 },
292 ],
293 'args' => [
294 'id' => [
295 'type' => 'integer',
296 'required' => true,
297 ],
298 ],
299 'schema' => [$this, 'getSchema'],
300 ]
301 );
302 }
303
304 /**
305 * @since 4.13.0 add schema description
306 * @since 4.9.0 Set proper JSON Schema version
307 * @since 4.0.0
308 */
309 public function getSchema(): array
310 {
311 return [
312 '$schema' => 'http://json-schema.org/draft-04/schema#',
313 'title' => 'givewp/campaign',
314 'description' => esc_html__('Campaign routes for CRUD operations', 'give'),
315 'type' => 'object',
316 'properties' => [
317 'id' => [
318 'type' => 'integer',
319 'description' => esc_html__('Campaign ID', 'give'),
320 'readonly' => true,
321 ],
322 'pagePermalink' => [
323 'type' => ['string', 'null'],
324 'description' => esc_html__('Campaign page permalink', 'give'),
325 'readonly' => true,
326 ],
327 'title' => [
328 'type' => 'string',
329 'description' => esc_html__('Campaign title', 'give'),
330 'minLength' => 3,
331 'maxLength' => 128,
332 'required' => true,
333 ],
334 'status' => [
335 'enum' => [CampaignStatus::ACTIVE,CampaignStatus::ARCHIVED],
336 'description' => esc_html__('Campaign status', 'give'),
337 'default' => CampaignStatus::ACTIVE,
338 ],
339 'shortDescription' => [
340 'type' => ['string', 'null'],
341 'description' => esc_html__('Campaign short description', 'give'),
342 'maxLength' => 120,
343 ],
344 'longDescription' => [
345 'type' => ['string', 'null'],
346 'description' => esc_html__('Campaign long description', 'give'),
347 ],
348 'logo' => [
349 'type' => ['string', 'null'],
350 'format' => 'uri',
351 'description' => esc_html__('Campaign logo URL', 'give'),
352 ],
353 'image' => [
354 'type' => ['string', 'null'],
355 'format' => 'uri',
356 'description' => esc_html__('Campaign featured image URL', 'give'),
357 ],
358 'primaryColor' => [
359 'type' => ['string', 'null'],
360 'description' => esc_html__('Primary color for the campaign', 'give'),
361 ],
362 'secondaryColor' => [
363 'type' => ['string', 'null'],
364 'description' => esc_html__('Secondary color for the campaign', 'give'),
365 ],
366 'goal' => [
367 'type' => 'integer',
368 'description' => esc_html__('Campaign goal', 'give'),
369 'errorMessage' => esc_html__('Must be a number', 'give'),
370 'required' => true,
371 ],
372 'goalType' => [
373 'enum' => array_values(CampaignGoalType::toArray()),
374 'description' => esc_html__('Campaign goal type', 'give'),
375 'required' => true,
376 ],
377 'goalStats' => [
378 'type' => 'object',
379 'description' => esc_html__('Campaign goal statistics', 'give'),
380 'readonly' => true,
381 'properties' => [
382 'actual' => [
383 'type' => ['integer', 'number'],
384 'description' => esc_html__('Actual progress value', 'give'),
385 ],
386 'actualFormatted' => [
387 'type' => ['string', 'number'],
388 'description' => esc_html__('Formatted actual progress', 'give'),
389 ],
390 'percentage' => [
391 'type' => 'number',
392 'description' => esc_html__('Progress percentage', 'give'),
393 ],
394 'goal' => [
395 'type' => ['integer', 'number'],
396 'description' => esc_html__('Goal value', 'give'),
397 ],
398 'goalFormatted' => [
399 'type' => ['string', 'number'],
400 'description' => esc_html__('Formatted goal value', 'give'),
401 ],
402 ],
403 ],
404 'type' => [
405 'type' => 'string',
406 'enum' => array_values(CampaignType::toArray()),
407 'description' => esc_html__('Campaign type', 'give'),
408 'default' => CampaignType::CORE,
409 ],
410 'defaultFormId' => [
411 'type' => 'integer',
412 'description' => esc_html__('Default campaign form ID', 'give'),
413 'readonly' => true,
414 ],
415 'defaultFormTitle' => [
416 'type' => 'string',
417 'description' => esc_html__('Default campaign form title', 'give'),
418 'readonly' => true,
419 ],
420 'pageId' => [
421 'type' => ['integer', 'null'],
422 'description' => esc_html__('Campaign page ID', 'give'),
423 ],
424 'startDate' => [
425 'type' => ['string', 'null'],
426 'description' => esc_html__('Campaign start date', 'give'),
427 'format' => 'date-time',
428 ],
429 'endDate' => [
430 'type' => ['string', 'null'],
431 'description' => esc_html__('Campaign end date', 'give'),
432 'format' => 'date-time',
433 ],
434 'createdAt' => [
435 'type' => ['string', 'null'],
436 'description' => esc_html__('Campaign creation date (Y-m-d H:i:s)', 'give'),
437 'format' => 'date-time',
438 ],
439 ],
440 'allOf' => [
441 [
442 'if' => [
443 'properties' => [
444 'goalType' => [
445 'const' => 'amount',
446 ],
447 ],
448 ],
449 'then' => [
450 'properties' => [
451 'goal' => [
452 //'minimum' => 1,
453 'type' => 'integer',
454 ],
455 ],
456 'errorMessage' => [
457 'properties' => [
458 'goal' => esc_html__('Goal amount must be greater than 0', 'give'),
459 ],
460 ],
461 ],
462 ],
463 [
464 'if' => [
465 'properties' => [
466 'goalType' => [
467 'const' => 'donations',
468 ],
469 ],
470 ],
471 'then' => [
472 'properties' => [
473 'goal' => [
474 'minimum' => 1,
475 'type' => 'integer',
476 ],
477 ],
478 'errorMessage' => [
479 'properties' => [
480 'goal' => esc_html__('Number of donations must be greater than 0', 'give'),
481 ],
482 ],
483 ],
484 ],
485 [
486 'if' => [
487 'properties' => [
488 'goalType' => [
489 'const' => 'donors',
490 ],
491 ],
492 ],
493 'then' => [
494 'properties' => [
495 'goal' => [
496 'minimum' => 1,
497 'type' => 'integer',
498 ],
499 ],
500 'errorMessage' => [
501 'properties' => [
502 'goal' => esc_html__('Number of donors must be greater than 0', 'give'),
503 ],
504 ],
505 ],
506 ],
507 [
508 'if' => [
509 'properties' => [
510 'goalType' => [
511 'const' => 'amountFromSubscriptions',
512 ],
513 ],
514 ],
515 'then' => [
516 'properties' => [
517 'goal' => [
518 'minimum' => 1,
519 'type' => 'integer',
520 ],
521 ],
522 'errorMessage' => [
523 'properties' => [
524 'goal' => esc_html__('Goal recurring amount must be greater than 0', 'give'),
525 ],
526 ],
527 ],
528 ],
529 [
530 'if' => [
531 'properties' => [
532 'goalType' => [
533 'const' => 'subscriptions',
534 ],
535 ],
536 ],
537 'then' => [
538 'properties' => [
539 'goal' => [
540 'minimum' => 1,
541 'type' => 'integer',
542 ],
543 ],
544 'errorMessage' => [
545 'properties' => [
546 'goal' => esc_html__('Number of recurring donations must be greater than 0', 'give'),
547 ],
548 ],
549 ],
550 ],
551 [
552 'if' => [
553 'properties' => [
554 'goalType' => [
555 'const' => 'donorsFromSubscriptions',
556 ],
557 ],
558 ],
559 'then' => [
560 'properties' => [
561 'goal' => [
562 'minimum' => 1,
563 'type' => 'number',
564 ],
565 ],
566 'errorMessage' => [
567 'properties' => [
568 'goal' => esc_html__('Number of recurring donors must be greater than 0', 'give'),
569 ],
570 ],
571 ],
572 ],
573 ],
574 ];
575 }
576
577 /**
578 * @since 4.0.0
579 */
580 public function registerCreateCampaignPage(): void
581 {
582 register_rest_route(
583 CampaignRoute::NAMESPACE,
584 CampaignRoute::CAMPAIGN . '/page',
585 [
586 [
587 'methods' => WP_REST_Server::CREATABLE,
588 'callback' => function (WP_REST_Request $request) {
589 return $this->campaignRequestController->createCampaignPage($request);
590 },
591 'permission_callback' => function () {
592 return current_user_can('manage_options');
593 },
594 ],
595 'args' => [
596 'id' => [
597 'type' => 'integer',
598 'required' => true,
599 ],
600 ],
601 ]
602 );
603 }
604 }
605