Actions
4 years ago
DataTransferObjects
4 years ago
Endpoints
4 years ago
Factories
4 years ago
LegacyListeners
4 years ago
Models
4 years ago
Properties
4 years ago
Repositories
4 years ago
ValueObjects
4 years ago
resources
4 years ago
DonationsAdminPage.php
4 years ago
ServiceProvider.php
4 years ago
DonationsAdminPage.php
164 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Donations; |
| 4 | |
| 5 | use Give\Helpers\EnqueueScript; |
| 6 | use WP_REST_Request; |
| 7 | |
| 8 | class DonationsAdminPage |
| 9 | { |
| 10 | /** |
| 11 | * @var string |
| 12 | */ |
| 13 | private $apiRoot; |
| 14 | |
| 15 | /** |
| 16 | * @var string |
| 17 | */ |
| 18 | private $apiNonce; |
| 19 | |
| 20 | public function __construct() |
| 21 | { |
| 22 | $this->apiRoot = esc_url_raw(rest_url('give-api/v2/admin/donations')); |
| 23 | $this->apiNonce = wp_create_nonce('wp_rest'); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @since 2.20.0 |
| 28 | */ |
| 29 | public function registerMenuItem() |
| 30 | { |
| 31 | remove_submenu_page( |
| 32 | 'edit.php?post_type=give_forms', |
| 33 | 'give-payment-history' |
| 34 | ); |
| 35 | |
| 36 | remove_action( |
| 37 | 'give_forms_page_give-payment-history', |
| 38 | 'give_payment_history_page' |
| 39 | ); |
| 40 | |
| 41 | add_submenu_page( |
| 42 | 'edit.php?post_type=give_forms', |
| 43 | esc_html__('Donations', 'give'), |
| 44 | esc_html__('Donations', 'give'), |
| 45 | 'edit_give_forms', |
| 46 | 'give-payment-history', |
| 47 | [$this, 'render'], |
| 48 | 5 |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @since 2.20.0 |
| 54 | */ |
| 55 | public function loadScripts() |
| 56 | { |
| 57 | $data = [ |
| 58 | 'apiRoot' => $this->apiRoot, |
| 59 | 'apiNonce' => $this->apiNonce, |
| 60 | 'preload' => $this->preloadDonations(), |
| 61 | 'forms' => $this->getForms(), |
| 62 | ]; |
| 63 | |
| 64 | EnqueueScript::make('give-admin-donations', 'assets/dist/js/give-admin-donations.js') |
| 65 | ->loadInFooter() |
| 66 | ->registerTranslations() |
| 67 | ->registerLocalizeData('GiveDonations', $data)->enqueue(); |
| 68 | |
| 69 | wp_enqueue_style( |
| 70 | 'give-admin-ui-font', |
| 71 | 'https://fonts.googleapis.com/css2?family=Open+Sans:wght@400..700&display=swap', |
| 72 | [], |
| 73 | null |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Render admin page container |
| 79 | * @since 2.20.0 |
| 80 | */ |
| 81 | public function render() |
| 82 | { |
| 83 | if (isset($_GET['view']) && 'view-payment-details' === $_GET['view']) { |
| 84 | include GIVE_PLUGIN_DIR . 'includes/admin/payments/view-payment-details.php'; |
| 85 | } else { |
| 86 | echo '<div id="give-admin-donations-root"></div>'; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Helper function to determine if current page is Give Donors admin page |
| 92 | * @since 2.20.0 |
| 93 | * |
| 94 | * @return bool |
| 95 | */ |
| 96 | public static function isShowing() |
| 97 | { |
| 98 | return isset($_GET['page']) && $_GET['page'] === 'give-payment-history' && !isset($_GET['view']); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | /** |
| 103 | * Get first page of results from REST API to display as initial table data |
| 104 | * |
| 105 | * @since 2.20.0 |
| 106 | * @return array |
| 107 | */ |
| 108 | private function preloadDonations() |
| 109 | { |
| 110 | $queryParameters = [ |
| 111 | 'page' => 1, |
| 112 | 'perPage' => 30, |
| 113 | ]; |
| 114 | |
| 115 | if(isset($_GET['search'])) |
| 116 | { |
| 117 | $queryParameters['search'] = urldecode($_GET['search']); |
| 118 | } |
| 119 | |
| 120 | $request = WP_REST_Request::from_url(add_query_arg( |
| 121 | $queryParameters, |
| 122 | $this->apiRoot |
| 123 | )); |
| 124 | |
| 125 | return rest_do_request($request)->get_data(); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Retrieve a list of donation forms to populate the form filter dropdown |
| 130 | * |
| 131 | * @since 2.20.0 |
| 132 | * @return array |
| 133 | */ |
| 134 | private function getForms() |
| 135 | { |
| 136 | $queryParameters = [ |
| 137 | 'page' => 1, |
| 138 | 'perPage' => 50, |
| 139 | 'status' => 'any' |
| 140 | ]; |
| 141 | |
| 142 | $request = WP_REST_Request::from_url(add_query_arg( |
| 143 | $queryParameters, |
| 144 | esc_url_raw(rest_url('give-api/v2/admin/forms')) |
| 145 | )); |
| 146 | |
| 147 | $data = rest_do_request($request)->get_data(); |
| 148 | |
| 149 | $options = array_map(static function ($form) { |
| 150 | return [ |
| 151 | 'value' => $form['id'], |
| 152 | 'text' => $form['name'], |
| 153 | ]; |
| 154 | }, $data['items']); |
| 155 | |
| 156 | return array_merge([ |
| 157 | [ |
| 158 | 'value' => '0', |
| 159 | 'text' => 'Any', |
| 160 | ] |
| 161 | ], $options); |
| 162 | } |
| 163 | } |
| 164 |