← Back
Editing: SubscriptionsAdminPage.php
<?php namespace Give\Subscriptions; use Give\Subscriptions\Actions\LoadSubscriptionDetailsAssets; use Give\Subscriptions\Actions\LoadSubscriptionsListTableAssets; use Give\Subscriptions\Models\Subscription; class SubscriptionsAdminPage { /** * @since 2.24.0 */ public function loadScripts() { give(LoadSubscriptionsListTableAssets::class)(); } /** * Render the Subscription Details page. * * @since 4.19.1 Escape output. Guard the id param. * @since 4.8.0 */ public function render() { if (self::isShowingDetailsPage()) { remove_action('give_forms_page_give-subscriptions', 'give_subscriptions_page'); $subscription = Subscription::find(absint($_GET['id'] ?? 0)); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only id param; it only selects which record to show and saves nothing. if ( ! $subscription) { wp_die(esc_html__('Subscription not found', 'give'), 404); } give(LoadSubscriptionDetailsAssets::class)(); } else { give(LoadSubscriptionsListTableAssets::class)(); } echo '<div id="give-admin-subscriptions-root"></div>'; } /** * Display a button on the old subscriptions table that switches to the React view * * @since 4.19.1 Escape translated output. * @since 2.24.0 */ public function renderReactSwitch() { ?> <script type="text/javascript"> function showReactTable () { fetch( '<?php echo esc_url_raw(rest_url('give-api/v2/admin/subscriptions/view?isLegacy=0')) ?>', { method: 'GET', headers: { ['X-WP-Nonce']: '<?php echo esc_js(wp_create_nonce('wp_rest')) ?>' } }) .then((res) => { window.location.reload(); }); } jQuery( function() { jQuery(jQuery(".wrap .wp-header-end")).before( '<button class="page-title-action" onclick="showReactTable()"><?php echo esc_js(__('Switch to New View', 'give')) ?></button>' ); }); </script> <?php } /** * Helper function to determine if current page is Give Subscriptions admin page * * @since 2.24.0 * * @return bool */ public static function isShowing() { return isset($_GET['page']) && $_GET['page'] === 'give-subscriptions' && ! isset($_GET['view']); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only page check; it only decides what to show and saves nothing. } /** * @since 4.8.0 */ public static function isShowingDetailsPage(): bool { return isset($_GET['id'], $_GET['page']) && 'give-subscriptions' === $_GET['page']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only page check; it only decides what to show and saves nothing. } }
Save File
Cancel