GetAsyncFormDataForListView.php
1 year ago
GiveGoalProgressStats.php
1 year ago
LoadAsyncDataAssets.php
1 year ago
GetAsyncFormDataForListView.php
100 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms\AsyncData\Actions; |
| 4 | |
| 5 | use Give\DonationForms\AsyncData\AdminFormListView\AdminFormListViewOptions; |
| 6 | use Give\DonationForms\AsyncData\AsyncDataHelpers; |
| 7 | use Give\DonationForms\AsyncData\FormGrid\FormGridViewOptions; |
| 8 | |
| 9 | /** |
| 10 | * @since 3.16.0 |
| 11 | */ |
| 12 | class GetAsyncFormDataForListView |
| 13 | { |
| 14 | /** |
| 15 | * @since 3.16.0 |
| 16 | */ |
| 17 | public function __invoke() |
| 18 | { |
| 19 | $options = give_clean($_GET); |
| 20 | |
| 21 | if ( ! isset($options['nonce']) || ! check_ajax_referer('GiveDonationFormsAsyncDataAjaxNonce', 'nonce')) { |
| 22 | wp_send_json_error([ |
| 23 | 'errorMsg' => __('The current user does not have permission to execute this operation.', 'give'), |
| 24 | ]); |
| 25 | } |
| 26 | |
| 27 | if ( ! isset($options['formId'])) { |
| 28 | wp_send_json_error(['errorMsg' => __('Missing Form ID.', 'give')]); |
| 29 | } |
| 30 | |
| 31 | $formId = absint($options['formId']); |
| 32 | if ('give_forms' !== get_post_type($formId)) { |
| 33 | wp_send_json_error(['errorMsg' => __('Invalid post type.', 'give')]); |
| 34 | } |
| 35 | |
| 36 | $transientName = 'give_async_data_for_list_view_form_' . $formId; |
| 37 | |
| 38 | $data = get_transient($transientName); |
| 39 | |
| 40 | if ($data) { |
| 41 | wp_send_json_success($data); |
| 42 | } |
| 43 | |
| 44 | $amountRaised = 0; |
| 45 | $percentComplete = 0; |
| 46 | if ($this->isAsyncProgressBar()) { |
| 47 | $goalStats = give_goal_progress_stats($formId); |
| 48 | $amountRaised = $goalStats['actual']; |
| 49 | $percentComplete = ('percentage' === $goalStats['format']) ? str_replace('%', '', |
| 50 | $goalStats['actual']) : max(min($goalStats['progress'], 100), 0); |
| 51 | } |
| 52 | |
| 53 | $donationsCount = 0; |
| 54 | if ($this->isAsyncDonationCount()) { |
| 55 | $donationsCount = AsyncDataHelpers::getFormDonationsCountValue($formId); |
| 56 | } |
| 57 | |
| 58 | $revenue = $amountRaised; |
| 59 | if (0 === $revenue && $this->isAsyncRevenue()) { |
| 60 | $revenue = AsyncDataHelpers::getFormRevenueValue($formId); |
| 61 | $revenue = give_currency_filter(give_format_amount($revenue)); |
| 62 | } |
| 63 | |
| 64 | $response = [ |
| 65 | 'amountRaised' => $amountRaised, |
| 66 | 'percentComplete' => $percentComplete, |
| 67 | 'donationsCount' => $donationsCount, |
| 68 | 'revenue' => $revenue, |
| 69 | ]; |
| 70 | |
| 71 | set_transient($transientName, $response, MINUTE_IN_SECONDS * 5); |
| 72 | |
| 73 | wp_send_json_success($response); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @since 3.16.0 |
| 78 | */ |
| 79 | private function isAsyncProgressBar(): bool |
| 80 | { |
| 81 | return AdminFormListViewOptions::isGoalColumnAsync() || FormGridViewOptions::isProgressBarAmountRaisedAsync(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @since 3.16.0 |
| 86 | */ |
| 87 | private function isAsyncDonationCount(): bool |
| 88 | { |
| 89 | return AdminFormListViewOptions::isDonationColumnAsync() || FormGridViewOptions::isProgressBarDonationsCountAsync(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @since 3.16.0 |
| 94 | */ |
| 95 | private function isAsyncRevenue(): bool |
| 96 | { |
| 97 | return AdminFormListViewOptions::isRevenueColumnAsync(); |
| 98 | } |
| 99 | } |
| 100 |