Actions
4 years ago
DataTransferObjects
3 years ago
Endpoints
3 years ago
Factories
3 years ago
LegacyListeners
3 years ago
ListTable
3 years ago
Migrations
3 years ago
Models
3 years ago
Properties
3 years ago
Repositories
3 years ago
ValueObjects
3 years ago
resources
3 years ago
DonationsAdminPage.php
3 years ago
ServiceProvider.php
3 years ago
DonationsAdminPage.php
141 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Donations; |
| 4 | |
| 5 | use Give\Donations\ListTable\DonationsListTable; |
| 6 | use Give\Framework\Database\DB; |
| 7 | use Give\Helpers\EnqueueScript; |
| 8 | use Give\Helpers\Utils; |
| 9 | |
| 10 | class DonationsAdminPage |
| 11 | { |
| 12 | /** |
| 13 | * @var string |
| 14 | */ |
| 15 | private $apiRoot; |
| 16 | |
| 17 | /** |
| 18 | * @var string |
| 19 | */ |
| 20 | private $apiNonce; |
| 21 | |
| 22 | /** |
| 23 | * @var string |
| 24 | */ |
| 25 | private $adminUrl; |
| 26 | |
| 27 | public function __construct() |
| 28 | { |
| 29 | $this->apiRoot = esc_url_raw(rest_url('give-api/v2/admin/donations')); |
| 30 | $this->apiNonce = wp_create_nonce('wp_rest'); |
| 31 | $this->adminUrl = admin_url(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @since 2.20.0 |
| 36 | */ |
| 37 | public function registerMenuItem() |
| 38 | { |
| 39 | remove_submenu_page( |
| 40 | 'edit.php?post_type=give_forms', |
| 41 | 'give-payment-history' |
| 42 | ); |
| 43 | |
| 44 | remove_action( |
| 45 | 'give_forms_page_give-payment-history', |
| 46 | 'give_payment_history_page' |
| 47 | ); |
| 48 | |
| 49 | add_submenu_page( |
| 50 | 'edit.php?post_type=give_forms', |
| 51 | esc_html__('Donations', 'give'), |
| 52 | esc_html__('Donations', 'give'), |
| 53 | 'edit_give_forms', |
| 54 | 'give-payment-history', |
| 55 | [$this, 'render'] |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @since 2.27.0 Adds "addonsBulkActions" to the GiveDonations object |
| 61 | * @since 2.24.0 Add ListTable columns |
| 62 | * @since 2.20.0 |
| 63 | * @since 2.21.2 Localized the admin URL as a base for URL concatenation. |
| 64 | */ |
| 65 | public function loadScripts() |
| 66 | { |
| 67 | $data = [ |
| 68 | 'apiRoot' => $this->apiRoot, |
| 69 | 'apiNonce' => $this->apiNonce, |
| 70 | 'forms' => $this->getForms(), |
| 71 | 'table' => give(DonationsListTable::class)->toArray(), |
| 72 | 'adminUrl' => $this->adminUrl, |
| 73 | 'paymentMode' => give_is_test_mode(), |
| 74 | 'manualDonations' => Utils::isPluginActive('give-manual-donations/give-manual-donations.php'), |
| 75 | 'pluginUrl' => GIVE_PLUGIN_URL, |
| 76 | 'addonsBulkActions' => [], |
| 77 | ]; |
| 78 | |
| 79 | EnqueueScript::make('give-admin-donations', 'assets/dist/js/give-admin-donations.js') |
| 80 | ->loadInFooter() |
| 81 | ->registerTranslations() |
| 82 | ->registerLocalizeData('GiveDonations', $data)->enqueue(); |
| 83 | |
| 84 | wp_enqueue_style( |
| 85 | 'give-admin-ui-font', |
| 86 | 'https://fonts.googleapis.com/css2?family=Open+Sans:wght@400..700&display=swap', |
| 87 | [], |
| 88 | null |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Render admin page container |
| 94 | * @since 2.20.0 |
| 95 | */ |
| 96 | public function render() |
| 97 | { |
| 98 | if (isset($_GET['view']) && 'view-payment-details' === $_GET['view']) { |
| 99 | include GIVE_PLUGIN_DIR . 'includes/admin/payments/view-payment-details.php'; |
| 100 | } else { |
| 101 | echo '<div id="give-admin-donations-root"></div>'; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Helper function to determine if current page is Give Donors admin page |
| 107 | * @since 2.20.0 |
| 108 | * |
| 109 | * @return bool |
| 110 | */ |
| 111 | public static function isShowing() |
| 112 | { |
| 113 | return isset($_GET['page']) && $_GET['page'] === 'give-payment-history' && !isset($_GET['view']); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Retrieve a list of donation forms to populate the form filter dropdown |
| 118 | * |
| 119 | * @since 2.20.0 |
| 120 | * @return array |
| 121 | */ |
| 122 | private function getForms() |
| 123 | { |
| 124 | $options = DB::table('posts') |
| 125 | ->select( |
| 126 | ['ID', 'value'], |
| 127 | ['post_title', 'text'] |
| 128 | ) |
| 129 | ->where('post_type', 'give_forms') |
| 130 | ->whereIn('post_status', ['publish', 'draft', 'pending', 'private']) |
| 131 | ->getAll(ARRAY_A); |
| 132 | |
| 133 | return array_merge([ |
| 134 | [ |
| 135 | 'value' => '0', |
| 136 | 'text' => 'Any', |
| 137 | ], |
| 138 | ], $options); |
| 139 | } |
| 140 | } |
| 141 |