| 1 |
<?php |
| 2 |
/** |
| 3 |
* Reset Data Tool |
| 4 |
* |
| 5 |
* @package GamiPress\Admin\Tools\Reset_Data |
| 6 |
* @author GamiPress <contact@gamipress.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 7 |
* @since 1.1.7 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Register Reset Data Tool meta boxes |
| 14 |
* |
| 15 |
* @since 1.1.7 |
| 16 |
* |
| 17 |
* @param array $meta_boxes |
| 18 |
* |
| 19 |
* @return array |
| 20 |
*/ |
| 21 |
function gamipress_reset_data_tool_meta_boxes( $meta_boxes ) { |
| 22 |
|
| 23 |
$meta_boxes['reset-data'] = array( |
| 24 |
'title' => gamipress_dashicon( 'trash' ) . __( 'Reset Data', 'gamipress' ), |
| 25 |
'fields' => apply_filters( 'gamipress_reset_data_tool_fields', array( |
| 26 |
'data_to_reset' => array( |
| 27 |
'desc' => __( 'Choose the stored data you want to reset.', 'gamipress' ), |
| 28 |
'type' => 'multicheck', |
| 29 |
'classes' => 'gamipress-switch', |
| 30 |
'options' => array( |
| 31 |
'achievement_types' => __( 'Achievement Types', 'gamipress' ), |
| 32 |
'achievements' => __( 'Achievements', 'gamipress' ), |
| 33 |
'steps' => __( 'Steps', 'gamipress' ), |
| 34 |
'points_types' => __( 'Points Types', 'gamipress' ), |
| 35 |
'points_awards' => __( 'Points Awards', 'gamipress' ), |
| 36 |
'points_deducts' => __( 'Points Deductions', 'gamipress' ), |
| 37 |
'rank_types' => __( 'Rank Types', 'gamipress' ), |
| 38 |
'ranks' => __( 'Ranks', 'gamipress' ), |
| 39 |
'rank_requirements' => __( 'Rank Requirements', 'gamipress' ), |
| 40 |
'logs' => __( 'Logs', 'gamipress' ), |
| 41 |
'earnings' => __( 'Users Earnings', 'gamipress' ), |
| 42 |
'earned_points' => __( 'Users Earned Points', 'gamipress' ), |
| 43 |
'earned_achievements' => __( 'Users Earned Achievements', 'gamipress' ), |
| 44 |
'earned_ranks' => __( 'Users Earned Ranks', 'gamipress' ), |
| 45 |
), |
| 46 |
), |
| 47 |
'reset_data' => array( |
| 48 |
'label' => __( 'Reset Data', 'gamipress' ), |
| 49 |
'desc' => '<span style="color: color: #a00;">' . __( '<strong>Important!</strong> Just use this tool on a test environment or if really you know what are you doing.', 'gamipress' ) . '</span>', |
| 50 |
'type' => 'button', |
| 51 |
'button' => 'danger gamipress-button-danger' |
| 52 |
), |
| 53 |
) ) |
| 54 |
); |
| 55 |
|
| 56 |
return $meta_boxes; |
| 57 |
|
| 58 |
} |
| 59 |
add_filter( 'gamipress_tools_general_meta_boxes', 'gamipress_reset_data_tool_meta_boxes', 9999 ); |
| 60 |
|
| 61 |
/** |
| 62 |
* GamiPress after tools page |
| 63 |
* |
| 64 |
* @since 1.1.7 |
| 65 |
* |
| 66 |
* @param string $content Content to be filtered |
| 67 |
* |
| 68 |
* @return mixed string $host if detected, false otherwise |
| 69 |
*/ |
| 70 |
function gamipress_reset_data_tool_page_bottom( $content ) { |
| 71 |
|
| 72 |
ob_start(); |
| 73 |
?> |
| 74 |
<!-- Reset Data Dialog --> |
| 75 |
<div id="reset-data-dialog" title="Are you sure you want to delete this data?" style="display: none;"> |
| 76 |
<span><?php _e( 'This action will delete all of the entries you selected. It will be completely <strong>irrecoverable</strong>.', 'gamipress' ); ?></span> |
| 77 |
<br><br> |
| 78 |
<span><?php _e( 'Please, confirm the data that will be removed:', 'gamipress' ); ?></span> |
| 79 |
<div id="reset-data-reminder"></div> |
| 80 |
</div> |
| 81 |
<?php |
| 82 |
$content .= ob_get_clean(); |
| 83 |
|
| 84 |
return $content; |
| 85 |
} |
| 86 |
add_filter( 'gamipress_tools_page_bottom', 'gamipress_reset_data_tool_page_bottom' ); |
| 87 |
|
| 88 |
/** |
| 89 |
* AJAX handler for the reset data tool |
| 90 |
* |
| 91 |
* @since 1.1.5 |
| 92 |
* @updated 1.4.0 Added user earnings reset |
| 93 |
*/ |
| 94 |
function gamipress_ajax_reset_data_tool() { |
| 95 |
// Security check, forces to die if not security passed |
| 96 |
check_ajax_referer( 'gamipress_admin', 'nonce' ); |
| 97 |
|
| 98 |
// Check user capabilities |
| 99 |
if( ! current_user_can( gamipress_get_manager_capability() ) ) { |
| 100 |
wp_send_json_error( __( 'You are not allowed to perform this action.', 'gamipress' ) ); |
| 101 |
} |
| 102 |
|
| 103 |
// Check parameters received |
| 104 |
if( ! isset( $_POST['items'] ) || empty( $_POST['items'] ) ) { |
| 105 |
wp_send_json_error( __( 'No items selected.', 'gamipress' ) ); |
| 106 |
} |
| 107 |
|
| 108 |
ignore_user_abort( true ); |
| 109 |
|
| 110 |
if ( ! gamipress_is_function_disabled( 'set_time_limit' ) ) { |
| 111 |
set_time_limit( 0 ); |
| 112 |
} |
| 113 |
|
| 114 |
global $wpdb; |
| 115 |
|
| 116 |
// Setup db table names |
| 117 |
$posts = GamiPress()->db->posts; |
| 118 |
$user_earnings = GamiPress()->db->user_earnings; |
| 119 |
|
| 120 |
// Setup vars |
| 121 |
$achievement_types = gamipress_get_achievement_types(); |
| 122 |
$points_types = gamipress_get_points_types(); |
| 123 |
$rank_types = gamipress_get_rank_types(); |
| 124 |
|
| 125 |
foreach( $_POST['items'] as $item ) { |
| 126 |
|
| 127 |
switch( $item ) { |
| 128 |
case 'achievement_types': |
| 129 |
|
| 130 |
$wpdb->delete( $posts, array( |
| 131 |
'post_type' => 'achievement-type' |
| 132 |
) ); |
| 133 |
|
| 134 |
break; |
| 135 |
case 'achievements': |
| 136 |
|
| 137 |
$wpdb->delete( $posts, array( |
| 138 |
'post_type' => array_keys( $achievement_types ) |
| 139 |
) ); |
| 140 |
|
| 141 |
break; |
| 142 |
case 'steps': |
| 143 |
|
| 144 |
$wpdb->delete( $posts, array( |
| 145 |
'post_type' => 'step' |
| 146 |
) ); |
| 147 |
|
| 148 |
break; |
| 149 |
case 'points_types': |
| 150 |
|
| 151 |
$wpdb->delete( $posts, array( |
| 152 |
'post_type' => 'points-type' |
| 153 |
) ); |
| 154 |
|
| 155 |
break; |
| 156 |
case 'points_awards': |
| 157 |
|
| 158 |
$wpdb->delete( $posts, array( |
| 159 |
'post_type' => 'points-award' |
| 160 |
) ); |
| 161 |
|
| 162 |
break; |
| 163 |
case 'points_deducts': |
| 164 |
|
| 165 |
$wpdb->delete( $posts, array( |
| 166 |
'post_type' => 'points-deduct' |
| 167 |
) ); |
| 168 |
|
| 169 |
break; |
| 170 |
case 'rank_types': |
| 171 |
|
| 172 |
$wpdb->delete( $posts, array( |
| 173 |
'post_type' => 'rank-type' |
| 174 |
) ); |
| 175 |
|
| 176 |
break; |
| 177 |
case 'ranks': |
| 178 |
|
| 179 |
$wpdb->delete( $posts, array( |
| 180 |
'post_type' => array_keys( $rank_types ) |
| 181 |
) ); |
| 182 |
|
| 183 |
break; |
| 184 |
case 'rank_requirements': |
| 185 |
|
| 186 |
$wpdb->delete( $posts, array( |
| 187 |
'post_type' => 'rank-requirement' |
| 188 |
) ); |
| 189 |
|
| 190 |
break; |
| 191 |
case 'logs': |
| 192 |
|
| 193 |
$logs = GamiPress()->db->logs; |
| 194 |
$logs_meta = GamiPress()->db->logs_meta; |
| 195 |
|
| 196 |
// Reset from gamipress_logs table |
| 197 |
$wpdb->query( "DELETE FROM {$logs} WHERE 1=1" ); |
| 198 |
|
| 199 |
// Reset from gamipress_logs_meta table |
| 200 |
$wpdb->query( "DELETE FROM {$logs_meta} WHERE 1=1" ); |
| 201 |
|
| 202 |
break; |
| 203 |
case 'earnings': |
| 204 |
case 'earned_points': |
| 205 |
case 'earned_achievements': |
| 206 |
case 'earned_ranks': |
| 207 |
break; |
| 208 |
default: |
| 209 |
do_action( 'gamipress_reset_data_tool_reset', $item ); |
| 210 |
break; |
| 211 |
} |
| 212 |
|
| 213 |
// User earnings |
| 214 |
if( |
| 215 |
in_array( 'earnings', $_POST['items'] ) |
| 216 |
|| in_array( 'earned_points', $_POST['items'] ) |
| 217 |
|| in_array( 'earned_achievements', $_POST['items'] ) |
| 218 |
|| in_array( 'earned_ranks', $_POST['items'] ) |
| 219 |
) { |
| 220 |
|
| 221 |
// For points and ranks, we need to reset all users meta |
| 222 |
if( in_array( 'earned_points', $_POST['items'] ) || in_array( 'earned_ranks', $_POST['items'] ) ) { |
| 223 |
|
| 224 |
// Get all stored users |
| 225 |
$users = $wpdb->get_results( "SELECT {$wpdb->users}.ID FROM {$wpdb->users}" ); |
| 226 |
|
| 227 |
foreach( $users as $user ) { |
| 228 |
|
| 229 |
// Reset user earned points |
| 230 |
if( in_array( 'earned_points', $_POST['items'] ) ) { |
| 231 |
|
| 232 |
foreach( $points_types as $points_type => $points_type_data ) { |
| 233 |
// Reset user's points total |
| 234 |
gamipress_update_user_meta( $user->ID, "_gamipress_{$points_type}_points", 0 ); |
| 235 |
} |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
// Reset user earned ranks |
| 240 |
if( in_array( 'earned_ranks', $_POST['items'] ) ) { |
| 241 |
|
| 242 |
foreach( $rank_types as $rank_type => $rank_type_data ) { |
| 243 |
// Reset user's ranks |
| 244 |
gamipress_delete_user_meta( $user->ID, "_gamipress_{$rank_type}_rank" ); |
| 245 |
} |
| 246 |
|
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
if( in_array( 'earned_points', $_POST['items'] ) ) { |
| 253 |
// Reset all points awards and deducts gamipress_user_earnings table |
| 254 |
$wpdb->query( "DELETE FROM {$user_earnings} WHERE post_type IN ( 'points-award', 'points-deduct' )" ); |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
if( in_array( 'earned_achievements', $_POST['items'] ) ) { |
| 259 |
// Reset all achievements and steps from gamipress_user_earnings table |
| 260 |
$wpdb->query( "DELETE FROM {$user_earnings} WHERE post_type IN ( 'step', '" . implode( "', '", array_keys( $achievement_types ) ) . "' )" ); |
| 261 |
|
| 262 |
} |
| 263 |
|
| 264 |
if( in_array( 'earned_ranks', $_POST['items'] ) ) { |
| 265 |
// Reset all ranks and rank requirements from gamipress_user_earnings table |
| 266 |
$wpdb->query( "DELETE FROM {$user_earnings} WHERE post_type IN ( 'rank-requirement', '" . implode( "', '", array_keys( $rank_types ) ) . "' )" ); |
| 267 |
|
| 268 |
} |
| 269 |
|
| 270 |
if( in_array( 'earnings', $_POST['items'] ) ) { |
| 271 |
// Reset all the gamipress_user_earnings table |
| 272 |
$wpdb->query( "DELETE FROM {$user_earnings} WHERE 1=1" ); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
} |
| 277 |
|
| 278 |
// Flush the GamiPress cache |
| 279 |
gamipress_flush_cache(); |
| 280 |
|
| 281 |
// Return a success message |
| 282 |
wp_send_json_success( __( 'Data has been reset successfully.', 'gamipress' ) ); |
| 283 |
} |
| 284 |
add_action( 'wp_ajax_gamipress_reset_data_tool', 'gamipress_ajax_reset_data_tool' ); |