AddPaymentModeToSubscriptionTable.php
118 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Subscriptions\Migrations; |
| 4 | |
| 5 | use Give\Framework\Database\DB; |
| 6 | use Give\Framework\Database\Exceptions\DatabaseQueryException; |
| 7 | use Give\Framework\Migrations\Contracts\Migration; |
| 8 | use Give\Framework\Migrations\Exceptions\DatabaseMigrationException; |
| 9 | use Give_Updates; |
| 10 | |
| 11 | /** |
| 12 | * @since 2.24.0 |
| 13 | */ |
| 14 | class AddPaymentModeToSubscriptionTable extends Migration |
| 15 | { |
| 16 | /** |
| 17 | * @inheritDoc |
| 18 | * |
| 19 | * @since 2.24.0 |
| 20 | */ |
| 21 | public static function title(): string |
| 22 | { |
| 23 | return 'Add payment mode column to subscription table'; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @inheritDoc |
| 28 | * |
| 29 | * @since 2.24.0 |
| 30 | */ |
| 31 | public static function timestamp() |
| 32 | { |
| 33 | return strtotime('2022-11-30'); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @inheritDoc |
| 38 | * |
| 39 | * @since 2.24.0 |
| 40 | */ |
| 41 | public static function id(): string |
| 42 | { |
| 43 | return 'add_paymentmode_to_subscription_table'; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @inheritDoc |
| 48 | * |
| 49 | * @since 2.24.0 |
| 50 | * |
| 51 | * @throws DatabaseMigrationException |
| 52 | */ |
| 53 | public function run() |
| 54 | { |
| 55 | $this->addPaymentModeColumn(); |
| 56 | $this->processPaymentModeForExistingSubscriptions(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Add payment mode column to subscription table. |
| 61 | * |
| 62 | * @since 2.24.0 |
| 63 | * |
| 64 | * @return void |
| 65 | * @throws DatabaseMigrationException |
| 66 | */ |
| 67 | private function addPaymentModeColumn() |
| 68 | { |
| 69 | global $wpdb; |
| 70 | |
| 71 | $subscriptionTableName = "{$wpdb->prefix}give_subscriptions"; |
| 72 | |
| 73 | try { |
| 74 | maybe_add_column( |
| 75 | $subscriptionTableName, |
| 76 | 'payment_mode', |
| 77 | "ALTER TABLE `$subscriptionTableName` ADD COLUMN `payment_mode` varchar(20) NOT NULL DEFAULT '' AFTER `parent_payment_id`" |
| 78 | ); |
| 79 | } catch (DatabaseQueryException $exception) { |
| 80 | throw new DatabaseMigrationException('An error occurred adding the payment mode column to the subscription table', |
| 81 | 0, $exception); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Process payment mode for existing subscriptions. |
| 87 | * |
| 88 | * @since 2.24.0 |
| 89 | * |
| 90 | * @return void |
| 91 | * @throws DatabaseMigrationException |
| 92 | */ |
| 93 | private function processPaymentModeForExistingSubscriptions() |
| 94 | { |
| 95 | global $wpdb; |
| 96 | |
| 97 | $subscriptionTableName = "{$wpdb->prefix}give_subscriptions"; |
| 98 | $donationMetaTableName = "{$wpdb->prefix}give_donationmeta"; |
| 99 | |
| 100 | try { |
| 101 | DB::query( |
| 102 | " |
| 103 | UPDATE |
| 104 | $subscriptionTableName subscription |
| 105 | LEFT JOIN $donationMetaTableName donationMeta ON subscription.parent_payment_id = donationMeta.donation_id |
| 106 | SET |
| 107 | subscription.payment_mode = donationMeta.meta_value |
| 108 | WHERE |
| 109 | donationMeta.meta_key = '_give_payment_mode' |
| 110 | " |
| 111 | ); |
| 112 | } catch (DatabaseQueryException $exception) { |
| 113 | throw new DatabaseMigrationException('An error occurred processing the payment mode for existing subscriptions', |
| 114 | 0, $exception); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 |