Actions
8 months ago
DataTransferObjects
8 months ago
Endpoints
8 months ago
Factories
8 months ago
LegacyListeners
3 years ago
ListTable
7 months ago
Migrations
8 months ago
Models
8 months ago
Repositories
8 months ago
ValueObjects
8 months ago
ViewModels
9 months ago
resources
7 months ago
ServiceProvider.php
8 months ago
SubscriptionQuery.php
9 months ago
SubscriptionsAdminPage.php
9 months ago
ServiceProvider.php
112 lines
| 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\Actions\RegisterSubscriptionEntity; |
| 10 | use Give\Subscriptions\LegacyListeners\DispatchGiveSubscriptionPostCreate; |
| 11 | use Give\Subscriptions\LegacyListeners\DispatchGiveSubscriptionPreCreate; |
| 12 | use Give\Subscriptions\ListTable\SubscriptionsListTable; |
| 13 | use Give\Subscriptions\Migrations\AddCampaignId; |
| 14 | use Give\Subscriptions\Migrations\AddCampaignIdColumn; |
| 15 | use Give\Subscriptions\Migrations\AddPaymentModeToSubscriptionTable; |
| 16 | use Give\Subscriptions\Migrations\BackfillMissingCampaignIdForDonations; |
| 17 | use Give\Subscriptions\Migrations\CreateSubscriptionTables; |
| 18 | use Give\Subscriptions\Migrations\UpdateProductID; |
| 19 | use Give\Subscriptions\Repositories\SubscriptionNotesRepository; |
| 20 | use Give\Subscriptions\Repositories\SubscriptionRepository; |
| 21 | |
| 22 | class ServiceProvider implements ServiceProviderInterface |
| 23 | { |
| 24 | /** |
| 25 | * @inheritDoc |
| 26 | * |
| 27 | * @since 4.8.0 Register Subscription Repository to container |
| 28 | */ |
| 29 | public function register() |
| 30 | { |
| 31 | give()->singleton('subscriptions', SubscriptionRepository::class); |
| 32 | give()->singleton('subscriptionNotes', SubscriptionNotesRepository::class); |
| 33 | give()->singleton(SubscriptionsListTable::class, function() { |
| 34 | $listTable = new SubscriptionsListTable(); |
| 35 | Hooks::doAction('givewp_subscriptions_list_table', $listTable); |
| 36 | |
| 37 | return $listTable; |
| 38 | }); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @inheritDoc |
| 43 | */ |
| 44 | public function boot() |
| 45 | { |
| 46 | $this->bootLegacyListeners(); |
| 47 | $this->registerMigrations(); |
| 48 | $this->registerSubscriptionEntity(); |
| 49 | $this->registerSubscriptionAdminOptions(); |
| 50 | |
| 51 | $userId = get_current_user_id(); |
| 52 | $showLegacy = get_user_meta($userId, '_give_subscriptions_archive_show_legacy', true); |
| 53 | // only register new admin page if user hasn't chosen to use the old one |
| 54 | if (empty($showLegacy) && SubscriptionsAdminPage::isShowing()) { |
| 55 | Hooks::addAction('give_forms_page_give-subscriptions', SubscriptionsAdminPage::class, 'render', 1); |
| 56 | } elseif (SubscriptionsAdminPage::isShowing()) { |
| 57 | Hooks::addAction('admin_head', SubscriptionsAdminPage::class, 'renderReactSwitch'); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Legacy Listeners |
| 63 | * |
| 64 | * @since 2.19.6 |
| 65 | */ |
| 66 | private function bootLegacyListeners() |
| 67 | { |
| 68 | Hooks::addAction('givewp_subscription_creating', DispatchGiveSubscriptionPreCreate::class); |
| 69 | Hooks::addAction('givewp_subscription_created', DispatchGiveSubscriptionPostCreate::class); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Registers database migrations with the MigrationsRunner |
| 74 | * |
| 75 | * @since 4.11.0 add AddCampaignIdColumn and AddCampaignId migrations |
| 76 | * @since 2.24.0 |
| 77 | */ |
| 78 | private function registerMigrations() |
| 79 | { |
| 80 | /** @var MigrationsRegister $register */ |
| 81 | $register = give(MigrationsRegister::class); |
| 82 | $register->addMigrations([ |
| 83 | CreateSubscriptionTables::class, |
| 84 | AddPaymentModeToSubscriptionTable::class, |
| 85 | BackfillMissingCampaignIdForDonations::class, |
| 86 | AddCampaignIdColumn::class, |
| 87 | AddCampaignId::class, |
| 88 | UpdateProductID::class |
| 89 | ]); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @since 4.8.0 |
| 94 | */ |
| 95 | private function registerSubscriptionEntity() |
| 96 | { |
| 97 | Hooks::addAction('init', RegisterSubscriptionEntity::class); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @since 4.8.0 |
| 102 | */ |
| 103 | private function registerSubscriptionAdminOptions() |
| 104 | { |
| 105 | add_action('admin_enqueue_scripts', function() { |
| 106 | if (SubscriptionsAdminPage::isShowing()) { |
| 107 | give(LoadSubscriptionAdminOptions::class)(); |
| 108 | } |
| 109 | }); |
| 110 | } |
| 111 | } |
| 112 |