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