| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class LP_Updater |
| 5 |
* |
| 6 |
* Update helper class providing update functions |
| 7 |
* |
| 8 |
* @author ThimPress |
| 9 |
* @package LearnPress/Classes |
| 10 |
* @version 3.0.0 |
| 11 |
*/ |
| 12 |
class LP_Updater { |
| 13 |
/** |
| 14 |
* Map version upgrade. |
| 15 |
* |
| 16 |
* @var array |
| 17 |
*/ |
| 18 |
public $db_map_version = array(); |
| 19 |
|
| 20 |
/** |
| 21 |
* LP_Updater constructor. |
| 22 |
*/ |
| 23 |
protected function __construct() { |
| 24 |
$this->db_map_version = apply_filters( |
| 25 |
'lp/upgrade/db/map_version', |
| 26 |
array( |
| 27 |
'3' => 4, // DB v3 need up DB v4 |
| 28 |
'4' => 5, // DB v4 need up DB v5 |
| 29 |
'5' => 6, // DB v4 need up DB v6 |
| 30 |
) |
| 31 |
); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Check LP Database need upgrade. |
| 36 |
* |
| 37 |
* @return bool|int |
| 38 |
* @author tungnx |
| 39 |
* @version 1.0.1 |
| 40 |
* @since 4.0.0 |
| 41 |
*/ |
| 42 |
public function check_lp_db_need_upgrade() { |
| 43 |
if ( ! current_user_can( 'administrator' ) ) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
$db_current_version = (int) get_option( LP_KEY_DB_VERSION, 0 ); |
| 48 |
/*if ( ! $db_current_version ) { |
| 49 |
return false; |
| 50 |
}*/ |
| 51 |
|
| 52 |
$db_require_version = LearnPress::instance()->db_version; |
| 53 |
if ( version_compare( $db_require_version, $db_current_version, '<=' ) ) { |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
if ( array_key_exists( $db_current_version, $this->db_map_version ) ) { |
| 58 |
return $this->db_map_version[ $db_current_version . '' ]; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* For case not have key "learnpress_db_version" on DB, still have columns of learnpress. |
| 63 |
* After a long time, need remove fix fast |
| 64 |
*/ |
| 65 |
$lp_db = LP_Database::getInstance(); |
| 66 |
$check_tb_lp_order_items_exists = $lp_db->check_table_exists( $lp_db->tb_lp_order_items ); |
| 67 |
$check_tb_lp_user_item_results_exists = $lp_db->check_table_exists( $lp_db->tb_lp_user_item_results ); |
| 68 |
$check_col_item_id_on_lp_order_items = $lp_db->check_col_table( $lp_db->tb_lp_order_items, 'item_id' ); |
| 69 |
|
| 70 |
if ( $check_tb_lp_order_items_exists && ( ! $check_tb_lp_user_item_results_exists || ! $check_col_item_id_on_lp_order_items ) ) { |
| 71 |
update_option( LP_KEY_DB_VERSION, 3 ); |
| 72 |
|
| 73 |
return $this->db_map_version['3']; |
| 74 |
} |
| 75 |
|
| 76 |
// End. |
| 77 |
|
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Load file upgrade database. |
| 83 |
* |
| 84 |
* @return null|object |
| 85 |
* @author tungnx |
| 86 |
* @version 1.0.1 |
| 87 |
* @since 4.0.0 |
| 88 |
*/ |
| 89 |
public function load_file_version_upgrade_db() { |
| 90 |
$class_handle = null; |
| 91 |
$db_version_up_to = $this->check_lp_db_need_upgrade(); |
| 92 |
|
| 93 |
if ( $db_version_up_to ) { |
| 94 |
$file_update = LP_PLUGIN_PATH . 'inc/updates/learnpress-upgrade-' . $db_version_up_to . '.php'; |
| 95 |
|
| 96 |
if ( file_exists( $file_update ) ) { |
| 97 |
include_once $file_update; |
| 98 |
$name_class_handle_upgrade = 'LP_Upgrade_' . $db_version_up_to; |
| 99 |
|
| 100 |
if ( class_exists( $name_class_handle_upgrade ) |
| 101 |
&& is_callable( array( $name_class_handle_upgrade, 'get_instance' ) ) ) { |
| 102 |
$class_handle = $name_class_handle_upgrade::get_instance(); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return $class_handle; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Get singleton instance of the class. |
| 112 |
* |
| 113 |
* @return bool|LP_Updater |
| 114 |
*/ |
| 115 |
public static function instance() { |
| 116 |
static $instance; |
| 117 |
if ( is_null( $instance ) ) { |
| 118 |
$instance = new self(); |
| 119 |
} |
| 120 |
|
| 121 |
return $instance; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
return LP_Updater::instance(); |
| 126 |
|