src/API/REST/V3/Routes/Campaigns
Permissions
8 months ago
ValueObjects
1 year ago
ViewModels
10 months ago
CampaignCommentsController.php
5.7 KB
5 months ago
CampaignController.php
30.2 KB
5 months ago
CampaignPageController.php
5.4 KB
8 months ago
CampaignRevenueController.php
7.7 KB
8 months ago
CampaignStatisticsController.php
4.8 KB
8 months ago
CampaignController.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.6, at src/API/REST/V3/Routes/Campaigns/CampaignController.php
| 1 | <?php |
| 2 | |
| 3 | namespace Give\API\REST\V3\Routes\Campaigns; |
| 4 | |
| 5 | use DateTime; |
| 6 | use Exception; |
| 7 | use Give\API\REST\V3\Routes\Campaigns\Permissions\CampaignPermissions; |
| 8 | use Give\API\REST\V3\Routes\Campaigns\ValueObjects\CampaignRoute; |
| 9 | use Give\API\REST\V3\Routes\Campaigns\ViewModels\CampaignViewModel; |
| 10 | use Give\API\REST\V3\Support\Headers; |
| 11 | use Give\API\REST\V3\Support\Item; |
| 12 | use Give\Campaigns\Actions\DuplicateCampaign; |
| 13 | use Give\Campaigns\Models\Campaign; |
| 14 | use Give\Campaigns\Repositories\CampaignRepository; |
| 15 | use Give\Campaigns\Repositories\CampaignsDataRepository; |
| 16 | use Give\Campaigns\ValueObjects\CampaignGoalType; |
| 17 | use Give\Campaigns\ValueObjects\CampaignStatus; |
| 18 | use Give\Campaigns\ValueObjects\CampaignType; |
| 19 | use Give\Donations\ValueObjects\DonationMetaKeys; |
| 20 | use Give\Framework\Database\DB; |
| 21 | use Give\Framework\Permissions\Facades\UserPermissions; |
| 22 | use WP_Error; |
| 23 | use WP_REST_Controller; |
| 24 | use WP_REST_Request; |
| 25 | use WP_REST_Response; |
| 26 | use WP_REST_Server; |
| 27 | |
| 28 | /** |
| 29 | * @since 4.13.1 |
| 30 | */ |
| 31 | class CampaignController extends WP_REST_Controller |
| 32 | { |
| 33 | /** |
| 34 | * @var string |
| 35 | */ |
| 36 | protected $namespace; |
| 37 | |
| 38 | /** |
| 39 | * @var string |
| 40 | */ |
| 41 | protected $rest_base; |
| 42 | |
| 43 | /** |
| 44 | * @since 4.13.1 |
| 45 | */ |
| 46 | public function __construct() |
| 47 | { |
| 48 | $this->namespace = CampaignRoute::NAMESPACE; |
| 49 | $this->rest_base = CampaignRoute::CAMPAIGNS; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @since 4.14.0 updated permission logic with UserPermissions facade |
| 54 | * @since 4.13.1 |
| 55 | */ |
| 56 | public function register_routes() |
| 57 | { |
| 58 | // Single campaign routes |
| 59 | register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', [ |
| 60 | [ |
| 61 | 'methods' => WP_REST_Server::READABLE, |
| 62 | 'callback' => [$this, 'get_item'], |
| 63 | 'permission_callback' => function (WP_REST_Request $request) { |
| 64 | return CampaignPermissions::validationForGetItem($request); |
| 65 | }, |
| 66 | 'args' => [ |
| 67 | 'id' => [ |
| 68 | 'type' => 'integer', |
| 69 | 'required' => true, |
| 70 | ], |
| 71 | ], |
| 72 | ], |
| 73 | [ |
| 74 | 'methods' => WP_REST_Server::EDITABLE, |
| 75 | 'callback' => [$this, 'update_item'], |
| 76 | 'permission_callback' => function () { |
| 77 | return UserPermissions::campaigns()->canEdit(); |
| 78 | }, |
| 79 | 'args' => rest_get_endpoint_args_for_schema($this->get_item_schema(), WP_REST_Server::EDITABLE), |
| 80 | ], |
| 81 | 'schema' => [$this, 'get_public_item_schema'], |
| 82 | ]); |
| 83 | |
| 84 | // Collections and create |
| 85 | register_rest_route($this->namespace, '/' . $this->rest_base, [ |
| 86 | [ |
| 87 | 'methods' => WP_REST_Server::READABLE, |
| 88 | 'callback' => [$this, 'get_items'], |
| 89 | 'permission_callback' => function (WP_REST_Request $request) { |
| 90 | return CampaignPermissions::validationForGetItems($request); |
| 91 | }, |
| 92 | 'args' => $this->get_collection_params(), |
| 93 | ], |
| 94 | [ |
| 95 | 'methods' => WP_REST_Server::CREATABLE, |
| 96 | 'callback' => [$this, 'create_item'], |
| 97 | 'permission_callback' => function () { |
| 98 | return UserPermissions::campaigns()->canCreate(); |
| 99 | }, |
| 100 | 'args' => array_merge( |
| 101 | rest_get_endpoint_args_for_schema($this->get_item_schema(), WP_REST_Server::CREATABLE), |
| 102 | [ |
| 103 | 'logo' => [ |
| 104 | 'type' => 'string', |
| 105 | 'format' => 'uri', |
| 106 | 'required' => false, |
| 107 | ], |
| 108 | 'image' => [ |
| 109 | 'type' => 'string', |
| 110 | 'format' => 'uri', |
| 111 | 'required' => false, |
| 112 | ], |
| 113 | 'startDateTime' => [ |
| 114 | 'type' => 'string', |
| 115 | 'format' => 'date-time', |
| 116 | 'required' => false, |
| 117 | 'validate_callback' => 'rest_parse_date', |
| 118 | 'sanitize_callback' => function ($value) { |
| 119 | return new DateTime($value, wp_timezone()); |
| 120 | }, |
| 121 | ], |
| 122 | 'endDateTime' => [ |
| 123 | 'type' => 'string', |
| 124 | 'format' => 'date-time', |
| 125 | 'required' => false, |
| 126 | 'validate_callback' => 'rest_parse_date', |
| 127 | 'sanitize_callback' => function ($value) { |
| 128 | return new DateTime($value, wp_timezone()); |
| 129 | }, |
| 130 | ], |
| 131 | ] |
| 132 | ), |
| 133 | ], |
| 134 | 'schema' => [$this, 'get_public_item_schema'], |
| 135 | ]); |
| 136 | |
| 137 | // Merge campaigns |
| 138 | register_rest_route($this->namespace, '/' . CampaignRoute::CAMPAIGN . '/merge', [ |
| 139 | [ |
| 140 | 'methods' => WP_REST_Server::EDITABLE, |
| 141 | 'callback' => [$this, 'merge_items'], |
| 142 | 'permission_callback' => function () { |
| 143 | return UserPermissions::campaigns()->canEdit(); |
| 144 | }, |
| 145 | 'args' => [ |
| 146 | 'id' => [ |
| 147 | 'type' => 'integer', |
| 148 | 'required' => true, |
| 149 | ], |
| 150 | 'campaignsToMergeIds' => [ |
| 151 | 'type' => 'array', |
| 152 | 'required' => true, |
| 153 | 'items' => [ |
| 154 | 'type' => 'integer', |
| 155 | ], |
| 156 | ], |
| 157 | ], |
| 158 | ], |
| 159 | 'schema' => [$this, 'get_public_item_schema'], |
| 160 | ]); |
| 161 | |
| 162 | // Duplicate campaign |
| 163 | register_rest_route($this->namespace, '/' . CampaignRoute::CAMPAIGN . '/duplicate', [ |
| 164 | [ |
| 165 | 'methods' => WP_REST_Server::CREATABLE, |
| 166 | 'callback' => [$this, 'duplicate_item'], |
| 167 | 'permission_callback' => function () { |
| 168 | return UserPermissions::campaigns()->canCreate(); |
| 169 | }, |
| 170 | 'args' => [ |
| 171 | 'id' => [ |
| 172 | 'type' => 'integer', |
| 173 | 'required' => true, |
| 174 | ], |
| 175 | ], |
| 176 | ], |
| 177 | 'schema' => [$this, 'get_public_item_schema'], |
| 178 | ]); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * @since 4.13.1 |
| 183 | */ |
| 184 | public function get_items($request) |
| 185 | { |
| 186 | $ids = $request->get_param('ids'); |
| 187 | $page = $request->get_param('page'); |
| 188 | $perPage = $request->get_param('per_page'); |
| 189 | $status = $request->get_param('status'); |
| 190 | $sortBy = $request->get_param('sortBy'); |
| 191 | $orderBy = $request->get_param('orderBy'); |
| 192 | $search = $request->get_param('search'); |
| 193 | |
| 194 | $query = Campaign::query(); |
| 195 | |
| 196 | $query->whereIn('status', $status); |
| 197 | |
| 198 | if (!empty($ids)) { |
| 199 | $query->whereIn('id', $ids); |
| 200 | } |
| 201 | |
| 202 | if ($search) { |
| 203 | $query->whereLike('campaign_title', '%%' . $search . '%%'); |
| 204 | } |
| 205 | |
| 206 | $totalQuery = clone $query; |
| 207 | |
| 208 | $query |
| 209 | ->limit($perPage) |
| 210 | ->offset(($page - 1) * $perPage); |
| 211 | |
| 212 | switch ($sortBy) { |
| 213 | case 'date': |
| 214 | $query->orderBy('date_created', $orderBy); |
| 215 | |
| 216 | break; |
| 217 | case 'amount': |
| 218 | $query |
| 219 | ->selectRaw( |
| 220 | '(SELECT SUM(amount) FROM %1s WHERE campaign_id = campaigns.id) AS amount', |
| 221 | DB::prefix('give_revenue') |
| 222 | ) |
| 223 | ->orderBy('amount', $orderBy); |
| 224 | |
| 225 | break; |
| 226 | case 'donations': |
| 227 | $query |
| 228 | ->selectRaw( |
| 229 | '(SELECT COUNT(donation_id) FROM %1s WHERE campaign_id = campaigns.id) AS donationsCount', |
| 230 | DB::prefix('give_revenue') |
| 231 | ) |
| 232 | ->orderBy('donationsCount', $orderBy); |
| 233 | |
| 234 | break; |
| 235 | case 'donors': |
| 236 | $postsTable = DB::prefix('posts'); |
| 237 | $metaTable = DB::prefix('give_donationmeta'); |
| 238 | $campaignIdKey = DonationMetaKeys::CAMPAIGN_ID; |
| 239 | $donorIdKey = DonationMetaKeys::DONOR_ID; |
| 240 | |
| 241 | $query |
| 242 | ->selectRaw( |
| 243 | "( |
| 244 | SELECT COUNT(DISTINCT donorId.meta_value) |
| 245 | FROM {$postsTable} AS donation |
| 246 | LEFT JOIN {$metaTable} campaignId ON donation.ID = campaignId.donation_id AND campaignId.meta_key = '{$campaignIdKey}' |
| 247 | LEFT JOIN {$metaTable} donorId ON donation.ID = donorId.donation_id AND donorId.meta_key = '{$donorIdKey}' |
| 248 | WHERE post_type = 'give_payment' |
| 249 | AND donation.post_status IN ('publish', 'give_subscription') |
| 250 | AND campaignId.meta_value = campaigns.id |
| 251 | ) AS donorsCount" |
| 252 | ) |
| 253 | ->orderBy('donorsCount', $orderBy); |
| 254 | |
| 255 | break; |
| 256 | } |
| 257 | |
| 258 | $campaigns = $query->getAll() ?? []; |
| 259 | |
| 260 | $ids = array_map(function ($campaign) { |
| 261 | return $campaign->id; |
| 262 | }, $campaigns); |
| 263 | |
| 264 | $campaignsData = !empty($ids) |
| 265 | ? CampaignsDataRepository::campaigns($ids) |
| 266 | : null; |
| 267 | |
| 268 | $campaigns = array_map(function ($campaign) use ($campaignsData, $request) { |
| 269 | $view = new CampaignViewModel($campaign); |
| 270 | |
| 271 | if ($campaignsData) { |
| 272 | $view->setData($campaignsData); |
| 273 | } |
| 274 | |
| 275 | $item = $view->exports(); |
| 276 | |
| 277 | return $this->prepare_response_for_collection( |
| 278 | $this->prepare_item_for_response($item, $request) |
| 279 | ); |
| 280 | }, $campaigns); |
| 281 | |
| 282 | $totalCampaigns = empty($campaigns) ? 0 : $totalQuery->count(); |
| 283 | $response = rest_ensure_response($campaigns); |
| 284 | $response = Headers::addPagination($response, $request, $totalCampaigns, $perPage, $this->rest_base); |
| 285 | |
| 286 | return $response; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * @since 4.13.1 |
| 291 | * |
| 292 | * @return WP_REST_Response|\WP_Error |
| 293 | */ |
| 294 | public function get_item($request) |
| 295 | { |
| 296 | $campaign = Campaign::find($request->get_param('id')); |
| 297 | |
| 298 | if (!$campaign) { |
| 299 | $response = new WP_Error('campaign_not_found', __('Campaign not found', 'give'), ['status' => 404]); |
| 300 | |
| 301 | return rest_ensure_response($response); |
| 302 | } |
| 303 | |
| 304 | $canViewPrivate = UserPermissions::campaigns()->canViewPrivate(); |
| 305 | |
| 306 | if (!$campaign->status->isActive() && !$canViewPrivate) { |
| 307 | $response = new WP_Error( |
| 308 | 'rest_forbidden', |
| 309 | __('You do not have permission to view this campaign.', 'give'), |
| 310 | ['status' => CampaignPermissions::authorizationStatusCode()] |
| 311 | ); |
| 312 | |
| 313 | return rest_ensure_response($response); |
| 314 | } |
| 315 | |
| 316 | $item = (new CampaignViewModel($campaign))->exports(); |
| 317 | |
| 318 | $response = $this->prepare_item_for_response($item, $request); |
| 319 | |
| 320 | return rest_ensure_response($response); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * @since 4.13.1 |
| 325 | */ |
| 326 | public function prepare_item_for_response($item, $request) |
| 327 | { |
| 328 | try { |
| 329 | $campaignId = $item['id'] ?? $request->get_param('id') ?? null; |
| 330 | |
| 331 | if ($campaignId) { |
| 332 | $self_url = rest_url(sprintf('%s/%s/%d', $this->namespace, $this->rest_base, $campaignId)); |
| 333 | |
| 334 | $links = [ |
| 335 | 'self' => ['href' => $self_url] |
| 336 | ]; |
| 337 | } else { |
| 338 | $links = []; |
| 339 | } |
| 340 | |
| 341 | $itemWithDatesFormatted = Item::formatDatesForResponse($item, ['startDate', 'endDate', 'createdAt']); |
| 342 | |
| 343 | $response = new WP_REST_Response($itemWithDatesFormatted); |
| 344 | |
| 345 | if (!empty($links)) { |
| 346 | $response->add_links($links); |
| 347 | } |
| 348 | |
| 349 | $response->data = $this->add_additional_fields_to_object($response->data, $request); |
| 350 | |
| 351 | return $response; |
| 352 | } catch (Exception $e) { |
| 353 | return new WP_Error( |
| 354 | 'prepare_item_for_response_error', |
| 355 | sprintf( |
| 356 | __('Error while preparing campaign for response: %s', 'give'), |
| 357 | $e->getMessage() |
| 358 | ), |
| 359 | ['status' => 400] |
| 360 | ); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * @since 4.13.1 |
| 366 | * |
| 367 | * @return WP_REST_Response|\WP_Error |
| 368 | */ |
| 369 | public function create_item($request) |
| 370 | { |
| 371 | try { |
| 372 | $campaign = Campaign::create([ |
| 373 | 'type' => CampaignType::CORE(), |
| 374 | 'title' => $request->get_param('title'), |
| 375 | 'shortDescription' => $request->get_param('shortDescription') ?? '', |
| 376 | 'longDescription' => '', |
| 377 | 'logo' => '', |
| 378 | 'image' => $request->get_param('image') ?? '', |
| 379 | 'primaryColor' => '#0b72d9', |
| 380 | 'secondaryColor' => '#27ae60', |
| 381 | 'goal' => (int)$request->get_param('goal'), |
| 382 | 'goalType' => new CampaignGoalType($request->get_param('goalType')), |
| 383 | 'status' => CampaignStatus::ACTIVE(), |
| 384 | 'startDate' => $request->get_param('startDateTime'), |
| 385 | 'endDate' => $request->get_param('endDateTime'), |
| 386 | ]); |
| 387 | |
| 388 | $fieldsUpdate = $this->update_additional_fields_for_object($campaign, $request); |
| 389 | |
| 390 | if (is_wp_error($fieldsUpdate)) { |
| 391 | return $fieldsUpdate; |
| 392 | } |
| 393 | |
| 394 | $item = (new CampaignViewModel($campaign))->exports(); |
| 395 | |
| 396 | $response = $this->prepare_item_for_response($item, $request); |
| 397 | $response->set_status(201); |
| 398 | |
| 399 | return rest_ensure_response($response); |
| 400 | } catch (Exception $e) { |
| 401 | return new WP_Error('create_campaign_error', __('Error while creating campaign', 'give'), ['status' => 400]); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * @return WP_REST_Response|\WP_Error |
| 407 | */ |
| 408 | public function update_item($request) |
| 409 | { |
| 410 | $campaign = Campaign::find($request->get_param('id')); |
| 411 | |
| 412 | if (!$campaign) { |
| 413 | $response = new WP_Error('campaign_not_found', __('Campaign not found', 'give'), ['status' => 404]); |
| 414 | |
| 415 | return rest_ensure_response($response); |
| 416 | } |
| 417 | |
| 418 | $statusMap = [ |
| 419 | 'archived' => CampaignStatus::ARCHIVED(), |
| 420 | 'draft' => CampaignStatus::DRAFT(), |
| 421 | 'active' => CampaignStatus::ACTIVE(), |
| 422 | ]; |
| 423 | |
| 424 | foreach ($request->get_params() as $key => $value) { |
| 425 | switch ($key) { |
| 426 | case 'id': |
| 427 | break; |
| 428 | case 'status': |
| 429 | $status = array_key_exists($value, $statusMap) |
| 430 | ? $statusMap[$value] |
| 431 | : CampaignStatus::DRAFT(); |
| 432 | |
| 433 | $campaign->status = $status; |
| 434 | |
| 435 | break; |
| 436 | case 'goal': |
| 437 | $campaign->goal = (int)$value; |
| 438 | break; |
| 439 | case 'goalType': |
| 440 | $campaign->goalType = new CampaignGoalType($value); |
| 441 | break; |
| 442 | case 'defaultFormId': |
| 443 | give(CampaignRepository::class)->updateDefaultCampaignForm( |
| 444 | $campaign, |
| 445 | $request->get_param('defaultFormId') |
| 446 | ); |
| 447 | break; |
| 448 | case 'pageId': |
| 449 | $campaign->pageId = (int)$value; |
| 450 | break; |
| 451 | default: |
| 452 | if ($campaign->hasProperty($key)) { |
| 453 | $campaign->$key = $value; |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | if ($campaign->isDirty()) { |
| 459 | $campaign->save(); |
| 460 | } |
| 461 | |
| 462 | $fieldsUpdate = $this->update_additional_fields_for_object($campaign, $request); |
| 463 | |
| 464 | if (is_wp_error($fieldsUpdate)) { |
| 465 | return $fieldsUpdate; |
| 466 | } |
| 467 | |
| 468 | $item = (new CampaignViewModel($campaign))->exports(); |
| 469 | |
| 470 | $response = $this->prepare_item_for_response($item, $request); |
| 471 | |
| 472 | return rest_ensure_response($response); |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * @since 4.13.1 |
| 477 | * |
| 478 | * @return WP_REST_Response|\WP_Error |
| 479 | */ |
| 480 | public function merge_items($request) |
| 481 | { |
| 482 | try { |
| 483 | $destinationCampaign = Campaign::find($request->get_param('id')); |
| 484 | |
| 485 | if (!$destinationCampaign) { |
| 486 | $response = new WP_Error('campaign_not_found', __('Campaign not found', 'give'), ['status' => 404]); |
| 487 | |
| 488 | return rest_ensure_response($response); |
| 489 | } |
| 490 | |
| 491 | $campaignsToMerge = Campaign::query()->whereIn('id', $request->get_param('campaignsToMergeIds'))->getAll(); |
| 492 | |
| 493 | $destinationCampaign->merge(...$campaignsToMerge); |
| 494 | |
| 495 | $item = (new CampaignViewModel($destinationCampaign))->exports(); |
| 496 | |
| 497 | $response = $this->prepare_item_for_response($item, $request); |
| 498 | $response->set_status(200); |
| 499 | |
| 500 | return rest_ensure_response($response); |
| 501 | } catch (Exception $e) { |
| 502 | return new WP_Error('merge_campaigns_error', __('Error while merging campaigns', 'give'), ['status' => 400]); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * @since 4.13.1 |
| 508 | * |
| 509 | * @return WP_REST_Response|\WP_Error |
| 510 | */ |
| 511 | public function duplicate_item($request) |
| 512 | { |
| 513 | $campaign = Campaign::find((int)$request->get_param('id')); |
| 514 | |
| 515 | if (!$campaign) { |
| 516 | $response = new WP_Error('campaign_not_found', __('Campaign not found', 'give'), ['status' => 404]); |
| 517 | |
| 518 | return rest_ensure_response($response); |
| 519 | } |
| 520 | |
| 521 | $duplicatedCampaign = (new DuplicateCampaign())($campaign); |
| 522 | |
| 523 | $item = array_merge((new CampaignViewModel($duplicatedCampaign))->exports(), [ |
| 524 | 'errors' => 0, // needed by the list table |
| 525 | ]); |
| 526 | |
| 527 | $response = $this->prepare_item_for_response($item, $request); |
| 528 | $response->set_status(201); |
| 529 | |
| 530 | return rest_ensure_response($response); |
| 531 | } |
| 532 | |
| 533 | /** |
| 534 | * @since 4.13.1 |
| 535 | */ |
| 536 | public function get_collection_params(): array |
| 537 | { |
| 538 | return [ |
| 539 | 'status' => [ |
| 540 | 'type' => 'array', |
| 541 | 'items' => [ |
| 542 | 'type' => 'string', |
| 543 | 'enum' => ['active', 'draft', 'archived'], |
| 544 | ], |
| 545 | 'default' => ['active'], |
| 546 | ], |
| 547 | 'ids' => [ |
| 548 | 'type' => 'array', |
| 549 | 'default' => [], |
| 550 | ], |
| 551 | 'page' => [ |
| 552 | 'type' => 'integer', |
| 553 | 'default' => 1, |
| 554 | 'minimum' => 1, |
| 555 | ], |
| 556 | 'per_page' => [ |
| 557 | 'type' => 'integer', |
| 558 | 'default' => 30, |
| 559 | 'minimum' => 1, |
| 560 | 'maximum' => 100, |
| 561 | ], |
| 562 | 'sortBy' => [ |
| 563 | 'type' => 'string', |
| 564 | 'enum' => [ |
| 565 | 'date', |
| 566 | 'amount', |
| 567 | 'donors', |
| 568 | 'donations', |
| 569 | ], |
| 570 | 'default' => 'date', |
| 571 | ], |
| 572 | 'orderBy' => [ |
| 573 | 'type' => 'string', |
| 574 | 'enum' => ['asc', 'desc'], |
| 575 | 'default' => 'desc', |
| 576 | ], |
| 577 | 'search' => [ |
| 578 | 'type' => 'string', |
| 579 | 'default' => '', |
| 580 | ], |
| 581 | ]; |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * @since 4.13.1 |
| 586 | */ |
| 587 | public function get_item_schema(): array |
| 588 | { |
| 589 | $schema = [ |
| 590 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 591 | 'title' => 'givewp/campaign', |
| 592 | 'description' => esc_html__('Campaign routes for CRUD operations', 'give'), |
| 593 | 'type' => 'object', |
| 594 | 'properties' => [ |
| 595 | 'id' => [ |
| 596 | 'type' => 'integer', |
| 597 | 'description' => esc_html__('Campaign ID', 'give'), |
| 598 | 'readonly' => true, |
| 599 | ], |
| 600 | 'pagePermalink' => [ |
| 601 | 'type' => ['string', 'null'], |
| 602 | 'description' => esc_html__('Campaign page permalink', 'give'), |
| 603 | 'readonly' => true, |
| 604 | ], |
| 605 | 'title' => [ |
| 606 | 'type' => 'string', |
| 607 | 'description' => esc_html__('Campaign title', 'give'), |
| 608 | 'minLength' => 3, |
| 609 | 'maxLength' => 128, |
| 610 | 'required' => true, |
| 611 | ], |
| 612 | 'status' => [ |
| 613 | 'type' => 'string', |
| 614 | 'enum' => [CampaignStatus::ACTIVE, CampaignStatus::ARCHIVED], |
| 615 | 'description' => esc_html__('Campaign status', 'give'), |
| 616 | 'default' => CampaignStatus::ACTIVE, |
| 617 | ], |
| 618 | 'shortDescription' => [ |
| 619 | 'type' => ['string', 'null'], |
| 620 | 'description' => esc_html__('Campaign short description', 'give'), |
| 621 | 'maxLength' => 120, |
| 622 | ], |
| 623 | 'longDescription' => [ |
| 624 | 'type' => ['string', 'null'], |
| 625 | 'description' => esc_html__('Campaign long description', 'give'), |
| 626 | ], |
| 627 | 'logo' => [ |
| 628 | 'type' => ['string', 'null'], |
| 629 | 'format' => 'uri', |
| 630 | 'description' => esc_html__('Campaign logo URL', 'give'), |
| 631 | ], |
| 632 | 'image' => [ |
| 633 | 'type' => ['string', 'null'], |
| 634 | 'format' => 'uri', |
| 635 | 'description' => esc_html__('Campaign featured image URL', 'give'), |
| 636 | ], |
| 637 | 'primaryColor' => [ |
| 638 | 'type' => ['string', 'null'], |
| 639 | 'description' => esc_html__('Primary color for the campaign', 'give'), |
| 640 | ], |
| 641 | 'secondaryColor' => [ |
| 642 | 'type' => ['string', 'null'], |
| 643 | 'description' => esc_html__('Secondary color for the campaign', 'give'), |
| 644 | ], |
| 645 | 'goal' => [ |
| 646 | 'type' => 'integer', |
| 647 | 'description' => esc_html__('Campaign goal', 'give'), |
| 648 | 'errorMessage' => esc_html__('Must be a number', 'give'), |
| 649 | 'required' => true, |
| 650 | ], |
| 651 | 'goalType' => [ |
| 652 | 'type' => 'string', |
| 653 | 'enum' => array_values(CampaignGoalType::toArray()), |
| 654 | 'description' => esc_html__('Campaign goal type', 'give'), |
| 655 | 'required' => true, |
| 656 | ], |
| 657 | 'goalStats' => [ |
| 658 | 'type' => 'object', |
| 659 | 'description' => esc_html__('Campaign goal statistics', 'give'), |
| 660 | 'readonly' => true, |
| 661 | 'properties' => [ |
| 662 | 'actual' => [ |
| 663 | 'type' => ['integer', 'number'], |
| 664 | 'description' => esc_html__('Actual progress value', 'give'), |
| 665 | ], |
| 666 | 'actualFormatted' => [ |
| 667 | 'type' => ['string', 'number'], |
| 668 | 'description' => esc_html__('Formatted actual progress', 'give'), |
| 669 | ], |
| 670 | 'percentage' => [ |
| 671 | 'type' => 'number', |
| 672 | 'description' => esc_html__('Progress percentage', 'give'), |
| 673 | ], |
| 674 | 'goal' => [ |
| 675 | 'type' => ['integer', 'number'], |
| 676 | 'description' => esc_html__('Goal value', 'give'), |
| 677 | ], |
| 678 | 'goalFormatted' => [ |
| 679 | 'type' => ['string', 'number'], |
| 680 | 'description' => esc_html__('Formatted goal value', 'give'), |
| 681 | ], |
| 682 | ], |
| 683 | ], |
| 684 | 'type' => [ |
| 685 | 'type' => 'string', |
| 686 | 'enum' => array_values(CampaignType::toArray()), |
| 687 | 'description' => esc_html__('Campaign type', 'give'), |
| 688 | 'default' => CampaignType::CORE, |
| 689 | ], |
| 690 | 'defaultFormId' => [ |
| 691 | 'type' => 'integer', |
| 692 | 'description' => esc_html__('Default campaign form ID', 'give'), |
| 693 | 'readonly' => true, |
| 694 | ], |
| 695 | 'defaultFormTitle' => [ |
| 696 | 'type' => 'string', |
| 697 | 'description' => esc_html__('Default campaign form title', 'give'), |
| 698 | 'readonly' => true, |
| 699 | ], |
| 700 | 'pageId' => [ |
| 701 | 'type' => ['integer', 'null'], |
| 702 | 'description' => esc_html__('Campaign page ID', 'give'), |
| 703 | ], |
| 704 | 'startDate' => [ |
| 705 | 'type' => ['string', 'null'], |
| 706 | 'description' => esc_html__('Campaign start date', 'give'), |
| 707 | 'format' => 'date-time', |
| 708 | ], |
| 709 | 'endDate' => [ |
| 710 | 'type' => ['string', 'null'], |
| 711 | 'description' => esc_html__('Campaign end date', 'give'), |
| 712 | 'format' => 'date-time', |
| 713 | ], |
| 714 | 'createdAt' => [ |
| 715 | 'type' => ['string', 'null'], |
| 716 | 'description' => esc_html__('Campaign creation date (Y-m-d H:i:s)', 'give'), |
| 717 | 'format' => 'date-time', |
| 718 | ], |
| 719 | ], |
| 720 | 'allOf' => [ |
| 721 | [ |
| 722 | 'if' => [ |
| 723 | 'properties' => [ |
| 724 | 'goalType' => [ |
| 725 | 'const' => 'amount', |
| 726 | ], |
| 727 | ], |
| 728 | ], |
| 729 | 'then' => [ |
| 730 | 'properties' => [ |
| 731 | 'goal' => [ |
| 732 | 'type' => 'integer', |
| 733 | ], |
| 734 | ], |
| 735 | 'errorMessage' => [ |
| 736 | 'properties' => [ |
| 737 | 'goal' => esc_html__('Goal amount must be greater than 0', 'give'), |
| 738 | ], |
| 739 | ], |
| 740 | ], |
| 741 | ], |
| 742 | [ |
| 743 | 'if' => [ |
| 744 | 'properties' => [ |
| 745 | 'goalType' => [ |
| 746 | 'const' => 'donations', |
| 747 | ], |
| 748 | ], |
| 749 | ], |
| 750 | 'then' => [ |
| 751 | 'properties' => [ |
| 752 | 'goal' => [ |
| 753 | 'minimum' => 1, |
| 754 | 'type' => 'integer', |
| 755 | ], |
| 756 | ], |
| 757 | 'errorMessage' => [ |
| 758 | 'properties' => [ |
| 759 | 'goal' => esc_html__('Number of donations must be greater than 0', 'give'), |
| 760 | ], |
| 761 | ], |
| 762 | ], |
| 763 | ], |
| 764 | [ |
| 765 | 'if' => [ |
| 766 | 'properties' => [ |
| 767 | 'goalType' => [ |
| 768 | 'const' => 'donors', |
| 769 | ], |
| 770 | ], |
| 771 | ], |
| 772 | 'then' => [ |
| 773 | 'properties' => [ |
| 774 | 'goal' => [ |
| 775 | 'minimum' => 1, |
| 776 | 'type' => 'integer', |
| 777 | ], |
| 778 | ], |
| 779 | 'errorMessage' => [ |
| 780 | 'properties' => [ |
| 781 | 'goal' => esc_html__('Number of donors must be greater than 0', 'give'), |
| 782 | ], |
| 783 | ], |
| 784 | ], |
| 785 | ], |
| 786 | [ |
| 787 | 'if' => [ |
| 788 | 'properties' => [ |
| 789 | 'goalType' => [ |
| 790 | 'const' => 'amountFromSubscriptions', |
| 791 | ], |
| 792 | ], |
| 793 | ], |
| 794 | 'then' => [ |
| 795 | 'properties' => [ |
| 796 | 'goal' => [ |
| 797 | 'minimum' => 1, |
| 798 | 'type' => 'integer', |
| 799 | ], |
| 800 | ], |
| 801 | 'errorMessage' => [ |
| 802 | 'properties' => [ |
| 803 | 'goal' => esc_html__('Goal recurring amount must be greater than 0', 'give'), |
| 804 | ], |
| 805 | ], |
| 806 | ], |
| 807 | ], |
| 808 | [ |
| 809 | 'if' => [ |
| 810 | 'properties' => [ |
| 811 | 'goalType' => [ |
| 812 | 'const' => 'subscriptions', |
| 813 | ], |
| 814 | ], |
| 815 | ], |
| 816 | 'then' => [ |
| 817 | 'properties' => [ |
| 818 | 'goal' => [ |
| 819 | 'minimum' => 1, |
| 820 | 'type' => 'integer', |
| 821 | ], |
| 822 | ], |
| 823 | 'errorMessage' => [ |
| 824 | 'properties' => [ |
| 825 | 'goal' => esc_html__('Number of recurring donations must be greater than 0', 'give'), |
| 826 | ], |
| 827 | ], |
| 828 | ], |
| 829 | ], |
| 830 | [ |
| 831 | 'if' => [ |
| 832 | 'properties' => [ |
| 833 | 'goalType' => [ |
| 834 | 'const' => 'donorsFromSubscriptions', |
| 835 | ], |
| 836 | ], |
| 837 | ], |
| 838 | 'then' => [ |
| 839 | 'properties' => [ |
| 840 | 'goal' => [ |
| 841 | 'minimum' => 1, |
| 842 | 'type' => 'number', |
| 843 | ], |
| 844 | ], |
| 845 | 'errorMessage' => [ |
| 846 | 'properties' => [ |
| 847 | 'goal' => esc_html__('Number of recurring donors must be greater than 0', 'give'), |
| 848 | ], |
| 849 | ], |
| 850 | ], |
| 851 | ], |
| 852 | ], |
| 853 | ]; |
| 854 | |
| 855 | return $this->add_additional_fields_schema($schema); |
| 856 | } |
| 857 | } |
| 858 |