Actions
3 years ago
DataTransferObjects
3 years ago
Endpoints
2 years ago
Factories
2 years ago
LegacyListeners
3 years ago
ListTable
3 years ago
Migrations
3 years ago
Models
3 years ago
Repositories
3 years ago
ValueObjects
3 years ago
resources
3 years ago
ServiceProvider.php
3 years ago
SubscriptionsAdminPage.php
3 years ago
SubscriptionsAdminPage.php
128 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Subscriptions; |
| 4 | |
| 5 | use Give\Framework\Database\DB; |
| 6 | use Give\Helpers\EnqueueScript; |
| 7 | use Give\Subscriptions\ListTable\SubscriptionsListTable; |
| 8 | |
| 9 | class SubscriptionsAdminPage |
| 10 | { |
| 11 | /** |
| 12 | * @var string |
| 13 | */ |
| 14 | private $apiRoot; |
| 15 | |
| 16 | /** |
| 17 | * @var string |
| 18 | */ |
| 19 | private $apiNonce; |
| 20 | |
| 21 | /** |
| 22 | * @var string |
| 23 | */ |
| 24 | private $adminUrl; |
| 25 | |
| 26 | public function __construct() |
| 27 | { |
| 28 | $this->apiRoot = esc_url_raw(rest_url('give-api/v2/admin/subscriptions')); |
| 29 | $this->apiNonce = wp_create_nonce('wp_rest'); |
| 30 | $this->adminUrl = admin_url(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @since 2.24.0 |
| 35 | */ |
| 36 | public function loadScripts() |
| 37 | { |
| 38 | $data = [ |
| 39 | 'apiRoot' => $this->apiRoot, |
| 40 | 'apiNonce' => $this->apiNonce, |
| 41 | 'forms' => $this->getForms(), |
| 42 | 'table' => give(SubscriptionsListTable::class)->toArray(), |
| 43 | 'adminUrl' => $this->adminUrl, |
| 44 | 'paymentMode' => give_is_test_mode(), |
| 45 | 'pluginUrl' => GIVE_PLUGIN_URL |
| 46 | ]; |
| 47 | |
| 48 | EnqueueScript::make('give-admin-subscriptions', 'assets/dist/js/give-admin-subscriptions.js') |
| 49 | ->loadInFooter() |
| 50 | ->registerTranslations() |
| 51 | ->registerLocalizeData('GiveSubscriptions', $data)->enqueue(); |
| 52 | |
| 53 | wp_enqueue_style( |
| 54 | 'give-admin-ui-font', |
| 55 | 'https://fonts.googleapis.com/css2?family=Open+Sans:wght@400..700&display=swap', |
| 56 | [], |
| 57 | null |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Retrieve a list of donation forms to populate the form filter dropdown |
| 63 | * |
| 64 | * @since 2.24.0 |
| 65 | * |
| 66 | * @return array |
| 67 | */ |
| 68 | private function getForms() |
| 69 | { |
| 70 | $options = DB::table('posts') |
| 71 | ->select( |
| 72 | ['ID', 'value'], |
| 73 | ['post_title', 'text'] |
| 74 | ) |
| 75 | ->where('post_type', 'give_forms') |
| 76 | ->whereIn('post_status', ['publish', 'draft', 'pending', 'private']) |
| 77 | ->getAll(ARRAY_A); |
| 78 | |
| 79 | return array_merge([ |
| 80 | [ |
| 81 | 'value' => '0', |
| 82 | 'text' => 'Any', |
| 83 | ] |
| 84 | ], $options); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Display a button on the old subscriptions table that switches to the React view |
| 89 | * |
| 90 | * @since 2.24.0 |
| 91 | */ |
| 92 | public function renderReactSwitch() |
| 93 | { |
| 94 | ?> |
| 95 | <script type="text/javascript"> |
| 96 | function showReactTable () { |
| 97 | fetch( '<?php echo esc_url_raw(rest_url('give-api/v2/admin/subscriptions/view?isLegacy=0')) ?>', { |
| 98 | method: 'GET', |
| 99 | headers: { |
| 100 | ['X-WP-Nonce']: '<?php echo wp_create_nonce('wp_rest') ?>' |
| 101 | } |
| 102 | }) |
| 103 | .then((res) => { |
| 104 | window.location.reload(); |
| 105 | }); |
| 106 | } |
| 107 | jQuery( function() { |
| 108 | jQuery(jQuery(".wrap .wp-header-end")).before( |
| 109 | '<button class="page-title-action" onclick="showReactTable()">Switch to New View</button>' |
| 110 | ); |
| 111 | }); |
| 112 | </script> |
| 113 | <?php |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Helper function to determine if current page is Give Subscriptions admin page |
| 118 | * |
| 119 | * @since 2.24.0 |
| 120 | * |
| 121 | * @return bool |
| 122 | */ |
| 123 | public static function isShowing() |
| 124 | { |
| 125 | return isset($_GET['page']) && $_GET['page'] === 'give-subscriptions' && !isset($_GET['view']); |
| 126 | } |
| 127 | } |
| 128 |