| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Revenue\Migrations; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Give\Framework\Migrations\Contracts\Migration; |
| 7 |
use Give\Log\Log; |
| 8 |
use Give\Revenue\Repositories\Revenue; |
| 9 |
use Give\ValueObjects\Money; |
| 10 |
use Give_Updates; |
| 11 |
use WP_Query; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class AddPastDonationToRevenueTable |
| 15 |
* |
| 16 |
* Use this table to migrated past donations data to revenue table. |
| 17 |
* This data migration will perform in background. |
| 18 |
* |
| 19 |
* @package Give\Revenue\Migrations |
| 20 |
* |
| 21 |
* @since 2.9.0 |
| 22 |
*/ |
| 23 |
class AddPastDonationsToRevenueTable extends Migration |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Register background update. |
| 27 |
* |
| 28 |
* @since 2.9.0 |
| 29 |
* |
| 30 |
* @param Give_Updates $give_updates |
| 31 |
* |
| 32 |
*/ |
| 33 |
public function register($give_updates) |
| 34 |
{ |
| 35 |
$give_updates->register( |
| 36 |
[ |
| 37 |
'id' => self::id(), |
| 38 |
'version' => '2.9.0', |
| 39 |
'callback' => [$this, 'run'], |
| 40 |
] |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* @inheritdoc |
| 46 |
*/ |
| 47 |
public function run() |
| 48 |
{ |
| 49 |
global $post; |
| 50 |
|
| 51 |
/* @var Revenue $revenueRepository */ |
| 52 |
$revenueRepository = give(Revenue::class); |
| 53 |
$give_updates = Give_Updates::get_instance(); |
| 54 |
|
| 55 |
$donations = new WP_Query( |
| 56 |
[ |
| 57 |
'paged' => $give_updates->step, |
| 58 |
'status' => 'any', |
| 59 |
'order' => 'ASC', |
| 60 |
'post_type' => ['give_payment'], |
| 61 |
'posts_per_page' => 100, |
| 62 |
] |
| 63 |
); |
| 64 |
|
| 65 |
if ($donations->have_posts()) { |
| 66 |
$give_updates->set_percentage($donations->found_posts, $give_updates->step * 100); |
| 67 |
|
| 68 |
while ($donations->have_posts()) { |
| 69 |
$donations->the_post(); |
| 70 |
|
| 71 |
if ($revenueRepository->isDonationExist($post->ID)) { |
| 72 |
continue; |
| 73 |
} |
| 74 |
|
| 75 |
if ( ! ($amount = give()->payment_meta->get_meta($post->ID, '_give_cs_base_amount', true))) { |
| 76 |
$amount = give_donation_amount($post->ID); |
| 77 |
} |
| 78 |
|
| 79 |
$revenueData = [ |
| 80 |
'donation_id' => $post->ID, |
| 81 |
'form_id' => give_get_payment_form_id($post->ID), |
| 82 |
'amount' => Money::of($amount, give_get_option('currency'))->getMinorAmount(), |
| 83 |
]; |
| 84 |
|
| 85 |
try { |
| 86 |
$revenueRepository->insert($revenueData); |
| 87 |
} catch (Exception $e) { |
| 88 |
$give_updates->pause_db_update(true); |
| 89 |
update_option('give_upgrade_error', 1, false); |
| 90 |
|
| 91 |
Log::error( |
| 92 |
esc_html__('An error occurred inserting data into the revenue table', 'give'), |
| 93 |
[ |
| 94 |
'source' => 'Revenue Migration', |
| 95 |
'Data' => $revenueData, |
| 96 |
'Error' => $e->getMessage(), |
| 97 |
] |
| 98 |
); |
| 99 |
|
| 100 |
wp_die(); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
wp_reset_postdata(); |
| 105 |
} else { |
| 106 |
// Update Ran Successfully. |
| 107 |
give_set_upgrade_complete(self::id()); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* @inheritdoc |
| 113 |
*/ |
| 114 |
public static function id() |
| 115 |
{ |
| 116 |
return 'add-past-donation-data-to-revenue-table'; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @inheritdoc |
| 121 |
*/ |
| 122 |
public static function timestamp() |
| 123 |
{ |
| 124 |
return strtotime('2019-09-24'); |
| 125 |
} |
| 126 |
} |
| 127 |
|