| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class LP_REST_Users_Controller |
| 5 |
* |
| 6 |
* @since 3.3.0 |
| 7 |
*/ |
| 8 |
class LP_REST_Admin_Database_Controller extends LP_Abstract_REST_Controller { |
| 9 |
/** |
| 10 |
* @var LP_User |
| 11 |
*/ |
| 12 |
protected $user = null; |
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
$this->namespace = 'lp/v1'; |
| 16 |
$this->rest_base = 'database'; |
| 17 |
parent::__construct(); |
| 18 |
|
| 19 |
add_filter( 'rest_pre_dispatch', array( $this, 'rest_pre_dispatch' ), 10, 3 ); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Init data prepares for callbacks of rest |
| 24 |
* |
| 25 |
* @param $null |
| 26 |
* @param WP_REST_Server $server |
| 27 |
* @param WP_REST_Request $request |
| 28 |
* |
| 29 |
* @return mixed |
| 30 |
*/ |
| 31 |
public function rest_pre_dispatch( $response, $handler, $request ) { |
| 32 |
|
| 33 |
return $response; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Register rest routes. |
| 38 |
*/ |
| 39 |
public function register_routes() { |
| 40 |
$this->routes = array( |
| 41 |
'upgrade' => array( |
| 42 |
array( |
| 43 |
'methods' => WP_REST_Server::CREATABLE, |
| 44 |
'callback' => array( $this, 'upgrade' ), |
| 45 |
'permission_callback' => array( $this, 'check_admin_permission' ), |
| 46 |
), |
| 47 |
), |
| 48 |
'get_steps' => array( |
| 49 |
array( |
| 50 |
'methods' => WP_REST_Server::CREATABLE, |
| 51 |
'callback' => array( $this, 'get_steps' ), |
| 52 |
'permission_callback' => array( $this, 'check_admin_permission' ), |
| 53 |
), |
| 54 |
), |
| 55 |
'agree_terms' => array( |
| 56 |
array( |
| 57 |
'methods' => WP_REST_Server::CREATABLE, |
| 58 |
'callback' => array( $this, 'agree_terms_upgrade' ), |
| 59 |
'permission_callback' => array( $this, 'check_admin_permission' ), |
| 60 |
), |
| 61 |
), |
| 62 |
'check-db-valid-re-upgrade' => array( |
| 63 |
array( |
| 64 |
'methods' => WP_REST_Server::CREATABLE, |
| 65 |
'callback' => array( $this, 'check_DB_valid_to_re_upgrade' ), |
| 66 |
'permission_callback' => array( $this, 'check_admin_permission' ), |
| 67 |
), |
| 68 |
), |
| 69 |
'del-tb-lp-upgrade-db' => array( |
| 70 |
array( |
| 71 |
'methods' => WP_REST_Server::CREATABLE, |
| 72 |
'callback' => array( $this, 'delete_tb_lp_upgrade_db' ), |
| 73 |
'permission_callback' => array( $this, 'check_admin_permission' ), |
| 74 |
), |
| 75 |
), |
| 76 |
); |
| 77 |
|
| 78 |
parent::register_routes(); |
| 79 |
} |
| 80 |
|
| 81 |
public function check_admin_permission() { |
| 82 |
return LP_Abstract_API::check_admin_permission(); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Set agree terms upgrade database. |
| 87 |
* |
| 88 |
* @param WP_REST_Request $request . |
| 89 |
* |
| 90 |
* @return void |
| 91 |
*/ |
| 92 |
public function agree_terms_upgrade( WP_REST_Request $request ) { |
| 93 |
$result = new LP_REST_Response(); |
| 94 |
|
| 95 |
if ( $request->get_param( 'agree_terms' ) ) { |
| 96 |
LP_Settings::update_option( 'agree_terms', LP_Updater::instance()->check_lp_db_need_upgrade() ); |
| 97 |
$result->status = 'success'; |
| 98 |
} |
| 99 |
|
| 100 |
wp_send_json( $result ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Upgrade DB |
| 105 |
* |
| 106 |
* @param WP_REST_Request $request . |
| 107 |
* |
| 108 |
* @return void |
| 109 |
*/ |
| 110 |
public function upgrade( WP_REST_Request $request ) { |
| 111 |
$lp_updater = LP_Updater::instance(); |
| 112 |
$result = new LP_REST_Response(); |
| 113 |
$class_handle = $lp_updater->load_file_version_upgrade_db(); |
| 114 |
|
| 115 |
if ( empty( $class_handle ) ) { |
| 116 |
$result->message = sprintf( |
| 117 |
'%s %s', |
| 118 |
__( 'The LP Database is Latest:', 'learnpress' ), |
| 119 |
'v' . get_option( LP_KEY_DB_VERSION ) |
| 120 |
); |
| 121 |
wp_send_json( $result ); |
| 122 |
} |
| 123 |
|
| 124 |
$params = $request->get_params(); |
| 125 |
|
| 126 |
wp_send_json( $class_handle->handle( $params ) ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Get Steps upgrade completed. |
| 131 |
*/ |
| 132 |
public function get_steps() { |
| 133 |
$lp_updater = LP_Updater::instance(); |
| 134 |
$lp_db = LP_Database::getInstance(); |
| 135 |
$steps_completed = array(); |
| 136 |
$steps_default = array(); |
| 137 |
|
| 138 |
$class_handle = $lp_updater->load_file_version_upgrade_db(); |
| 139 |
|
| 140 |
if ( ! empty( $class_handle ) ) { |
| 141 |
$steps_default = $class_handle->group_steps; |
| 142 |
|
| 143 |
$tb_lp_upgrade_db_exists = $lp_db->check_table_exists( $lp_db->tb_lp_upgrade_db ); |
| 144 |
|
| 145 |
if ( $tb_lp_upgrade_db_exists ) { |
| 146 |
$steps_completed = $lp_db->get_steps_completed(); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
$steps = array( |
| 151 |
'steps_default' => $steps_default, |
| 152 |
'steps_completed' => $steps_completed, |
| 153 |
); |
| 154 |
|
| 155 |
wp_send_json( $steps ); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Check DB valid to re upgrade |
| 160 |
* |
| 161 |
* can_re_upgrade | 1: can, 0: can't |
| 162 |
*/ |
| 163 |
public function check_DB_valid_to_re_upgrade() { |
| 164 |
$response = new LP_REST_Response(); |
| 165 |
$lp_db = LP_Database::getInstance(); |
| 166 |
$response->data->can_re_upgrade = 0; |
| 167 |
|
| 168 |
$col_start_time_gmt_exist = $lp_db->check_col_table( $lp_db->tb_lp_user_items, 'start_time_gmt' ); |
| 169 |
$col_graduation_exist = $lp_db->check_col_table( $lp_db->tb_lp_user_items, 'graduation' ); |
| 170 |
$col_item_id_exist = $lp_db->check_col_table( $lp_db->tb_lp_order_items, 'item_id' ); |
| 171 |
|
| 172 |
if ( $col_start_time_gmt_exist || ! $col_graduation_exist || ! $col_item_id_exist ) { |
| 173 |
$response->data->can_re_upgrade = 1; |
| 174 |
} |
| 175 |
|
| 176 |
$response->status = 'success'; |
| 177 |
|
| 178 |
wp_send_json( $response ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Remove table lp_upgrade_db |
| 183 |
* |
| 184 |
* can_re_upgrade | 1: can, 0: can't |
| 185 |
*/ |
| 186 |
public function delete_tb_lp_upgrade_db() { |
| 187 |
$response = new LP_REST_Response(); |
| 188 |
$lp_db = LP_Database::getInstance(); |
| 189 |
$response->data->can_re_upgrade = 0; |
| 190 |
$tables = array( |
| 191 |
$lp_db->tb_lp_user_items, |
| 192 |
$lp_db->tb_lp_user_itemmeta, |
| 193 |
$lp_db->tb_lp_question_answers, |
| 194 |
$lp_db->tb_postmeta, |
| 195 |
$lp_db->tb_options, |
| 196 |
); |
| 197 |
|
| 198 |
try { |
| 199 |
$result = $lp_db->drop_table( $lp_db->tb_lp_upgrade_db ); |
| 200 |
update_option( LP_KEY_DB_VERSION, 3 ); |
| 201 |
|
| 202 |
foreach ( $tables as $table ) { |
| 203 |
// Drop - Rename tables bk. |
| 204 |
$tb_bk = $table . '_bk'; |
| 205 |
$tb_bk_exists = $lp_db->check_table_exists( $tb_bk ); |
| 206 |
if ( $tb_bk_exists ) { |
| 207 |
$lp_db->drop_table( $table ); |
| 208 |
$lp_db->rename_table( $tb_bk, $table ); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
if ( $result ) { |
| 213 |
$response->status = 'success'; |
| 214 |
$response->data->url = admin_url( 'admin.php?page=learn-press-tools&tab=database&action=upgrade-db' ); |
| 215 |
} |
| 216 |
} catch ( Exception $e ) { |
| 217 |
$response->message = $e->getMessage(); |
| 218 |
} |
| 219 |
|
| 220 |
wp_send_json( $response ); |
| 221 |
} |
| 222 |
} |
| 223 |
|