DonateController.php
2 years ago
DonationConfirmationReceiptViewController.php
1 year ago
DonationFormViewController.php
3 months ago
DonationFormsRequestController.php
5 months ago
DonationFormsRequestController.php
149 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms\Controllers; |
| 4 | |
| 5 | use Exception; |
| 6 | use Give\Campaigns\Repositories\CampaignRepository; |
| 7 | use Give\Campaigns\ValueObjects\CampaignType; |
| 8 | use Give\DonationForms\Models\DonationForm; |
| 9 | use Give\DonationForms\Routes\Permissions\DonationFormPermissions; |
| 10 | use Give\DonationForms\ValueObjects\DonationFormStatus; |
| 11 | use Give\DonationForms\ValueObjects\DonationFormsRoute; |
| 12 | use Give\Framework\QueryBuilder\QueryBuilder; |
| 13 | use WP_Error; |
| 14 | use WP_REST_Request; |
| 15 | use WP_REST_Response; |
| 16 | |
| 17 | /** |
| 18 | * @since 4.2.0 |
| 19 | */ |
| 20 | class DonationFormsRequestController |
| 21 | { |
| 22 | /** |
| 23 | * @since 4.10.1 Added status check to ensure non-authorized users can only access published forms |
| 24 | * @since 4.2.0 |
| 25 | */ |
| 26 | public function getForm(WP_REST_Request $request) |
| 27 | { |
| 28 | $form = DonationForm::find($request->get_param('id')); |
| 29 | |
| 30 | if ( ! $form) { |
| 31 | return new WP_REST_Response(__('Form not found', 'give'), 404); |
| 32 | } |
| 33 | |
| 34 | if (!$form->status->isPublished() && !DonationFormPermissions::canViewPrivate()) { |
| 35 | return new WP_Error( |
| 36 | 'rest_forbidden', |
| 37 | __('You do not have permission to view this donation form.', 'give'), |
| 38 | ['status' => DonationFormPermissions::authorizationStatusCode()] |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | return new WP_REST_Response($form->toArray()); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @since 4.2.0 |
| 47 | */ |
| 48 | public function getForms(WP_REST_Request $request): WP_REST_Response |
| 49 | { |
| 50 | $ids = $request->get_param('ids'); |
| 51 | $page = $request->get_param('page'); |
| 52 | $perPage = $request->get_param('per_page'); |
| 53 | $status = $request->get_param('status'); |
| 54 | |
| 55 | $query = DonationForm::query(); |
| 56 | |
| 57 | if ( ! in_array('orphaned', $status)) { |
| 58 | $query->whereIn('post_status', $status); |
| 59 | } else { |
| 60 | // get orphaned forms only |
| 61 | $query |
| 62 | ->whereNotIn('ID', function (QueryBuilder $builder) { |
| 63 | $builder |
| 64 | ->from('give_campaign_forms') |
| 65 | ->select('form_id'); |
| 66 | }) |
| 67 | // p2p forms |
| 68 | ->whereNotIn('ID', function (QueryBuilder $builder) { |
| 69 | $builder |
| 70 | ->from('give_campaigns') |
| 71 | ->select('form_id') |
| 72 | ->where('campaign_type', CampaignType::CORE, '!='); |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | if ( ! empty($ids)) { |
| 77 | $query->whereIn('id', $ids); |
| 78 | } |
| 79 | |
| 80 | $totalQuery = clone $query; |
| 81 | |
| 82 | $query |
| 83 | ->limit($perPage) |
| 84 | ->offset(($page - 1) * $perPage); |
| 85 | |
| 86 | $forms = $query->getAll() ?? []; |
| 87 | $totalForms = empty($forms) ? 0 : $totalQuery->count(); |
| 88 | $totalPages = $totalForms === 0 ? 0 : (int)ceil($totalForms / $perPage); |
| 89 | |
| 90 | $forms = array_map(function ($form) { |
| 91 | return $form->toArray(); |
| 92 | }, $forms); |
| 93 | |
| 94 | $response = rest_ensure_response($forms); |
| 95 | $response->header('X-WP-Total', $totalForms); |
| 96 | $response->header('X-WP-TotalPages', $totalPages); |
| 97 | |
| 98 | $base = add_query_arg( |
| 99 | map_deep($request->get_query_params(), function ($value) { |
| 100 | if (is_bool($value)) { |
| 101 | $value = $value ? 'true' : 'false'; |
| 102 | } |
| 103 | |
| 104 | return urlencode($value); |
| 105 | }), |
| 106 | rest_url(DonationFormsRoute::FORMS) |
| 107 | ); |
| 108 | |
| 109 | if ($page > 1) { |
| 110 | $prevPage = $page - 1; |
| 111 | |
| 112 | if ($prevPage > $totalPages) { |
| 113 | $prevPage = $totalPages; |
| 114 | } |
| 115 | |
| 116 | $response->link_header('prev', add_query_arg('page', $prevPage, $base)); |
| 117 | } |
| 118 | |
| 119 | if ($totalPages > $page) { |
| 120 | $nextPage = $page + 1; |
| 121 | $response->link_header('next', add_query_arg('page', $nextPage, $base)); |
| 122 | } |
| 123 | |
| 124 | return $response; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @since 4.2.0 |
| 129 | * |
| 130 | * @throws Exception |
| 131 | */ |
| 132 | public function associateFormsWithCampaign(WP_REST_Request $request): WP_REST_Response |
| 133 | { |
| 134 | $formIDs = $request->get_param('formIDs'); |
| 135 | $campaignId = $request->get_param('campaignId'); |
| 136 | $campaignRepository = give(CampaignRepository::class); |
| 137 | |
| 138 | if ($campaign = $campaignRepository->getById($campaignId)) { |
| 139 | foreach ($formIDs as $formID) { |
| 140 | $campaignRepository->addCampaignForm($campaign, $formID); |
| 141 | } |
| 142 | |
| 143 | return new WP_REST_Response($formIDs); |
| 144 | } |
| 145 | |
| 146 | return new WP_REST_Response('Campaign not found', 404); |
| 147 | } |
| 148 | } |
| 149 |