PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
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
← All changes | inc/admin/class-lp-reset-data.php +118 -326 4.3.94.4.8 View file →
@@ -1,326 +1,118 @@
1 -<?php
2 -class LP_Reset_Data {
3 - public static function init() {
4 - $ajax_events = array(
5 - 'search-users',
6 - 'reset-user-courses' => 'ajax_reset_user_courses',
7 - 'reset-user-item' => 'ajax_reset_user_item',
8 - );
9 -
10 - foreach ( $ajax_events as $action => $callback ) {
11 - if ( is_numeric( $action ) ) {
12 - $action = $callback;
13 - }
14 -
15 - $actions = LP_Request::parse_action( $action );
16 - $method = $actions['action'];
17 -
18 - if ( ! is_callable( $callback ) ) {
19 - $callback = array( __CLASS__, $callback );
20 -
21 - if ( ! is_callable( $callback ) ) {
22 - $method = preg_replace( '/-/', '_', $method );
23 - $callback = array( __CLASS__, $method );
24 - }
25 - }
26 - LP_Request::register_ajax( "rs-{$action}", $callback );
27 - }
28 - }
29 -
30 - public static function ajax_reset_user_item() {
31 - if ( ! current_user_can( ADMIN_ROLE ) ) {
32 - return;
33 - }
34 -
35 - $nonce = LP_Request::get_param( 'nonce' );
36 - if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
37 - die( 'Nonce is invalid!' );
38 - }
39 -
40 - $user_id = LP_Request::get_string( 'user_id' );
41 - $item_id = LP_Request::get_int( 'item_id' );
42 -
43 - if ( ! is_numeric( $user_id ) ) {
44 - $user_email = get_user_by( 'email', $user_id );
45 - $user_login = get_user_by( 'login', $user_id );
46 -
47 - if ( $user_email ) {
48 - $user_id = $user_email->ID;
49 - } elseif ( $user_login ) {
50 - $user_id = $user_login->ID;
51 - }
52 - }
53 -
54 - global $wpdb;
55 -
56 - $query = $wpdb->prepare(
57 - "
58 - SELECT user_item_id
59 - FROM {$wpdb->learnpress_user_items}
60 - WHERE user_id = %d AND item_id = %d
61 - ",
62 - $user_id,
63 - $item_id
64 - );
65 -
66 - $user_item_ids = $wpdb->get_col( $query );
67 - if ( $user_item_ids ) {
68 - $query = "
69 - SELECT DISTINCT parent_id AS parent, item_id
70 - FROM {$wpdb->learnpress_user_items}
71 - WHERE user_item_id IN(" . join( ',', $user_item_ids ) . ')
72 - ';
73 - $parents = $wpdb->get_results( $query );
74 -
75 - $format = array_fill( 0, sizeof( $user_item_ids ), '%d' );
76 - $query = $wpdb->prepare(
77 - "
78 - DELETE
79 - FROM {$wpdb->learnpress_user_itemmeta}
80 - WHERE learnpress_user_item_id IN(" . join( ',', $format ) . ')
81 - ',
82 - $user_item_ids
83 - );
84 - $wpdb->query( $query );
85 -
86 - $query = $wpdb->prepare(
87 - "
88 - DELETE
89 - FROM {$wpdb->learnpress_user_items}
90 - WHERE user_id = %d AND item_id = %d
91 - ",
92 - $user_id,
93 - $item_id
94 - );
95 -
96 - $wpdb->query( $query );
97 -
98 - if ( $parents ) {
99 - foreach ( $parents as $parent ) {
100 - $retaken_items = learn_press_get_user_item_meta( $parent->parent, '_retaken_items', true );
101 - if ( $retaken_items ) {
102 - if ( ! isset( $retaken_items[ $parent->item_id ] ) ) {
103 - continue;
104 - }
105 -
106 - unset( $retaken_items[ $parent->item_id ] );
107 - learn_press_update_user_item_meta( $parent->parent, '_retaken_items', $retaken_items );
108 - }
109 - }
110 - }
111 -
112 - echo __( 'Item progress is deleted', 'learnpress' );
113 - } else {
114 - echo __( 'No data found', 'learnpress' );
115 - }
116 - // LP_Debug::rollbackTransaction();
117 - die();
118 - }
119 -
120 - public static function search_users() {
121 - global $wpdb;
122 -
123 - $nonce = LP_Request::get_param( 'nonce' );
124 - if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
125 - die( 'Nonce is invalid!' );
126 - }
127 -
128 - if ( ! current_user_can( 'administrator' ) ) {
129 - return;
130 - }
131 -
132 - $s = LP_Request::get_string( 's' );
133 - $query = $wpdb->prepare(
134 - "
135 - SELECT ID AS id, user_login AS username, user_email AS email, '' AS status
136 - FROM {$wpdb->users}
137 - WHERE user_login LIKE %s
138 - OR user_email LIKE %s
139 - ",
140 - '%' . $wpdb->esc_like( $s ) . '%',
141 - '%' . $wpdb->esc_like( $s ) . '%'
142 - );
143 -
144 - $users = array();
145 -
146 - $rows = $wpdb->get_results( $query );
147 - if ( $rows ) {
148 - $user_ids = wp_list_pluck( $rows, 'id' );
149 - $format = array_fill( 0, sizeof( $user_ids ), '%d' );
150 - $args = $user_ids;
151 - $args[] = LP_COURSE_CPT;
152 - $query = $wpdb->prepare( "SELECT user_id, item_id FROM {$wpdb->learnpress_user_items} WHERE user_id IN(" . join( ',', $format ) . ') AND item_type = %s', $args );
153 -
154 - $items = $wpdb->get_results( $query );
155 - if ( $items ) {
156 - $uids = wp_list_pluck( $items, 'user_id' );
157 - for ( $n = sizeof( $rows ), $i = $n - 1; $i >= 0; $i -- ) {
158 -
159 - if ( ! in_array( $rows[ $i ]->id, $uids ) ) {
160 - unset( $rows[ $i ] );
161 - continue;
162 - }
163 -
164 - if ( empty( $rows[ $i ]->courses ) ) {
165 - $rows[ $i ]->courses = array();
166 - }
167 - foreach ( $items as $item ) {
168 - if ( $item->user_id == $rows[ $i ]->id ) {
169 - $rows[ $i ]->courses[ $item->item_id ] = array(
170 - 'url' => get_the_permalink( $item->item_id ),
171 - 'id' => $item->item_id,
172 - 'title' => get_the_title( $item->item_id ),
173 - );
174 - }
175 - }
176 - }
177 - } else {
178 - $rows = array();
179 - }
180 -
181 - if ( $rows ) {
182 - foreach ( $rows as $k => $row ) {
183 - $users[] = $row;
184 - if ( sizeof( $users ) > 100 ) {
185 - break;
186 - }
187 - }
188 - }
189 - }
190 -
191 - learn_press_send_json( $users );
192 - }
193 -
194 - public static function ajax_reset_user_courses() {
195 - $user_id = LP_Request::get_int( 'user_id' );
196 - $course_id = LP_Request::get_int( 'course_id' );
197 - $object_reset = LP_Request::get_string( 'object_reset' );
198 - $user = learn_press_get_user( $user_id );
199 - if ( ! current_user_can( 'administrator' ) ) {
200 - return;
201 - }
202 -
203 - $nonce = LP_Request::get_param( 'nonce' );
204 - if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
205 - die( 'Nonce is invalid!' );
206 - }
207 -
208 - if ( $course_id && $object_reset == 'single' ) {
209 - $user_course_data = $user->get_course_data( $course_id );
210 - if ( ! $user_course_data ) {
211 - return;
212 - }
213 -
214 - // Set status, start_time, end_time of course to enrol.
215 - $user_course_data->set_status( LP_COURSE_ENROLLED )
216 - ->set_start_time( time() )
217 - ->set_end_time()
218 - ->set_graduation( LP_COURSE_GRADUATION_IN_PROGRESS )
219 - ->update();
220 - // Remove items' course user learned.
221 - $filter_remove = new LP_User_Items_Filter();
222 - $filter_remove->parent_id = $user_course_data->get_user_item_id();
223 - $filter_remove->user_id = $user_course_data->get_user_id();
224 - $filter_remove->limit = - 1;
225 - LP_User_Items_DB::getInstance()->remove_items_of_user_course( $filter_remove );
226 - }
227 - if ( $object_reset == 'all' && $user_id ) {
228 - global $wpdb;
229 - $query = $wpdb->prepare(
230 - "
231 - SELECT item_id
232 - FROM {$wpdb->learnpress_user_items}
233 - WHERE user_id = %d AND item_type='lp_course'
234 - ",
235 - $user_id
236 - );
237 - $user_item_ids = $wpdb->get_col( $query );
238 - if ( $user_item_ids ) {
239 - foreach ( $user_item_ids as $user_item_id ) {
240 - $course_id = $user_item_id;
241 - $user_course_data = $user->get_course_data( $course_id );
242 - if ( ! $user_course_data ) {
243 - continue;
244 - }
245 -
246 - // Set status, start_time, end_time of course to enrolled.
247 - $user_course_data->set_status( LP_COURSE_ENROLLED )
248 - ->set_start_time( time() )
249 - ->set_end_time()
250 - ->set_graduation( LP_COURSE_GRADUATION_IN_PROGRESS )
251 - ->update();
252 - // Remove items' course user learned.
253 - $filter_remove = new LP_User_Items_Filter();
254 - $filter_remove->parent_id = $user_course_data->get_user_item_id();
255 - $filter_remove->user_id = $user_course_data->get_user_id();
256 - $filter_remove->limit = - 1;
257 - LP_User_Items_DB::getInstance()->remove_items_of_user_course( $filter_remove );
258 - }
259 - }
260 - }
261 -
262 - die();
263 - }
264 -
265 - public static function get_user_item_courses( $course_id, $user_id = 0 ) {
266 - global $wpdb;
267 -
268 - $where = '';
269 - if ( $user_id ) {
270 - $where = $wpdb->prepare( 'AND user_id = %d', $user_id );
271 - }
272 -
273 - $query = $wpdb->prepare(
274 - "
275 - SELECT *
276 - FROM {$wpdb->learnpress_user_items}
277 - WHERE item_type = %s
278 - AND item_id = %d
279 - $where
280 - ",
281 - LP_COURSE_CPT,
282 - $course_id
283 - );
284 -
285 - echo "$query\n";
286 -
287 - return $wpdb->get_results( $query );
288 - }
289 -
290 - public static function delete_user_items_by_id( $ids ) {
291 - settype( $ids, 'array' );
292 -
293 - global $wpdb;
294 -
295 - // Delete meta
296 - $format = array_fill( 0, sizeof( $ids ), '%d' );
297 - $query = $wpdb->prepare( "DELETE FROM {$wpdb->learnpress_user_itemmeta} WHERE learnpress_user_item_id IN(" . join( ',', $format ) . ')', $ids );
298 - $wpdb->query( $query );
299 - echo "$query\n";
300 -
301 - // Delete items
302 - $query = $wpdb->prepare( "DELETE FROM {$wpdb->learnpress_user_items} WHERE user_item_id IN(" . join( ',', $format ) . ')', $ids );
303 - $wpdb->query( $query );
304 -
305 - echo "$query\n";
306 -
307 - }
308 -
309 - public static function get_user_items_by_parent( $parent_id ) {
310 - global $wpdb;
311 -
312 - $query = $wpdb->prepare(
313 - "
314 - SELECT *
315 - FROM {$wpdb->learnpress_user_items}
316 - WHERE parent_id = %d
317 - ",
318 - $parent_id
319 - );
320 -
321 - echo "$query\n";
322 -
323 - return $wpdb->get_results( $query );
324 - }
325 -}
326 -LP_Reset_Data::init();
1 +<?php
2 +class LP_Reset_Data {
3 + public static function init() {
4 + $ajax_events = array(
5 + 'reset-user-item' => 'ajax_reset_user_item',
6 + );
7 +
8 + foreach ( $ajax_events as $action => $callback ) {
9 + if ( is_numeric( $action ) ) {
10 + $action = $callback;
11 + }
12 +
13 + $actions = LP_Request::parse_action( $action );
14 + $method = $actions['action'];
15 +
16 + if ( ! is_callable( $callback ) ) {
17 + $callback = array( __CLASS__, $callback );
18 +
19 + if ( ! is_callable( $callback ) ) {
20 + $method = preg_replace( '/-/', '_', $method );
21 + $callback = array( __CLASS__, $method );
22 + }
23 + }
24 + LP_Request::register_ajax( "rs-{$action}", $callback );
25 + }
26 + }
27 +
28 + public static function ajax_reset_user_item() {
29 + if ( ! current_user_can( ADMIN_ROLE ) ) {
30 + return;
31 + }
32 +
33 + $nonce = LP_Request::get_param( 'nonce' );
34 + if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
35 + die( 'Nonce is invalid!' );
36 + }
37 +
38 + $user_id = LP_Request::get_string( 'user_id' );
39 + $item_id = LP_Request::get_int( 'item_id' );
40 +
41 + if ( ! is_numeric( $user_id ) ) {
42 + $user_email = get_user_by( 'email', $user_id );
43 + $user_login = get_user_by( 'login', $user_id );
44 +
45 + if ( $user_email ) {
46 + $user_id = $user_email->ID;
47 + } elseif ( $user_login ) {
48 + $user_id = $user_login->ID;
49 + }
50 + }
51 +
52 + global $wpdb;
53 +
54 + $query = $wpdb->prepare(
55 + "
56 + SELECT user_item_id
57 + FROM {$wpdb->learnpress_user_items}
58 + WHERE user_id = %d AND item_id = %d
59 + ",
60 + $user_id,
61 + $item_id
62 + );
63 +
64 + $user_item_ids = $wpdb->get_col( $query );
65 + if ( $user_item_ids ) {
66 + $query = "
67 + SELECT DISTINCT parent_id AS parent, item_id
68 + FROM {$wpdb->learnpress_user_items}
69 + WHERE user_item_id IN(" . join( ',', $user_item_ids ) . ')
70 + ';
71 + $parents = $wpdb->get_results( $query );
72 +
73 + $format = array_fill( 0, sizeof( $user_item_ids ), '%d' );
74 + $query = $wpdb->prepare(
75 + "
76 + DELETE
77 + FROM {$wpdb->learnpress_user_itemmeta}
78 + WHERE learnpress_user_item_id IN(" . join( ',', $format ) . ')
79 + ',
80 + $user_item_ids
81 + );
82 + $wpdb->query( $query );
83 +
84 + $query = $wpdb->prepare(
85 + "
86 + DELETE
87 + FROM {$wpdb->learnpress_user_items}
88 + WHERE user_id = %d AND item_id = %d
89 + ",
90 + $user_id,
91 + $item_id
92 + );
93 +
94 + $wpdb->query( $query );
95 +
96 + if ( $parents ) {
97 + foreach ( $parents as $parent ) {
98 + $retaken_items = learn_press_get_user_item_meta( $parent->parent, '_retaken_items', true );
99 + if ( $retaken_items ) {
100 + if ( ! isset( $retaken_items[ $parent->item_id ] ) ) {
101 + continue;
102 + }
103 +
104 + unset( $retaken_items[ $parent->item_id ] );
105 + learn_press_update_user_item_meta( $parent->parent, '_retaken_items', $retaken_items );
106 + }
107 + }
108 + }
109 +
110 + echo __( 'Item progress is deleted', 'learnpress' );
111 + } else {
112 + echo __( 'No data found', 'learnpress' );
113 + }
114 + // LP_Debug::rollbackTransaction();
115 + die();
116 + }
117 +}
118 +LP_Reset_Data::init();