| @@ -22,27 +22,57 @@ | ||
| 22 | 22 | * |
| 23 | 23 | * @since 0.0.2 |
| 24 | 24 | */ |
| 25 | 25 | public static function migrator() { |
| 26 | + self::create_missing_tables(); | |
| 26 | 27 | self::update_tables(); |
| 27 | 28 | } |
| 28 | 29 | |
| 29 | 30 | /** |
| 31 | + * Create any table added since this install was activated (dbDelta is | |
| 32 | + * idempotent). The migrator only runs once per version change, so an | |
| 33 | + * existing install picks up e.g. the better_payment_subscription_order table | |
| 34 | + * on its first load after an update — activation never re-runs there. | |
| 35 | + * | |
| 36 | + * Kept separate from update_tables(), which stays ALTER-only: the test | |
| 37 | + * environment rewrites CREATE TABLE to CREATE TEMPORARY TABLE, so a | |
| 38 | + * create inside a column-migration test would shadow the real table. | |
| 39 | + * | |
| 40 | + * @since 2.4.0 | |
| 41 | + */ | |
| 42 | + public static function create_missing_tables() { | |
| 43 | + ( new \Better_Payment\Lite\Installer() )->create_tables(); | |
| 44 | + } | |
| 45 | + | |
| 46 | + /** | |
| 30 | 47 | * Update the plugin tables |
| 31 | - * | |
| 48 | + * | |
| 32 | 49 | * @since 0.0.2 |
| 33 | 50 | */ |
| 34 | 51 | public static function update_tables() { |
| 52 | + //Add column | |
| 53 | + // `refund_info` is Pro's: only Pro's refunds write it, and Better Payment Pro | |
| 54 | + // 1.4.4+ adds the column itself (Pro Installer::add_refund_info_column()). Lite | |
| 55 | + // still adds it, idempotently, as a data guard for Pro 1.4.3: that Pro relies | |
| 56 | + // on Lite for it, and on a fresh install its refund would reach the gateway | |
| 57 | + // and then fail to record, leaving a refunded row marked paid. Stop once no | |
| 58 | + // supported site can run Pro 1.4.3. | |
| 59 | + self::add_column( 'refund_info', 'longtext' ); | |
| 60 | + self::add_column( 'campaign_id', 'text' ); | |
| 61 | + } | |
| 62 | + | |
| 63 | + private static function add_column( $column_name, $column_type ) { | |
| 35 | 64 | global $wpdb; |
| 36 | 65 | $wpdb->hide_errors(); |
| 37 | 66 | $table_name = "{$wpdb->prefix}better_payment"; |
| 38 | - | |
| 39 | - //Add column | |
| 40 | - $column_name = sanitize_text_field("refund_info"); | |
| 41 | - $column_type = sanitize_text_field("longtext"); | |
| 42 | - $row = $wpdb->get_results( "SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '$table_name' AND column_name = '$column_name'" ); | |
| 43 | - | |
| 44 | - if(empty($row)){ | |
| 45 | - $wpdb->query($wpdb->prepare("ALTER TABLE $table_name ADD $column_name $column_type")); | |
| 67 | + | |
| 68 | + // $row = $wpdb->get_results( "SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '$table_name' AND column_name = '$column_name'" ); | |
| 69 | + | |
| 70 | + $columns = $wpdb->get_col( "DESC $table_name", 0 ); | |
| 71 | + | |
| 72 | + if (in_array($column_name, $columns)) { | |
| 73 | + return; | |
| 46 | 74 | } |
| 75 | + | |
| 76 | + $wpdb->query($wpdb->prepare("ALTER TABLE $table_name ADD $column_name $column_type")); | |
| 47 | 77 | } |
| 48 | 78 | } |