From 6dc8d1c02a057bc51a2af23c25b6e742505ffeb9 Mon Sep 17 00:00:00 2001 From: Ante Laca Date: Wed, 6 Dec 2023 16:15:39 +0100 Subject: [PATCH 1/7] refactor: use give_update_meta instead of add_post_meta --- includes/class-give-donate-form.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-give-donate-form.php b/includes/class-give-donate-form.php index e59635a0d6..cbc1d322b5 100644 --- a/includes/class-give-donate-form.php +++ b/includes/class-give-donate-form.php @@ -921,7 +921,7 @@ public function get_sales() { if ( ! isset( $this->sales ) ) { if ( '' == give_get_meta( $this->ID, '_give_form_sales', true ) ) { - add_post_meta( $this->ID, '_give_form_sales', 0 ); + give_update_meta( $this->ID, '_give_form_sales', 0 ); } // End if $this->sales = give_get_meta( $this->ID, '_give_form_sales', true ); @@ -1009,7 +1009,7 @@ public function get_earnings() { if ( ! isset( $this->earnings ) ) { if ( '' == give_get_meta( $this->ID, '_give_form_earnings', true ) ) { - add_post_meta( $this->ID, '_give_form_earnings', 0 ); + give_update_meta( $this->ID, '_give_form_earnings', 0 ); } $this->earnings = give_get_meta( $this->ID, '_give_form_earnings', true ); From 8d1e842681c7e30657510415bd1875d2e3f9e8be Mon Sep 17 00:00:00 2001 From: Ante Laca Date: Wed, 6 Dec 2023 16:16:00 +0100 Subject: [PATCH 2/7] feature: add migration --- .../Migrations/RemoveDuplicateMeta.php | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/DonationForms/Migrations/RemoveDuplicateMeta.php diff --git a/src/DonationForms/Migrations/RemoveDuplicateMeta.php b/src/DonationForms/Migrations/RemoveDuplicateMeta.php new file mode 100644 index 0000000000..8ca2ae48a0 --- /dev/null +++ b/src/DonationForms/Migrations/RemoveDuplicateMeta.php @@ -0,0 +1,90 @@ +select('ID') + ->where('post_type', 'give_forms') + ->getAll(); + + foreach ($posts as $post) { + $formEarnings = give_get_meta($post->ID, '_give_form_earnings'); + + // Update meta with duplicate keys only + if (count($formEarnings) > 1) { + // Delete all meta + give_delete_meta($post->ID, '_give_form_earnings'); + + // Get earnings from donations + $earnings = $this->getEarningsFromDonations($post->ID); + give_update_meta($post->ID, '_give_form_earnings', $earnings); + } + } + } + + /** + * Get earnings from donations by Form ID + * + * @param $formId + * + * @return int + */ + private function getEarningsFromDonations($formId): int + { + $donations = Db::table('posts') + ->attachMeta( + 'give_donationmeta', + 'ID', + 'donation_id', + [DonationMetaKeys::FORM_ID(), 'formId'], + [DonationMetaKeys::AMOUNT(), 'amount']) + ->where('post_type', 'give_payment') + ->where('give_donationmeta_attach_meta_formId.meta_value', $formId) + ->getAll('ARRAY_A'); + + $amounts = array_column($donations, 'amount'); + + return array_sum($amounts); + } + +} From 41c9cb926b61c4cf3e0866f505fef5eac3c31528 Mon Sep 17 00:00:00 2001 From: Ante Laca Date: Wed, 6 Dec 2023 16:16:10 +0100 Subject: [PATCH 3/7] feature: register migration --- src/DonationForms/ServiceProvider.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/DonationForms/ServiceProvider.php b/src/DonationForms/ServiceProvider.php index eae6828dc2..556e72c296 100644 --- a/src/DonationForms/ServiceProvider.php +++ b/src/DonationForms/ServiceProvider.php @@ -17,6 +17,7 @@ use Give\DonationForms\FormDesigns\MultiStepFormDesign\MultiStepFormDesign; use Give\DonationForms\FormPage\TemplateHandler; use Give\DonationForms\Migrations\CleanMultipleSlashesOnDB; +use Give\DonationForms\Migrations\RemoveDuplicateMeta; use Give\DonationForms\Repositories\DonationFormRepository; use Give\DonationForms\Routes\AuthenticationRoute; use Give\DonationForms\Routes\DonateRoute; @@ -73,6 +74,7 @@ public function boot() give(MigrationsRegister::class)->addMigrations([ CleanMultipleSlashesOnDB::class, + RemoveDuplicateMeta::class, ]); } From bcaf7e1102e13e1cd1e0125d161ccbb911468773 Mon Sep 17 00:00:00 2001 From: Ante Laca Date: Thu, 7 Dec 2023 09:38:49 +0100 Subject: [PATCH 4/7] refactor: caste meta values to float --- src/FormMigration/Actions/TransferDonations.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/FormMigration/Actions/TransferDonations.php b/src/FormMigration/Actions/TransferDonations.php index cffa126f20..3eeaae2ebe 100644 --- a/src/FormMigration/Actions/TransferDonations.php +++ b/src/FormMigration/Actions/TransferDonations.php @@ -45,14 +45,14 @@ public function __invoke($destinationId) give_update_meta( $destinationId, '_give_form_sales', - give_get_meta($this->sourceId, '_give_form_sales', true) + (float)give_get_meta($this->sourceId, '_give_form_sales', true) ); give_update_meta($this->sourceId, '_give_form_sales', 0); give_update_meta( $destinationId, '_give_form_earnings', - give_get_meta($this->sourceId, '_give_form_earnings', true) + (float)give_get_meta($this->sourceId, '_give_form_earnings', true) ); give_update_meta($this->sourceId, '_give_form_earnings', 0); From cd3c76776a2372835bd967c7c46619aa30087c7e Mon Sep 17 00:00:00 2001 From: Ante Laca Date: Thu, 7 Dec 2023 09:44:09 +0100 Subject: [PATCH 5/7] refactor: caste meta value to int --- src/FormMigration/Actions/TransferDonations.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FormMigration/Actions/TransferDonations.php b/src/FormMigration/Actions/TransferDonations.php index 3eeaae2ebe..fdfb63bd53 100644 --- a/src/FormMigration/Actions/TransferDonations.php +++ b/src/FormMigration/Actions/TransferDonations.php @@ -45,7 +45,7 @@ public function __invoke($destinationId) give_update_meta( $destinationId, '_give_form_sales', - (float)give_get_meta($this->sourceId, '_give_form_sales', true) + (int)give_get_meta($this->sourceId, '_give_form_sales', true) ); give_update_meta($this->sourceId, '_give_form_sales', 0); From a8109d3d462ec38332d3765c6b0f02d7fcb35ad4 Mon Sep 17 00:00:00 2001 From: Ante Laca Date: Thu, 7 Dec 2023 10:09:06 +0100 Subject: [PATCH 6/7] feature: remove duplicate meta and set correct value for _give_form_sales --- .../Migrations/RemoveDuplicateMeta.php | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/DonationForms/Migrations/RemoveDuplicateMeta.php b/src/DonationForms/Migrations/RemoveDuplicateMeta.php index 8ca2ae48a0..c5c581e7a7 100644 --- a/src/DonationForms/Migrations/RemoveDuplicateMeta.php +++ b/src/DonationForms/Migrations/RemoveDuplicateMeta.php @@ -49,6 +49,7 @@ public function run() foreach ($posts as $post) { $formEarnings = give_get_meta($post->ID, '_give_form_earnings'); + $formSales = give_get_meta($post->ID, '_give_form_sales'); // Update meta with duplicate keys only if (count($formEarnings) > 1) { @@ -59,6 +60,15 @@ public function run() $earnings = $this->getEarningsFromDonations($post->ID); give_update_meta($post->ID, '_give_form_earnings', $earnings); } + + if (count($formSales) > 1) { + // Delete all meta + give_delete_meta($post->ID, '_give_form_sales'); + + // Get sales count from donations + $sales = $this->getSalesCountFromDonations($post->ID); + give_update_meta($post->ID, '_give_form_sales', $sales); + } } } @@ -67,9 +77,9 @@ public function run() * * @param $formId * - * @return int + * @return float | int */ - private function getEarningsFromDonations($formId): int + private function getEarningsFromDonations($formId) { $donations = Db::table('posts') ->attachMeta( @@ -87,4 +97,26 @@ private function getEarningsFromDonations($formId): int return array_sum($amounts); } + /** + * Get sales count from donations by Form ID + * + * @param $formId + * + * @return int + */ + private function getSalesCountFromDonations($formId): int + { + return Db::table('posts') + ->attachMeta( + 'give_donationmeta', + 'ID', + 'donation_id', + [DonationMetaKeys::FORM_ID(), 'formId'] + ) + ->where('post_type', 'give_payment') + ->where('post_status', 'publish') + ->where('give_donationmeta_attach_meta_formId.meta_value', $formId) + ->count('ID'); + } + } From b29729ff110b24c13adeb2e963861371d985c025 Mon Sep 17 00:00:00 2001 From: alaca Date: Sat, 9 Dec 2023 17:45:39 +0100 Subject: [PATCH 7/7] refactor: use constants for meta keys; use correct class name --- src/DonationForms/Migrations/RemoveDuplicateMeta.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/DonationForms/Migrations/RemoveDuplicateMeta.php b/src/DonationForms/Migrations/RemoveDuplicateMeta.php index c5c581e7a7..5716430d9e 100644 --- a/src/DonationForms/Migrations/RemoveDuplicateMeta.php +++ b/src/DonationForms/Migrations/RemoveDuplicateMeta.php @@ -81,13 +81,13 @@ public function run() */ private function getEarningsFromDonations($formId) { - $donations = Db::table('posts') + $donations = DB::table('posts') ->attachMeta( 'give_donationmeta', 'ID', 'donation_id', - [DonationMetaKeys::FORM_ID(), 'formId'], - [DonationMetaKeys::AMOUNT(), 'amount']) + [DonationMetaKeys::FORM_ID, 'formId'], + [DonationMetaKeys::AMOUNT, 'amount']) ->where('post_type', 'give_payment') ->where('give_donationmeta_attach_meta_formId.meta_value', $formId) ->getAll('ARRAY_A'); @@ -106,12 +106,12 @@ private function getEarningsFromDonations($formId) */ private function getSalesCountFromDonations($formId): int { - return Db::table('posts') + return DB::table('posts') ->attachMeta( 'give_donationmeta', 'ID', 'donation_id', - [DonationMetaKeys::FORM_ID(), 'formId'] + [DonationMetaKeys::FORM_ID, 'formId'] ) ->where('post_type', 'give_payment') ->where('post_status', 'publish')