AddPastDonationsToRevenueTable.php
140 lines
| 1 | <?php |
| 2 | namespace Give\Revenue\Migrations; |
| 3 | |
| 4 | use Give\Framework\Migrations\Contracts\Migration; |
| 5 | use Give\Framework\Migrations\Exceptions\DatabaseMigrationException; |
| 6 | use Give\Revenue\Repositories\Revenue; |
| 7 | use Give\ValueObjects\Money; |
| 8 | use Give_Updates; |
| 9 | use InvalidArgumentException; |
| 10 | use WP_Query; |
| 11 | use Exception; |
| 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 | * Register background update. |
| 26 | * |
| 27 | * @param Give_Updates $give_updates |
| 28 | * |
| 29 | * @since 2.9.0 |
| 30 | */ |
| 31 | public function register( $give_updates ) { |
| 32 | $give_updates->register( |
| 33 | [ |
| 34 | 'id' => self::id(), |
| 35 | 'version' => '2.9.0', |
| 36 | 'callback' => [ $this, 'run' ], |
| 37 | ] |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @inheritdoc |
| 43 | */ |
| 44 | public function run() { |
| 45 | global $post; |
| 46 | |
| 47 | /* @var Revenue $revenueRepository */ |
| 48 | $revenueRepository = give( Revenue::class ); |
| 49 | $give_updates = Give_Updates::get_instance(); |
| 50 | |
| 51 | $donations = new WP_Query( |
| 52 | [ |
| 53 | 'paged' => $give_updates->step, |
| 54 | 'status' => 'any', |
| 55 | 'order' => 'ASC', |
| 56 | 'post_type' => [ 'give_payment' ], |
| 57 | 'posts_per_page' => 100, |
| 58 | ] |
| 59 | ); |
| 60 | |
| 61 | if ( $donations->have_posts() ) { |
| 62 | $give_updates->set_percentage( $donations->found_posts, $give_updates->step * 100 ); |
| 63 | |
| 64 | while ( $donations->have_posts() ) { |
| 65 | $donations->the_post(); |
| 66 | |
| 67 | if ( $revenueRepository->isDonationExist( $post->ID ) ) { |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | if ( ! ( $amount = give()->payment_meta->get_meta( $post->ID, '_give_cs_base_amount', true ) ) ) { |
| 72 | $amount = give_donation_amount( $post->ID ); |
| 73 | } |
| 74 | |
| 75 | $revenueData = [ |
| 76 | 'donation_id' => $post->ID, |
| 77 | 'form_id' => give_get_payment_form_id( $post->ID ), |
| 78 | 'amount' => Money::of( $amount, give_get_option( 'currency' ) )->getMinorAmount(), |
| 79 | ]; |
| 80 | |
| 81 | $revenueRepository->insert( $revenueData ); |
| 82 | $this->pauseUpdateOnError( $give_updates, $revenueData ); |
| 83 | } |
| 84 | |
| 85 | wp_reset_postdata(); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | // Update Ran Successfully. |
| 90 | give_set_upgrade_complete( self::id() ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @inheritdoc |
| 95 | */ |
| 96 | public static function id() { |
| 97 | return 'add-past-donation-data-to-revenue-table'; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @inheritdoc |
| 102 | */ |
| 103 | public static function timestamp() { |
| 104 | return strtotime( '2019-09-24' ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Pause update process and add log. |
| 109 | * |
| 110 | * @since 2.9.2 |
| 111 | * @since 2.9.4 Add second argument to function and mention donation data in exception message. |
| 112 | * |
| 113 | * @param Give_Updates $give_updates |
| 114 | * |
| 115 | * @param array $revenueData Donation data to insert into revenue table |
| 116 | */ |
| 117 | private function pauseUpdateOnError( $give_updates, $revenueData ) { |
| 118 | global $wpdb; |
| 119 | |
| 120 | if ( ! $wpdb->last_error ) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | give()->logs->add( |
| 125 | 'Update Error', |
| 126 | sprintf( |
| 127 | 'An error occurred inserting data into the revenue table: ' . "\n" . '%1$s' . "\n" . '%2$s', |
| 128 | $wpdb->last_error, |
| 129 | print_r( $revenueData, true ) |
| 130 | ), |
| 131 | 0, |
| 132 | 'update' |
| 133 | ); |
| 134 | |
| 135 | $give_updates->__pause_db_update( true ); |
| 136 | update_option( 'give_upgrade_error', 1, false ); |
| 137 | wp_die(); |
| 138 | } |
| 139 | } |
| 140 |