From 7e0326b1d0fbc1d8c438113b217a40d6c7e59726 Mon Sep 17 00:00:00 2001 From: Jon Waldstein Date: Thu, 16 Jan 2025 08:43:49 -0500 Subject: [PATCH] refactor: replace db with meta in donor repository --- src/Donors/Repositories/DonorRepository.php | 24 ++++----------- .../Repositories/TestDonorRepository.php | 29 +++++++++++++++++++ 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/Donors/Repositories/DonorRepository.php b/src/Donors/Repositories/DonorRepository.php index ce19e9a9bb..489b97f0d1 100644 --- a/src/Donors/Repositories/DonorRepository.php +++ b/src/Donors/Repositories/DonorRepository.php @@ -97,6 +97,7 @@ public function getAdditionalEmails(int $donorId) } /** + * @unreleased store meta using native WP functions * @since 3.7.0 Add support to "phone" property * @since 2.24.0 add support for $donor->totalAmountDonated and $donor->totalNumberOfDonation * @since 2.21.0 add actions givewp_donor_creating and givewp_donor_created @@ -142,22 +143,12 @@ public function insert(Donor $donor) $donorId = DB::last_insert_id(); foreach ($this->getCoreDonorMeta($donor) as $metaKey => $metaValue) { - DB::table('give_donormeta') - ->insert([ - 'donor_id' => $donorId, - 'meta_key' => $metaKey, - 'meta_value' => $metaValue, - ]); + give()->donor_meta->add_meta($donorId, $metaKey, $metaValue); } if (isset($donor->additionalEmails)) { foreach ($donor->additionalEmails as $additionalEmail) { - DB::table('give_donormeta') - ->insert([ - 'donor_id' => $donorId, - 'meta_key' => DonorMetaKeys::ADDITIONAL_EMAILS, - 'meta_value' => $additionalEmail, - ]); + give()->donor_meta->add_meta($donorId, DonorMetaKeys::ADDITIONAL_EMAILS, $additionalEmail); } } } catch (Exception $exception) { @@ -411,6 +402,7 @@ public function prepareQuery(): DonorModelQueryBuilder * Additional emails are assigned to the same additional_email meta key. * In order to update them we need to delete and re-insert. * + * @unreleased store meta using native WP functions * @since 2.19.6 * * @return void @@ -426,13 +418,7 @@ private function updateAdditionalEmails(Donor $donor) } foreach ($donor->additionalEmails as $additionalEmail) { - DB::table('give_donormeta') - ->where('donor_id', $donor->id) - ->insert([ - 'donor_id' => $donor->id, - 'meta_key' => DonorMetaKeys::ADDITIONAL_EMAILS, - 'meta_value' => $additionalEmail, - ]); + give()->donor_meta->add_meta($donor->id, DonorMetaKeys::ADDITIONAL_EMAILS, $additionalEmail); } } diff --git a/tests/Unit/Donors/Repositories/TestDonorRepository.php b/tests/Unit/Donors/Repositories/TestDonorRepository.php index f4f30b8614..0839ec1026 100644 --- a/tests/Unit/Donors/Repositories/TestDonorRepository.php +++ b/tests/Unit/Donors/Repositories/TestDonorRepository.php @@ -5,6 +5,7 @@ use Exception; use Give\Donors\Models\Donor; use Give\Donors\Repositories\DonorRepository; +use Give\Donors\ValueObjects\DonorMetaKeys; use Give\Framework\Database\DB; use Give\Framework\Exceptions\Primitives\InvalidArgumentException; use Give\Subscriptions\Repositories\SubscriptionRepository; @@ -211,4 +212,32 @@ public function testDeleteShouldRemoveDonorFromTheDatabase() $this->assertNull($donorQuery); $this->assertEmpty($donorMetaQuery); } + + /** + * @unreleased + * @throws \Give\Framework\Exceptions\Primitives\Exception|Exception + */ + public function testInsertShouldSafelyStoreMetaValues(): void + { + $name = (object)['first' => 'Jon', 'last' => 'Doe']; + + $serializedFirstName = serialize($name); + + $donor = new Donor(array_merge(Donor::factory()->definition(), [ + 'firstName' => $serializedFirstName, + ])); + + $repository = new DonorRepository(); + + $repository->insert($donor); + + $metaValue = give()->donor_meta->get_meta($donor->id, DonorMetaKeys::FIRST_NAME, true); + $metaQuery = DB::table('give_donormeta') + ->where('donor_id', $donor->id) + ->where('meta_key', DonorMetaKeys::FIRST_NAME) + ->get(); + + $this->assertSame($serializedFirstName, $metaValue); + $this->assertSame(serialize($serializedFirstName), $metaQuery->meta_value); + } }