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