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 |