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