PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 7.9.5
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v7.9.5
8.0.5 8.0.4 8.0.3 8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 All 47 releases
gamipress / includes / compatibility / 1.2.8.php

1.2.8.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 7.9.5, at includes/compatibility/1.2.8.php

1,076 lines 35.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * GamiPress 1.2.8 compatibility functions
4 *
5 * @package GamiPress\1.2.8
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 * Achievement functions
14 -------------------------- */
15
16 /**
17 * Get an array of all users who have earned a given achievement
18 *
19 * @since 1.0.0
20 *
21 * @param integer $achievement_id The given achievement's post ID
22 *
23 * @return array Array of user objects
24 */
25 function gamipress_get_achievement_earners_old( $achievement_id = 0 ) {
26
27 // Grab our earners
28 $earners = new WP_User_Query( array(
29 'meta_key' => '_gamipress_achievements',
30 'meta_value' => $achievement_id,
31 'meta_compare' => 'LIKE'
32 ) );
33
34 $earned_users = array();
35 foreach( $earners->results as $earner ) {
36 if ( gamipress_has_user_earned_achievement( $achievement_id, $earner->ID ) ) {
37 $earned_users[] = $earner;
38 }
39 }
40 // Send back our query results
41 return $earned_users;
42 }
43
44 /**
45 * Change all earned meta from one achievement type to another.
46 *
47 * @since 1.0.0
48 *
49 * @param string $original_type Original achievement type.
50 * @param string $new_type New achievement type.
51 */
52 function gamipress_update_earned_meta_achievement_types_old( $original_type = '', $new_type = '' ) {
53
54 $metas = gamipress_get_unserialized_achievement_metas( '_gamipress_achievements', $original_type );
55
56 if ( ! empty( $metas ) ) {
57 foreach ( $metas as $meta ) {
58 foreach ( $meta->meta_value as $site_id => $achievements ) {
59 $meta->meta_value[ $site_id ] = gamipress_update_meta_achievement_types( $achievements, $original_type, $new_type );
60 }
61 update_user_meta( $meta->user_id, $meta->meta_key, $meta->meta_value );
62 }
63 }
64
65 }
66
67 /* --------------------------
68 * Rules Engine
69 -------------------------- */
70
71 /**
72 * Revoke an achievement from a user
73 *
74 * @since 1.0.0
75 *
76 * @param integer $achievement_id The given achievement's post ID
77 * @param integer $user_id The given user's ID
78 *
79 * @return void
80 */
81 function gamipress_revoke_achievement_from_user_old( $achievement_id = 0, $user_id = 0 ) {
82
83 // Use the current user's ID if none specified
84 if ( ! $user_id )
85 $user_id = wp_get_current_user()->ID;
86
87 // Grab the user's earned achievements
88 $earned_achievements = gamipress_get_user_achievements( array( 'user_id' => $user_id ) );
89
90 // Loop through each achievement and drop the achievement we're revoking
91 foreach ( $earned_achievements as $key => $achievement ) {
92 if ( $achievement->ID == $achievement_id ) {
93
94 // Drop the achievement from our earnings
95 unset( $earned_achievements[$key] );
96
97 // Re-key our array
98 $earned_achievements = array_values( $earned_achievements );
99
100 // Update user's earned achievements
101 gamipress_update_user_achievements( array( 'user_id' => $user_id, 'all_achievements' => $earned_achievements ) );
102
103 // Available hook for taking further action when an achievement is revoked
104 do_action( 'gamipress_revoke_achievement', $user_id, $achievement_id );
105
106 // Stop after dropping one, because we don't want to delete ALL instances
107 break;
108 }
109 }
110
111 }
112
113 /* --------------------------
114 * User functions
115 -------------------------- */
116
117 /**
118 * Get a user's gamipress achievements
119 *
120 * @since 1.0.0
121 *
122 * @param array $args An array of all our relevant arguments
123 *
124 * @return array An array of all the achievement objects that matched our parameters, or empty if none
125 */
126 function gamipress_get_user_achievements_old( $args = array() ) {
127
128 // Setup our default args
129 $defaults = array(
130 'user_id' => 0, // The given user's ID
131 'site_id' => get_current_blog_id(), // The given site's ID
132 'achievement_id' => false, // A specific achievement's post ID
133 'achievement_type' => false, // A specific achievement type
134 'since' => 0, // A specific timestamp to use in place of $limit_in_days
135 );
136
137 $args = wp_parse_args( $args, $defaults );
138
139 // Use current user's ID if none specified
140 if ( ! $args['user_id'] )
141 $args['user_id'] = get_current_user_id();
142
143 // Grab the user's current achievements
144 $achievements = ( $earned_items = get_user_meta( absint( $args['user_id'] ), '_gamipress_achievements', true ) ) ? (array) $earned_items : array();
145
146 // If we want all sites (or no specific site), return the full array
147 if ( empty( $achievements ) || empty( $args['site_id']) || 'all' == $args['site_id'] )
148 return $achievements;
149
150 // Otherwise, we only want the specific site's achievements
151 $achievements = $achievements[$args['site_id']];
152
153 if ( is_array( $achievements ) && ! empty( $achievements ) ) {
154 foreach ( $achievements as $key => $achievement ) {
155
156 // Drop any achievements earned before our since timestamp
157 if ( absint($args['since']) > $achievement->date_earned )
158 unset($achievements[$key]);
159
160 // Drop any achievements that don't match our achievement ID
161 if ( ! empty( $args['achievement_id'] ) && absint( $args['achievement_id'] ) != $achievement->ID )
162 unset($achievements[$key]);
163
164 // Drop any achievements that don't match our achievement type
165 if ( ! empty( $args['achievement_type'] ) && ( $args['achievement_type'] != $achievement->post_type && ( !is_array( $args['achievement_type'] ) || !in_array( $achievement->post_type, $args['achievement_type'] ) ) ) )
166 unset($achievements[$key]);
167
168 if( isset( $args['display'] ) && $args['display'] ) {
169 // Unset hidden achievements on display context
170 $hidden = gamipress_get_hidden_achievement_by_id( $achievement->ID );
171
172 if( ! empty( $hidden ) ) {
173 unset( $achievements[$key] );
174 }
175 }
176 }
177 }
178
179 // Return our $achievements array_values (so our array keys start back at 0), or an empty array
180 return ( is_array( $achievements ) ? array_values( $achievements ) : array());
181
182 }
183
184 /**
185 * Updates the user's earned achievements
186 *
187 * We can either replace the achievement's array, or append new achievements to it.
188 *
189 * @since 1.0.0
190 *
191 * @param array $args An array containing all our relevant arguments
192 *
193 * @return integer|bool The updated umeta ID on success, false on failure
194 */
195 function gamipress_update_user_achievements_old( $args = array() ) {
196
197 // Setup our default args
198 $defaults = array(
199 'user_id' => 0, // The given user's ID
200 'site_id' => get_current_blog_id(), // The given site's ID
201 'all_achievements' => false, // An array of ALL achievements earned by the user
202 'new_achievements' => false, // An array of NEW achievements earned by the user
203 );
204 $args = wp_parse_args( $args, $defaults );
205
206 // Use current user's ID if none specified
207 if ( ! $args['user_id'] )
208 $args['user_id'] = get_current_user_id();
209
210 // If we don't already have an array stored for this site, create a fresh one
211 // Grab our user's achievements
212 $achievements = gamipress_get_user_achievements( array(
213 'user_id' => absint( $args['user_id'] ),
214 'site_id' => 'all'
215 ) );
216
217 if ( !isset( $achievements[$args['site_id']] ) )
218 $achievements[$args['site_id']] = array();
219
220 // Determine if we should be replacing or appending to our achievements array
221 if ( is_array( $args['all_achievements'] ) )
222 $achievements[$args['site_id']] = $args['all_achievements'];
223 elseif ( is_array( $args['new_achievements'] ) && ! empty( $args['new_achievements'] ) )
224 $achievements[$args['site_id']] = array_merge( $achievements[$args['site_id']], $args['new_achievements'] );
225
226 // Finally, update our user meta
227 return update_user_meta( absint( $args['user_id'] ), '_gamipress_achievements', $achievements);
228
229 }
230
231 /* --------------------------
232 * Logs
233 -------------------------- */
234
235 /**
236 * Register all GamiPress CPTs
237 *
238 * @since 1.0.0
239 * @return void
240 */
241 function gamipress_register_post_types_old() {
242
243 if( is_gamipress_upgraded_to( '1.2.8' ) ) {
244 return;
245 }
246
247 // Register Log
248 register_post_type( 'gamipress-log', array(
249 'labels' => array(
250 'name' => __( 'Logs', 'gamipress' ),
251 'singular_name' => __( 'Log', 'gamipress' ),
252 'add_new' => __( 'Add New', 'gamipress' ),
253 'add_new_item' => __( 'Add New Log Entry', 'gamipress' ),
254 'edit_item' => __( 'Edit Log Entry', 'gamipress' ),
255 'new_item' => __( 'New Log Entry', 'gamipress' ),
256 'all_items' => __( 'Logs', 'gamipress' ),
257 'view_item' => __( 'View Logs', 'gamipress' ),
258 'search_items' => __( 'Search Logs', 'gamipress' ),
259 'not_found' => __( 'No Log Entries found', 'gamipress' ),
260 'not_found_in_trash' => __( 'No Log Entries found in Trash', 'gamipress' ),
261 'parent_item_colon' => '',
262 'menu_name' => __( 'Logs', 'gamipress' )
263 ),
264 'public' => false,
265 'publicly_queryable' => false,
266 'show_ui' => current_user_can( gamipress_get_manager_capability() ),
267 'show_in_menu' => 'gamipress',
268 'show_in_nav_menus' => false,
269 'query_var' => true,
270 'rewrite' => array( 'slug' => 'gamipress-log' ),
271 'capability_type' => 'post',
272 'has_archive' => false,
273 'hierarchical' => false,
274 'menu_position' => null,
275 'supports' => array( '' )
276 ) );
277
278 }
279 add_action( 'init', 'gamipress_register_post_types_old', 11 );
280
281 /**
282 * Register custom meta boxes used throughout GamiPress
283 *
284 * @since 1.0.0
285 */
286 function gamipress_meta_boxes_old() {
287
288 if( is_gamipress_upgraded_to( '1.2.8' ) ) {
289 return;
290 }
291
292 // Start with an underscore to hide fields from custom fields list
293 $prefix = '_gamipress_';
294
295 // Log
296 gamipress_add_meta_box(
297 'log-data',
298 __( 'Log Data', 'gamipress' ),
299 'gamipress-log',
300 array(
301 'post_author' => array(
302 'name' => __( 'User', 'gamipress' ),
303 'desc' => __( 'User assigned to this log.', 'gamipress' ),
304 'type' => 'select',
305 'options_cb' => 'gamipress_log_post_author_options'
306 ),
307 $prefix . 'type' => array(
308 'name' => __( 'Type', 'gamipress' ),
309 'desc' => __( 'The log type.', 'gamipress' ),
310 'type' => 'select',
311 'options' => gamipress_get_log_types(),
312 ),
313 $prefix . 'pattern' => array(
314 'name' => __( 'Pattern', 'gamipress' ),
315 'desc' => __( 'The log output pattern. Available tags:', 'gamipress' ) . gamipress_get_log_pattern_tags_html(),
316 'type' => 'text',
317 ),
318 ),
319 array( 'priority' => 'high' )
320 );
321
322 }
323 add_action( 'cmb2_admin_init', 'gamipress_meta_boxes_old' );
324
325 /**
326 * GamiPress dashboard widget output.
327 */
328 function gamipress_dashboard_widget_logs_old() {
329 $posts = new WP_Query( array(
330 'post_type' => 'gamipress-log',
331 'post_status' => 'any',
332 'orderby' => 'date',
333 'order' => 'DESC',
334 'posts_per_page' => 5,
335 'no_found_rows' => true,
336 'cache_results' => false,
337 ) );
338
339 if ( $posts->have_posts() ) {
340
341 echo '<div id="gamipress-latest-logs" class="gamipress-latest-logs">';
342
343 echo '<ul>';
344
345 $today = date( 'Y-m-d', current_time( 'timestamp' ) );
346 $yesterday = date( 'Y-m-d', strtotime( '-1 day', current_time( 'timestamp' ) ) );
347
348 while ( $posts->have_posts() ) {
349 $posts->the_post();
350
351 $time = get_the_time( 'U' );
352 if ( date( 'Y-m-d', $time ) == $today ) {
353 $relative = __( 'Today', 'gamipress' );
354 } elseif ( date( 'Y-m-d', $time ) == $yesterday ) {
355 $relative = __( 'Yesterday', 'gamipress' );
356 } elseif ( date( 'Y', $time ) !== date( 'Y', current_time( 'timestamp' ) ) ) {
357 /* translators: date and time format for recent posts on the dashboard, from a different calendar year, see https://secure.php.net/date */
358 $relative = date_i18n( __( 'M jS Y' ), $time );
359 } else {
360 /* translators: date and time format for recent posts on the dashboard, see https://secure.php.net/date */
361 $relative = date_i18n( __( 'M jS' ), $time );
362 }
363
364 // Use the post edit link for those who can edit, the permalink otherwise.
365 $edit_post_link = current_user_can( 'edit_post', get_the_ID() ) ? get_edit_post_link() : get_permalink();
366
367 $draft_or_post_title = _draft_or_post_title();
368 printf(
369 '<li><a href="%1$s">%2$s</a> <span>%3$s</span></li>',
370 $edit_post_link,
371 $draft_or_post_title,
372 sprintf( _x( '%1$s, %2$s', 'dashboard' ), $relative, get_the_time() )
373 );
374 }
375
376 echo '</ul>';
377 echo '</div>';
378
379 } else {
380 echo '<p>' . __( 'Nothing to show :)', 'gamipress' ) .'</p>';
381 }
382
383 wp_reset_postdata();
384 }
385
386 function gamipress_log_post_author_options( $field ) {
387 global $post;
388
389 $post_author = get_post_field( 'post_author', $post->ID );
390 $user = get_userdata( $post_author );
391
392 return array( $post_author => $user->display_name . ' (' . $user->user_login . ')' );
393 }
394
395
396 // WordPress will handle the saving for us, so don't save post author to meta.
397 add_filter( 'cmb2_override_post_author_meta_save', '__return_true' );
398
399 // Show log title in edit log screen
400 function gamipress_admin_log_title_preview( $post ) {
401 if( $post->post_type === 'gamipress-log' ) : ?>
402 <div class="gamipress-log-title-preview">
403 <h1><?php echo get_the_title( $post->ID ); ?></h1>
404 </div>
405 <?php endif;
406 }
407 add_action( 'edit_form_after_title', 'gamipress_admin_log_title_preview' );
408
409 /**
410 * Return count of user logs with the specified meta data
411 *
412 * @since 1.1.8
413 *
414 * @param int $user_id
415 * @param array $log_meta
416 *
417 * @return bool
418 */
419 function gamipress_get_user_log_count_old( $user_id = 0, $log_meta = array() ) {
420
421 global $wpdb;
422
423 // Initialize query definitions
424 $joins = array();
425 $where = array();
426
427 // Initialize query args
428 $query_args = array( 'gamipress-log', $user_id );
429
430 foreach ( (array) $log_meta as $key => $meta ) {
431 $index = count( $joins );
432
433 // Setup query definitions
434 $joins[] = "LEFT JOIN $wpdb->postmeta AS pm{$index} ON ( p.ID = pm{$index}.post_id )";
435 $where[] = "pm{$index}.meta_key = %s AND pm{$index}.meta_value = %s";
436
437 // Setup query vars
438 $query_args[] = '_gamipress_' . sanitize_key( $key );
439 $query_args[] = $meta;
440 }
441
442 // Turn arrays into strings
443 $joins = implode( ' ', $joins );
444 $where = ( ! empty( $where ) ? '( '. implode( ' ) AND ( ', $where ) . ' ) ' : '' );
445
446 $user_triggers = $wpdb->get_var( $wpdb->prepare(
447 "
448 SELECT COUNT(*)
449 FROM $wpdb->posts AS p
450 {$joins}
451 WHERE p.post_type = %s
452 AND p.post_author = %s
453 AND ( {$where} )
454 ",
455 $query_args
456 ) );
457
458 return absint( $user_triggers );
459 }
460
461 /**
462 * Posts a log entry when a user unlocks any achievement post
463 *
464 * @since 1.0.0
465 *
466 * @param int $user_id The user ID
467 * @param string $access Access to this log ( public|private )
468 * @param array $log_meta Log meta data
469 *
470 * @return integer The post ID of the newly created log entry
471 */
472 function gamipress_insert_log_old( $user_id = 0, $access = 'public', $log_meta = array() ) {
473
474 // Post data
475 $log_data = array(
476 'post_type' => 'gamipress-log',
477 'post_status' => ( ( $access === 'public' ) ? 'publish' : 'private' ),
478 'post_author' => $user_id === 0 ? get_current_user_id() : $user_id,
479 'post_parent' => 0,
480 'post_title' => '',
481 'post_content' => ''
482 );
483
484 // Auto-generated post title
485 $log_data['post_title'] = gamipress_parse_log_pattern( $log_meta['pattern'], $log_data, $log_meta );
486
487 // Store log entry
488 $log_id = wp_insert_post( $log_data );
489
490 // Store log meta data
491 if ( $log_id && ! empty( $log_meta ) ) {
492 foreach ( (array) $log_meta as $key => $meta ) {
493 update_post_meta( $log_id, '_gamipress_' . sanitize_key( $key ), $meta );
494 }
495 }
496
497 // Hook to add custom data
498 do_action( 'gamipress_insert_log', $log_id, $log_data, $log_meta, $user_id );
499
500 return $log_id;
501
502 }
503
504 /**
505 * Parse log pattern replacements
506 *
507 * @since 1.0.0
508 *
509 * @param string $log_pattern The pattern
510 * @param array $log_data Log post data
511 * @param array $log_meta Log meta data
512 *
513 * @return integer The post ID of the newly created log entry
514 */
515 function gamipress_parse_log_pattern_old( $log_pattern = '', $log_data = array(), $log_meta = array()) {
516 global $gamipress_pattern_replacements;
517
518 $post_author = get_userdata( $log_data['post_author'] );
519
520 // Setup user pattern replacements
521 $gamipress_pattern_replacements['{user_id}'] = $post_author->ID;
522 $gamipress_pattern_replacements['{user}'] = ( is_admin() ? $post_author->display_name . ' (' . $post_author->user_login . ')' : $post_author->display_name );
523
524 foreach( $log_meta as $log_meta_key => $log_meta_value ) {
525 if( in_array( $log_meta_key, array( 'pattern', 'type' ) ) ) {
526 continue;
527 }
528
529 $gamipress_pattern_replacements['{' . $log_meta_key . '}'] = $log_meta_value;
530 }
531
532 do_action( 'gamipress_before_parse_log_pattern', $log_data, $log_meta );
533
534 return str_replace( array_keys( $gamipress_pattern_replacements ), $gamipress_pattern_replacements, $log_pattern );
535 }
536
537 /**
538 * Log pattern replacements for achievements earn
539 *
540 * @since 1.0.0
541 *
542 * @uses global $gamipress_pattern_replacements
543 *
544 * @param array $log_data Log post data
545 * @param array $log_meta Log meta data
546 */
547 function gamipress_parse_achievement_log_pattern_old( $log_data, $log_meta ) {
548
549 // If log has assigned an achievement, then add achievement pattern replacements
550 if( isset( $log_meta['achievement_id'] ) ) {
551
552 global $gamipress_pattern_replacements;
553
554 // Achievement pattern replacements
555 $achievement = get_post( $log_meta['achievement_id'] );
556 $achievement_types = gamipress_get_achievement_types();
557 $achievement_type = ( $achievement && isset( $achievement_types[$achievement->post_type]['singular_name'] ) ) ? $achievement_types[$achievement->post_type]['singular_name'] : '';
558
559 // {achievement} tag
560 $gamipress_pattern_replacements['{achievement}'] = $achievement->post_title;
561
562 // {achievement_type} tag
563 $gamipress_pattern_replacements['{achievement_type}'] = $achievement_type;
564 }
565
566 }
567
568 /**
569 * Log pattern replacements for points earn/award
570 *
571 * @since 1.0.0
572 *
573 * @uses global $gamipress_pattern_replacements
574 *
575 * @param array $log_data Log post data
576 * @param array $log_meta Log meta data
577 */
578 function gamipress_parse_points_log_pattern_old( $log_data, $log_meta ) {
579
580 // If log is a points based entry, then add points pattern replacements
581 if( $log_meta['type'] === 'points_award' || $log_meta['type'] === 'points_earn' ) {
582 global $gamipress_pattern_replacements;
583
584 $points_type = $log_meta['points_type'];
585
586 $points_types = gamipress_get_points_types();
587
588 // Default points label
589 $points_label = __( 'points', 'gamipress' );
590
591 if( isset( $points_types[$points_type] ) ) {
592 // Points type label
593 $points_label = strtolower( $points_types[$points_type]['plural_name'] );
594 }
595
596 // {points_type} tag
597 $gamipress_pattern_replacements['{points_type}'] = $points_label;
598
599 if( $log_meta['type'] === 'points_award' ) {
600 $admin = get_userdata( $log_meta['admin_id'] );
601
602 // {admin_username} tag
603 $gamipress_pattern_replacements['{admin}'] = ( is_admin() ? $admin->display_name . ' (' . $admin->user_login . ')' : $admin->display_name );
604 }
605 }
606
607 }
608
609 /**
610 * Update the post title of a log entry.
611 *
612 * @since 1.0.0
613 *
614 * @param array $data Post data.
615 * @param array $post_args Post args.
616 * @return array Updated post data.
617 */
618 function gamipress_maybe_apply_log_pattern_old( $data = array(), $post_args = array() ) {
619
620 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
621 return $data;
622 }
623
624 if ( $post_args['ID'] !== 0 && $post_args['post_type'] === 'gamipress-log' ) {
625 $data['post_title'] = gamipress_get_parsed_log_old( $post_args['ID'] );
626 }
627
628 return $data;
629
630 }
631 add_filter( 'wp_insert_post_data' , 'gamipress_maybe_apply_log_pattern_old' , 99, 2 );
632
633 /**
634 * Get and apply the pattern of a log entry.
635 *
636 * @since 1.0.0
637 *
638 * @param integer $log_id The log's ID.
639 * @return string Parsed log pattern.
640 */
641 function gamipress_get_parsed_log_old( $log_id = null ) {
642
643 $prefix = '_gamipress_';
644
645 if( $log_id === null ) {
646 $log_id = get_the_ID();
647 }
648
649 $log_data = get_post( $log_id, ARRAY_A );
650
651 if( ! $log_data ) {
652 return '';
653 }
654
655 if( $log_data['post_type'] !== 'gamipress-log' ) {
656 return '';
657 }
658
659 $log_meta = array(
660 'pattern' => get_post_meta( $log_id, $prefix . 'pattern', true ),
661 'type' => get_post_meta( $log_id, $prefix . 'type', true )
662 );
663
664 $log_meta = apply_filters( 'gamipress_get_log_meta_data_old', $log_meta, $log_id );
665
666 return gamipress_parse_log_pattern_old( $log_meta['pattern'], $log_data, $log_meta );
667
668 }
669
670 /**
671 * Get log meta data defaults from a log's post ID
672 *
673 * @param $log_meta
674 * @param $log_id
675 *
676 * @return mixed
677 */
678 function gamipress_get_log_meta_data_defaults_old( $log_meta, $log_id ) {
679 $prefix = '_gamipress_';
680
681 $meta_keys = array();
682
683 if( $log_meta['type'] === 'event_trigger' ) {
684 // Event trigger meta data
685 $meta_keys[] = 'trigger_type';
686 $meta_keys[] = 'count';
687 } else if( $log_meta['type'] === 'achievement_earn' || $log_meta['type'] === 'achievement_award' ) {
688 // Achievement earn meta data
689 $meta_keys[] = 'achievement_id';
690
691 if( $log_meta['type'] === 'achievement_award' ) {
692 // Specific achievement award meta data
693 $meta_keys[] = 'admin_id';
694 }
695 } else if( $log_meta['type'] === 'points_award' || $log_meta['type'] === 'points_earn' ) {
696 // Points earn/award meta data
697 $meta_keys[] = 'points';
698 $meta_keys[] = 'points_type';
699 $meta_keys[] = 'total_points';
700
701 if( $log_meta['type'] === 'points_award' ) {
702 // Specific points award meta data
703 $meta_keys[] = 'admin_id';
704 }
705 }
706
707 foreach( $meta_keys as $meta_key ) {
708 $log_meta[$meta_key] = get_post_meta( $log_id, $prefix . $meta_key, true );
709 }
710
711 return $log_meta;
712 }
713 add_filter( 'gamipress_get_log_meta_data_old', 'gamipress_get_log_meta_data_defaults', 10, 2 );
714
715 /**
716 * Logs List Shortcode.
717 *
718 * @since 1.0.0
719 *
720 * @param array $atts Shortcode attributes.
721 * @return string HTML markup.
722 */
723 function gamipress_logs_shortcode_old( $atts = array () ) {
724
725 global $gamipress_template_args;
726
727 $gamipress_template_args = array();
728
729 $atts = shortcode_atts( array(
730 'user_id' => '0',
731 'limit' => '10',
732 'orderby' => 'menu_order',
733 'order' => 'ASC',
734 'include' => '',
735 'exclude' => '',
736 ), $atts, 'gamipress_logs' );
737
738 gamipress_enqueue_scripts();
739
740 // GamiPress template args global
741 $gamipress_template_args = $atts;
742
743 // Query Achievements
744 $args = array(
745 'post_type' => 'gamipress-log',
746 'orderby' => $atts['orderby'],
747 'order' => $atts['order'],
748 'posts_per_page' => $atts['limit'],
749 'post_status' => 'publish',
750 );
751
752 // User
753 if( $atts['user_id'] !== '0' ) {
754 $args['author'] = $atts['user_id'];
755 }
756
757 // Build $include array
758 if ( ! is_array( $atts['include'] ) && ! empty( $atts['include'] ) ) {
759 $include = explode( ',', $atts['include'] );
760 }
761
762 // Build $exclude array
763 if ( ! is_array( $atts['exclude'] ) && ! empty( $atts['exclude'] ) ) {
764 $exclude = explode( ',', $atts['exclude'] );
765 }
766
767 // Include certain achievements
768 if ( isset( $include ) && ! empty( $include ) ) {
769 $args[ 'post__in' ] = $include;
770 }
771
772 // Exclude certain achievements
773 if ( isset( $exclude ) && ! empty( $exclude ) ) {
774 $args[ 'post__not_in' ] = $exclude;
775 }
776
777 $gamipress_template_args['query'] = new WP_Query( $args );
778
779 ob_start();
780 gamipress_get_template_part( 'old/logs' );
781 $output = ob_get_clean();
782
783 return $output;
784
785 }
786
787 /**
788 * Filters the post title.
789 *
790 * @param string $title The post title.
791 * @param int $id The post ID.
792 *
793 * @return string The formatted title
794 */
795 function gamipress_log_title_format_old( $title, $id = null ) {
796
797 if( $id === null ) {
798 $id = get_the_ID();
799 }
800
801 if( get_post_type( $id ) !== 'gamipress-log' ) {
802 return $title;
803 }
804
805 return gamipress_get_parsed_log( $id );
806 }
807 add_filter( 'the_title', 'gamipress_log_title_format_old', 10, 2 );
808
809 /**
810 * Get the count for the number of times is logged a user has triggered a particular trigger
811 *
812 * @since 1.0.0
813 *
814 * @param integer $user_id The given user's ID
815 * @param string $trigger The given trigger we're checking
816 * @param integer $since The since timestamp where retrieve the logs
817 * @param integer $site_id The desired Site ID to check
818 * @param array $args The triggered args or requirement object
819 *
820 * @return integer The total number of times a user has triggered the trigger
821 */
822 function gamipress_get_user_trigger_count_old( $user_id, $trigger, $since = 0, $site_id = 0, $args = array() ) {
823
824 global $wpdb;
825
826 // Set to current site id
827 if ( ! $site_id )
828 $site_id = get_current_blog_id();
829
830 $post_date = '';
831
832 if( $since !== 0 ) {
833 $now = date( 'Y-m-d' );
834 $since = date( 'Y-m-d', $since );
835
836 $post_date = "BETWEEN '$since' AND '$now'";
837
838 if( $since === $now ) {
839 $post_date = ">= '$now'";
840 }
841 }
842
843 // If is specific trigger then try to get the attached id
844 if( in_array( $trigger, array_keys( gamipress_get_specific_activity_triggers() ) ) ) {
845
846 $specific_id = 0;
847
848 // if isset this key it means $args is a requirement object
849 if( isset( $args['achievement_post'] ) ) {
850 $specific_id = absint( $args['achievement_post'] );
851 } else if( ! empty( $args ) ) {
852 $specific_id = gamipress_specific_trigger_get_id( $trigger, $args );
853 }
854
855 // If there is a specific id, then try to find the count
856 if( $specific_id !== 0 ) {
857 $user_triggers = $wpdb->get_var( $wpdb->prepare(
858 "
859 SELECT COUNT(*)
860 FROM $wpdb->posts AS p
861 LEFT JOIN $wpdb->postmeta AS pm1
862 ON ( p.ID = pm1.post_id )
863 LEFT JOIN $wpdb->postmeta AS pm2
864 ON ( p.ID = pm2.post_id )
865 LEFT JOIN $wpdb->postmeta AS pm3
866 ON ( p.ID = pm3.post_id )
867 WHERE p.post_type = %s
868 AND p.post_author = %s
869 AND CAST( p.post_date AS DATE ) {$post_date}
870 AND (
871 ( pm1.meta_key = %s AND pm1.meta_value = %s )
872 AND ( pm2.meta_key = %s AND pm2.meta_value = %s )
873 AND ( pm3.meta_key = %s AND pm3.meta_value = %s )
874 )
875 ",
876 'gamipress-log',
877 $user_id,
878 '_gamipress_type', 'event_trigger',
879 '_gamipress_trigger_type', $trigger,
880 '_gamipress_achievement_post', $specific_id
881 ) );
882 } else {
883 return 0;
884 }
885 } else {
886 // Single trigger count
887 $user_triggers = $wpdb->get_var( $wpdb->prepare(
888 "
889 SELECT COUNT(*)
890 FROM $wpdb->posts AS p
891 LEFT JOIN $wpdb->postmeta AS pm1
892 ON ( p.ID = pm1.post_id )
893 LEFT JOIN $wpdb->postmeta AS pm2
894 ON ( p.ID = pm2.post_id )
895 WHERE p.post_type = %s
896 AND p.post_author = %s
897 AND CAST( p.post_date AS DATE ) {$post_date}
898 AND (
899 ( pm1.meta_key = %s AND pm1.meta_value = %s )
900 AND ( pm2.meta_key = %s AND pm2.meta_value = %s )
901 )
902 ",
903 'gamipress-log',
904 $user_id,
905 '_gamipress_type', 'event_trigger',
906 '_gamipress_trigger_type', $trigger
907 ) );
908 }
909
910 // If we have any triggers, return the current count for the given trigger
911 return absint( $user_triggers );
912
913 }
914
915 /**
916 * Add log extra data meta box
917 *
918 * @since 1.0.0
919 * @return void
920 */
921 function gamipress_add_log_extra_data_ui_meta_box_old() {
922 add_meta_box( 'gamipress_log_extra_data_ui', __( 'Extra Data', 'gamipress' ), 'gamipress_log_extra_data_ui_meta_box_old', 'gamipress-log', 'advanced', 'default' );
923 }
924 add_action( 'add_meta_boxes', 'gamipress_add_log_extra_data_ui_meta_box_old' );
925
926 /**
927 * Renders the HTML for meta box, refreshes whenever a new step is added
928 *
929 * @since 1.0.0
930 * @param WP_Post $post The current post object
931 * @return void
932 */
933 function gamipress_log_extra_data_ui_meta_box_old( $post = null ) {
934 ?>
935 <div id="log-extra-data-ui-old">
936 <?php gamipress_log_extra_data_ui_html_old( $post->ID ); ?>
937 </div>
938 <?php
939 }
940
941 /**
942 * Renders the HTML for meta box based on the log type given
943 *
944 * @since 1.0.0
945 * @param WP_Post $post The current post object
946 * @param string $type Type to render form
947 * @return void
948 */
949 function gamipress_log_extra_data_ui_html_old( $post_id, $type = null ) {
950 // Start with an underscore to hide fields from custom fields list
951 $prefix = '_gamipress_';
952 $fields = array();
953
954 if( $type === null ) {
955 $type = get_post_meta( $post_id, $prefix .'type', true );
956 }
957
958 if( $type === 'event_trigger' ) {
959
960 $fields = array(
961 array(
962 'name' => __( 'Trigger', 'gamipress' ),
963 'desc' => __( 'The event user has triggered.', 'gamipress' ),
964 'id' => $prefix . 'trigger_type',
965 'type' => 'advanced_select',
966 'options' => gamipress_get_activity_triggers(),
967 ),
968 array(
969 'name' => __( 'Count', 'gamipress' ),
970 'desc' => __( 'Number of times user triggered this event until this log.', 'gamipress' ),
971 'id' => $prefix . 'count',
972 'type' => 'text',
973 ),
974 );
975
976 $trigger = get_post_meta( $post_id, $prefix . 'trigger_type', true );
977
978 // If is a specific activity trigger, then add the achievement_post field
979 if( in_array( $trigger, array_keys( gamipress_get_specific_activity_triggers() ) ) ) {
980
981 $achievement_post_id = get_post_meta( $post_id, $prefix . 'achievement_post', true );
982
983 $fields[] = array(
984 'name' => __( 'Assigned Post', 'gamipress' ),
985 'desc' => __( 'Attached post to this log.', 'gamipress' ),
986 'id' => $prefix . 'achievement_post',
987 'type' => 'select',
988 'options' => array(
989 $achievement_post_id => get_post_field( 'post_title', $achievement_post_id ),
990 ),
991 );
992 }
993 } else if( $type === 'achievement_earn' || $type === 'achievement_award' ) {
994 $achievement_id = get_post_meta( $post_id, $prefix . 'achievement_id', true );
995
996 $fields = array(
997 array(
998 'name' => __( 'Achievement', 'gamipress' ),
999 'desc' => __( 'Achievement user has earned.', 'gamipress' ),
1000 'id' => $prefix . 'achievement_id',
1001 'type' => 'select',
1002 'options' => array(
1003 $achievement_id => get_post_field( 'post_title', $achievement_id ),
1004 ),
1005 ),
1006 );
1007 } else if( $type === 'points_award' || $type === 'points_earn' ) {
1008 // Grab our points types as an array
1009 $points_types_options = array(
1010 '' => 'Default'
1011 );
1012
1013 foreach( gamipress_get_points_types() as $slug => $data ) {
1014 $points_types_options[$slug] = $data['plural_name'];
1015 }
1016
1017 $fields = array(
1018 array(
1019 'name' => __( 'Points', 'gamipress' ),
1020 'desc' => __( 'Points user has earned.', 'gamipress' ),
1021 'id' => $prefix . 'points',
1022 'type' => 'text_small',
1023 ),
1024 array(
1025 'name' => __( 'Points Type', 'gamipress' ),
1026 'desc' => __( 'Points type user has earned.', 'gamipress' ),
1027 'id' => $prefix . 'points_type',
1028 'type' => 'select',
1029 'options' => $points_types_options
1030 ),
1031 array(
1032 'name' => __( 'Total Points', 'gamipress' ),
1033 'desc' => __( 'Total points user has earned until this log.', 'gamipress' ),
1034 'id' => $prefix . 'total_points',
1035 'type' => 'text_small',
1036 ),
1037 );
1038
1039 if( $type === 'points_award' ) {
1040 $admin_id = get_post_meta( $post_id, $prefix . 'admin_id', true );
1041 $admin = get_userdata( $admin_id );
1042
1043 $fields[] = array(
1044 'name' => __( 'Administrator', 'gamipress' ),
1045 'desc' => __( 'User has made the award.', 'gamipress' ),
1046 'id' => $prefix . 'admin_id',
1047 'type' => 'select',
1048 'options' => array(
1049 $admin_id => $admin->user_login,
1050 ),
1051 );
1052 }
1053 }
1054
1055 $fields = apply_filters( 'gamipress_log_extra_data_fields', $fields, $post_id, $type );
1056
1057 if( ! empty( $fields ) ) {
1058 // Create a new box to render the form
1059 $cmb2 = new CMB2( array(
1060 'id' => 'log_extra_data_ui_box',
1061 'classes' => 'gamipress-form gamipress-box-form',
1062 'hookup' => false,
1063 'show_on' => array(
1064 'key' => 'gamipress-log',
1065 'value' => $post_id
1066 ),
1067 'fields' => $fields
1068 ) );
1069
1070 $cmb2->object_id( $post_id );
1071
1072 $cmb2->show_form();
1073 } else {
1074 _e( 'No extra data registered', 'gamipress' );
1075 }
1076 }