Actions
9 months ago
DataTransferObjects
9 months ago
Endpoints
1 year ago
Factories
9 months ago
LegacyListeners
3 years ago
ListTable
1 year ago
Migrations
1 year ago
Models
9 months ago
Repositories
9 months ago
ValueObjects
9 months ago
ViewModels
9 months ago
resources
9 months ago
ServiceProvider.php
9 months ago
SubscriptionQuery.php
9 months ago
SubscriptionsAdminPage.php
9 months ago
SubscriptionsAdminPage.php
92 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Subscriptions; |
| 4 | |
| 5 | use Give\Subscriptions\Actions\LoadSubscriptionDetailsAssets; |
| 6 | use Give\Subscriptions\Actions\LoadSubscriptionsListTableAssets; |
| 7 | use Give\Subscriptions\Models\Subscription; |
| 8 | |
| 9 | class SubscriptionsAdminPage |
| 10 | { |
| 11 | /** |
| 12 | * @since 2.24.0 |
| 13 | */ |
| 14 | public function loadScripts() |
| 15 | { |
| 16 | give(LoadSubscriptionsListTableAssets::class)(); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Render the Subscription Details page. |
| 21 | * |
| 22 | * @since 4.8.0 |
| 23 | */ |
| 24 | public function render() |
| 25 | { |
| 26 | if (self::isShowingDetailsPage()) { |
| 27 | remove_action('give_forms_page_give-subscriptions', 'give_subscriptions_page'); |
| 28 | |
| 29 | $subscription = Subscription::find(absint($_GET['id'])); |
| 30 | |
| 31 | if ( ! $subscription) { |
| 32 | wp_die(__('Subscription not found', 'give'), 404); |
| 33 | } |
| 34 | |
| 35 | give(LoadSubscriptionDetailsAssets::class)(); |
| 36 | } else { |
| 37 | give(LoadSubscriptionsListTableAssets::class)(); |
| 38 | } |
| 39 | |
| 40 | echo '<div id="give-admin-subscriptions-root"></div>'; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Display a button on the old subscriptions table that switches to the React view |
| 45 | * |
| 46 | * @since 2.24.0 |
| 47 | */ |
| 48 | public function renderReactSwitch() |
| 49 | { |
| 50 | ?> |
| 51 | <script type="text/javascript"> |
| 52 | function showReactTable () { |
| 53 | fetch( '<?php echo esc_url_raw(rest_url('give-api/v2/admin/subscriptions/view?isLegacy=0')) ?>', { |
| 54 | method: 'GET', |
| 55 | headers: { |
| 56 | ['X-WP-Nonce']: '<?php echo wp_create_nonce('wp_rest') ?>' |
| 57 | } |
| 58 | }) |
| 59 | .then((res) => { |
| 60 | window.location.reload(); |
| 61 | }); |
| 62 | } |
| 63 | jQuery( function() { |
| 64 | jQuery(jQuery(".wrap .wp-header-end")).before( |
| 65 | '<button class="page-title-action" onclick="showReactTable()"><?php _e('Switch to New View', 'give') ?></button>' |
| 66 | ); |
| 67 | }); |
| 68 | </script> |
| 69 | <?php |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Helper function to determine if current page is Give Subscriptions admin page |
| 74 | * |
| 75 | * @since 2.24.0 |
| 76 | * |
| 77 | * @return bool |
| 78 | */ |
| 79 | public static function isShowing() |
| 80 | { |
| 81 | return isset($_GET['page']) && $_GET['page'] === 'give-subscriptions' && ! isset($_GET['view']); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @since 4.8.0 |
| 86 | */ |
| 87 | public static function isShowingDetailsPage(): bool |
| 88 | { |
| 89 | return isset($_GET['id'], $_GET['page']) && 'give-subscriptions' === $_GET['page']; |
| 90 | } |
| 91 | } |
| 92 |