| @@ -1,0 +1,729 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * User Earnings | |
| 4 | + * | |
| 5 | + * @package GamiPress\Custom_Tables\User_Earnings | |
| 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_user_earnings_labels() { | |
| 20 | + | |
| 21 | + return array( | |
| 22 | + 'singular' => __( 'User Earning', 'gamipress' ), | |
| 23 | + 'plural' => __( 'User Earnings', 'gamipress' ), | |
| 24 | + 'labels' => array( | |
| 25 | + 'not_found' => __( 'This user has not earned anything', 'gamipress' ), | |
| 26 | + 'list_menu_title' => __( 'User Earnings', 'gamipress' ), | |
| 27 | + ), | |
| 28 | + ); | |
| 29 | + | |
| 30 | +} | |
| 31 | +add_filter( 'ct_gamipress_user_earnings_labels', 'gamipress_user_earnings_labels' ); | |
| 32 | + | |
| 33 | +/** | |
| 34 | + * Parse query args for user earnings to be applied on WHERE clause | |
| 35 | + * | |
| 36 | + * @since 1.2.8 | |
| 37 | + * @updated 1.6.5 Added support to before and after query vars | |
| 38 | + * | |
| 39 | + * @param string $where | |
| 40 | + * @param CT_Query $ct_query | |
| 41 | + * | |
| 42 | + * @return string | |
| 43 | + */ | |
| 44 | +function gamipress_user_earnings_query_where( $where, $ct_query ) { | |
| 45 | + | |
| 46 | + global $ct_table; | |
| 47 | + | |
| 48 | + if( $ct_table->name !== 'gamipress_user_earnings' ) { | |
| 49 | + return $where; | |
| 50 | + } | |
| 51 | + | |
| 52 | + $table_name = $ct_table->db->table_name; | |
| 53 | + | |
| 54 | + // Shorthand | |
| 55 | + $qv = $ct_query->query_vars; | |
| 56 | + | |
| 57 | + // User ID | |
| 58 | + $where .= gamipress_custom_table_where( $qv, 'user_id', 'user_id', 'integer' ); | |
| 59 | + | |
| 60 | + // Post ID | |
| 61 | + $where .= gamipress_custom_table_where( $qv, 'post_id', 'post_id', 'integer' ); | |
| 62 | + | |
| 63 | + // Post Type | |
| 64 | + $post_type_where = gamipress_custom_table_where( $qv, 'post_type', 'post_type', 'string' ); | |
| 65 | + | |
| 66 | + // Post Type (using the 'type' key) | |
| 67 | + if( isset( $qv['type'] ) && ! empty( $qv['type'] ) ) { | |
| 68 | + $post_type_where = gamipress_custom_table_where( $qv, 'type', 'post_type', 'string' ); | |
| 69 | + } | |
| 70 | + | |
| 71 | + // Points Type | |
| 72 | + $points_type_where = gamipress_custom_table_where( $qv, 'points_type', 'points_type', 'string' ); | |
| 73 | + | |
| 74 | + $post_type_where = str_replace( ' AND ', '', $post_type_where ); | |
| 75 | + $points_type_where = str_replace( ' AND ', '', $points_type_where ); | |
| 76 | + | |
| 77 | + if( ! empty( $post_type_where ) && ! empty( $points_type_where ) ) { | |
| 78 | + | |
| 79 | + if( isset( $qv['force_types'] ) && $qv['force_types'] === true ) { | |
| 80 | + $where .= " AND ( {$post_type_where} AND {$points_type_where} )"; | |
| 81 | + } else { | |
| 82 | + // If is querying by post and points type, then need to set this conditional as OR | |
| 83 | + $where .= " AND ( {$post_type_where} OR {$points_type_where} )"; | |
| 84 | + } | |
| 85 | + | |
| 86 | + } else if( ! empty( $post_type_where ) ) { | |
| 87 | + | |
| 88 | + // Where if just looking for post types and not for points types | |
| 89 | + $where .= " AND {$post_type_where}"; | |
| 90 | + | |
| 91 | + } else if( ! empty( $points_type_where ) ) { | |
| 92 | + | |
| 93 | + // Where if just looking for points types and not for post types | |
| 94 | + $where .= " AND {$points_type_where}"; | |
| 95 | + | |
| 96 | + } | |
| 97 | + | |
| 98 | + // Post type NOT IN | |
| 99 | + $where .= gamipress_custom_table_where( $qv, 'post_type__not_in', 'post_type', 'string', '!=', 'NOT IN' ); | |
| 100 | + | |
| 101 | + // Include | |
| 102 | + $where .= gamipress_custom_table_where( $qv, 'user_earning__in', 'user_earning_id', 'integer' ); | |
| 103 | + | |
| 104 | + // Exclude | |
| 105 | + $where .= gamipress_custom_table_where( $qv, 'user_earning__not_in', 'user_earning_id', 'integer', '!=', 'NOT IN' ); | |
| 106 | + | |
| 107 | + // Since | |
| 108 | + if( isset( $qv['since'] ) ) { | |
| 109 | + | |
| 110 | + $since = $qv['since']; | |
| 111 | + | |
| 112 | + // Turn a string date into time | |
| 113 | + if( gettype( $qv['since'] ) === 'string' ) { | |
| 114 | + $since = strtotime( $qv['since'] ); | |
| 115 | + } | |
| 116 | + | |
| 117 | + if( $since > 0 ) { | |
| 118 | + $since = date( 'Y-m-d H:i:s', $since ); | |
| 119 | + $where .= " AND {$table_name}.date > '{$since}'"; | |
| 120 | + } | |
| 121 | + } | |
| 122 | + | |
| 123 | + // Before | |
| 124 | + if( isset( $qv['before'] ) ) { | |
| 125 | + | |
| 126 | + $before = $qv['before']; | |
| 127 | + | |
| 128 | + // Turn a string date into time | |
| 129 | + if( gettype( $qv['before'] ) === 'string' ) { | |
| 130 | + $before = strtotime( $qv['before'] ); | |
| 131 | + } | |
| 132 | + | |
| 133 | + if( $before > 0 ) { | |
| 134 | + $before = date( 'Y-m-d H:i:s', $before ); | |
| 135 | + $where .= " AND {$table_name}.date < '{$before}'"; | |
| 136 | + } | |
| 137 | + | |
| 138 | + } | |
| 139 | + | |
| 140 | + // After | |
| 141 | + if( isset( $qv['after'] ) ) { | |
| 142 | + | |
| 143 | + $after = $qv['after']; | |
| 144 | + | |
| 145 | + // Turn a string date into time | |
| 146 | + if( gettype( $qv['after'] ) === 'string' ) { | |
| 147 | + $after = strtotime( $qv['after'] ); | |
| 148 | + } | |
| 149 | + | |
| 150 | + if( $after > 0 ) { | |
| 151 | + $after = date( 'Y-m-d H:i:s', $after ); | |
| 152 | + $where .= " AND {$table_name}.date > '{$after}'"; | |
| 153 | + } | |
| 154 | + | |
| 155 | + } | |
| 156 | + | |
| 157 | + // Parent post type | |
| 158 | + if( isset( $qv['parent_post_type'] ) && ! empty( $qv['parent_post_type'] ) ) { | |
| 159 | + | |
| 160 | + if( is_array( $qv['parent_post_type'] ) ) { | |
| 161 | + // Sanitize | |
| 162 | + $parent_post_type = array_map( 'sanitize_text_field', $qv['parent_post_type'] ); | |
| 163 | + $parent_post_type = "'" . implode( "', '", $parent_post_type ) . "'"; | |
| 164 | + | |
| 165 | + $where .= " OR ppt.meta_value IN ( {$parent_post_type} )"; | |
| 166 | + } else { | |
| 167 | + // Sanitize | |
| 168 | + $parent_post_type = sanitize_text_field( $qv['parent_post_type'] ); | |
| 169 | + $where .= " OR ppt.meta_value = '{$parent_post_type}'"; | |
| 170 | + } | |
| 171 | + } | |
| 172 | + | |
| 173 | + return $where; | |
| 174 | + | |
| 175 | +} | |
| 176 | +add_filter( 'ct_query_where', 'gamipress_user_earnings_query_where', 10, 2 ); | |
| 177 | + | |
| 178 | +/** | |
| 179 | + * Parse query args for user earnings to be applied on JOIN clause | |
| 180 | + * | |
| 181 | + * @since 1.2.8 | |
| 182 | + * | |
| 183 | + * @param string $join | |
| 184 | + * @param CT_Query $ct_query | |
| 185 | + * | |
| 186 | + * @return string | |
| 187 | + */ | |
| 188 | +function gamipress_user_earnings_query_join( $join, $ct_query ) { | |
| 189 | + | |
| 190 | + global $ct_table; | |
| 191 | + | |
| 192 | + if( $ct_table->name !== 'gamipress_user_earnings' ) { | |
| 193 | + return $join; | |
| 194 | + } | |
| 195 | + | |
| 196 | + $table_name = $ct_table->db->table_name; | |
| 197 | + $meta_table_name = $ct_table->meta->db->table_name; | |
| 198 | + | |
| 199 | + // Shorthand | |
| 200 | + $qv = $ct_query->query_vars; | |
| 201 | + | |
| 202 | + // Parent post type | |
| 203 | + if( isset( $qv['parent_post_type'] ) ) { | |
| 204 | + $join .= " LEFT JOIN {$meta_table_name} ppt ON ( ppt.user_earning_id = {$table_name}.user_earning_id AND ppt.meta_key = '_gamipress_parent_post_type' )"; | |
| 205 | + } | |
| 206 | + | |
| 207 | + return $join; | |
| 208 | + | |
| 209 | +} | |
| 210 | +add_filter( 'ct_query_join', 'gamipress_user_earnings_query_join', 10, 2 ); | |
| 211 | + | |
| 212 | +/** | |
| 213 | + * Define the search fields for user earnings | |
| 214 | + * | |
| 215 | + * @since 1.6.4.2 | |
| 216 | + * | |
| 217 | + * @param array $search_fields | |
| 218 | + * | |
| 219 | + * @return array | |
| 220 | + */ | |
| 221 | +function gamipress_user_earnings_search_fields( $search_fields ) { | |
| 222 | + | |
| 223 | + $search_fields[] = 'title'; | |
| 224 | + | |
| 225 | + return $search_fields; | |
| 226 | + | |
| 227 | +} | |
| 228 | +add_filter( 'ct_query_gamipress_user_earnings_search_fields', 'gamipress_user_earnings_search_fields' ); | |
| 229 | + | |
| 230 | +/** | |
| 231 | + * Parse search query args for user earnings | |
| 232 | + * | |
| 233 | + * @since 1.6.4.2 | |
| 234 | + * | |
| 235 | + * @param string $search | |
| 236 | + * @param CT_Query $ct_query | |
| 237 | + * | |
| 238 | + * @return string | |
| 239 | + */ | |
| 240 | +function gamipress_user_earnings_query_search( $search, $ct_query ) { | |
| 241 | + | |
| 242 | + global $ct_table, $wpdb; | |
| 243 | + | |
| 244 | + if( $ct_table->name !== 'gamipress_user_earnings' ) { | |
| 245 | + return $search; | |
| 246 | + } | |
| 247 | + | |
| 248 | + $table_name = $ct_table->db->table_name; | |
| 249 | + | |
| 250 | + // Shorthand | |
| 251 | + $qv = $ct_query->query_vars; | |
| 252 | + | |
| 253 | + // Check if is search and query is not filtered by an specific user | |
| 254 | + if( isset( $qv['s'] ) && ! empty( $qv['s'] ) && ! isset( $qv['user_id'] ) ) { | |
| 255 | + | |
| 256 | + // Made a user sub-search to retrieve them | |
| 257 | + $users = get_users( array( | |
| 258 | + 'search' => sprintf( '*%s*', $qv['s'] ), | |
| 259 | + 'search_columns' => array( | |
| 260 | + 'user_login', | |
| 261 | + 'user_email', | |
| 262 | + 'display_name', | |
| 263 | + ), | |
| 264 | + 'fields' => 'ID', | |
| 265 | + ) ); | |
| 266 | + | |
| 267 | + if( ! empty( $users ) ) { | |
| 268 | + $search .= " OR ( {$table_name}.user_id IN (" . implode( ',', array_map( 'absint', $users ) ) . ") )"; | |
| 269 | + } | |
| 270 | + | |
| 271 | + } | |
| 272 | + | |
| 273 | + return $search; | |
| 274 | + | |
| 275 | +} | |
| 276 | +add_filter( 'ct_query_search', 'gamipress_user_earnings_query_search', 10, 2 ); | |
| 277 | + | |
| 278 | +/** | |
| 279 | + * Parse query args for user earnings to be applied on GROUP BY clause | |
| 280 | + * | |
| 281 | + * @since 1.5.5 | |
| 282 | + * | |
| 283 | + * @param string $groupby | |
| 284 | + * @param CT_Query $ct_query | |
| 285 | + * | |
| 286 | + * @return string | |
| 287 | + */ | |
| 288 | +function gamipress_user_earnings_query_group_by( $groupby, $ct_query ) { | |
| 289 | + | |
| 290 | + global $ct_table; | |
| 291 | + | |
| 292 | + if( $ct_table->name !== 'gamipress_user_earnings' ) { | |
| 293 | + return $groupby; | |
| 294 | + } | |
| 295 | + | |
| 296 | + // Shorthand | |
| 297 | + $qv = $ct_query->query_vars; | |
| 298 | + | |
| 299 | + // Check if 'groupby' has been passed to the query | |
| 300 | + if( isset( $qv['groupby'] ) && ! empty( $qv['groupby'] ) ) { | |
| 301 | + | |
| 302 | + $groupby = $qv['groupby']; | |
| 303 | + | |
| 304 | + } | |
| 305 | + | |
| 306 | + return $groupby; | |
| 307 | + | |
| 308 | +} | |
| 309 | +add_filter( 'ct_query_groupby', 'gamipress_user_earnings_query_group_by', 10, 2 ); | |
| 310 | + | |
| 311 | +/** | |
| 312 | + * Bulk actions for user earnings list view | |
| 313 | + * | |
| 314 | + * @since 1.3.9.7 | |
| 315 | + * | |
| 316 | + * @param array $actions | |
| 317 | + * | |
| 318 | + * @return array | |
| 319 | + */ | |
| 320 | +function custom_gamipress_user_earnings_bulk_actions( $actions = array() ) { | |
| 321 | + | |
| 322 | + return array(); | |
| 323 | + | |
| 324 | +} | |
| 325 | +add_filter( 'gamipress_user_earnings_bulk_actions', 'custom_gamipress_user_earnings_bulk_actions' ); | |
| 326 | + | |
| 327 | +/** | |
| 328 | + * Columns for user earnings list view | |
| 329 | + * | |
| 330 | + * @since 1.2.8 | |
| 331 | + * | |
| 332 | + * @param array $columns | |
| 333 | + * | |
| 334 | + * @return array | |
| 335 | + */ | |
| 336 | +function gamipress_manage_user_earnings_columns( $columns = array() ) { | |
| 337 | + | |
| 338 | + global $ct_query, $pagenow; | |
| 339 | + | |
| 340 | + if( ( isset( $ct_query->query_vars['is_earners_box'] ) && $ct_query->query_vars['is_earners_box'] ) | |
| 341 | + || in_array( $pagenow, array( 'post.php', 'post-new.php' ) ) ) { | |
| 342 | + | |
| 343 | + // post.php and post-new.php are for the earners meta box | |
| 344 | + $columns['user_id'] = __( 'User', 'gamipress' ); | |
| 345 | + $columns['date'] = __( 'Date', 'gamipress' ); | |
| 346 | + | |
| 347 | + } else if( $pagenow === 'admin.php' ) { | |
| 348 | + | |
| 349 | + // admin.php is for the user earnings screen | |
| 350 | + $columns['name'] = __( 'Title', 'gamipress' ); | |
| 351 | + $columns['user_id'] = __( 'User', 'gamipress' ); | |
| 352 | + $columns['points'] = __( 'Points', 'gamipress' ); | |
| 353 | + $columns['date'] = __( 'Date', 'gamipress' ); | |
| 354 | + | |
| 355 | + } else { | |
| 356 | + | |
| 357 | + $columns['name'] = __( 'Title', 'gamipress' ); | |
| 358 | + $columns['points'] = __( 'Points', 'gamipress' ); | |
| 359 | + $columns['date'] = __( 'Date', 'gamipress' ); | |
| 360 | + | |
| 361 | + } | |
| 362 | + | |
| 363 | + if( current_user_can( gamipress_get_manager_capability() ) ) { | |
| 364 | + $columns['action'] = __( 'Actions', 'gamipress' ); | |
| 365 | + } | |
| 366 | + | |
| 367 | + return $columns; | |
| 368 | +} | |
| 369 | +add_filter( 'manage_gamipress_user_earnings_columns', 'gamipress_manage_user_earnings_columns' ); | |
| 370 | + | |
| 371 | +/** | |
| 372 | + * Sortable columns for user earnings list view | |
| 373 | + * | |
| 374 | + * @since 1.6.7 | |
| 375 | + * | |
| 376 | + * @param array $sortable_columns | |
| 377 | + * | |
| 378 | + * @return array | |
| 379 | + */ | |
| 380 | +function gamipress_manage_user_earnings_sortable_columns( $sortable_columns ) { | |
| 381 | + | |
| 382 | + global $pagenow; | |
| 383 | + | |
| 384 | + if( $pagenow === 'admin.php' ) { | |
| 385 | + $sortable_columns['name'] = array( 'title', false ); | |
| 386 | + $sortable_columns['date'] = array( 'date', true ); | |
| 387 | + $sortable_columns['user_id'] = array( 'user_id', false ); | |
| 388 | + $sortable_columns['points'] = array( 'points', false ); | |
| 389 | + } | |
| 390 | + | |
| 391 | + return $sortable_columns; | |
| 392 | + | |
| 393 | +} | |
| 394 | +add_filter( 'manage_gamipress_user_earnings_sortable_columns', 'gamipress_manage_user_earnings_sortable_columns' ); | |
| 395 | + | |
| 396 | +/** | |
| 397 | + * Define the field labels for user earnings views | |
| 398 | + * | |
| 399 | + * @since 1.8.3 | |
| 400 | + * | |
| 401 | + * @param array $field_labels | |
| 402 | + * | |
| 403 | + * @return array | |
| 404 | + */ | |
| 405 | +function gamipress_user_earnings_views_field_labels( $field_labels = array() ) { | |
| 406 | + | |
| 407 | + foreach( gamipress_get_achievement_types() as $type => $data ) { | |
| 408 | + $field_labels[$type] = $data['plural_name']; | |
| 409 | + } | |
| 410 | + | |
| 411 | + $field_labels['step'] = __( 'Steps', 'gamipress' ); | |
| 412 | + | |
| 413 | + foreach( gamipress_get_points_types() as $type => $data ) { | |
| 414 | + $field_labels[$type] = $data['plural_name']; | |
| 415 | + } | |
| 416 | + | |
| 417 | + $field_labels['points-award'] = __( 'Points Awards', 'gamipress' ); | |
| 418 | + $field_labels['points-deduct'] = __( 'Points Deductions', 'gamipress' ); | |
| 419 | + | |
| 420 | + foreach( gamipress_get_rank_types() as $type => $data ) { | |
| 421 | + $field_labels[$type] = $data['plural_name']; | |
| 422 | + } | |
| 423 | + | |
| 424 | + $field_labels['rank-requirement'] = __( 'Rank Requirements', 'gamipress' ); | |
| 425 | + | |
| 426 | + return $field_labels; | |
| 427 | +} | |
| 428 | +//add_filter( 'ct_list_gamipress_user_earnings_views_field_labels', 'gamipress_user_earnings_views_field_labels' ); | |
| 429 | + | |
| 430 | +/** | |
| 431 | + * Since the post_type query var causes issues, we need to pass it as type and process it differently | |
| 432 | + * | |
| 433 | + * @since 1.8.3 | |
| 434 | + * | |
| 435 | + * @param array $field_labels | |
| 436 | + * | |
| 437 | + * @return array | |
| 438 | + */ | |
| 439 | +function gamipress_user_earnings_get_list_views( $views = array() ) { | |
| 440 | + | |
| 441 | + global $wpdb, $ct_table; | |
| 442 | + | |
| 443 | + $field_id = 'post_type'; | |
| 444 | + $field_key = 'type'; | |
| 445 | + $field_labels = gamipress_user_earnings_views_field_labels(); | |
| 446 | + | |
| 447 | + // Get the number of entries per each different field value | |
| 448 | + $results = $wpdb->get_results( "SELECT {$field_id}, COUNT( * ) AS num_entries FROM {$ct_table->db->table_name} GROUP BY {$field_id}", ARRAY_A ); | |
| 449 | + | |
| 450 | + $counts = array(); | |
| 451 | + | |
| 452 | + // Loop them to build the counts array | |
| 453 | + foreach( $results as $result ) { | |
| 454 | + $counts[$result[$field_id]] = absint( $result['num_entries'] ); | |
| 455 | + } | |
| 456 | + | |
| 457 | + $list_link = ct_get_list_link( $ct_table->name ); | |
| 458 | + $current = isset( $_GET[$field_key] ) ? $_GET[$field_key] : ''; | |
| 459 | + | |
| 460 | + // Setup the 'All' view | |
| 461 | + $all_count = absint( $wpdb->get_var( "SELECT COUNT( * ) FROM {$ct_table->db->table_name}" ) ); | |
| 462 | + $views['all'] = '<a href="' . $list_link . '" class="' . ( empty( $current ) ? 'current' : '' ) . '">' . __( 'All', 'ct' ) . ' <span class="count">(' . $all_count . ')</span></a>'; | |
| 463 | + | |
| 464 | + foreach( $counts as $value => $count ) { | |
| 465 | + | |
| 466 | + // Skip fields that are not intended to being displayed | |
| 467 | + if( ! isset( $field_labels[$value] ) ) { | |
| 468 | + continue; | |
| 469 | + } | |
| 470 | + | |
| 471 | + $label = $field_labels[$value]; | |
| 472 | + $url = $list_link . '&' . $field_key . '=' . $value; | |
| 473 | + | |
| 474 | + $views[$value] = '<a href="' . $url . '" class="' . ( $current === $value ? 'current' : '' ) . '">' . $label . ' <span class="count">(' . $count . ')</span>' . '</a>'; | |
| 475 | + } | |
| 476 | + | |
| 477 | + return $views; | |
| 478 | +} | |
| 479 | +add_filter( 'gamipress_user_earnings_get_views', 'gamipress_user_earnings_get_list_views' ); | |
| 480 | + | |
| 481 | +/** | |
| 482 | + * Columns rendering for user earnings list view | |
| 483 | + * | |
| 484 | + * @since 1.2.8 | |
| 485 | + * | |
| 486 | + * @param string $column_name | |
| 487 | + * @param integer $object_id | |
| 488 | + */ | |
| 489 | +function gamipress_manage_user_earnings_custom_column( $column_name, $object_id ) { | |
| 490 | + | |
| 491 | + $can_manage = current_user_can( gamipress_get_manager_capability() ); | |
| 492 | + | |
| 493 | + // Setup vars | |
| 494 | + $requirement_types = gamipress_get_requirement_types(); | |
| 495 | + $achievement_types = gamipress_get_achievement_types(); | |
| 496 | + $rank_types = gamipress_get_rank_types(); | |
| 497 | + $user_earning = ct_get_object( $object_id ); | |
| 498 | + | |
| 499 | + switch( $column_name ) { | |
| 500 | + case 'name': | |
| 501 | + | |
| 502 | + // On network wide active installs, we need to switch to main blog mostly for posts permalinks and thumbnails | |
| 503 | + $blog_id = gamipress_switch_to_main_site_if_network_wide_active(); | |
| 504 | + | |
| 505 | + // Check if assigned post exists to modify the output | |
| 506 | + if( gamipress_post_exists( $user_earning->post_id ) ) : ?> | |
| 507 | + | |
| 508 | + <?php if( in_array( $user_earning->post_type, gamipress_get_requirement_types_slugs() ) ) : ?> | |
| 509 | + | |
| 510 | + <?php if( $user_earning->post_type === 'step' && $parent_achievement = gamipress_get_step_achievement( $user_earning->post_id ) ) : ?> | |
| 511 | + | |
| 512 | + <?php // Step ?> | |
| 513 | + | |
| 514 | + <?php // Achievement thumbnail ?> | |
| 515 | + <?php echo gamipress_get_achievement_post_thumbnail( $parent_achievement->ID, array( 32, 32 ) ); ?> | |
| 516 | + | |
| 517 | + <?php // Step title ?> | |
| 518 | + <strong><?php echo $user_earning->title; ?></strong> | |
| 519 | + <br> | |
| 520 | + | |
| 521 | + <?php // Step relationship details ?> | |
| 522 | + <?php echo ( isset( $requirement_types[$user_earning->post_type] ) ? $requirement_types[$user_earning->post_type]['singular_name'] : '' ); ?> | |
| 523 | + <?php echo ( isset( $achievement_types[$parent_achievement->post_type] ) ? ', ' . $achievement_types[$parent_achievement->post_type]['singular_name'] . ': ' : '' ); ?> | |
| 524 | + <?php echo '<a href="' . ( $can_manage ? get_edit_post_link( $parent_achievement->ID ) : get_post_permalink( $parent_achievement->ID ) ) . '">' . gamipress_get_post_field( 'post_title', $parent_achievement->ID ) . '</a>'; ?> | |
| 525 | + | |
| 526 | + <?php elseif( $user_earning->post_type === 'points-award' && $points_type = gamipress_get_points_award_points_type( $user_earning->post_id ) ) : ?> | |
| 527 | + | |
| 528 | + <?php // Points award ?> | |
| 529 | + | |
| 530 | + <?php // Points type thumbnail ?> | |
| 531 | + <?php echo gamipress_get_points_type_thumbnail( $points_type->ID, array( 32, 32 ) ); ?> | |
| 532 | + | |
| 533 | + <?php // Points award title ?> | |
| 534 | + <strong><?php echo $user_earning->title; ?></strong> | |
| 535 | + <br> | |
| 536 | + | |
| 537 | + <?php // Points award relationship details ?> | |
| 538 | + <?php echo ( isset( $requirement_types[$user_earning->post_type] ) ? $requirement_types[$user_earning->post_type]['singular_name'] : '' ); ?> | |
| 539 | + <?php echo ', ' . ( $can_manage ? '<a href="' . get_edit_post_link( $points_type->ID ) . '">' . gamipress_get_post_field( 'post_title', $points_type->ID ) . '</a>' : gamipress_get_post_field( 'post_title', $points_type->ID ) ); ?> | |
| 540 | + | |
| 541 | + <?php elseif( $user_earning->post_type === 'points-deduct' && $points_type = gamipress_get_points_deduct_points_type( $user_earning->post_id ) ) : ?> | |
| 542 | + | |
| 543 | + <?php // Points deduct ?> | |
| 544 | + | |
| 545 | + <?php // Points type thumbnail ?> | |
| 546 | + <?php echo gamipress_get_points_type_thumbnail( $points_type->ID, array( 32, 32 ) ); ?> | |
| 547 | + | |
| 548 | + <?php // Points deduct title ?> | |
| 549 | + <strong><?php echo $user_earning->title; ?></strong> | |
| 550 | + <br> | |
| 551 | + | |
| 552 | + <?php // Points deduct relationship details ?> | |
| 553 | + <?php echo ( isset( $requirement_types[$user_earning->post_type] ) ? $requirement_types[$user_earning->post_type]['singular_name'] : '' ); ?> | |
| 554 | + <?php echo ', ' . ( $can_manage ? '<a href="' . get_edit_post_link( $points_type->ID ) . '">' . gamipress_get_post_field( 'post_title', $points_type->ID ) . '</a>' : gamipress_get_post_field( 'post_title', $points_type->ID ) ); ?> | |
| 555 | + | |
| 556 | + <?php elseif( $user_earning->post_type === 'rank-requirement' && $rank = gamipress_get_rank_requirement_rank( $user_earning->post_id ) ) : ?> | |
| 557 | + | |
| 558 | + <?php // Rank Requirement ?> | |
| 559 | + | |
| 560 | + <?php // Rank thumbnail ?> | |
| 561 | + <?php echo gamipress_get_rank_post_thumbnail( $rank->ID, array( 32, 32 ) ); ?> | |
| 562 | + | |
| 563 | + <?php // Rank requirement title ?> | |
| 564 | + <strong><?php echo $user_earning->title; ?></strong> | |
| 565 | + <br> | |
| 566 | + | |
| 567 | + <?php // Rank relationship details ?> | |
| 568 | + <?php echo ( isset( $requirement_types[$user_earning->post_type] ) ? $requirement_types[$user_earning->post_type]['singular_name'] : '' ); ?> | |
| 569 | + <?php echo ( isset( $rank_types[$rank->post_type] ) ? ', ' . $rank_types[$rank->post_type]['singular_name'] . ': ' : '' ); ?> | |
| 570 | + <?php echo '<a href="' . ( $can_manage ? get_edit_post_link( $rank->ID ) : get_post_permalink( $rank->ID ) ) . '">' . gamipress_get_post_field( 'post_title', $rank->ID ) . '</a>'; ?> | |
| 571 | + | |
| 572 | + <?php endif; ?> | |
| 573 | + | |
| 574 | + <?php elseif( in_array( $user_earning->post_type, gamipress_get_achievement_types_slugs() ) ) : ?> | |
| 575 | + | |
| 576 | + <?php // Achievement ?> | |
| 577 | + | |
| 578 | + <?php // Achievement thumbnail ?> | |
| 579 | + <?php echo gamipress_get_achievement_post_thumbnail( $user_earning->post_id, array( 32, 32 ) ); ?> | |
| 580 | + | |
| 581 | + <?php // Achievement title ?> | |
| 582 | + <strong><?php echo '<a href="' . ( $can_manage ? get_edit_post_link( $user_earning->post_id ) : get_post_permalink( $user_earning->post_id ) ) . '">' . gamipress_get_post_field( 'post_title', $user_earning->post_id ) . '</a>'; ?></strong> | |
| 583 | + <?php echo ( isset( $achievement_types[$user_earning->post_type] ) ? '<br>' . $achievement_types[$user_earning->post_type]['singular_name'] : '' ); ?> | |
| 584 | + | |
| 585 | + <?php elseif( in_array( $user_earning->post_type, gamipress_get_rank_types_slugs() ) ) : ?> | |
| 586 | + | |
| 587 | + <?php // Rank ?> | |
| 588 | + | |
| 589 | + <?php // Rank thumbnail ?> | |
| 590 | + <?php echo gamipress_get_rank_post_thumbnail( $user_earning->post_id, array( 32, 32 ) ); ?> | |
| 591 | + | |
| 592 | + <?php // Rank title ?> | |
| 593 | + <strong><?php echo '<a href="' . ( $can_manage ? get_edit_post_link( $user_earning->post_id ) : get_post_permalink( $user_earning->post_id ) ) . '">' . gamipress_get_post_field( 'post_title', $user_earning->post_id ) . '</a>'; ?></strong> | |
| 594 | + <?php echo ( isset( $rank_types[$user_earning->post_type] ) ? '<br>' . $rank_types[$user_earning->post_type]['singular_name'] : '' ); ?> | |
| 595 | + | |
| 596 | + <?php else : ?> | |
| 597 | + | |
| 598 | + <?php // Default output ?> | |
| 599 | + | |
| 600 | + <?php // Thumbnail ?> | |
| 601 | + <?php echo gamipress_get_points_type_thumbnail( $user_earning->post_id, array( 32, 32 ) ); ?> | |
| 602 | + | |
| 603 | + <strong><?php echo $user_earning->title; ?></strong> | |
| 604 | + <br> | |
| 605 | + <?php echo ( $can_manage ? '<a href="' . get_edit_post_link( $user_earning->post_id ) . '">' . gamipress_get_post_field( 'post_title', $user_earning->post_id ) . '</a>' : gamipress_get_post_field( 'post_title', $user_earning->post_id ) ); ?> | |
| 606 | + | |
| 607 | + <?php endif; ?> | |
| 608 | + <?php else : ?> | |
| 609 | + | |
| 610 | + <?php // Default output if assigned post doesn't exists ?> | |
| 611 | + | |
| 612 | + <strong><?php echo $user_earning->title; ?></strong> | |
| 613 | + | |
| 614 | + <?php if( $post_type_object = get_post_type_object( $user_earning->post_type ) ) : ?> | |
| 615 | + <br> | |
| 616 | + <?php echo $post_type_object->labels->singular_name; ?> | |
| 617 | + <?php endif; ?> | |
| 618 | + | |
| 619 | + <?php endif; ?> | |
| 620 | + | |
| 621 | + <button type="button" class="toggle-row"><span class="screen-reader-text"><?php __( 'Show more details', 'gamipress' ); ?></span></button> | |
| 622 | + | |
| 623 | + <?php | |
| 624 | + | |
| 625 | + // If switched to blog, return back to que current blog | |
| 626 | + if( $blog_id !== get_current_blog_id() && is_multisite() ) { | |
| 627 | + restore_current_blog(); | |
| 628 | + } | |
| 629 | + | |
| 630 | + break; | |
| 631 | + case 'user_id': | |
| 632 | + $user = get_userdata( $user_earning->user_id ); | |
| 633 | + | |
| 634 | + if( $user ) : | |
| 635 | + | |
| 636 | + if( current_user_can( 'edit_users' ) ) { | |
| 637 | + ?> | |
| 638 | + <?php echo get_avatar( $user->ID, 32 ) ?> | |
| 639 | + <strong><?php echo $user->display_name; ?></strong> (<a href="<?php echo get_edit_user_link( $user_earning->user_id ); ?>"><?php echo $user->user_login; ?></a>) | |
| 640 | + <br> | |
| 641 | + <?php echo $user->user_email; ?> | |
| 642 | + <?php | |
| 643 | + } else { | |
| 644 | + echo $user->display_name; | |
| 645 | + } | |
| 646 | + | |
| 647 | + endif; | |
| 648 | + break; | |
| 649 | + case 'points': | |
| 650 | + $points = (int) $user_earning->points; | |
| 651 | + | |
| 652 | + if( $points !== 0 && gamipress_get_points_type( $user_earning->points_type ) ) { | |
| 653 | + | |
| 654 | + // For points deducts turn amount to negative | |
| 655 | + if( $user_earning->post_type === 'points-deduct' && $points > 0 ) { | |
| 656 | + $negative_points = $points * -1; | |
| 657 | + echo gamipress_format_points( $negative_points, $user_earning->points_type ); | |
| 658 | + } else { | |
| 659 | + echo gamipress_format_points( $points, $user_earning->points_type ); | |
| 660 | + } | |
| 661 | + | |
| 662 | + } | |
| 663 | + break; | |
| 664 | + case 'date': | |
| 665 | + $time = strtotime( $user_earning->date ); | |
| 666 | + ?> | |
| 667 | + | |
| 668 | + <abbr title="<?php echo date( 'Y/m/d g:i:s a', $time ); ?>"><?php echo date( 'Y/m/d', $time ); ?></abbr> | |
| 669 | + | |
| 670 | + <?php | |
| 671 | + break; | |
| 672 | + case 'action': | |
| 673 | + | |
| 674 | + // If user is not manager then skip this | |
| 675 | + if( ! $can_manage ) { | |
| 676 | + break; | |
| 677 | + } | |
| 678 | + | |
| 679 | + // Setup our revoke URL | |
| 680 | + $revoke_url = add_query_arg( array( | |
| 681 | + 'action' => 'revoke', | |
| 682 | + 'user_id' => absint( $user_earning->user_id ), | |
| 683 | + 'achievement_id' => absint( $user_earning->post_id ), | |
| 684 | + 'user_earning_id' => absint( $user_earning->user_earning_id ), | |
| 685 | + ) ); | |
| 686 | + ?> | |
| 687 | + | |
| 688 | + <span class="delete"><a class="error gamipress-revoke-user-earning" href="<?php echo esc_url( wp_nonce_url( $revoke_url, 'gamipress_revoke_achievement' ) ); ?>"><?php _e( 'Revoke Award', 'gamipress' ); ?></a></span> | |
| 689 | + | |
| 690 | + <?php if( gamipress_is_achievement( $user_earning->post_id ) || gamipress_is_rank( $user_earning->post_id ) ) : ?> | |
| 691 | + <?php $credential_url = gamipress_get_credential_url( $user_earning ); ?> | |
| 692 | + <br> | |
| 693 | + <a class="gamipress-credential-link" href="<?php echo esc_url( $credential_url ); ?>" target="_blank"><?php _e( 'View Credential', 'gamipress' ); ?></a> | |
| 694 | + <?php endif; ?> | |
| 695 | + | |
| 696 | + <?php | |
| 697 | + break; | |
| 698 | + } | |
| 699 | +} | |
| 700 | +add_action( 'manage_gamipress_user_earnings_custom_column', 'gamipress_manage_user_earnings_custom_column', 10, 2 ); | |
| 701 | + | |
| 702 | +/** | |
| 703 | + * Remove row actions on user earnings table | |
| 704 | + * | |
| 705 | + * @since 1.2.8 | |
| 706 | + * | |
| 707 | + * @param array $row_actions | |
| 708 | + * @param stdClass $object | |
| 709 | + * | |
| 710 | + * @return array | |
| 711 | + */ | |
| 712 | +function gamipress_user_earnings_row_actions( $row_actions, $object ) { | |
| 713 | + return array(); | |
| 714 | +} | |
| 715 | +add_filter( 'gamipress_user_earnings_row_actions', 'gamipress_user_earnings_row_actions', 10, 2 ); | |
| 716 | + | |
| 717 | +// CT Ajax List | |
| 718 | +// Override ajax list capability | |
| 719 | +add_filter( 'ct_ajax_list_table_gamipress_user_earnings_capability', 'gamipress_get_manager_capability' ); | |
| 720 | + | |
| 721 | +// Parse query args | |
| 722 | +function ct_ajax_list_table_gamipress_user_earnings_query_args( $query_args ) { | |
| 723 | + | |
| 724 | + $query_args = gamipress_sanitize_query_args( $query_args, gamipress_user_earnings_query_vars_rules() ); | |
| 725 | + | |
| 726 | + return $query_args; | |
| 727 | + | |
| 728 | +} | |
| 729 | +add_filter( 'ct_ajax_list_table_gamipress_user_earnings_query_args', 'ct_ajax_list_table_gamipress_user_earnings_query_args' ); | |