DataTransferObjects
3 years ago
Endpoints
3 years ago
Exceptions
4 years ago
Factories
3 years ago
ListTable
3 years ago
Models
3 years ago
Repositories
3 years ago
ValueObjects
3 years ago
resources
3 years ago
DonorsAdminPage.php
3 years ago
ServiceProvider.php
3 years ago
DonorsAdminPage.php
156 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Donors; |
| 4 | |
| 5 | use Give\Donors\ListTable\DonorsListTable; |
| 6 | use Give\Framework\Database\DB; |
| 7 | use Give\Helpers\EnqueueScript; |
| 8 | |
| 9 | class DonorsAdminPage |
| 10 | { |
| 11 | /** |
| 12 | * Root URL for this page's endpoints |
| 13 | * @var string |
| 14 | */ |
| 15 | private $apiRoot; |
| 16 | |
| 17 | /** |
| 18 | * Nonce for authentication with WP REST API |
| 19 | * @var string |
| 20 | */ |
| 21 | private $apiNonce; |
| 22 | |
| 23 | /** |
| 24 | * @var string |
| 25 | */ |
| 26 | private $adminUrl; |
| 27 | |
| 28 | /** |
| 29 | * @since 2.20.0 |
| 30 | */ |
| 31 | public function __construct() |
| 32 | { |
| 33 | $this->apiRoot = esc_url_raw(rest_url('give-api/v2/admin/donors')); |
| 34 | $this->apiNonce = wp_create_nonce('wp_rest'); |
| 35 | $this->adminUrl = admin_url(); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @since 2.20.0 |
| 40 | */ |
| 41 | public function registerMenuItem() |
| 42 | { |
| 43 | remove_submenu_page( |
| 44 | 'edit.php?post_type=give_forms', |
| 45 | 'give-donors' |
| 46 | ); |
| 47 | |
| 48 | add_submenu_page( |
| 49 | 'edit.php?post_type=give_forms', |
| 50 | esc_html__('Donors', 'give'), |
| 51 | esc_html__('Donors', 'give'), |
| 52 | 'edit_give_forms', |
| 53 | 'give-donors', |
| 54 | [$this, 'render'] |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @since 2.20.0 |
| 60 | */ |
| 61 | public function loadScripts() |
| 62 | { |
| 63 | $data = [ |
| 64 | 'apiRoot' => $this->apiRoot, |
| 65 | 'apiNonce' => $this->apiNonce, |
| 66 | 'forms' => $this->getForms(), |
| 67 | 'table' => give(DonorsListTable::class)->toArray(), |
| 68 | 'adminUrl' => $this->adminUrl, |
| 69 | 'pluginUrl' => GIVE_PLUGIN_URL |
| 70 | ]; |
| 71 | |
| 72 | EnqueueScript::make('give-admin-donors', 'assets/dist/js/give-admin-donors.js') |
| 73 | ->loadInFooter() |
| 74 | ->registerTranslations() |
| 75 | ->registerLocalizeData('GiveDonors', $data)->enqueue(); |
| 76 | |
| 77 | wp_enqueue_style( |
| 78 | 'give-admin-ui-font', |
| 79 | 'https://fonts.googleapis.com/css2?family=Open+Sans:wght@400..700&display=swap', |
| 80 | [], |
| 81 | null |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Preload initial table data |
| 87 | * @since 2.20.0 |
| 88 | */ |
| 89 | public function getForms(){ |
| 90 | $options = DB::table('posts') |
| 91 | ->select( |
| 92 | ['ID', 'value'], |
| 93 | ['post_title', 'text'] |
| 94 | ) |
| 95 | ->where('post_type', 'give_forms') |
| 96 | ->whereIn('post_status', ['publish', 'draft', 'pending', 'private']) |
| 97 | ->getAll(ARRAY_A); |
| 98 | |
| 99 | return array_merge([ |
| 100 | [ |
| 101 | 'value' => '0', |
| 102 | 'text' => 'Any', |
| 103 | ] |
| 104 | ], $options); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Render admin page container |
| 109 | * @since 2.20.0 |
| 110 | */ |
| 111 | public function render() |
| 112 | { |
| 113 | echo '<div id="give-admin-donors-root"></div>'; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Display a button on the old donation forms table that switches to the React view |
| 118 | * |
| 119 | * @since 2.20.0 |
| 120 | */ |
| 121 | public function renderReactSwitch() |
| 122 | { |
| 123 | ?> |
| 124 | <script type="text/javascript"> |
| 125 | function showReactTable () { |
| 126 | fetch( '<?php echo esc_url_raw(rest_url('give-api/v2/admin/donors/view?isLegacy=0')) ?>', { |
| 127 | method: 'GET', |
| 128 | headers: { |
| 129 | ['X-WP-Nonce']: '<?php echo wp_create_nonce('wp_rest') ?>' |
| 130 | } |
| 131 | }) |
| 132 | .then((res) => { |
| 133 | window.location.reload(); |
| 134 | }); |
| 135 | } |
| 136 | jQuery( function() { |
| 137 | jQuery(jQuery(".wrap .wp-header-end")).before( |
| 138 | '<button class="page-title-action" onclick="showReactTable()">Switch to New View</button>' |
| 139 | ); |
| 140 | }); |
| 141 | </script> |
| 142 | <?php |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Helper function to determine if current page is Give Donors admin page |
| 147 | * @since 2.20.0 |
| 148 | * |
| 149 | * @return bool |
| 150 | */ |
| 151 | public static function isShowing() |
| 152 | { |
| 153 | return isset($_GET['page']) && $_GET['page'] === 'give-donors' && !isset($_GET['id']); |
| 154 | } |
| 155 | } |
| 156 |