ValueObjects
1 year ago
GetCampaignComments.php
1 year ago
GetCampaignRevenue.php
1 year ago
GetCampaignStatistics.php
1 year ago
RegisterCampaignRoutes.php
1 year ago
RegisterCampaignRoutes.php
520 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\API\REST\V3\Routes\Campaigns; |
| 4 | |
| 5 | use DateTime; |
| 6 | use Give\API\REST\V3\Routes\Campaigns\ValueObjects\CampaignRoute; |
| 7 | use Give\Campaigns\Controllers\CampaignRequestController; |
| 8 | use WP_REST_Request; |
| 9 | use WP_REST_Server; |
| 10 | |
| 11 | /** |
| 12 | * @since 4.0.0 |
| 13 | */ |
| 14 | class RegisterCampaignRoutes |
| 15 | { |
| 16 | /** |
| 17 | * @var CampaignRequestController |
| 18 | */ |
| 19 | protected $campaignRequestController; |
| 20 | |
| 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.0.0 |
| 48 | */ |
| 49 | public function registerGetCampaign() |
| 50 | { |
| 51 | register_rest_route( |
| 52 | CampaignRoute::NAMESPACE, |
| 53 | CampaignRoute::CAMPAIGN, |
| 54 | [ |
| 55 | [ |
| 56 | 'methods' => WP_REST_Server::READABLE, |
| 57 | 'callback' => function (WP_REST_Request $request) { |
| 58 | return $this->campaignRequestController->getCampaign($request); |
| 59 | }, |
| 60 | 'permission_callback' => function () { |
| 61 | return '__return_true'; |
| 62 | }, |
| 63 | ], |
| 64 | 'args' => [ |
| 65 | 'id' => [ |
| 66 | 'type' => 'integer', |
| 67 | 'required' => true, |
| 68 | ], |
| 69 | ], |
| 70 | ] |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get Campaigns route |
| 76 | * |
| 77 | * @since 4.0.0 |
| 78 | */ |
| 79 | public function registerGetCampaigns() |
| 80 | { |
| 81 | register_rest_route( |
| 82 | CampaignRoute::NAMESPACE, |
| 83 | CampaignRoute::CAMPAIGNS, |
| 84 | [ |
| 85 | [ |
| 86 | 'methods' => WP_REST_Server::READABLE, |
| 87 | 'callback' => function (WP_REST_Request $request) { |
| 88 | return $this->campaignRequestController->getCampaigns($request); |
| 89 | }, |
| 90 | 'permission_callback' => '__return_true', |
| 91 | ], |
| 92 | 'args' => [ |
| 93 | 'status' => [ |
| 94 | 'type' => 'array', |
| 95 | 'items' => [ |
| 96 | 'type' => 'string', |
| 97 | 'enum' => ['active', 'draft', 'archived'], |
| 98 | ], |
| 99 | 'default' => ['active'], |
| 100 | ], |
| 101 | 'ids' => [ |
| 102 | 'type' => 'array', |
| 103 | 'default' => [], |
| 104 | ], |
| 105 | 'page' => [ |
| 106 | 'type' => 'integer', |
| 107 | 'default' => 1, |
| 108 | 'minimum' => 1, |
| 109 | ], |
| 110 | 'per_page' => [ |
| 111 | 'type' => 'integer', |
| 112 | 'default' => 30, |
| 113 | 'minimum' => 1, |
| 114 | 'maximum' => 100, |
| 115 | ], |
| 116 | 'sortBy' => [ |
| 117 | 'type' => 'enum', |
| 118 | 'enum' => [ |
| 119 | 'date', |
| 120 | 'amount', |
| 121 | 'donors', |
| 122 | 'donations', |
| 123 | ], |
| 124 | 'default' => 'date', |
| 125 | ], |
| 126 | 'orderBy' => [ |
| 127 | 'type' => 'enum', |
| 128 | 'enum' => [ |
| 129 | 'asc', |
| 130 | 'desc', |
| 131 | ], |
| 132 | 'default' => 'desc', |
| 133 | ], |
| 134 | ], |
| 135 | ] |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Update Campaign route |
| 141 | * |
| 142 | * @since 4.0.0 |
| 143 | */ |
| 144 | public function registerUpdateCampaign() |
| 145 | { |
| 146 | register_rest_route( |
| 147 | CampaignRoute::NAMESPACE, |
| 148 | CampaignRoute::CAMPAIGN, |
| 149 | [ |
| 150 | [ |
| 151 | 'methods' => WP_REST_Server::EDITABLE, |
| 152 | 'callback' => function (WP_REST_Request $request) { |
| 153 | return $this->campaignRequestController->updateCampaign($request); |
| 154 | }, |
| 155 | 'permission_callback' => function () { |
| 156 | return current_user_can('manage_options'); |
| 157 | }, |
| 158 | ], |
| 159 | 'args' => rest_get_endpoint_args_for_schema($this->getSchema(), WP_REST_Server::EDITABLE), |
| 160 | 'schema' => [$this, 'getSchema'], |
| 161 | ] |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | /** |
| 167 | * Update Campaign route |
| 168 | * |
| 169 | * @since 4.0.0 |
| 170 | */ |
| 171 | public function registerMergeCampaigns() |
| 172 | { |
| 173 | register_rest_route( |
| 174 | CampaignRoute::NAMESPACE, |
| 175 | CampaignRoute::CAMPAIGN . '/merge', |
| 176 | [ |
| 177 | [ |
| 178 | 'methods' => WP_REST_Server::EDITABLE, |
| 179 | 'callback' => function (WP_REST_Request $request) { |
| 180 | return $this->campaignRequestController->mergeCampaigns($request); |
| 181 | }, |
| 182 | 'permission_callback' => function () { |
| 183 | return current_user_can('manage_options'); |
| 184 | }, |
| 185 | ], |
| 186 | 'args' => [ |
| 187 | 'id' => [ |
| 188 | 'type' => 'integer', |
| 189 | 'required' => true, |
| 190 | ], |
| 191 | 'campaignsToMergeIds' => [ |
| 192 | 'type' => 'array', |
| 193 | 'required' => true, |
| 194 | 'items' => [ |
| 195 | 'type' => 'integer', |
| 196 | ], |
| 197 | ], |
| 198 | ], |
| 199 | ] |
| 200 | ); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | /** |
| 205 | * Create Campaign route |
| 206 | * |
| 207 | * @since 4.0.0 |
| 208 | */ |
| 209 | public function registerCreateCampaign() |
| 210 | { |
| 211 | register_rest_route( |
| 212 | CampaignRoute::NAMESPACE, |
| 213 | CampaignRoute::CAMPAIGNS, |
| 214 | [ |
| 215 | [ |
| 216 | 'methods' => WP_REST_Server::CREATABLE, |
| 217 | 'callback' => function (WP_REST_Request $request) { |
| 218 | return $this->campaignRequestController->createCampaign($request); |
| 219 | }, |
| 220 | 'permission_callback' => function () { |
| 221 | return current_user_can('manage_options'); |
| 222 | }, |
| 223 | ], |
| 224 | 'args' => [ |
| 225 | 'title' => [ |
| 226 | 'type' => 'string', |
| 227 | 'required' => true, |
| 228 | 'sanitize_callback' => 'sanitize_text_field', |
| 229 | ], |
| 230 | 'shortDescription' => [ |
| 231 | 'type' => 'string', |
| 232 | 'required' => false, |
| 233 | 'sanitize_callback' => 'sanitize_text_field', |
| 234 | ], |
| 235 | 'startDateTime' => [ |
| 236 | 'type' => 'string', |
| 237 | 'format' => 'date-time', // @link https://datatracker.ietf.org/doc/html/rfc3339#section-5.8 |
| 238 | 'required' => false, |
| 239 | 'validate_callback' => 'rest_parse_date', |
| 240 | 'sanitize_callback' => function ($value) { |
| 241 | return new DateTime($value); |
| 242 | }, |
| 243 | ], |
| 244 | 'endDateTime' => [ |
| 245 | 'type' => 'string', |
| 246 | 'format' => 'date-time', // @link https://datatracker.ietf.org/doc/html/rfc3339#section-5.8 |
| 247 | 'required' => false, |
| 248 | 'validate_callback' => 'rest_parse_date', |
| 249 | 'sanitize_callback' => function ($value) { |
| 250 | return new DateTime($value); |
| 251 | }, |
| 252 | ], |
| 253 | ], |
| 254 | ] |
| 255 | ); |
| 256 | } |
| 257 | |
| 258 | |
| 259 | /** |
| 260 | * @since 4.2.0 |
| 261 | */ |
| 262 | public function registerDuplicateCampaign() |
| 263 | { |
| 264 | register_rest_route( |
| 265 | CampaignRoute::NAMESPACE, |
| 266 | CampaignRoute::CAMPAIGN . '/duplicate', |
| 267 | [ |
| 268 | [ |
| 269 | 'methods' => WP_REST_Server::CREATABLE, |
| 270 | 'callback' => function (WP_REST_Request $request) { |
| 271 | return $this->campaignRequestController->duplicateCampaign($request); |
| 272 | }, |
| 273 | 'permission_callback' => function () { |
| 274 | return current_user_can('manage_options'); |
| 275 | }, |
| 276 | ], |
| 277 | 'args' => [ |
| 278 | 'id' => [ |
| 279 | 'type' => 'integer', |
| 280 | 'required' => true, |
| 281 | ], |
| 282 | ], |
| 283 | ] |
| 284 | ); |
| 285 | } |
| 286 | |
| 287 | |
| 288 | /** |
| 289 | * @since 4.0.0 |
| 290 | */ |
| 291 | public function getSchema(): array |
| 292 | { |
| 293 | return [ |
| 294 | 'title' => 'campaign', |
| 295 | 'type' => 'object', |
| 296 | 'properties' => [ |
| 297 | 'id' => [ |
| 298 | 'type' => 'integer', |
| 299 | 'description' => esc_html__('Campaign ID', 'give'), |
| 300 | ], |
| 301 | 'title' => [ |
| 302 | 'type' => 'string', |
| 303 | 'description' => esc_html__('Campaign title', 'give'), |
| 304 | 'minLength' => 3, |
| 305 | 'maxLength' => 128, |
| 306 | 'errorMessage' => esc_html__('Campaign title is required', 'give'), |
| 307 | ], |
| 308 | 'status' => [ |
| 309 | 'enum' => ['active', 'inactive', 'draft', 'pending', 'processing', 'failed', 'archived'], |
| 310 | 'description' => esc_html__('Campaign status', 'give'), |
| 311 | ], |
| 312 | 'shortDescription' => [ |
| 313 | 'type' => 'string', |
| 314 | 'description' => esc_html__('Campaign short description', 'give'), |
| 315 | 'maxLength' => 120, |
| 316 | ], |
| 317 | 'primaryColor' => [ |
| 318 | 'type' => 'string', |
| 319 | 'description' => esc_html__('Primary color for the campaign', 'give'), |
| 320 | ], |
| 321 | 'secondaryColor' => [ |
| 322 | 'type' => 'string', |
| 323 | 'description' => esc_html__('Secondary color for the campaign', 'give'), |
| 324 | ], |
| 325 | 'goal' => [ |
| 326 | 'type' => 'number', |
| 327 | 'description' => esc_html__('Campaign goal', 'give'), |
| 328 | 'errorMessage' => esc_html__('Must be a number', 'give'), |
| 329 | ], |
| 330 | 'goalProgress' => [ |
| 331 | 'type' => 'number', |
| 332 | 'description' => esc_html__('Campaign goal progress', 'give'), |
| 333 | ], |
| 334 | 'goalType' => [ |
| 335 | 'enum' => [ |
| 336 | 'amount', |
| 337 | 'donations', |
| 338 | 'donors', |
| 339 | 'amountFromSubscriptions', |
| 340 | 'subscriptions', |
| 341 | 'donorsFromSubscriptions', |
| 342 | ], |
| 343 | 'description' => esc_html__('Campaign goal type', 'give'), |
| 344 | ], |
| 345 | 'defaultFormId' => [ |
| 346 | 'type' => 'integer', |
| 347 | 'description' => esc_html__('Default campaign form ID', 'give'), |
| 348 | ], |
| 349 | 'pageId' => [ |
| 350 | 'type' => 'integer', |
| 351 | 'description' => esc_html__('Campaign page ID', 'give'), |
| 352 | ], |
| 353 | ], |
| 354 | 'required' => ['id', 'title', 'goal', 'goalType'], |
| 355 | 'allOf' => [ |
| 356 | [ |
| 357 | 'if' => [ |
| 358 | 'properties' => [ |
| 359 | 'goalType' => [ |
| 360 | 'const' => 'amount', |
| 361 | ], |
| 362 | ], |
| 363 | ], |
| 364 | 'then' => [ |
| 365 | 'properties' => [ |
| 366 | 'goal' => [ |
| 367 | //'minimum' => 1, |
| 368 | 'type' => 'number', |
| 369 | ], |
| 370 | ], |
| 371 | 'errorMessage' => [ |
| 372 | 'properties' => [ |
| 373 | 'goal' => esc_html__('Goal amount must be greater than 0', 'give'), |
| 374 | ], |
| 375 | ], |
| 376 | ], |
| 377 | ], |
| 378 | [ |
| 379 | 'if' => [ |
| 380 | 'properties' => [ |
| 381 | 'goalType' => [ |
| 382 | 'const' => 'donations', |
| 383 | ], |
| 384 | ], |
| 385 | ], |
| 386 | 'then' => [ |
| 387 | 'properties' => [ |
| 388 | 'goal' => [ |
| 389 | 'minimum' => 1, |
| 390 | 'type' => 'number', |
| 391 | ], |
| 392 | ], |
| 393 | 'errorMessage' => [ |
| 394 | 'properties' => [ |
| 395 | 'goal' => esc_html__('Number of donations must be greater than 0', 'give'), |
| 396 | ], |
| 397 | ], |
| 398 | ], |
| 399 | ], |
| 400 | [ |
| 401 | 'if' => [ |
| 402 | 'properties' => [ |
| 403 | 'goalType' => [ |
| 404 | 'const' => 'donors', |
| 405 | ], |
| 406 | ], |
| 407 | ], |
| 408 | 'then' => [ |
| 409 | 'properties' => [ |
| 410 | 'goal' => [ |
| 411 | 'minimum' => 1, |
| 412 | 'type' => 'number', |
| 413 | ], |
| 414 | ], |
| 415 | 'errorMessage' => [ |
| 416 | 'properties' => [ |
| 417 | 'goal' => esc_html__('Number of donors must be greater than 0', 'give'), |
| 418 | ], |
| 419 | ], |
| 420 | ], |
| 421 | ], |
| 422 | [ |
| 423 | 'if' => [ |
| 424 | 'properties' => [ |
| 425 | 'goalType' => [ |
| 426 | 'const' => 'amountFromSubscriptions', |
| 427 | ], |
| 428 | ], |
| 429 | ], |
| 430 | 'then' => [ |
| 431 | 'properties' => [ |
| 432 | 'goal' => [ |
| 433 | 'minimum' => 1, |
| 434 | 'type' => 'number', |
| 435 | ], |
| 436 | ], |
| 437 | 'errorMessage' => [ |
| 438 | 'properties' => [ |
| 439 | 'goal' => esc_html__('Goal recurring amount must be greater than 0', 'give'), |
| 440 | ], |
| 441 | ], |
| 442 | ], |
| 443 | ], |
| 444 | [ |
| 445 | 'if' => [ |
| 446 | 'properties' => [ |
| 447 | 'goalType' => [ |
| 448 | 'const' => 'subscriptions', |
| 449 | ], |
| 450 | ], |
| 451 | ], |
| 452 | 'then' => [ |
| 453 | 'properties' => [ |
| 454 | 'goal' => [ |
| 455 | 'minimum' => 1, |
| 456 | 'type' => 'number', |
| 457 | ], |
| 458 | ], |
| 459 | 'errorMessage' => [ |
| 460 | 'properties' => [ |
| 461 | 'goal' => esc_html__('Number of recurring donations must be greater than 0', 'give'), |
| 462 | ], |
| 463 | ], |
| 464 | ], |
| 465 | ], |
| 466 | [ |
| 467 | 'if' => [ |
| 468 | 'properties' => [ |
| 469 | 'goalType' => [ |
| 470 | 'const' => 'donorsFromSubscriptions', |
| 471 | ], |
| 472 | ], |
| 473 | ], |
| 474 | 'then' => [ |
| 475 | 'properties' => [ |
| 476 | 'goal' => [ |
| 477 | 'minimum' => 1, |
| 478 | 'type' => 'number', |
| 479 | ], |
| 480 | ], |
| 481 | 'errorMessage' => [ |
| 482 | 'properties' => [ |
| 483 | 'goal' => esc_html__('Number of recurring donors must be greater than 0', 'give'), |
| 484 | ], |
| 485 | ], |
| 486 | ], |
| 487 | ], |
| 488 | ], |
| 489 | ]; |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * @since 4.0.0 |
| 494 | */ |
| 495 | public function registerCreateCampaignPage(): void |
| 496 | { |
| 497 | register_rest_route( |
| 498 | CampaignRoute::NAMESPACE, |
| 499 | CampaignRoute::CAMPAIGN . '/page', |
| 500 | [ |
| 501 | [ |
| 502 | 'methods' => WP_REST_Server::CREATABLE, |
| 503 | 'callback' => function (WP_REST_Request $request) { |
| 504 | return $this->campaignRequestController->createCampaignPage($request); |
| 505 | }, |
| 506 | 'permission_callback' => function () { |
| 507 | return current_user_can('manage_options'); |
| 508 | }, |
| 509 | ], |
| 510 | 'args' => [ |
| 511 | 'id' => [ |
| 512 | 'type' => 'integer', |
| 513 | 'required' => true, |
| 514 | ], |
| 515 | ], |
| 516 | ] |
| 517 | ); |
| 518 | } |
| 519 | } |
| 520 |