| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class LP_REST_Admin_Tools_Controller |
| 5 |
* |
| 6 |
* @since 4.0.3 |
| 7 |
* @author tungnx |
| 8 |
* @version 1.0.0 |
| 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 |
); |
| 44 |
|
| 45 |
parent::register_routes(); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Create indexs. |
| 50 |
* |
| 51 |
* @param WP_REST_Request $request . |
| 52 |
* |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public function create_indexs( WP_REST_Request $request ) { |
| 56 |
$response = new LP_REST_Response(); |
| 57 |
$lp_db = LP_Database::getInstance(); |
| 58 |
|
| 59 |
try { |
| 60 |
$tables = $request->get_param( 'tables' ); |
| 61 |
$table = $request->get_param( 'table' ); |
| 62 |
$table_keys = array(); |
| 63 |
|
| 64 |
$lp_db->wpdb->query( 'SET autocommit = 0' ); |
| 65 |
|
| 66 |
if ( empty( $tables ) ) { |
| 67 |
throw new Exception( 'Param invalid!' ); |
| 68 |
} else { |
| 69 |
$table_keys = array_keys( $tables ); |
| 70 |
} |
| 71 |
|
| 72 |
if ( empty( $table ) ) { |
| 73 |
$table = $lp_db->tb_lp_user_items; |
| 74 |
} elseif ( array_key_exists( $table, $table_keys ) ) { |
| 75 |
throw new Exception( 'Table invalid!' ); |
| 76 |
} |
| 77 |
|
| 78 |
// Create Indexs for a table. |
| 79 |
if ( $table === $lp_db->tb_lp_question_answermeta ) { |
| 80 |
$lp_db->drop_indexs_table( $lp_db->tb_lp_question_answermeta ); |
| 81 |
$lp_db->wpdb->query( |
| 82 |
" |
| 83 |
ALTER TABLE {$lp_db->tb_lp_question_answermeta} |
| 84 |
ADD INDEX question_answer_meta (`learnpress_question_answer_id`, `meta_key`(150)) |
| 85 |
" |
| 86 |
); |
| 87 |
$lp_db->check_execute_has_error(); |
| 88 |
} elseif ( $table === $lp_db->tb_lp_section_items ) { |
| 89 |
$lp_db->drop_indexs_table( $lp_db->tb_lp_section_items ); |
| 90 |
|
| 91 |
$lp_db->wpdb->query( |
| 92 |
" |
| 93 |
ALTER TABLE {$lp_db->tb_lp_section_items} |
| 94 |
ADD INDEX section_item (`section_id`, `item_id`) |
| 95 |
" |
| 96 |
); |
| 97 |
$lp_db->check_execute_has_error(); |
| 98 |
} else { |
| 99 |
$lp_db->add_indexs_table( $table, $tables[ $table ] ); |
| 100 |
} |
| 101 |
|
| 102 |
// Set next table key. |
| 103 |
$index_key = array_search( $table, $table_keys ); |
| 104 |
++ $index_key; |
| 105 |
|
| 106 |
if ( ! array_key_exists( $index_key, $table_keys ) ) { |
| 107 |
$response->status = 'finished'; |
| 108 |
$response->data->percent = 100; |
| 109 |
} else { |
| 110 |
$response->data->table = $table_keys[ $index_key ]; |
| 111 |
$response->data->percent = 100; |
| 112 |
$response->status = 'success'; |
| 113 |
} |
| 114 |
} catch ( Exception $e ) { |
| 115 |
$response->message = $e->getMessage(); |
| 116 |
} |
| 117 |
|
| 118 |
wp_send_json( $response ); |
| 119 |
} |
| 120 |
|
| 121 |
public function get_list_tables_indexs( WP_REST_Request $request ) { |
| 122 |
$response = new LP_REST_Response(); |
| 123 |
$lp_db = LP_Database::getInstance(); |
| 124 |
|
| 125 |
$tables_indexs = array( |
| 126 |
$lp_db->tb_lp_user_items => array( 'user_id', 'item_id', 'item_type', 'status', 'ref_type', 'ref_id', 'parent_id' ), |
| 127 |
$lp_db->tb_lp_user_itemmeta => array( 'learnpress_user_item_id', 'meta_key', 'meta_value' ), |
| 128 |
$lp_db->tb_lp_quiz_questions => array( 'quiz_id', 'question_id' ), |
| 129 |
$lp_db->tb_lp_question_answers => array( 'question_id' ), |
| 130 |
$lp_db->tb_lp_question_answermeta => '', |
| 131 |
$lp_db->tb_lp_order_items => array( 'order_id', 'item_id', 'item_type' ), |
| 132 |
$lp_db->tb_lp_order_itemmeta => array( 'learnpress_order_item_id', 'meta_key', 'meta_value' ), |
| 133 |
$lp_db->tb_lp_sections => array( 'section_course_id' ), |
| 134 |
$lp_db->tb_lp_section_items => '', |
| 135 |
); |
| 136 |
|
| 137 |
$response->data->tables = $tables_indexs; |
| 138 |
$response->data->table = $lp_db->tb_lp_user_items; |
| 139 |
$response->status = 'success'; |
| 140 |
|
| 141 |
wp_send_json( $response ); |
| 142 |
} |
| 143 |
|
| 144 |
public function clean_tables( WP_REST_Request $request ) { |
| 145 |
$response = new LP_REST_Response(); |
| 146 |
$lp_db_sessions = LP_Sessions_DB::getInstance(); |
| 147 |
$tables = $request->get_param( 'tables' ); |
| 148 |
$item_before_process = $request->get_param( 'itemtotal' ); |
| 149 |
if ( empty( $tables ) ) { |
| 150 |
throw new Exception( 'Param invalid!' ); |
| 151 |
} |
| 152 |
|
| 153 |
if ( empty( $item_before_process ) ) { |
| 154 |
$item_before_process = 0; |
| 155 |
} |
| 156 |
|
| 157 |
if ( $item_before_process == 0 ) { |
| 158 |
$response->data->percent == 100; |
| 159 |
$response->status = 'finished'; |
| 160 |
wp_send_json( $response ); |
| 161 |
} |
| 162 |
|
| 163 |
try { |
| 164 |
// Delete resuilt in table select |
| 165 |
if ( $tables == 'learnpress_sessions' ) { |
| 166 |
$lp_db_sessions->delete_rows(); |
| 167 |
// check the number of lines remaining after each query |
| 168 |
$item_after_process = $lp_db_sessions->count_row_db_sessions(); |
| 169 |
$response->data->processed = $item_before_process - $item_after_process; |
| 170 |
$percent = ( ( $item_before_process - $item_after_process ) / $item_before_process ) * 100; |
| 171 |
$response->data->percent = number_format_i18n( $percent, '2' ); |
| 172 |
} |
| 173 |
if ( $response->data->percent == 100 ) { |
| 174 |
$response->status = 'finished'; |
| 175 |
} else { |
| 176 |
$response->status = 'success'; |
| 177 |
} |
| 178 |
} catch ( Exception $e ) { |
| 179 |
$response->message = $e->getMessage(); |
| 180 |
} |
| 181 |
wp_send_json( $response ); |
| 182 |
} |
| 183 |
} |
| 184 |
|