| 1 |
<?php |
| 2 |
|
| 3 |
namespace Better_Payment\Lite\Classes; |
| 4 |
|
| 5 |
/** |
| 6 |
* Exit if accessed directly |
| 7 |
*/ |
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* The plugin migrator class |
| 14 |
* |
| 15 |
* @since 0.0.2 |
| 16 |
*/ |
| 17 |
class Migrator |
| 18 |
{ |
| 19 |
|
| 20 |
/** |
| 21 |
* Initialize the plugin |
| 22 |
* |
| 23 |
* @since 0.0.2 |
| 24 |
*/ |
| 25 |
public static function migrator() { |
| 26 |
self::create_missing_tables(); |
| 27 |
self::update_tables(); |
| 28 |
} |
| 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 |
/** |
| 47 |
* Update the plugin tables |
| 48 |
* |
| 49 |
* @since 0.0.2 |
| 50 |
*/ |
| 51 |
public static function update_tables() { |
| 52 |
//Add column |
| 53 |
// Transitional: only Better Payment Pro's refunds write `refund_info`. Pro 1.4.4 |
| 54 |
// adds the column itself (Pro Installer::add_refund_info_column()); Pro <= 1.4.3 |
| 55 |
// relies on this. Stop adding it when ProCompatibility::MIN_PRO_VERSION >= 1.4.4. |
| 56 |
self::add_column( 'refund_info', 'longtext' ); |
| 57 |
self::add_column( 'campaign_id', 'text' ); |
| 58 |
} |
| 59 |
|
| 60 |
private static function add_column( $column_name, $column_type ) { |
| 61 |
global $wpdb; |
| 62 |
$wpdb->hide_errors(); |
| 63 |
$table_name = "{$wpdb->prefix}better_payment"; |
| 64 |
|
| 65 |
// $row = $wpdb->get_results( "SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '$table_name' AND column_name = '$column_name'" ); |
| 66 |
|
| 67 |
$columns = $wpdb->get_col( "DESC $table_name", 0 ); |
| 68 |
|
| 69 |
if (in_array($column_name, $columns)) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
$wpdb->query($wpdb->prepare("ALTER TABLE $table_name ADD $column_name $column_type")); |
| 74 |
} |
| 75 |
} |
| 76 |
|