| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Subscriptions; |
| 4 |
|
| 5 |
use Give\Framework\Migrations\MigrationsRegister; |
| 6 |
use Give\Helpers\Hooks; |
| 7 |
use Give\ServiceProviders\ServiceProvider as ServiceProviderInterface; |
| 8 |
use Give\Subscriptions\Actions\LoadSubscriptionAdminOptions; |
| 9 |
use Give\Subscriptions\LegacyListeners\DispatchGiveSubscriptionPostCreate; |
| 10 |
use Give\Subscriptions\LegacyListeners\DispatchGiveSubscriptionPreCreate; |
| 11 |
use Give\Subscriptions\ListTable\SubscriptionsListTable; |
| 12 |
use Give\Subscriptions\Migrations\AddCampaignId; |
| 13 |
use Give\Subscriptions\Migrations\AddCampaignIdColumn; |
| 14 |
use Give\Subscriptions\Migrations\AddPaymentModeToSubscriptionTable; |
| 15 |
use Give\Subscriptions\Migrations\BackfillMissingCampaignIdForDonations; |
| 16 |
use Give\Subscriptions\Migrations\CreateSubscriptionTables; |
| 17 |
use Give\Subscriptions\Migrations\UpdateProductID; |
| 18 |
use Give\Subscriptions\Repositories\SubscriptionNotesRepository; |
| 19 |
use Give\Subscriptions\Repositories\SubscriptionRepository; |
| 20 |
|
| 21 |
class ServiceProvider implements ServiceProviderInterface |
| 22 |
{ |
| 23 |
/** |
| 24 |
* @inheritDoc |
| 25 |
* |
| 26 |
* @since 4.8.0 Register Subscription Repository to container |
| 27 |
*/ |
| 28 |
public function register() |
| 29 |
{ |
| 30 |
give()->singleton('subscriptions', SubscriptionRepository::class); |
| 31 |
give()->singleton('subscriptionNotes', SubscriptionNotesRepository::class); |
| 32 |
give()->singleton(SubscriptionsListTable::class, function() { |
| 33 |
$listTable = new SubscriptionsListTable(); |
| 34 |
Hooks::doAction('givewp_subscriptions_list_table', $listTable); |
| 35 |
|
| 36 |
return $listTable; |
| 37 |
}); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @inheritDoc |
| 42 |
* |
| 43 |
* @since 4.14.0 move subscription page registration to method to defer conditionals and DB queries to appropriate hooks. |
| 44 |
*/ |
| 45 |
public function boot() |
| 46 |
{ |
| 47 |
$this->bootLegacyListeners(); |
| 48 |
$this->registerMigrations(); |
| 49 |
$this->registerSubscriptionAdminOptions(); |
| 50 |
$this->registerSubscriptionsAdminPage(); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Register the subscriptions admin page, deferring conditionals and DB queries to appropriate hooks. |
| 55 |
* |
| 56 |
* @since 4.14.0 |
| 57 |
*/ |
| 58 |
private function registerSubscriptionsAdminPage() |
| 59 |
{ |
| 60 |
// Register new admin page if user hasn't chosen to use the legacy one |
| 61 |
add_action('give_forms_page_give-subscriptions', function () { |
| 62 |
if ($this->shouldShowLegacySubscriptionsPage()) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
give(SubscriptionsAdminPage::class)->render(); |
| 67 |
}, 1); |
| 68 |
|
| 69 |
// Render the "Switch to New View" button on the legacy subscriptions page |
| 70 |
add_action('admin_head', function () { |
| 71 |
if (!SubscriptionsAdminPage::isShowing()) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
if (!$this->shouldShowLegacySubscriptionsPage()) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
give(SubscriptionsAdminPage::class)->renderReactSwitch(); |
| 80 |
}); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @since 4.14.0 |
| 85 |
*/ |
| 86 |
private function shouldShowLegacySubscriptionsPage(): bool |
| 87 |
{ |
| 88 |
$userId = get_current_user_id(); |
| 89 |
|
| 90 |
return (bool) get_user_meta($userId, '_give_subscriptions_archive_show_legacy', true); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Legacy Listeners |
| 95 |
* |
| 96 |
* @since 2.19.6 |
| 97 |
*/ |
| 98 |
private function bootLegacyListeners() |
| 99 |
{ |
| 100 |
Hooks::addAction('givewp_subscription_creating', DispatchGiveSubscriptionPreCreate::class); |
| 101 |
Hooks::addAction('givewp_subscription_created', DispatchGiveSubscriptionPostCreate::class); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Registers database migrations with the MigrationsRunner |
| 106 |
* |
| 107 |
* @since 4.11.0 add AddCampaignIdColumn and AddCampaignId migrations |
| 108 |
* @since 2.24.0 |
| 109 |
*/ |
| 110 |
private function registerMigrations() |
| 111 |
{ |
| 112 |
/** @var MigrationsRegister $register */ |
| 113 |
$register = give(MigrationsRegister::class); |
| 114 |
$register->addMigrations([ |
| 115 |
CreateSubscriptionTables::class, |
| 116 |
AddPaymentModeToSubscriptionTable::class, |
| 117 |
BackfillMissingCampaignIdForDonations::class, |
| 118 |
AddCampaignIdColumn::class, |
| 119 |
AddCampaignId::class, |
| 120 |
UpdateProductID::class |
| 121 |
]); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* @since 4.8.0 |
| 126 |
*/ |
| 127 |
private function registerSubscriptionAdminOptions() |
| 128 |
{ |
| 129 |
add_action('admin_enqueue_scripts', function() { |
| 130 |
if (SubscriptionsAdminPage::isShowing()) { |
| 131 |
give(LoadSubscriptionAdminOptions::class)(); |
| 132 |
} |
| 133 |
}); |
| 134 |
} |
| 135 |
} |
| 136 |
|