PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.3.2
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.3.2
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 4.2.0 All 139 releases
learnpress / inc / rest-api / v1 / admin / class-lp-admin-rest-tools-controller.php

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

280 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class LP_REST_Admin_Tools_Controller
5 *
6 * @since 4.0.3
7 * @author tungnx
8 * @version 1.0.1
9 */
10 class LP_REST_Admin_Tools_Controller extends LP_Abstract_REST_Controller {
11 public function __construct() {
12 $this->namespace = 'lp/v1/admin';
13 $this->rest_base = 'tools';
14 parent::__construct();
15 }
16
17 /**
18 * Register rest routes.
19 */
20 public function register_routes() {
21 $this->routes = array(
22 'create-indexs' => array(
23 array(
24 'methods' => WP_REST_Server::CREATABLE,
25 'callback' => array( $this, 'create_indexs' ),
26 'permission_callback' => '__return_true',
27 ),
28 ),
29 'list-tables-indexs' => array(
30 array(
31 'methods' => WP_REST_Server::CREATABLE,
32 'callback' => array( $this, 'get_list_tables_indexs' ),
33 'permission_callback' => '__return_true',
34 ),
35 ),
36 'clean-tables' => array(
37 array(
38 'methods' => WP_REST_Server::CREATABLE,
39 'callback' => array( $this, 'clean_tables' ),
40 'permission_callback' => '__return_true',
41 ),
42 ),
43 'admin-notices' => array(
44 array(
45 'methods' => WP_REST_Server::ALLMETHODS,
46 'callback' => array( $this, 'admin_notices' ),
47 'permission_callback' => '__return_true',
48 ),
49 ),
50 );
51
52 parent::register_routes();
53 }
54
55 /**
56 * Create indexs.
57 *
58 * @param WP_REST_Request $request .
59 *
60 * @return void
61 */
62 public function create_indexs( WP_REST_Request $request ) {
63 $response = new LP_REST_Response();
64 $lp_db = LP_Database::getInstance();
65
66 try {
67 $tables = $request->get_param( 'tables' );
68 $table = $request->get_param( 'table' );
69 $table_keys = array();
70
71 $lp_db->wpdb->query( 'SET autocommit = 0' );
72
73 if ( empty( $tables ) ) {
74 throw new Exception( 'Param invalid!' );
75 } else {
76 $table_keys = array_keys( $tables );
77 }
78
79 if ( empty( $table ) ) {
80 $table = $lp_db->tb_lp_user_items;
81 } elseif ( array_key_exists( $table, $table_keys ) ) {
82 throw new Exception( 'Table invalid!' );
83 }
84
85 // Create Indexs for a table.
86 if ( $table === $lp_db->tb_lp_question_answermeta ) {
87 $lp_db->drop_indexs_table( $lp_db->tb_lp_question_answermeta );
88 $lp_db->wpdb->query(
89 "
90 ALTER TABLE {$lp_db->tb_lp_question_answermeta}
91 ADD INDEX question_answer_meta (`learnpress_question_answer_id`, `meta_key`(150))
92 "
93 );
94 $lp_db->check_execute_has_error();
95 } elseif ( $table === $lp_db->tb_lp_section_items ) {
96 $lp_db->drop_indexs_table( $lp_db->tb_lp_section_items );
97
98 $lp_db->wpdb->query(
99 "
100 ALTER TABLE {$lp_db->tb_lp_section_items}
101 ADD INDEX section_item (`section_id`, `item_id`)
102 "
103 );
104 $lp_db->check_execute_has_error();
105 } else {
106 $lp_db->add_indexs_table( $table, $tables[ $table ] );
107 }
108
109 // Set next table key.
110 $index_key = array_search( $table, $table_keys );
111 ++ $index_key;
112
113 if ( ! array_key_exists( $index_key, $table_keys ) ) {
114 $response->status = 'finished';
115 $response->data->percent = 100;
116 } else {
117 $response->data->table = $table_keys[ $index_key ];
118 $response->data->percent = 100;
119 $response->status = 'success';
120 }
121 } catch ( Exception $e ) {
122 $response->message = $e->getMessage();
123 }
124
125 wp_send_json( $response );
126 }
127
128 public function get_list_tables_indexs( WP_REST_Request $request ) {
129 $response = new LP_REST_Response();
130 $lp_db = LP_Database::getInstance();
131
132 $tables_indexs = array(
133 $lp_db->tb_lp_user_items => array( 'user_id', 'item_id', 'item_type', 'status', 'ref_type', 'ref_id', 'parent_id' ),
134 $lp_db->tb_lp_user_itemmeta => array( 'learnpress_user_item_id', 'meta_key', 'meta_value' ),
135 $lp_db->tb_lp_quiz_questions => array( 'quiz_id', 'question_id' ),
136 $lp_db->tb_lp_question_answers => array( 'question_id' ),
137 $lp_db->tb_lp_question_answermeta => '',
138 $lp_db->tb_lp_order_items => array( 'order_id', 'item_id', 'item_type' ),
139 $lp_db->tb_lp_order_itemmeta => array( 'learnpress_order_item_id', 'meta_key', 'meta_value' ),
140 $lp_db->tb_lp_sections => array( 'section_course_id' ),
141 $lp_db->tb_lp_section_items => '',
142 );
143
144 $response->data->tables = $tables_indexs;
145 $response->data->table = $lp_db->tb_lp_user_items;
146 $response->status = 'success';
147
148 wp_send_json( $response );
149 }
150
151 public function clean_tables( WP_REST_Request $request ) {
152 $response = new LP_REST_Response();
153 $lp_db_sessions = LP_Sessions_DB::getInstance();
154 $tables = $request->get_param( 'tables' );
155 $item_before_process = $request->get_param( 'itemtotal' );
156 if ( empty( $tables ) ) {
157 throw new Exception( 'Param invalid!' );
158 }
159
160 if ( empty( $item_before_process ) ) {
161 $item_before_process = 0;
162 }
163
164 if ( $item_before_process == 0 ) {
165 $response->data->percent = 100;
166 $response->status = 'finished';
167 wp_send_json( $response );
168 }
169
170 try {
171 // Delete result in table select
172 if ( $tables == 'learnpress_sessions' ) {
173 $lp_db_sessions->delete_rows();
174 // check the number of lines remaining after each query
175 $item_after_process = $lp_db_sessions->count_row_db_sessions();
176 $response->data->processed = $item_before_process - $item_after_process;
177 $percent = ( ( $item_before_process - $item_after_process ) / $item_before_process ) * 100;
178 $response->data->percent = number_format_i18n( $percent, '2' );
179 }
180 if ( $response->data->percent == 100 ) {
181 $response->status = 'finished';
182 } else {
183 $response->status = 'success';
184 }
185 } catch ( Exception $e ) {
186 $response->message = $e->getMessage();
187 }
188 wp_send_json( $response );
189 }
190
191 /**
192 * Show admin notices.
193 *
194 * @param WP_REST_Request $request
195 *
196 * @return void
197 * @since 4.1.7.3.2
198 * @version 1.0.0
199 */
200 public function admin_notices( WP_REST_Request $request ) {
201 $response = new LP_REST_Response();
202 $content = '';
203
204 try {
205 $params = $request->get_params();
206 $admin_notices = $_SESSION['lp_admin_notices_dismiss'] ?? [];
207 $lp_beta_version_info = LP_Admin_Notice::check_lp_beta_version();
208
209 if ( isset( $params['dismiss'] ) ) {
210 if ( $lp_beta_version_info ) {
211 // Store version of LP beta to session.
212 $_SESSION['lp_beta_version'] = $lp_beta_version_info['version'] ?? 0;
213 }
214 $admin_notices[ $params['dismiss'] ] = $params['dismiss'];
215 $_SESSION['lp_admin_notices_dismiss'] = $admin_notices;
216 $response->message = __( 'Dismissed!', 'learnpress' );
217 } else {
218 $show_notice_lp_beta_version = false;
219 /**
220 * Check if LP beta version is not dismissed or dismissed version lower than current version, will bet to so
221 */
222 if ( $lp_beta_version_info && ! isset( $_GET['tab'] ) &&
223 ( ! isset( $_SESSION['lp_beta_version'] ) || version_compare( $_SESSION['lp_beta_version'], $lp_beta_version_info['version'], '<' ) ) ) {
224 $show_notice_lp_beta_version = true;
225 }
226
227 $rules = apply_filters(
228 'learn-press/admin-notices',
229 [
230 // Check wp_remote call success.
231 'check_wp_remote' => [
232 'template' => 'admin-notices/wp-remote.php',
233 'check' => LP_Admin_Ajax::check_wp_remote(),
234 ],
235 // Check name plugin base.
236 'check_plugin_base' => [
237 'template' => 'admin-notices/plugin-base.php',
238 'check' => LP_Admin_Notice::check_plugin_base(),
239 ],
240 // Show beta version of LP.
241 'lp-beta-version' => [
242 'template' => 'admin-notices/beta-version.php',
243 'check' => $show_notice_lp_beta_version,
244 'info' => $lp_beta_version_info,
245 'dismiss' => 1,
246 ],
247 // Show message needs upgrades database compatible with LP version current.
248 'lp-upgrade-db' => [
249 'template' => 'admin-notices/upgrade-db.php',
250 'check' => LP_Updater::instance()->check_lp_db_need_upgrade(),
251 ],
252 // Show message wrong permalink structure.
253 'lp-permalink' => [
254 'template' => 'admin-notices/permalink-wrong.php',
255 'check' => ! get_option( 'permalink_structure' ),
256 ],
257 // Show notice setup wizard.
258 'lp-setup-wizard' => [
259 'template' => 'admin-notices/setup-wizard.php',
260 'check' => ! get_option( 'learn_press_setup_wizard_completed', false ) && ! isset( $admin_notices['lp-setup-wizard'] ),
261 'dismiss' => 1,
262 ],
263 ]
264 );
265
266 foreach ( $rules as $template_data ) {
267 $content .= learn_press_admin_view( $template_data['template'] ?? '', [ 'data' => $template_data ], true, true );
268 }
269 }
270
271 $response->status = 'success';
272 $response->data->content = $content;
273 } catch ( Exception $e ) {
274 $response->message = $e->getMessage();
275 }
276
277 wp_send_json( $response );
278 }
279 }
280