| @@ -1,0 +1,431 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Logs | |
| 4 | + * | |
| 5 | + * @package GamiPress\Custom_Tables\Logs | |
| 6 | + * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.2.8 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Custom Table Labels | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + * | |
| 17 | + * @return array | |
| 18 | + */ | |
| 19 | +function gamipress_logs_labels() { | |
| 20 | + | |
| 21 | + return array( | |
| 22 | + 'singular' => __( 'Log', 'gamipress' ), | |
| 23 | + 'plural' => __( 'Logs', 'gamipress' ), | |
| 24 | + 'labels' => array( | |
| 25 | + 'list_menu_title' => __( 'Logs', 'gamipress' ), | |
| 26 | + ), | |
| 27 | + ); | |
| 28 | + | |
| 29 | +} | |
| 30 | +add_filter( 'ct_gamipress_logs_labels', 'gamipress_logs_labels' ); | |
| 31 | + | |
| 32 | +/** | |
| 33 | + * Parse query args for logs | |
| 34 | + * | |
| 35 | + * @since 1.2.8 | |
| 36 | + * @updated 1.6.5 Added support to before and after query vars | |
| 37 | + * | |
| 38 | + * @param string $where | |
| 39 | + * @param CT_Query $ct_query | |
| 40 | + * | |
| 41 | + * @return string | |
| 42 | + */ | |
| 43 | +function gamipress_logs_query_where( $where, $ct_query ) { | |
| 44 | + | |
| 45 | + global $ct_table; | |
| 46 | + | |
| 47 | + if( $ct_table->name !== 'gamipress_logs' ) { | |
| 48 | + return $where; | |
| 49 | + } | |
| 50 | + | |
| 51 | + $table_name = $ct_table->db->table_name; | |
| 52 | + | |
| 53 | + // Shorthand | |
| 54 | + $qv = $ct_query->query_vars; | |
| 55 | + | |
| 56 | + // Type | |
| 57 | + $where .= gamipress_custom_table_where( $qv, 'type', 'type', 'string' ); | |
| 58 | + | |
| 59 | + // Trigger Type | |
| 60 | + $where .= gamipress_custom_table_where( $qv, 'trigger_type', 'trigger_type', 'string' ); | |
| 61 | + | |
| 62 | + // Access | |
| 63 | + $where .= gamipress_custom_table_where( $qv, 'access', 'access', 'string' ); | |
| 64 | + | |
| 65 | + // User ID | |
| 66 | + $where .= gamipress_custom_table_where( $qv, 'user_id', 'user_id', 'integer' ); | |
| 67 | + | |
| 68 | + // Include | |
| 69 | + $where .= gamipress_custom_table_where( $qv, 'log__in', 'log_id', 'integer' ); | |
| 70 | + | |
| 71 | + // Exclude | |
| 72 | + $where .= gamipress_custom_table_where( $qv, 'log__not_in', 'log_id', 'integer', '!=', 'NOT IN' ); | |
| 73 | + | |
| 74 | + // Before | |
| 75 | + if( isset( $qv['before'] ) ) { | |
| 76 | + | |
| 77 | + $before = $qv['before']; | |
| 78 | + | |
| 79 | + // Turn a string date into time | |
| 80 | + if( gettype( $qv['before'] ) === 'string' ) { | |
| 81 | + $before = strtotime( $qv['before'] ); | |
| 82 | + } | |
| 83 | + | |
| 84 | + if( $before > 0 ) { | |
| 85 | + $before = date( 'Y-m-d H:i:s', $before ); | |
| 86 | + $where .= " AND {$table_name}.date < '{$before}'"; | |
| 87 | + } | |
| 88 | + | |
| 89 | + } | |
| 90 | + | |
| 91 | + // After | |
| 92 | + if( isset( $qv['after'] ) ) { | |
| 93 | + | |
| 94 | + $after = $qv['after']; | |
| 95 | + | |
| 96 | + // Turn a string date into time | |
| 97 | + if( gettype( $qv['after'] ) === 'string' ) { | |
| 98 | + $after = strtotime( $qv['after'] ); | |
| 99 | + } | |
| 100 | + | |
| 101 | + if( $after > 0 ) { | |
| 102 | + $after = date( 'Y-m-d H:i:s', $after ); | |
| 103 | + $where .= " AND {$table_name}.date > '{$after}'"; | |
| 104 | + } | |
| 105 | + | |
| 106 | + } | |
| 107 | + | |
| 108 | + return $where; | |
| 109 | +} | |
| 110 | +add_filter( 'ct_query_where', 'gamipress_logs_query_where', 10, 2 ); | |
| 111 | + | |
| 112 | +/** | |
| 113 | + * Define the search fields for logs | |
| 114 | + * | |
| 115 | + * @since 1.6.4.2 | |
| 116 | + * | |
| 117 | + * @param array $search_fields | |
| 118 | + * | |
| 119 | + * @return array | |
| 120 | + */ | |
| 121 | +function gamipress_logs_search_fields( $search_fields ) { | |
| 122 | + | |
| 123 | + $search_fields[] = 'title'; | |
| 124 | + | |
| 125 | + return $search_fields; | |
| 126 | + | |
| 127 | +} | |
| 128 | +add_filter( 'ct_query_gamipress_logs_search_fields', 'gamipress_logs_search_fields' ); | |
| 129 | + | |
| 130 | +/** | |
| 131 | + * Parse search query args for logs | |
| 132 | + * | |
| 133 | + * @since 1.3.9.7 | |
| 134 | + * | |
| 135 | + * @param string $search | |
| 136 | + * @param CT_Query $ct_query | |
| 137 | + * | |
| 138 | + * @return string | |
| 139 | + */ | |
| 140 | +function gamipress_logs_query_search( $search, $ct_query ) { | |
| 141 | + | |
| 142 | + global $ct_table, $wpdb; | |
| 143 | + | |
| 144 | + if( $ct_table->name !== 'gamipress_logs' ) { | |
| 145 | + return $search; | |
| 146 | + } | |
| 147 | + | |
| 148 | + $table_name = $ct_table->db->table_name; | |
| 149 | + | |
| 150 | + // Shorthand | |
| 151 | + $qv = $ct_query->query_vars; | |
| 152 | + | |
| 153 | + // Check if is search and query is not filtered by an specific user | |
| 154 | + if( isset( $qv['s'] ) && ! empty( $qv['s'] ) && ! isset( $qv['user_id'] ) ) { | |
| 155 | + | |
| 156 | + // Made a user sub-search to retrieve them | |
| 157 | + $users = get_users( array( | |
| 158 | + 'search' => sprintf( '*%s*', $qv['s'] ), | |
| 159 | + 'search_columns' => array( | |
| 160 | + 'user_login', | |
| 161 | + 'user_email', | |
| 162 | + 'display_name', | |
| 163 | + ), | |
| 164 | + 'fields' => 'ID', | |
| 165 | + ) ); | |
| 166 | + | |
| 167 | + if( ! empty( $users ) ) { | |
| 168 | + $search .= " AND ( {$table_name}.user_id IN (" . implode( ',', array_map( 'absint', $users ) ) . ") )"; | |
| 169 | + } | |
| 170 | + | |
| 171 | + } | |
| 172 | + | |
| 173 | + return $search; | |
| 174 | + | |
| 175 | +} | |
| 176 | +add_filter( 'ct_query_search', 'gamipress_logs_query_search', 10, 2 ); | |
| 177 | + | |
| 178 | +/** | |
| 179 | + * Define the field for logs views | |
| 180 | + * | |
| 181 | + * @since 1.8.3 | |
| 182 | + * | |
| 183 | + * @param string $field_id | |
| 184 | + * | |
| 185 | + * @return string | |
| 186 | + */ | |
| 187 | +function gamipress_logs_views_field( $field_id = '' ) { | |
| 188 | + return 'type'; | |
| 189 | +} | |
| 190 | +add_filter( 'ct_list_gamipress_logs_views_field', 'gamipress_logs_views_field' ); | |
| 191 | + | |
| 192 | +/** | |
| 193 | + * Define the field labels for logs views | |
| 194 | + * | |
| 195 | + * @since 1.8.3 | |
| 196 | + * | |
| 197 | + * @param array $field_labels | |
| 198 | + * | |
| 199 | + * @return array | |
| 200 | + */ | |
| 201 | +function gamipress_logs_views_field_labels( $field_labels = array() ) { | |
| 202 | + return gamipress_get_log_types(); | |
| 203 | +} | |
| 204 | +add_filter( 'ct_list_gamipress_logs_views_field_labels', 'gamipress_logs_views_field_labels' ); | |
| 205 | + | |
| 206 | +/** | |
| 207 | + * Columns for logs list view | |
| 208 | + * | |
| 209 | + * @since 1.2.8 | |
| 210 | + * | |
| 211 | + * @param array $columns | |
| 212 | + * | |
| 213 | + * @return array | |
| 214 | + */ | |
| 215 | +function gamipress_manage_logs_columns( $columns = array() ) { | |
| 216 | + | |
| 217 | + $columns['title'] = __( 'Title', 'gamipress' ); | |
| 218 | + $columns['type'] = __( 'Type', 'gamipress' ); | |
| 219 | + $columns['user_id'] = __( 'User', 'gamipress' ); | |
| 220 | + $columns['post_id'] = __( 'Post', 'gamipress' ); | |
| 221 | + $columns['admin_id'] = __( 'Administrator', 'gamipress' ); | |
| 222 | + $columns['date'] = __( 'Date', 'gamipress' ); | |
| 223 | + | |
| 224 | + return $columns; | |
| 225 | +} | |
| 226 | +add_filter( 'manage_gamipress_logs_columns', 'gamipress_manage_logs_columns' ); | |
| 227 | + | |
| 228 | +/** | |
| 229 | + * Sortable columns for logs list view | |
| 230 | + * | |
| 231 | + * @since 1.6.7 | |
| 232 | + * | |
| 233 | + * @param array $sortable_columns | |
| 234 | + * | |
| 235 | + * @return array | |
| 236 | + */ | |
| 237 | +function gamipress_manage_logs_sortable_columns( $sortable_columns ) { | |
| 238 | + | |
| 239 | + $sortable_columns['title'] = array( 'title', false ); | |
| 240 | + $sortable_columns['type'] = array( 'type', false ); | |
| 241 | + $sortable_columns['user_id'] = array( 'user_id', false ); | |
| 242 | + $sortable_columns['post_id'] = array( 'post_id', false ); | |
| 243 | + $sortable_columns['admin_id'] = array( 'admin_id', false ); | |
| 244 | + $sortable_columns['date'] = array( 'date', true ); | |
| 245 | + | |
| 246 | + return $sortable_columns; | |
| 247 | + | |
| 248 | +} | |
| 249 | +add_filter( 'manage_gamipress_logs_sortable_columns', 'gamipress_manage_logs_sortable_columns' ); | |
| 250 | + | |
| 251 | +/** | |
| 252 | + * Columns rendering for logs list view | |
| 253 | + * | |
| 254 | + * @since 1.2.8 | |
| 255 | + * | |
| 256 | + * @param string $column_name | |
| 257 | + * @param integer $object_id | |
| 258 | + */ | |
| 259 | +function gamipress_manage_logs_custom_column( $column_name, $object_id ) { | |
| 260 | + | |
| 261 | + // Setup vars | |
| 262 | + $log = ct_get_object( $object_id ); | |
| 263 | + | |
| 264 | + switch( $column_name ) { | |
| 265 | + case 'title': | |
| 266 | + ?> | |
| 267 | + | |
| 268 | + <strong> | |
| 269 | + <a href="<?php echo ct_get_edit_link( 'gamipress_logs', $log->log_id ); ?>"><?php echo apply_filters( 'gamipress_render_log_title', $log->title, $log->log_id ); ?></a> - <span class="post-state"><?php echo $log->access === 'public' ? __( 'Public', 'gamipress' ) : __( 'Private', 'gamipress' ); ?></span> | |
| 270 | + </strong> | |
| 271 | + | |
| 272 | + <?php | |
| 273 | + break; | |
| 274 | + case 'type': | |
| 275 | + $log_types = gamipress_get_log_types(); | |
| 276 | + | |
| 277 | + $post_id = ct_get_object_meta( $object_id, '_gamipress_achievement_id', true ); | |
| 278 | + $post_type = gamipress_get_post_type( $post_id ); | |
| 279 | + | |
| 280 | + if( $log->type === 'achievement_earn' || $log->type === 'achievement_award' && in_array( $post_type, gamipress_get_requirement_types_slugs() ) ) { | |
| 281 | + | |
| 282 | + switch( $post_type ) { | |
| 283 | + case 'step': | |
| 284 | + if( $log->type === 'achievement_earn' ) { | |
| 285 | + echo __( 'Achievement Step Earn', 'gamipress' ); | |
| 286 | + } else { | |
| 287 | + echo __( 'Achievement Step Award', 'gamipress' ); | |
| 288 | + } | |
| 289 | + break; | |
| 290 | + case 'rank-requirement': | |
| 291 | + if( $log->type === 'achievement_earn' ) { | |
| 292 | + echo __( 'Rank Requirement Earn', 'gamipress' ); | |
| 293 | + } else { | |
| 294 | + echo __( 'Rank Requirement Award', 'gamipress' ); | |
| 295 | + } | |
| 296 | + break; | |
| 297 | + default: | |
| 298 | + echo isset( $log_types[$log->type] ) ? $log_types[$log->type] : $log->type; | |
| 299 | + break; | |
| 300 | + } | |
| 301 | + | |
| 302 | + } else { | |
| 303 | + echo isset( $log_types[$log->type] ) ? $log_types[$log->type] : $log->type; | |
| 304 | + } | |
| 305 | + | |
| 306 | + break; | |
| 307 | + case 'user_id': | |
| 308 | + $user = get_userdata( $log->user_id ); | |
| 309 | + | |
| 310 | + if( $user ) : | |
| 311 | + | |
| 312 | + if( current_user_can( 'edit_users' ) ) { | |
| 313 | + ?> | |
| 314 | + | |
| 315 | + <a href="<?php echo get_edit_user_link( $log->user_id ); ?>"><?php echo $user->display_name . ' (' . $user->user_login . ')'; ?></a> | |
| 316 | + | |
| 317 | + <?php | |
| 318 | + } else { | |
| 319 | + echo $user->display_name; | |
| 320 | + } | |
| 321 | + | |
| 322 | + endif; | |
| 323 | + break; | |
| 324 | + case 'post_id': | |
| 325 | + | |
| 326 | + $trigger = ct_get_object_meta( $object_id, '_gamipress_trigger_type', true ); | |
| 327 | + | |
| 328 | + if( in_array( $trigger, array_keys( gamipress_get_specific_activity_triggers() ) ) ) : | |
| 329 | + // Is is an specific activity trigger, then will get a post assigned | |
| 330 | + | |
| 331 | + $post_id = ct_get_object_meta($object_id, '_gamipress_achievement_post', true); | |
| 332 | + $site_id = ct_get_object_meta($object_id, '_gamipress_achievement_post_site_id', true); | |
| 333 | + | |
| 334 | + if( $post_id ) : | |
| 335 | + | |
| 336 | + $post_title = gamipress_get_specific_activity_trigger_post_title( $post_id, $trigger, $site_id ); | |
| 337 | + | |
| 338 | + if( current_user_can( 'edit_post', $post_id ) ) : ?> | |
| 339 | + | |
| 340 | + <a href="<?php echo get_edit_post_link( $post_id ); ?>"><?php echo $post_title; ?></a> | |
| 341 | + | |
| 342 | + <?php else : | |
| 343 | + | |
| 344 | + echo $post_title; | |
| 345 | + | |
| 346 | + endif; | |
| 347 | + | |
| 348 | + endif; | |
| 349 | + | |
| 350 | + else : | |
| 351 | + // Also, some activity triggers stores a post ID (for example, comments and visits) | |
| 352 | + | |
| 353 | + $post_id = ct_get_object_meta($object_id, '_gamipress_post_id', true); | |
| 354 | + | |
| 355 | + if( $post_id ) : | |
| 356 | + | |
| 357 | + $post_title = gamipress_get_specific_activity_trigger_post_title( $post_id, $trigger, get_current_blog_id() ); | |
| 358 | + | |
| 359 | + if( current_user_can( 'edit_post', $post_id ) ) : ?> | |
| 360 | + | |
| 361 | + <a href="<?php echo get_edit_post_link( $post_id ); ?>"><?php echo $post_title; ?></a> | |
| 362 | + | |
| 363 | + <?php else : | |
| 364 | + | |
| 365 | + echo $post_title; | |
| 366 | + | |
| 367 | + endif; | |
| 368 | + | |
| 369 | + endif; | |
| 370 | + | |
| 371 | + endif; | |
| 372 | + | |
| 373 | + break; | |
| 374 | + case 'admin_id': | |
| 375 | + | |
| 376 | + if( in_array( $log->type, array( 'achievement_award', 'points_award', 'points_revoke', 'rank_award' ) ) ) : | |
| 377 | + | |
| 378 | + $admin_id = ct_get_object_meta( $object_id, '_gamipress_admin_id', true ); | |
| 379 | + $admin = get_userdata( $admin_id ); | |
| 380 | + | |
| 381 | + if( $admin ) : | |
| 382 | + | |
| 383 | + if( current_user_can( 'edit_users' ) ) { | |
| 384 | + ?> | |
| 385 | + | |
| 386 | + <a href="<?php echo get_edit_user_link( $admin_id ); ?>"><?php echo $admin->display_name . ' (' . $admin->user_login . ')'; ?></a> | |
| 387 | + | |
| 388 | + <?php | |
| 389 | + } else { | |
| 390 | + echo $admin->display_name; | |
| 391 | + } | |
| 392 | + | |
| 393 | + endif; | |
| 394 | + | |
| 395 | + endif; | |
| 396 | + | |
| 397 | + break; | |
| 398 | + case 'date': | |
| 399 | + ?> | |
| 400 | + | |
| 401 | + <abbr title="<?php echo date( 'Y/m/d g:i:s a', strtotime( $log->date ) ); ?>"><?php echo date( 'Y/m/d', strtotime( $log->date ) ); ?></abbr> | |
| 402 | + | |
| 403 | + <?php | |
| 404 | + break; | |
| 405 | + } | |
| 406 | +} | |
| 407 | +add_action( 'manage_gamipress_logs_custom_column', 'gamipress_manage_logs_custom_column', 10, 2 ); | |
| 408 | + | |
| 409 | +/** | |
| 410 | + * Remove submit div box on logs | |
| 411 | + * | |
| 412 | + * @since 1.2.8 | |
| 413 | + */ | |
| 414 | +function gamipress_add_logs_meta_boxes() { | |
| 415 | + remove_meta_box( 'submitdiv', 'gamipress_logs', 'side' ); | |
| 416 | +} | |
| 417 | +add_action( 'add_meta_boxes', 'gamipress_add_logs_meta_boxes' ); | |
| 418 | + | |
| 419 | +// CT Ajax List | |
| 420 | +// Override ajax list capability | |
| 421 | +add_filter( 'ct_ajax_list_table_gamipress_logs_capability', 'gamipress_get_manager_capability' ); | |
| 422 | + | |
| 423 | +// Parse query args | |
| 424 | +function ct_ajax_list_table_gamipress_logs_query_args( $query_args ) { | |
| 425 | + | |
| 426 | + $query_args = gamipress_sanitize_query_args( $query_args, gamipress_logs_query_vars_rules() ); | |
| 427 | + | |
| 428 | + return $query_args; | |
| 429 | + | |
| 430 | +} | |
| 431 | +add_filter( 'ct_ajax_list_table_gamipress_logs_query_args', 'ct_ajax_list_table_gamipress_logs_query_args' ); | |