Reports.php
118 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 | |
| 13 | defined('ABSPATH') || exit; |
| 14 | |
| 15 | /** |
| 16 | * Manages reports admin page |
| 17 | */ |
| 18 | class Reports |
| 19 | { |
| 20 | /** |
| 21 | * Initialize Reports Admin page |
| 22 | */ |
| 23 | public function init() |
| 24 | { |
| 25 | add_action('admin_menu', [$this, 'add_page']); |
| 26 | add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']); |
| 27 | } |
| 28 | |
| 29 | public function __construct() |
| 30 | { |
| 31 | // Do nothing |
| 32 | } |
| 33 | |
| 34 | // Enqueue app scripts |
| 35 | public function enqueue_scripts($base) |
| 36 | { |
| 37 | if ($base !== 'give_forms_page_give-reports') { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | if (isset($_GET['legacy'])) { |
| 42 | $script = " |
| 43 | jQuery(document).ready(() => { |
| 44 | const anchors = [].slice.call(document.querySelectorAll('a[href*=give-reports]')); |
| 45 | anchors.forEach((anchor) => { |
| 46 | if (anchor.getAttribute('id') === 'new-reports-link') { |
| 47 | return; |
| 48 | } |
| 49 | anchor.setAttribute('href', anchor.getAttribute('href') + '&legacy=true'); |
| 50 | }); |
| 51 | }); |
| 52 | "; |
| 53 | wp_add_inline_script('jquery', $script); |
| 54 | |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | $data = [ |
| 59 | 'legacyReportsUrl' => admin_url('/edit.php?post_type=give_forms&page=give-reports&legacy=true'), |
| 60 | 'allTimeStart' => $this->get_all_time_start(), |
| 61 | 'currencies' => array_keys(give_get_currencies_list()), |
| 62 | 'currency' => give_get_currency(), |
| 63 | 'testMode' => give_is_test_mode(), |
| 64 | ]; |
| 65 | |
| 66 | EnqueueScript::make('give-admin-reports-v3-js', 'assets/dist/js/admin-reports.js') |
| 67 | ->loadInFooter() |
| 68 | ->dependencies(['wp-api']) |
| 69 | ->registerTranslations() |
| 70 | ->registerLocalizeData('giveReportsData', $data)->enqueue(); |
| 71 | } |
| 72 | |
| 73 | // Add Reports submenu page to admin menu |
| 74 | public function add_page() |
| 75 | { |
| 76 | $render = [$this, 'render_template']; |
| 77 | if (isset($_GET['legacy'])) { |
| 78 | $render = [Give()->give_settings, 'output']; |
| 79 | } |
| 80 | |
| 81 | add_submenu_page( |
| 82 | 'edit.php?post_type=give_forms', |
| 83 | esc_html__('Donation Reports', 'give'), |
| 84 | esc_html__('Reports', 'give'), |
| 85 | 'view_give_reports', |
| 86 | 'give-reports', |
| 87 | $render |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | public function render_template() |
| 92 | { |
| 93 | include_once GIVE_PLUGIN_DIR . 'src/Views/Admin/Pages/templates/reports-template.php'; |
| 94 | } |
| 95 | |
| 96 | public function get_all_time_start() |
| 97 | { |
| 98 | $start = date_create('01/01/2015'); |
| 99 | $end = date_create(); |
| 100 | |
| 101 | // Setup donation query args (get sanitized start/end date from request) |
| 102 | $args = [ |
| 103 | 'number' => 1, |
| 104 | 'paged' => 1, |
| 105 | 'orderby' => 'date', |
| 106 | 'order' => 'ASC', |
| 107 | 'start_date' => $start->format('Y-m-d H:i:s'), |
| 108 | 'end_date' => $end->format('Y-m-d H:i:s'), |
| 109 | ]; |
| 110 | |
| 111 | // Get array of 50 recent donations |
| 112 | $donations = new \Give_Payments_Query($args); |
| 113 | $donations = $donations->get_payments(); |
| 114 | |
| 115 | return isset($donations[0]) ? $donations[0]->date : $start->format('Y-m-d H:i:s'); |
| 116 | } |
| 117 | } |
| 118 |