Reports.php
154 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Reports Page class |
| 5 | * |
| 6 | * @package Give |
| 7 | */ |
| 8 | |
| 9 | namespace Give\Views\Admin\Pages; |
| 10 | |
| 11 | use Give\Helpers\EnqueueScript; |
| 12 | use Give\Helpers\Utils; |
| 13 | use Give\Framework\Permissions\Facades\UserPermissions; |
| 14 | defined('ABSPATH') || exit; |
| 15 | |
| 16 | /** |
| 17 | * Manages reports admin page |
| 18 | */ |
| 19 | class Reports |
| 20 | { |
| 21 | /** |
| 22 | * Initialize Reports Admin page |
| 23 | */ |
| 24 | public function init() |
| 25 | { |
| 26 | add_action('admin_menu', [$this, 'add_page'], 40); |
| 27 | add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']); |
| 28 | } |
| 29 | |
| 30 | // Enqueue app scripts |
| 31 | public function enqueue_scripts($base) |
| 32 | { |
| 33 | if ($base !== 'give_forms_page_give-reports') { |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | if (isset($_GET['legacy'])) { |
| 38 | $script = " |
| 39 | jQuery(document).ready(() => { |
| 40 | const anchors = [].slice.call(document.querySelectorAll('a[href*=give-reports]')); |
| 41 | anchors.forEach((anchor) => { |
| 42 | if (anchor.getAttribute('id') === 'new-reports-link') { |
| 43 | return; |
| 44 | } |
| 45 | anchor.setAttribute('href', anchor.getAttribute('href') + '&legacy=true'); |
| 46 | }); |
| 47 | }); |
| 48 | "; |
| 49 | wp_add_inline_script('jquery', $script); |
| 50 | |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | wp_enqueue_style('givewp-design-system-foundation'); |
| 55 | |
| 56 | $data = [ |
| 57 | 'legacyReportsUrl' => admin_url('/edit.php?post_type=give_forms&page=give-reports&legacy=true'), |
| 58 | 'allTimeStart' => $this->get_all_time_start(), |
| 59 | 'currencies' => array_keys(give_get_currencies_list()), |
| 60 | 'currency' => give_get_currency(), |
| 61 | 'testMode' => give_is_test_mode(), |
| 62 | 'pluginUrl' => GIVE_PLUGIN_URL, |
| 63 | 'dismissedRecommendations' => $this->getDismissedRecommendations(), |
| 64 | 'apiRoot' => esc_url_raw(rest_url('give-api/v2/reports')), |
| 65 | 'apiNonce' => wp_create_nonce('wp_rest'), |
| 66 | ]; |
| 67 | |
| 68 | EnqueueScript::make('give-admin-reports-v3-js', 'build/assets/dist/js/admin-reports.js') |
| 69 | ->loadInFooter() |
| 70 | ->dependencies(['wp-api']) |
| 71 | ->registerTranslations() |
| 72 | ->registerLocalizeData('giveReportsData', $data)->enqueue(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Add Reports submenu page to admin menu |
| 77 | * @since 4.14.0 update permission capability to use facade |
| 78 | */ |
| 79 | public function add_page() |
| 80 | { |
| 81 | $render = [$this, 'render_template']; |
| 82 | if (isset($_GET['legacy'])) { |
| 83 | $render = [Give()->give_settings, 'output']; |
| 84 | } |
| 85 | |
| 86 | add_submenu_page( |
| 87 | 'edit.php?post_type=give_forms', |
| 88 | esc_html__('Donation Reports', 'give'), |
| 89 | esc_html__('Reports', 'give'), |
| 90 | UserPermissions::reports()->viewCap(), |
| 91 | 'give-reports', |
| 92 | $render |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | public function render_template() |
| 97 | { |
| 98 | include_once GIVE_PLUGIN_DIR . 'src/Views/Admin/Pages/templates/reports-template.php'; |
| 99 | } |
| 100 | |
| 101 | public function get_all_time_start() |
| 102 | { |
| 103 | $start = date_create('01/01/2015'); |
| 104 | $end = date_create(); |
| 105 | |
| 106 | // Setup donation query args (get sanitized start/end date from request) |
| 107 | $args = [ |
| 108 | 'number' => 1, |
| 109 | 'paged' => 1, |
| 110 | 'orderby' => 'date', |
| 111 | 'order' => 'ASC', |
| 112 | 'start_date' => $start->format('Y-m-d H:i:s'), |
| 113 | 'end_date' => $end->format('Y-m-d H:i:s'), |
| 114 | ]; |
| 115 | |
| 116 | // Get array of 50 recent donations |
| 117 | $donations = new \Give_Payments_Query($args); |
| 118 | $donations = $donations->get_payments(); |
| 119 | |
| 120 | return isset($donations[0]) ? $donations[0]->date : $start->format('Y-m-d H:i:s'); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Retrieve a list of dismissed recommendations. |
| 125 | * |
| 126 | * @since 2.27.1 |
| 127 | * |
| 128 | * @return array |
| 129 | */ |
| 130 | private function getDismissedRecommendations(): array |
| 131 | { |
| 132 | $dismissedRecommendations = []; |
| 133 | |
| 134 | $recurringAddonIsActive = Utils::isPluginActive('give-recurring/give-recurring.php'); |
| 135 | $feeRecoveryAddonIsActive = Utils::isPluginActive('give-fee-recovery/give-fee-recovery.php'); |
| 136 | |
| 137 | $optionNames = [ |
| 138 | 'givewp_reports_recurring_recommendation_dismissed' => $recurringAddonIsActive, |
| 139 | 'givewp_reports_fee_recovery_recommendation_dismissed' => $feeRecoveryAddonIsActive, |
| 140 | ]; |
| 141 | |
| 142 | foreach ($optionNames as $optionName => $isActive) { |
| 143 | $dismissed = get_option($optionName, false); |
| 144 | if ($dismissed || $isActive) { |
| 145 | $dismissedRecommendations[] = $optionName; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return $dismissedRecommendations; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | |
| 154 |