PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9
4.4.9 4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 All 140 releases
learnpress / inc / rest-api / v1 / admin / class-lp-admin-rest-database-controller.php

class-lp-admin-rest-database-controller.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.9, at inc/rest-api/v1/admin/class-lp-admin-rest-database-controller.php

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