| 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', 'build/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 |
wp_enqueue_style('givewp-design-system-foundation'); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Retrieve a list of donation forms to populate the form filter dropdown |
| 65 |
* |
| 66 |
* @since 2.24.0 |
| 67 |
* |
| 68 |
* @return array |
| 69 |
*/ |
| 70 |
private function getForms() |
| 71 |
{ |
| 72 |
$options = DB::table('posts') |
| 73 |
->select( |
| 74 |
['ID', 'value'], |
| 75 |
['post_title', 'text'] |
| 76 |
) |
| 77 |
->where('post_type', 'give_forms') |
| 78 |
->whereIn('post_status', ['publish', 'draft', 'pending', 'private']) |
| 79 |
->getAll(ARRAY_A); |
| 80 |
|
| 81 |
return array_merge([ |
| 82 |
[ |
| 83 |
'value' => '0', |
| 84 |
'text' => __('Any', 'give'), |
| 85 |
] |
| 86 |
], $options); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Display a button on the old subscriptions table that switches to the React view |
| 91 |
* |
| 92 |
* @since 2.24.0 |
| 93 |
*/ |
| 94 |
public function renderReactSwitch() |
| 95 |
{ |
| 96 |
?> |
| 97 |
<script type="text/javascript"> |
| 98 |
function showReactTable () { |
| 99 |
fetch( '<?php echo esc_url_raw(rest_url('give-api/v2/admin/subscriptions/view?isLegacy=0')) ?>', { |
| 100 |
method: 'GET', |
| 101 |
headers: { |
| 102 |
['X-WP-Nonce']: '<?php echo wp_create_nonce('wp_rest') ?>' |
| 103 |
} |
| 104 |
}) |
| 105 |
.then((res) => { |
| 106 |
window.location.reload(); |
| 107 |
}); |
| 108 |
} |
| 109 |
jQuery( function() { |
| 110 |
jQuery(jQuery(".wrap .wp-header-end")).before( |
| 111 |
'<button class="page-title-action" onclick="showReactTable()"><?php _e('Switch to New View', 'give') ?></button>' |
| 112 |
); |
| 113 |
}); |
| 114 |
</script> |
| 115 |
<?php |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Helper function to determine if current page is Give Subscriptions admin page |
| 120 |
* |
| 121 |
* @since 2.24.0 |
| 122 |
* |
| 123 |
* @return bool |
| 124 |
*/ |
| 125 |
public static function isShowing() |
| 126 |
{ |
| 127 |
return isset($_GET['page']) && $_GET['page'] === 'give-subscriptions' && !isset($_GET['view']); |
| 128 |
} |
| 129 |
} |
| 130 |
|