Actions
7 months ago
CustomFields
1 year ago
DataTransferObjects
7 months ago
Endpoints
5 months ago
Factories
1 year ago
LegacyListeners
9 months ago
ListTable
7 months ago
Listeners
1 year ago
Migrations
1 year ago
Models
8 months ago
Properties
1 year ago
Repositories
8 months ago
ValueObjects
8 months ago
resources
5 months ago
DonationsAdminPage.php
5 months ago
ServiceProvider.php
5 months ago
ServiceProvider.php
167 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Donations; |
| 4 | |
| 5 | use Give\Donations\Actions\LoadDonationAdminOptions; |
| 6 | use Give\Donations\CustomFields\Controllers\DonationDetailsController; |
| 7 | use Give\Donations\LegacyListeners\ClearDonationPostCache; |
| 8 | use Give\Donations\LegacyListeners\DispatchDonationNoteEmailNotification; |
| 9 | use Give\Donations\LegacyListeners\DispatchGiveInsertPayment; |
| 10 | use Give\Donations\LegacyListeners\DispatchGivePreInsertPayment; |
| 11 | use Give\Donations\LegacyListeners\DispatchGiveRecurringAddSubscriptionPaymentAndRecordPayment; |
| 12 | use Give\Donations\LegacyListeners\DispatchGiveUpdatePaymentStatus; |
| 13 | use Give\Donations\LegacyListeners\InsertSequentialId; |
| 14 | use Give\Donations\LegacyListeners\RemoveSequentialId; |
| 15 | use Give\Donations\LegacyListeners\UpdateDonorPaymentIds; |
| 16 | use Give\Donations\LegacyListeners\UpdateDonationMetaWithLegacyFormCurrencySettings; |
| 17 | use Give\Donations\Listeners\DonationCreated\UpdateDonationMetaWithCurrencySettings; |
| 18 | use Give\Donations\Listeners\DonationCreated\UpdateDonorMetaWithLastDonatedCurrency; |
| 19 | use Give\Donations\ListTable\DonationsListTable; |
| 20 | use Give\Donations\Migrations\AddMissingDonorIdToDonationComments; |
| 21 | use Give\Donations\Migrations\MoveDonationCommentToDonationMetaTable; |
| 22 | use Give\Donations\Migrations\RecalculateExchangeRate; |
| 23 | use Give\Donations\Migrations\SetAutomaticFormattingOption; |
| 24 | use Give\Donations\Migrations\UnserializeTitlePrefix; |
| 25 | use Give\Donations\Models\Donation; |
| 26 | use Give\Donations\Repositories\DonationNotesRepository; |
| 27 | use Give\Donations\Repositories\DonationRepository; |
| 28 | use Give\Framework\Migrations\MigrationsRegister; |
| 29 | use Give\Helpers\Hooks; |
| 30 | use Give\ServiceProviders\ServiceProvider as ServiceProviderInterface; |
| 31 | |
| 32 | class ServiceProvider implements ServiceProviderInterface |
| 33 | { |
| 34 | /** |
| 35 | * @inheritDoc |
| 36 | */ |
| 37 | public function register() |
| 38 | { |
| 39 | give()->singleton('donations', DonationRepository::class); |
| 40 | give()->singleton('donationNotes', DonationNotesRepository::class); |
| 41 | give()->singleton(DonationsListTable::class, function () { |
| 42 | $listTable = new DonationsListTable(); |
| 43 | Hooks::doAction('givewp_donations_list_table', $listTable); |
| 44 | |
| 45 | return $listTable; |
| 46 | }); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @inheritDoc |
| 51 | */ |
| 52 | public function boot() |
| 53 | { |
| 54 | $this->bootListeners(); |
| 55 | $this->bootLegacyListeners(); |
| 56 | $this->registerDonationsAdminPage(); |
| 57 | $this->addCustomFieldsToDonationDetails(); |
| 58 | $this->loadDonationAdminOptions(); |
| 59 | |
| 60 | give(MigrationsRegister::class)->addMigrations([ |
| 61 | AddMissingDonorIdToDonationComments::class, |
| 62 | SetAutomaticFormattingOption::class, |
| 63 | MoveDonationCommentToDonationMetaTable::class, |
| 64 | UnserializeTitlePrefix::class, |
| 65 | RecalculateExchangeRate::class, |
| 66 | ]); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Legacy Listeners |
| 71 | * @since 2.25.0 Call ClearDonationPostCache on the "givewp_donation_updated" hook |
| 72 | * @since 2.24.0 Remove UpdateSequentialId from "givewp_donation_updated" action hook. |
| 73 | * @since 2.19.6 |
| 74 | */ |
| 75 | private function bootLegacyListeners() |
| 76 | { |
| 77 | Hooks::addAction('givewp_donation_creating', DispatchGivePreInsertPayment::class); |
| 78 | |
| 79 | add_action('givewp_donation_created', static function (Donation $donation) { |
| 80 | (new UpdateDonationMetaWithLegacyFormCurrencySettings())($donation); |
| 81 | (new InsertSequentialId())($donation); |
| 82 | (new DispatchGiveInsertPayment())($donation); |
| 83 | (new UpdateDonorPaymentIds())($donation); |
| 84 | |
| 85 | if ($donation->subscriptionId && $donation->type->isRenewal()) { |
| 86 | (new DispatchGiveRecurringAddSubscriptionPaymentAndRecordPayment())($donation); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @notice |
| 91 | * Anytime we call give_update_payment_status the donor purchase_value and purchase_count get affected. |
| 92 | * We are doing this in the gateway api and in many other places. |
| 93 | * The listener below matches the functionality but the count seems to be overwritten elsewhere. |
| 94 | * Leaving this commented out until resolved or needed. |
| 95 | */ |
| 96 | //Call::invoke(UpdateDonorPurchaseValueAndCount::class, $donation); |
| 97 | }); |
| 98 | |
| 99 | add_action('givewp_donation_updated', function (Donation $donation) { |
| 100 | (new ClearDonationPostCache())($donation); |
| 101 | (new DispatchGiveUpdatePaymentStatus())($donation); |
| 102 | }); |
| 103 | |
| 104 | Hooks::addAction('givewp_donation_deleted', RemoveSequentialId::class); |
| 105 | |
| 106 | add_action('givewp_donation_note_created', static function ($donationNote) { |
| 107 | if ($donationNote->type->isDonor()) { |
| 108 | (new DispatchDonationNoteEmailNotification())($donationNote); |
| 109 | } |
| 110 | }); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Donations Admin page |
| 115 | * |
| 116 | * @since 4.14.0 defer conditionals and DB queries to admin_menu hook. |
| 117 | * @since 2.20.0 |
| 118 | */ |
| 119 | private function registerDonationsAdminPage() |
| 120 | { |
| 121 | add_action('admin_menu', function () { |
| 122 | $userId = get_current_user_id(); |
| 123 | $showLegacy = get_user_meta($userId, '_give_donations_archive_show_legacy', true); |
| 124 | |
| 125 | // only register new admin page if user hasn't chosen to use the old one |
| 126 | if (empty($showLegacy)) { |
| 127 | give(DonationsAdminPage::class)->registerMenuItem(); |
| 128 | } |
| 129 | }, 20); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @since 3.0.0 |
| 134 | */ |
| 135 | private function addCustomFieldsToDonationDetails() |
| 136 | { |
| 137 | add_action('give_view_donation_details_billing_after', static function ($donationId) { |
| 138 | echo (new DonationDetailsController())->show($donationId); |
| 139 | }); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @since 4.2.0 |
| 144 | */ |
| 145 | private function bootListeners() |
| 146 | { |
| 147 | add_action('givewp_donation_created', static function (Donation $donation) { |
| 148 | (new UpdateDonationMetaWithCurrencySettings())($donation); |
| 149 | |
| 150 | (new UpdateDonorMetaWithLastDonatedCurrency())($donation); |
| 151 | }); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @since 4.6.1 Move to admin_enqueue_scripts hook |
| 156 | * @since 4.6.0 |
| 157 | */ |
| 158 | private function loadDonationAdminOptions() |
| 159 | { |
| 160 | add_action('admin_enqueue_scripts', function () { |
| 161 | if (DonationsAdminPage::isShowingDetailsPage()) { |
| 162 | give(LoadDonationAdminOptions::class)(); |
| 163 | } |
| 164 | }); |
| 165 | } |
| 166 | } |
| 167 |