Actions
11 months ago
CustomFields
1 year ago
DataTransferObjects
1 year ago
Endpoints
1 year ago
Exceptions
2 years ago
Factories
1 year ago
ListTable
1 year ago
Migrations
1 year ago
Models
1 year ago
Repositories
1 year ago
ValueObjects
1 year ago
ViewModels
11 months ago
resources
11 months ago
DonorStatisticsQuery.php
1 year ago
DonorsAdminPage.php
11 months ago
DonorsQuery.php
1 year ago
ServiceProvider.php
11 months ago
DonorsAdminPage.php
114 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Donors; |
| 4 | |
| 5 | use Give\Donors\Actions\LoadDonorDetailsAssets; |
| 6 | use Give\Donors\Actions\LoadDonorsListTableAssets; |
| 7 | use Give\Donors\Models\Donor; |
| 8 | |
| 9 | class DonorsAdminPage |
| 10 | { |
| 11 | /** |
| 12 | * @since 2.20.0 |
| 13 | */ |
| 14 | public function registerMenuItem() |
| 15 | { |
| 16 | remove_submenu_page( |
| 17 | 'edit.php?post_type=give_forms', |
| 18 | 'give-donors' |
| 19 | ); |
| 20 | |
| 21 | add_submenu_page( |
| 22 | 'edit.php?post_type=give_forms', |
| 23 | esc_html__('Donors', 'give'), |
| 24 | esc_html__('Donors', 'give'), |
| 25 | 'edit_give_forms', |
| 26 | 'give-donors', |
| 27 | [$this, 'render'] |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Render admin page container |
| 33 | * |
| 34 | * @since 4.4.0 Add new details page view |
| 35 | * @since 2.20.0 |
| 36 | */ |
| 37 | public function render() |
| 38 | { |
| 39 | if (self::isShowingDetailsPage()) { |
| 40 | $donor = Donor::find(absint($_GET['id'])); |
| 41 | |
| 42 | if ( ! $donor) { |
| 43 | wp_die(__('Donor not found', 'give'), 404); |
| 44 | } |
| 45 | |
| 46 | give(LoadDonorDetailsAssets::class)(); |
| 47 | } elseif (self::isShowing()) { |
| 48 | give(LoadDonorsListTableAssets::class)(); |
| 49 | } |
| 50 | |
| 51 | echo '<div id="give-admin-donors-root"></div>'; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Display a button on the old donation forms table that switches to the React view |
| 56 | * |
| 57 | * @since 2.20.0 |
| 58 | */ |
| 59 | public function renderReactSwitch() |
| 60 | { |
| 61 | ?> |
| 62 | <script type="text/javascript"> |
| 63 | function showReactTable() { |
| 64 | fetch('<?php echo esc_url_raw(rest_url('give-api/v2/admin/donors/view?isLegacy=0')) ?>', { |
| 65 | method: 'GET', |
| 66 | headers: { |
| 67 | ['X-WP-Nonce']: '<?php echo wp_create_nonce('wp_rest') ?>', |
| 68 | }, |
| 69 | }) |
| 70 | .then((res) => { |
| 71 | window.location.reload(); |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | jQuery(function () { |
| 76 | jQuery(jQuery(".wrap .wp-header-end")).before( |
| 77 | '<button class="page-title-action" onclick="showReactTable()"><?php _e('Switch to New View', 'give') ?></button>', |
| 78 | ); |
| 79 | }); |
| 80 | </script> |
| 81 | <?php |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Helper function to determine if current page is Give Donors admin page |
| 86 | * @since 2.20.0 |
| 87 | * |
| 88 | * @return bool |
| 89 | */ |
| 90 | public static function isShowing() |
| 91 | { |
| 92 | return isset($_GET['page']) && $_GET['page'] === 'give-donors' && ! isset($_GET['id']); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /** |
| 97 | * @since 4.4.0 |
| 98 | */ |
| 99 | public static function isShowingDetailsPage(): bool |
| 100 | { |
| 101 | return isset($_GET['id'], $_GET['page']) && 'give-donors' === $_GET['page']; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Get the URL for the details page |
| 106 | * |
| 107 | * @since 4.4.0 |
| 108 | */ |
| 109 | public static function getDetailsPageUrl(int $donorId): string |
| 110 | { |
| 111 | return admin_url("edit.php?post_type=give_forms&page=give-donors&id=$donorId"); |
| 112 | } |
| 113 | } |
| 114 |