| 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(); |
| 119 |
|