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 ); diff --git a/src/DonationForms/Migrations/RemoveDuplicateMeta.php b/src/DonationForms/Migrations/RemoveDuplicateMeta.php new file mode 100644 index 0000000000..5716430d9e --- /dev/null +++ b/src/DonationForms/Migrations/RemoveDuplicateMeta.php @@ -0,0 +1,122 @@ +select('ID') + ->where('post_type', 'give_forms') + ->getAll(); + + 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) { + // 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); + } + + 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); + } + } + } + + /** + * Get earnings from donations by Form ID + * + * @param $formId + * + * @return float | int + */ + private function getEarningsFromDonations($formId) + { + $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); + } + + /** + * 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'); + } + +} 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, ]); } diff --git a/src/FormMigration/Actions/TransferDonations.php b/src/FormMigration/Actions/TransferDonations.php index cffa126f20..fdfb63bd53 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) + (int)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);