Actions
4 years ago
CustomFields
2 years ago
DataTransferObjects
2 years ago
Endpoints
2 years ago
Factories
3 years ago
LegacyListeners
3 years ago
ListTable
3 years ago
Migrations
1 year ago
Models
2 years ago
Properties
3 years ago
Repositories
1 year ago
ValueObjects
1 year ago
resources
1 year ago
DonationsAdminPage.php
1 year ago
ServiceProvider.php
1 year ago
DonationsAdminPage.php
176 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.1 Add dismissed recommendations |
| 61 | * @since 2.27.0 Adds "addonsBulkActions" to the GiveDonations object |
| 62 | * @since 2.24.0 Add ListTable columns |
| 63 | * @since 2.20.0 |
| 64 | * @since 2.21.2 Localized the admin URL as a base for URL concatenation. |
| 65 | */ |
| 66 | public function loadScripts() |
| 67 | { |
| 68 | $data = [ |
| 69 | 'apiRoot' => $this->apiRoot, |
| 70 | 'apiNonce' => $this->apiNonce, |
| 71 | 'forms' => $this->getForms(), |
| 72 | 'table' => give(DonationsListTable::class)->toArray(), |
| 73 | 'adminUrl' => $this->adminUrl, |
| 74 | 'paymentMode' => give_is_test_mode(), |
| 75 | 'manualDonations' => Utils::isPluginActive('give-manual-donations/give-manual-donations.php'), |
| 76 | 'pluginUrl' => GIVE_PLUGIN_URL, |
| 77 | 'dismissedRecommendations' => $this->getDismissedRecommendations(), |
| 78 | 'addonsBulkActions' => [], |
| 79 | ]; |
| 80 | |
| 81 | EnqueueScript::make('give-admin-donations', 'assets/dist/js/give-admin-donations.js') |
| 82 | ->loadInFooter() |
| 83 | ->registerTranslations() |
| 84 | ->registerLocalizeData('GiveDonations', $data)->enqueue(); |
| 85 | |
| 86 | wp_enqueue_style( |
| 87 | 'give-admin-ui-font', |
| 88 | 'https://fonts.googleapis.com/css2?family=Open+Sans:wght@400..700&display=swap', |
| 89 | [], |
| 90 | null |
| 91 | ); |
| 92 | |
| 93 | wp_enqueue_style('givewp-design-system-foundation'); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Render admin page container |
| 98 | * @since 2.20.0 |
| 99 | */ |
| 100 | public function render() |
| 101 | { |
| 102 | if (isset($_GET['view']) && 'view-payment-details' === $_GET['view']) { |
| 103 | include GIVE_PLUGIN_DIR . 'includes/admin/payments/view-payment-details.php'; |
| 104 | } else { |
| 105 | echo '<div id="give-admin-donations-root"></div>'; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Helper function to determine if current page is Give Donors admin page |
| 111 | * @since 2.20.0 |
| 112 | * |
| 113 | * @return bool |
| 114 | */ |
| 115 | public static function isShowing() |
| 116 | { |
| 117 | return isset($_GET['page']) && $_GET['page'] === 'give-payment-history' && ! isset($_GET['view']); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Retrieve a list of donation forms to populate the form filter dropdown |
| 122 | * |
| 123 | * @since 2.20.0 |
| 124 | * @return array |
| 125 | */ |
| 126 | private function getForms() |
| 127 | { |
| 128 | $options = DB::table('posts') |
| 129 | ->select( |
| 130 | ['ID', 'value'], |
| 131 | ['post_title', 'text'] |
| 132 | ) |
| 133 | ->where('post_type', 'give_forms') |
| 134 | ->whereIn('post_status', ['publish', 'draft', 'pending', 'private']) |
| 135 | ->getAll(ARRAY_A); |
| 136 | |
| 137 | return array_merge([ |
| 138 | [ |
| 139 | 'value' => '0', |
| 140 | 'text' => __('Any', 'give'), |
| 141 | ], |
| 142 | ], $options); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Retrieve a list of dismissed recommendations. |
| 147 | * |
| 148 | * @since 2.27.1 |
| 149 | * |
| 150 | * @return array |
| 151 | */ |
| 152 | private function getDismissedRecommendations(): array |
| 153 | { |
| 154 | $dismissedRecommendations = []; |
| 155 | |
| 156 | $recurringAddonIsActive = Utils::isPluginActive('give-recurring/give-recurring.php'); |
| 157 | $feeRecoveryAddonIsActive = Utils::isPluginActive('give-fee-recovery/give-fee-recovery.php'); |
| 158 | $designatedFundsAddonIsActive = Utils::isPluginActive('give-funds/give-funds.php'); |
| 159 | |
| 160 | $optionNames = [ |
| 161 | 'givewp_donations_recurring_recommendation_dismissed' => $recurringAddonIsActive, |
| 162 | 'givewp_donations_fee_recovery_recommendation_dismissed' => $feeRecoveryAddonIsActive, |
| 163 | 'givewp_donations_designated_funds_recommendation_dismissed' => $designatedFundsAddonIsActive, |
| 164 | ]; |
| 165 | |
| 166 | foreach ($optionNames as $optionName => $isActive) { |
| 167 | $dismissed = get_option($optionName, false); |
| 168 | if ($dismissed || $isActive) { |
| 169 | $dismissedRecommendations[] = $optionName; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return $dismissedRecommendations; |
| 174 | } |
| 175 | } |
| 176 |