CleanMultipleSlashesOnDB.php
2 years ago
RemoveDuplicateMeta.php
2 years ago
UpdateDonationLevelsSchema.php
2 years ago
RemoveDuplicateMeta.php
123 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms\Migrations; |
| 4 | |
| 5 | use Give\Donations\ValueObjects\DonationMetaKeys; |
| 6 | use Give\Framework\Database\DB; |
| 7 | use Give\Framework\Migrations\Contracts\Migration; |
| 8 | |
| 9 | /** |
| 10 | * Remove duplicate meta keys and set correct value for form earnings |
| 11 | * |
| 12 | * @since 3.3.0 |
| 13 | */ |
| 14 | class RemoveDuplicateMeta extends Migration |
| 15 | { |
| 16 | /** |
| 17 | * @inheritdoc |
| 18 | */ |
| 19 | public static function id() |
| 20 | { |
| 21 | return 'donation-forms-remove-duplicate-meta'; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @inheritdoc |
| 26 | */ |
| 27 | public static function title() |
| 28 | { |
| 29 | return 'Remove duplicate meta'; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @inheritdoc |
| 34 | */ |
| 35 | public static function timestamp() |
| 36 | { |
| 37 | return strtotime('2023-06-12'); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @since 3.3.0 |
| 42 | */ |
| 43 | public function run() |
| 44 | { |
| 45 | $posts = DB::table('posts') |
| 46 | ->select('ID') |
| 47 | ->where('post_type', 'give_forms') |
| 48 | ->getAll(); |
| 49 | |
| 50 | foreach ($posts as $post) { |
| 51 | $formEarnings = give_get_meta($post->ID, '_give_form_earnings'); |
| 52 | $formSales = give_get_meta($post->ID, '_give_form_sales'); |
| 53 | |
| 54 | // Update meta with duplicate keys only |
| 55 | if (count($formEarnings) > 1) { |
| 56 | // Delete all meta |
| 57 | give_delete_meta($post->ID, '_give_form_earnings'); |
| 58 | |
| 59 | // Get earnings from donations |
| 60 | $earnings = $this->getEarningsFromDonations($post->ID); |
| 61 | give_update_meta($post->ID, '_give_form_earnings', $earnings); |
| 62 | } |
| 63 | |
| 64 | if (count($formSales) > 1) { |
| 65 | // Delete all meta |
| 66 | give_delete_meta($post->ID, '_give_form_sales'); |
| 67 | |
| 68 | // Get sales count from donations |
| 69 | $sales = $this->getSalesCountFromDonations($post->ID); |
| 70 | give_update_meta($post->ID, '_give_form_sales', $sales); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get earnings from donations by Form ID |
| 77 | * |
| 78 | * @param $formId |
| 79 | * |
| 80 | * @return float | int |
| 81 | */ |
| 82 | private function getEarningsFromDonations($formId) |
| 83 | { |
| 84 | $donations = DB::table('posts') |
| 85 | ->attachMeta( |
| 86 | 'give_donationmeta', |
| 87 | 'ID', |
| 88 | 'donation_id', |
| 89 | [DonationMetaKeys::FORM_ID, 'formId'], |
| 90 | [DonationMetaKeys::AMOUNT, 'amount']) |
| 91 | ->where('post_type', 'give_payment') |
| 92 | ->where('give_donationmeta_attach_meta_formId.meta_value', $formId) |
| 93 | ->getAll('ARRAY_A'); |
| 94 | |
| 95 | $amounts = array_column($donations, 'amount'); |
| 96 | |
| 97 | return array_sum($amounts); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Get sales count from donations by Form ID |
| 102 | * |
| 103 | * @param $formId |
| 104 | * |
| 105 | * @return int |
| 106 | */ |
| 107 | private function getSalesCountFromDonations($formId): int |
| 108 | { |
| 109 | return DB::table('posts') |
| 110 | ->attachMeta( |
| 111 | 'give_donationmeta', |
| 112 | 'ID', |
| 113 | 'donation_id', |
| 114 | [DonationMetaKeys::FORM_ID, 'formId'] |
| 115 | ) |
| 116 | ->where('post_type', 'give_payment') |
| 117 | ->where('post_status', 'publish') |
| 118 | ->where('give_donationmeta_attach_meta_formId.meta_value', $formId) |
| 119 | ->count('ID'); |
| 120 | } |
| 121 | |
| 122 | } |
| 123 |