Skip to content

Commit

Permalink
Fix: duplicate meta keys (#7131)
Browse files Browse the repository at this point in the history
Co-authored-by: Ante Laca <[email protected]>
  • Loading branch information
alaca authored Dec 19, 2023
1 parent a8064e3 commit e11fe2c
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 4 deletions.
4 changes: 2 additions & 2 deletions includes/class-give-donate-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down
122 changes: 122 additions & 0 deletions src/DonationForms/Migrations/RemoveDuplicateMeta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace Give\DonationForms\Migrations;

use Give\Donations\ValueObjects\DonationMetaKeys;
use Give\Framework\Database\DB;
use Give\Framework\Migrations\Contracts\Migration;

/**
* Remove duplicate meta keys and set correct value for form earnings
*
* @unreleased
*/
class RemoveDuplicateMeta extends Migration
{
/**
* @inheritdoc
*/
public static function id()
{
return 'donation-forms-remove-duplicate-meta';
}

/**
* @inheritdoc
*/
public static function title()
{
return 'Remove duplicate meta';
}

/**
* @inheritdoc
*/
public static function timestamp()
{
return strtotime('2023-06-12');
}

/**
* @unreleased
*/
public function run()
{
$posts = DB::table('posts')
->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');
}

}
2 changes: 2 additions & 0 deletions src/DonationForms/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,6 +74,7 @@ public function boot()

give(MigrationsRegister::class)->addMigrations([
CleanMultipleSlashesOnDB::class,
RemoveDuplicateMeta::class,
]);
}

Expand Down
4 changes: 2 additions & 2 deletions src/FormMigration/Actions/TransferDonations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit e11fe2c

Please sign in to comment.