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