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