Actions
8 months ago
CustomFields
1 year ago
DataTransferObjects
1 year ago
Endpoints
8 months ago
Exceptions
2 years ago
Factories
1 year ago
ListTable
8 months ago
Migrations
1 year ago
Models
1 year ago
Repositories
8 months ago
ValueObjects
9 months ago
ViewModels
10 months ago
resources
8 months ago
DonorStatisticsQuery.php
9 months ago
DonorsAdminPage.php
8 months ago
DonorsQuery.php
10 months ago
ServiceProvider.php
8 months ago
ServiceProvider.php
121 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Donors; |
| 4 | |
| 5 | use Give\DonationForms\Models\DonationForm; |
| 6 | use Give\Donors\Actions\CreateUserFromDonor; |
| 7 | use Give\Donors\Actions\LoadDonorAdminOptions; |
| 8 | use Give\Donors\Actions\SendDonorUserRegistrationNotification; |
| 9 | use Give\Donors\Actions\UpdateAdminDonorDetails; |
| 10 | use Give\Donors\CustomFields\Controllers\DonorDetailsController; |
| 11 | use Give\Donors\Exceptions\FailedDonorUserCreationException; |
| 12 | use Give\Donors\ListTable\DonorsListTable; |
| 13 | use Give\Donors\Migrations\AddPhoneColumn; |
| 14 | use Give\Donors\Models\Donor; |
| 15 | use Give\Donors\Repositories\DonorNotesRepository; |
| 16 | use Give\Donors\Repositories\DonorRepositoryProxy; |
| 17 | use Give\Framework\Migrations\MigrationsRegister; |
| 18 | use Give\Helpers\Hooks; |
| 19 | use Give\Log\Log; |
| 20 | use Give\ServiceProviders\ServiceProvider as ServiceProviderInterface; |
| 21 | use Give_Donor as LegacyDonor; |
| 22 | |
| 23 | /** |
| 24 | * @since 2.19.6 |
| 25 | */ |
| 26 | class ServiceProvider implements ServiceProviderInterface |
| 27 | { |
| 28 | /** |
| 29 | * @inheritDoc |
| 30 | */ |
| 31 | public function register() |
| 32 | { |
| 33 | give()->singleton('donors', DonorRepositoryProxy::class); |
| 34 | give()->singleton('donorNotes', DonorNotesRepository::class); |
| 35 | give()->singleton(DonorsListTable::class, function () { |
| 36 | $listTable = new DonorsListTable(); |
| 37 | Hooks::doAction('givewp_donors_list_table', $listTable); |
| 38 | |
| 39 | return $listTable; |
| 40 | }); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @inheritDoc |
| 45 | * |
| 46 | * @since 3.7.0 Register "AddPhoneColumn" migration and add the "give_admin_donor_details_updating" action |
| 47 | */ |
| 48 | public function boot() |
| 49 | { |
| 50 | $userId = get_current_user_id(); |
| 51 | $showLegacy = DonorsAdminPage::isShowingNewDetailsPage() ? false : get_user_meta($userId, '_give_donors_archive_show_legacy', true); |
| 52 | // only register new admin page if user hasn't chosen to use the old one or is trying to access the new donor details page |
| 53 | if (empty($showLegacy)) { |
| 54 | Hooks::addAction('admin_menu', DonorsAdminPage::class, 'registerMenuItem', 30); |
| 55 | } elseif (DonorsAdminPage::isShowing()) { |
| 56 | Hooks::addAction('admin_head', DonorsAdminPage::class, 'renderReactSwitch'); |
| 57 | } |
| 58 | |
| 59 | $this->addCustomFieldsToDonorDetails(); |
| 60 | $this->enforceDonorsAsUsers(); |
| 61 | |
| 62 | give(MigrationsRegister::class)->addMigrations([ |
| 63 | AddPhoneColumn::class, |
| 64 | ]); |
| 65 | |
| 66 | Hooks::addAction('give_admin_donor_details_updating', UpdateAdminDonorDetails::class, '__invoke', 10, 2); |
| 67 | |
| 68 | $this->loadDonorAdminOptions(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @since 3.0.0 |
| 73 | */ |
| 74 | private function addCustomFieldsToDonorDetails() |
| 75 | { |
| 76 | add_action('give_donor_after_tables', static function (LegacyDonor $legacyDonor) { |
| 77 | /** @var Donor $donor */ |
| 78 | $donor = Donor::find($legacyDonor->id); |
| 79 | |
| 80 | echo (new DonorDetailsController())->show($donor); |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Hook into the donor creation process to ensure that donors are also users. |
| 86 | * @since 3.2.0 |
| 87 | */ |
| 88 | protected function enforceDonorsAsUsers() |
| 89 | { |
| 90 | add_action('givewp_donate_controller_donor_created', function (Donor $donor, $formId) { |
| 91 | if (!$donor->userId) { |
| 92 | try { |
| 93 | give(CreateUserFromDonor::class)->__invoke($donor); |
| 94 | |
| 95 | if (DonationForm::find($formId)->settings->registrationNotification) { |
| 96 | give(SendDonorUserRegistrationNotification::class)->__invoke($donor); |
| 97 | } |
| 98 | } catch (FailedDonorUserCreationException $e) { |
| 99 | Log::error($e->getLogMessage(), [ |
| 100 | 'donor' => $donor, |
| 101 | 'previous' => $e->getPrevious(), |
| 102 | ]); |
| 103 | } |
| 104 | } |
| 105 | }, 10, 2); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @since 4.6.1 Move to admin_enqueue_scripts hook |
| 110 | * @since 4.4.0 |
| 111 | */ |
| 112 | private function loadDonorAdminOptions() |
| 113 | { |
| 114 | add_action('admin_enqueue_scripts', function () { |
| 115 | if (DonorsAdminPage::isShowingDetailsPage()) { |
| 116 | give(LoadDonorAdminOptions::class)(); |
| 117 | } |
| 118 | }); |
| 119 | } |
| 120 | } |
| 121 |