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 |