| @@ -1,0 +1,213 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Logs Clean Up Tool | |
| 4 | + * | |
| 5 | + * @package GamiPress\Admin\Tools\Logs Clean Up | |
| 6 | + * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.8.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Register Reset Data Tool meta boxes | |
| 14 | + * | |
| 15 | + * @since 1.8.0 | |
| 16 | + * | |
| 17 | + * @param array $meta_boxes | |
| 18 | + * | |
| 19 | + * @return array | |
| 20 | + */ | |
| 21 | +function gamipress_logs_clean_up_tool_meta_boxes( $meta_boxes ) { | |
| 22 | + | |
| 23 | + $log_types = gamipress_get_log_types(); | |
| 24 | + $descriptions = array( | |
| 25 | + 'default' => __( 'This log has been registered by a plugin so contact his author for more details.', 'gamipress' ), | |
| 26 | + 'event_trigger' => __( 'Used to log users events perform. GamiPress uses these logs to determine the times a user did a specific action (for example, to award an achievement that requires commenting 3 times). Is safe to delete the old ones.', 'gamipress' ), | |
| 27 | + 'achievement_earn' => __( 'Used to log users achievement unlocks. Is not recommended to delete them.', 'gamipress' ), | |
| 28 | + 'achievement_award' => __( 'Used to log achievement awards perform by administrators. Is safe to delete them.', 'gamipress' ), | |
| 29 | + 'points_earn' => __( 'Used to log users points gains. Is not recommended to delete them.', 'gamipress' ), | |
| 30 | + 'points_deduct' => __( 'Used to log users points loss. Is not recommended to delete them.', 'gamipress' ), | |
| 31 | + 'points_expend' => __( 'Used to log users points expended (mainly, for unlocking achievements and ranks through expending points). Is not recommended to delete them.', 'gamipress' ), | |
| 32 | + 'points_award' => __( 'Used to log positive points movements perform by administrators. Is safe to delete them.', 'gamipress' ), | |
| 33 | + 'points_revoke' => __( 'Used to log negative points movements perform by administrators. Is safe to delete them.', 'gamipress' ), | |
| 34 | + 'rank_earn' => __( 'Used to log users rank unlocks. Is not recommended to delete them.', 'gamipress' ), | |
| 35 | + 'rank_award' => __( 'Used to log rank awards perform by administrators. Is safe to delete them.', 'gamipress' ), | |
| 36 | + ); | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * Filter to override log types descriptions for the Logs Clean Up Tool | |
| 40 | + * | |
| 41 | + * @since 1.8.0 | |
| 42 | + * | |
| 43 | + * @param array $descriptions Default log types descriptions | |
| 44 | + * | |
| 45 | + * @return array | |
| 46 | + */ | |
| 47 | + $descriptions = apply_filters( 'gamipress__logs_clean_up_tool_log_types_descriptions', $descriptions ); | |
| 48 | + | |
| 49 | + foreach( $log_types as $log_type => $label ) { | |
| 50 | + $description = ( isset( $descriptions[$log_type] ) ? $descriptions[$log_type] : $descriptions['default'] ); | |
| 51 | + | |
| 52 | + $log_types[$log_type] = '<strong>' . $label . '</strong><br><small>' . $description . '</small>'; | |
| 53 | + } | |
| 54 | + | |
| 55 | + $meta_boxes['logs-clean-up'] = array( | |
| 56 | + 'title' => gamipress_dashicon( 'trash' ) . __( 'Logs Clean Up', 'gamipress' ), | |
| 57 | + 'fields' => apply_filters( 'gamipress_logs_clean_up_tool_fields', array( | |
| 58 | + 'logs_type' => array( | |
| 59 | + 'name' => __( 'Type', 'gamipress' ), | |
| 60 | + 'tooltip' => __( 'Choose the log\'s type you want to clean up.', 'gamipress' ), | |
| 61 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 62 | + 'type' => 'multicheck', | |
| 63 | + 'classes' => 'gamipress-switch', | |
| 64 | + 'options' => $log_types, | |
| 65 | + ), | |
| 66 | + 'from' => array( | |
| 67 | + 'name' => __( 'From (Optional)', 'gamipress' ), | |
| 68 | + 'tooltip' => __( 'Choose the date from you want to clean up. Logs registered <strong>after</strong> this date will be deleted.', 'gamipress' ) | |
| 69 | + . '<br>' . __( 'Leave blank to no filter by this date.', 'gamipress' ), | |
| 70 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 71 | + 'type' => 'text_date_timestamp', | |
| 72 | + ), | |
| 73 | + 'to' => array( | |
| 74 | + 'name' => __( 'To (Optional)', 'gamipress' ), | |
| 75 | + 'tooltip' => __( 'Choose the date until you want to clean up. Logs registered <strong>before</strong> this date will be deleted.', 'gamipress' ) | |
| 76 | + . '<br>' . __( 'Leave blank to no filter by this date.', 'gamipress' ), | |
| 77 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 78 | + 'type' => 'text_date_timestamp', | |
| 79 | + ), | |
| 80 | + 'logs_clean_up_actions' => array( | |
| 81 | + 'desc' => __( 'You can click on the "Count Logs" button to preview how many logs will be affected.', 'gamipress' ), | |
| 82 | + 'type' => 'multi_buttons', | |
| 83 | + 'buttons' => array( | |
| 84 | + 'logs_clean_up_count' => array( | |
| 85 | + 'label' => __( 'Count Logs', 'gamipress' ), | |
| 86 | + 'type' => 'button', | |
| 87 | + ), | |
| 88 | + 'logs_clean_up' => array( | |
| 89 | + 'label' => __( 'Clean Logs', 'gamipress' ), | |
| 90 | + 'type' => 'button', | |
| 91 | + 'button' => 'primary', | |
| 92 | + ), | |
| 93 | + ), | |
| 94 | + ), | |
| 95 | + ) ) | |
| 96 | + ); | |
| 97 | + | |
| 98 | + return $meta_boxes; | |
| 99 | + | |
| 100 | +} | |
| 101 | +add_filter( 'gamipress_tools_general_meta_boxes', 'gamipress_logs_clean_up_tool_meta_boxes' ); | |
| 102 | + | |
| 103 | +/** | |
| 104 | + * Common handler for both logs clean up functions | |
| 105 | + * | |
| 106 | + * @since 1.8.0 | |
| 107 | + */ | |
| 108 | +function gamipress_ajax_logs_clean_up_tool_checks() { | |
| 109 | + // Security check, forces to die if not security passed | |
| 110 | + check_ajax_referer( 'gamipress_admin', 'nonce' ); | |
| 111 | + | |
| 112 | + // Check user capabilities | |
| 113 | + if( ! current_user_can( gamipress_get_manager_capability() ) ) { | |
| 114 | + wp_send_json_error( __( 'You are not allowed to perform this action.', 'gamipress' ) ); | |
| 115 | + } | |
| 116 | + | |
| 117 | + // Check parameters given | |
| 118 | + if( ! isset( $_POST['log_types'] ) || empty( $_POST['log_types'] ) ) { | |
| 119 | + wp_send_json_error( __( 'Please, choose at least 1 log type.', 'gamipress' ) ); | |
| 120 | + } | |
| 121 | + | |
| 122 | + ignore_user_abort( true ); | |
| 123 | + | |
| 124 | + if ( ! gamipress_is_function_disabled( 'set_time_limit' ) ) { | |
| 125 | + set_time_limit( 0 ); | |
| 126 | + } | |
| 127 | +} | |
| 128 | + | |
| 129 | +/** | |
| 130 | + * Setup the WHERE clause for both logs clean up functions | |
| 131 | + * | |
| 132 | + * @since 1.8.0 | |
| 133 | + * | |
| 134 | + * @return string | |
| 135 | + */ | |
| 136 | +function gamipress_ajax_logs_clean_up_tool_where() { | |
| 137 | + | |
| 138 | + $logs = GamiPress()->db->logs; | |
| 139 | + | |
| 140 | + // Setup vars | |
| 141 | + $log_types = esc_sql( $_POST['log_types'] ); | |
| 142 | + $from = strtotime($_POST['from'] ); | |
| 143 | + $to = strtotime( $_POST['to'] ); | |
| 144 | + | |
| 145 | + $sql = "FROM {$logs} WHERE type IN ( '" . implode( "', '", $log_types ) . "' )"; | |
| 146 | + | |
| 147 | + // From date | |
| 148 | + if( $from ) { | |
| 149 | + // Ensure format | |
| 150 | + $from = date( 'Y-m-d', $from ); | |
| 151 | + | |
| 152 | + $sql .= "AND date >= '{$from}'"; | |
| 153 | + } | |
| 154 | + | |
| 155 | + // To date | |
| 156 | + if( $to ) { | |
| 157 | + // Ensure format | |
| 158 | + $to = date( 'Y-m-d', $to ); | |
| 159 | + | |
| 160 | + $sql .= "AND date <= '{$to}'"; | |
| 161 | + } | |
| 162 | + | |
| 163 | + return $sql; | |
| 164 | +} | |
| 165 | + | |
| 166 | +/** | |
| 167 | + * Ajax handler for the logs clean up count tool | |
| 168 | + * | |
| 169 | + * @since 1.8.0 | |
| 170 | + */ | |
| 171 | +function gamipress_ajax_logs_clean_up_count_tool() { | |
| 172 | + // Process the tool checks (common for both processes) | |
| 173 | + gamipress_ajax_logs_clean_up_tool_checks(); | |
| 174 | + | |
| 175 | + global $wpdb; | |
| 176 | + | |
| 177 | + $where = gamipress_ajax_logs_clean_up_tool_where(); | |
| 178 | + | |
| 179 | + $count = $wpdb->get_var( "SELECT COUNT(*) {$where}" ); | |
| 180 | + | |
| 181 | + // Return a success message | |
| 182 | + wp_send_json_success( sprintf( __( '%d logs will be deleted.', 'gamipress' ), $count ) ); | |
| 183 | + | |
| 184 | +} | |
| 185 | +add_action( 'wp_ajax_gamipress_logs_clean_up_count_tool', 'gamipress_ajax_logs_clean_up_count_tool' ); | |
| 186 | + | |
| 187 | +/** | |
| 188 | + * Ajax handler for the logs clean up tool | |
| 189 | + * | |
| 190 | + * @since 1.8.0 | |
| 191 | + */ | |
| 192 | +function gamipress_ajax_logs_clean_up_tool() { | |
| 193 | + // Process the tool checks (common for both processes) | |
| 194 | + gamipress_ajax_logs_clean_up_tool_checks(); | |
| 195 | + | |
| 196 | + global $wpdb; | |
| 197 | + | |
| 198 | + $logs = GamiPress()->db->logs; | |
| 199 | + $logs_meta = GamiPress()->db->logs_meta; | |
| 200 | + | |
| 201 | + $where = gamipress_ajax_logs_clean_up_tool_where(); | |
| 202 | + | |
| 203 | + // Delete logs | |
| 204 | + $wpdb->query( "DELETE {$where}" ); | |
| 205 | + | |
| 206 | + // Delete orphaned log metas | |
| 207 | + $wpdb->query( "DELETE lm FROM {$logs_meta} lm LEFT JOIN {$logs} l ON l.log_id = lm.log_id WHERE l.log_id IS NULL" ); | |
| 208 | + | |
| 209 | + // Return a success message | |
| 210 | + wp_send_json_success( __( 'Logs clean up process has been done successfully.', 'gamipress' ) ); | |
| 211 | + | |
| 212 | +} | |
| 213 | +add_action( 'wp_ajax_gamipress_logs_clean_up_tool', 'gamipress_ajax_logs_clean_up_tool' ); | |