PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 7.9.8
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v7.9.8
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 7.8.7 All 46 releases
gamipress / includes / filters.php

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

2,296 lines 79.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Filters
4 *
5 * @package GamiPress\Filters
6 * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]>
7 * @since 1.0.0
8 */
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 /**
13 * Add filters to remove stuff from our singular pages and add back in how we want it
14 *
15 * @since 1.0.0
16 */
17 function gamipress_do_single_filters() {
18 // Check we're in the right place
19 if ( gamipress_is_single_achievement() || gamipress_is_single_rank() ) {
20 // Filter out the post title
21 // add_filter( 'the_title', 'gamipress_remove_to_reformat_entries_title', 10, 2 );
22
23 // and filter out the post image
24 add_filter( 'post_thumbnail_html', 'gamipress_remove_to_reformat_entries_thumbnail', 10, 2 );
25 }
26 }
27 add_action( 'wp_enqueue_scripts', 'gamipress_do_single_filters' );
28
29 /**
30 * Filter out the post title/post image and add back (later) how we want it
31 *
32 * @since 1.0.0
33 *
34 * @param string $html The page content prior to filtering
35 * @param int $post_id The post ID.
36 *
37 * @return string The page content after being filtered
38 */
39 function gamipress_remove_to_reformat_entries_title( $html = '', $post_id = 0 ) {
40
41 global $gamipress_template_args;
42
43 // Remove, but only on the main loop!
44 if ( ( gamipress_is_single_achievement( $post_id ) || gamipress_is_single_rank( $post_id ) ) // Ensure that is an achievement or a rank
45 && empty( $GLOBALS['gamipress_reformat_title'] ) // Ensure to pass it only one time
46 && ! is_array( $gamipress_template_args ) // Prevents to pass this check on GamiPress shortcodes
47 ) {
48 // Now that we're where we want to be, tell the filters to stop removing
49 $GLOBALS['gamipress_reformat_title'] = true;
50 return '';
51 }
52
53 // Nothing to see here... move along
54 return $html;
55 }
56
57 /**
58 * Filter out the post title/post image and add back (later) how we want it
59 *
60 * @since 1.0.0
61 *
62 * @param string $html The page content prior to filtering
63 * @param int $post_id The post ID.
64 *
65 * @return string The page content after being filtered
66 */
67 function gamipress_remove_to_reformat_entries_thumbnail( $html = '', $post_id = 0 ) {
68
69 global $gamipress_template_args;
70
71 // Remove, but only on the main loop!
72 if ( ( gamipress_is_single_achievement( $post_id ) || gamipress_is_single_rank( $post_id ) ) // Ensure that is an achievement or a rank
73 && empty( $GLOBALS['gamipress_reformat_thumbnail'] ) // Ensure to pass it only one time
74 && ! is_array( $gamipress_template_args ) // Prevents to pass this check on GamiPress shortcodes
75 ) {
76 // Now that we're where we want to be, tell the filters to stop removing
77 $GLOBALS['gamipress_reformat_thumbnail'] = true;
78 return '';
79 }
80
81 // Nothing to see here... move along
82 return $html;
83 }
84
85 /**
86 * Filter achievement content to add our removed content back
87 *
88 * @since 1.0.0
89 *
90 * @param string $content The page content
91 *
92 * @return string The page content after reformat
93 */
94 function gamipress_reformat_entries( $content ) {
95
96 if ( gamipress_is_single_achievement( get_the_ID() ) ) {
97
98 // Filter content, but only is a single achievement!
99 return gamipress_apply_single_template( $content, 'single-achievement' );
100
101 } else if ( gamipress_is_single_rank( get_the_ID() ) ) {
102
103 // Filter content, but only is a single rank!
104 return gamipress_apply_single_template( $content, 'single-rank' );
105 }
106
107 return $content;
108 }
109 add_filter( 'the_content', 'gamipress_reformat_entries' );
110
111 /**
112 * Apply the given single template
113 *
114 * @since 1.3.1
115 *
116 * @param string $content
117 * @param string $single_template
118 *
119 * @return string
120 */
121 function gamipress_apply_single_template( $content, $single_template = '' ) {
122
123 global $gamipress_template_args;
124
125 // Now that we're where we want to be, tell the filters to stop removing
126 $GLOBALS['gamipress_reformat_content'] = true;
127
128 // Initialize GamiPress template args global
129 $gamipress_template_args = array();
130
131 // Get the original post content (to use as achievement description)
132 $gamipress_template_args['original_content'] = $content;
133
134 // Set the configured layout
135 $gamipress_template_args['layout'] = gamipress_get_post_meta( get_the_ID(), '_gamipress_layout' );
136
137 // If not layout defined, fallback to left layout
138 if( empty( $gamipress_template_args['layout'] ) ) {
139 $gamipress_template_args['layout'] = 'left';
140 }
141
142 // Set the configured alignment
143 $gamipress_template_args['align'] = gamipress_get_post_meta( get_the_ID(), '_gamipress_align' );
144
145 // If not alignment defined, fallback to none alignment
146 if( empty( $gamipress_template_args['align'] ) ) {
147 $gamipress_template_args['align'] = 'none';
148 }
149
150 /**
151 * Single template arguments
152 *
153 * @since 7.6.5
154 *
155 * @param array $gamipress_template_args
156 * @param string $single_template
157 */
158 $gamipress_template_args = apply_filters( 'gamipress_single_template_args', $gamipress_template_args, $single_template );
159
160 ob_start();
161
162 // Try to load single-{template}-{post_type}.php, if not exists load single-{template}.php
163 gamipress_get_template_part( $single_template, gamipress_get_post_type( get_the_ID() ) );
164
165 $new_content = ob_get_clean();
166
167 // Ok, we're done reformatting
168 $GLOBALS['gamipress_reformat_content'] = false;
169
170 return $new_content;
171
172 }
173
174 /**
175 * Helper function tests that we're in the main loop
176 *
177 * @since 1.3.1
178 *
179 * @param bool|integer $id The page id
180 *
181 * @return boolean A boolean determining if the function is in the main loop
182 */
183 function gamipress_is_single_achievement( $id = false ) {
184
185 $slugs = gamipress_get_achievement_types_slugs();
186
187 // only run our filters on the achievement singular page
188 if ( is_admin() || empty( $slugs ) || ! is_singular( $slugs ) )
189 return false;
190 // w/o id, we're only checking template context
191 if ( ! $id )
192 return true;
193
194 // Checks several variables to be sure we're in the main loop (and won't effect things like post pagination titles)
195 return ( ( $GLOBALS['post']->ID == $id ) && in_the_loop() && empty( $GLOBALS['gamipress_reformat_content'] ) );
196
197 }
198
199 /**
200 * Generate HTML markup for an points type's points awards
201 *
202 * @since 1.0.0
203 *
204 * @param array $points_awards An points type's points awards
205 * @param int $user_id The given user's ID
206 * @param array $template_args The given template args
207 *
208 * @return string The markup for our list
209 */
210 function gamipress_get_points_awards_for_points_types_list_markup( $points_awards = array(), $user_id = 0, $template_args = array() ) {
211
212 // If we don't have any points awards, or our points awards aren't an array, return nothing
213 if ( ! $points_awards || ! is_array( $points_awards ) ) {
214 return null;
215 }
216
217 $count = count( $points_awards );
218
219 // If we have no points awards, return nothing
220 if ( ! $count ) {
221 return null;
222 }
223
224 // Grab the current user's ID if none was specifed
225 if ( ! $user_id ) {
226 $user_id = get_current_user_id();
227 }
228
229 $a = wp_parse_args( $template_args, gamipress_points_types_shortcode_defaults() );
230
231 // Setup our variables
232 $output = '';
233
234 if( $a['heading'] === 'yes' ) {
235
236 $points_type = gamipress_get_points_award_points_type( $points_awards[0]->ID );
237
238 if( $points_type ) {
239 $plural_name = gamipress_get_post_meta( '_gamipress_plural_name', $points_type->ID );
240
241 if( ! $plural_name )
242 $plural_name = $points_type->post_title;
243
244 $points_awards_heading = sprintf( '%1$d %2$s %3$s', $count, $plural_name, _n( 'Award', 'Awards', $count, 'gamipress' ) ); // 2 Credits Awards
245 } else {
246 $points_awards_heading = sprintf( '%1$d %2$s', $count, _n( 'Award', 'Awards', $count, 'gamipress' ) ); // 2 Awards
247 }
248
249 /**
250 * Filters the points award heading text
251 *
252 * @since 1.0.0
253 *
254 * @param string $points_awards_heading The heading text (eg: 2 Points Awards)
255 * @param array $points_awards The points awards
256 * @param int $user_id The user's ID
257 * @param array $template_args The given template args
258 *
259 * @return string
260 */
261 $points_awards_heading = apply_filters( 'gamipress_points_awards_heading', $points_awards_heading, $points_awards, $user_id, $template_args );
262
263 $output .= '<' . $a['heading_size'] . ' class="gamipress-points-awards-heading">' . $points_awards_heading . '</' . $a['heading_size'] . '>';
264
265 }
266
267 $output .= '<ul class="gamipress-points-awards">';
268
269 // Concatenate our output
270 foreach ( $points_awards as $points_award ) {
271
272 if( $user_id === 0 ) {
273 $earned_status = 'user-has-not-earned';
274 } else {
275 // Check if user can earn this points award and add the earned class
276 $can_earn = gamipress_can_user_earn_requirement( $points_award->ID, $user_id );
277 $earned_status = $can_earn ? 'user-has-not-earned' : 'user-has-earned';
278 }
279
280 $title = $points_award->post_title;
281
282 // If points award doesn't have a title, then try to build one
283 if( empty( $title ) ) {
284 $title = gamipress_build_requirement_title( $points_award->ID );
285 }
286
287 /**
288 * Filters the points award CSS class
289 *
290 * @since 1.0.0
291 *
292 * @param string $class CSS class based on earned status ('user-has-earned' | 'user-has-not-earned')
293 * @param stdClass $points_award Requirement object
294 * @param int $user_id User's ID
295 * @param array $template_args The given template args
296 *
297 * @return string
298 */
299 $class = apply_filters( 'gamipress_points_award_class', $earned_status, $points_award, $user_id, $template_args );
300
301 /**
302 * Filters the points award HTML title to display
303 *
304 * @since 1.0.0
305 *
306 * @param string $title Title to display
307 * @param stdClass $points_award Requirement object
308 * @param int $user_id User's ID
309 * @param array $template_args The given template args
310 *
311 * @return string
312 */
313 $title = apply_filters( 'gamipress_points_award_title_display', $title, $points_award, $user_id, $template_args );
314
315 $output .= '<li class="'. esc_attr( $class ) .'">'. $title . '</li>';
316 }
317
318 $output .= '</ul><!-- .gamipress-points-awards -->';
319
320 // Return our output
321 return $output;
322
323 }
324
325 /**
326 * Generate HTML markup for an points type's points deducts
327 *
328 * @since 1.3.7
329 *
330 * @param array $points_deducts An points type's points deducts
331 * @param int $user_id The given user's ID
332 * @param array $template_args The given template args
333 *
334 * @return string The markup for our list
335 */
336 function gamipress_get_points_deducts_for_points_types_list_markup( $points_deducts = array(), $user_id = 0, $template_args = array() ) {
337
338 // If we don't have any points deducts, or our points deducts aren't an array, return nothing
339 if ( ! $points_deducts || ! is_array( $points_deducts ) ) {
340 return null;
341 }
342
343 $count = count( $points_deducts );
344
345 // If we have no points deducts, return nothing
346 if ( ! $count ) {
347 return null;
348 }
349
350 // Grab the current user's ID if none was specifed
351 if ( ! $user_id ) {
352 $user_id = get_current_user_id();
353 }
354
355 $a = wp_parse_args( $template_args, gamipress_points_types_shortcode_defaults() );
356
357 // Setup our variables
358 $output = '';
359
360 if( $a['heading'] === 'yes' ) {
361
362 $points_type = gamipress_get_points_deduct_points_type( $points_deducts[0]->ID );
363
364 if( $points_type ) {
365 $plural_name = gamipress_get_post_meta( '_gamipress_plural_name', $points_type->ID );
366
367 if( ! $plural_name ) {
368 $plural_name = $points_type->post_title;
369 }
370
371 $points_deducts_heading = sprintf( '%1$d %2$s %3$s', $count, $plural_name, _n( 'Deduction', 'Deductions', $count, 'gamipress' ) ); // 2 Credits Deducts
372 } else {
373 $points_deducts_heading = sprintf( '%1$d %2$s', $count, _n( 'Deduction', 'Deductions', $count, 'gamipress' ) ); // 2 Deductions
374 }
375
376 /**
377 * Filters the points deduct heading text
378 *
379 * @since 1.3.7
380 *
381 * @param string $points_deducts_heading The heading text (eg: 2 Points Deductions)
382 * @param array $points_deducts The points deducts
383 * @param int $user_id The user's ID
384 * @param array $template_args The given template args
385 *
386 * @return string
387 */
388 $points_deducts_heading = apply_filters( 'gamipress_points_deducts_heading', $points_deducts_heading, $points_deducts, $user_id, $template_args );
389
390 $output .= '<' . $a['heading_size'] . ' class="gamipress-points-deducts-heading">' . $points_deducts_heading . '</' . $a['heading_size'] . '>';
391
392 }
393
394 $output .= '<ul class="gamipress-points-deducts">';
395
396 // Concatenate our output
397 foreach ( $points_deducts as $points_deduct ) {
398
399 if( $user_id === 0 ) {
400 $earned_status = 'user-has-not-earned';
401 } else {
402 // Check if user can earn this points deduct and add the earned class
403 $can_earn = gamipress_can_user_earn_requirement( $points_deduct->ID, $user_id );
404 $earned_status = $can_earn ? 'user-has-not-earned' : 'user-has-earned';
405 }
406
407 $title = $points_deduct->post_title;
408
409 // If points deduct doesn't have a title, then try to build one
410 if( empty( $title ) ) {
411 $title = gamipress_build_requirement_title( $points_deduct->ID );
412 }
413
414 /**
415 * Filters the points deduct CSS class
416 *
417 * @since 1.3.7
418 *
419 * @param string $class CSS class based on earned status ('user-has-earned' | 'user-has-not-earned')
420 * @param stdClass $points_deduct Requirement object
421 * @param int $user_id User's ID
422 * @param array $template_args The given template args
423 *
424 * @return string
425 */
426 $class = apply_filters( 'gamipress_points_deduct_class', $earned_status, $points_deduct, $user_id, $template_args );
427
428 /**
429 * Filters the points deduct HTML title to display
430 *
431 * @since 1.3.7
432 *
433 * @param string $title Title to display
434 * @param stdClass $points_deduct Requirement object
435 * @param int $user_id User's ID
436 * @param array $template_args The given template args
437 *
438 * @return string
439 */
440 $title = apply_filters( 'gamipress_points_deduct_title_display', $title, $points_deduct, $user_id, $template_args );
441
442 $output .= '<li class="'. esc_attr( $class ) .'">'. $title . '</li>';
443 }
444
445 $output .= '</ul><!-- .gamipress-points-deducts -->';
446
447 // Return our output
448 return $output;
449
450 }
451
452 /**
453 * Gets achievement's required steps and returns HTML markup for these steps
454 *
455 * @since 1.0.0
456 * @param int $achievement_id The given achievement's post ID
457 * @param int $user_id A given user's ID
458 *
459 * @return string The markup for our list
460 */
461 function gamipress_get_required_achievements_for_achievement_list( $achievement_id = 0, $user_id = 0 ) {
462
463 // Grab the current post ID if no achievement_id was specified
464 if ( ! $achievement_id ) {
465 global $post;
466 $achievement_id = $post->ID;
467 }
468
469 // Grab the current user's ID if none was specifed
470 if ( ! $user_id )
471 $user_id = wp_get_current_user()->ID;
472
473 // Grab our achievement's required steps
474 $steps = gamipress_get_achievement_steps( $achievement_id );
475
476 // Return our markup output
477 return gamipress_get_required_achievements_for_achievement_list_markup( $steps, $user_id );
478
479 }
480
481 /**
482 * Generate HTML markup for an achievement's required steps
483 *
484 * This will generate an unorderd list (<ul>) if steps are non-sequential
485 * and an ordered list (<ol>) if steps require sequentiality.
486 *
487 * @since 1.0.0
488 *
489 * @param array $steps An achievement's required steps
490 * @param int $achievement_id The given achievement's ID
491 * @param int $user_id The given user's ID
492 * @param array $template_args The given template args
493 *
494 * @return string The markup for our list
495 */
496 function gamipress_get_required_achievements_for_achievement_list_markup( $steps = array(), $achievement_id = 0, $user_id = 0, $template_args = array() ) {
497
498 // If we don't have any steps, or our steps aren't an array, return nothing
499 if ( ! $steps || ! is_array( $steps ) )
500 return null;
501
502 // Grab the current post ID if no achievement_id was specified
503 if ( ! $achievement_id ) {
504 global $post;
505 $achievement_id = $post->ID;
506 }
507
508 $count = count( $steps );
509
510 // If we have no steps, return nothing
511 if ( ! $count )
512 return null;
513
514 // Grab the current user's ID if none was specified
515 if ( ! $user_id )
516 $user_id = get_current_user_id();
517
518 $a = wp_parse_args( $template_args, gamipress_achievement_shortcode_defaults() );
519
520 // Setup our variables
521 $output = '';
522 $container = gamipress_is_achievement_sequential() ? 'ol' : 'ul';
523
524 if( $a['heading'] === 'yes' ) {
525
526 $steps_heading = sprintf( '%1$d %2$s', $count, _n( 'Step', 'Steps', $count, 'gamipress' ) );
527
528 /**
529 * Filters the steps heading text
530 *
531 * @since 1.0.0
532 *
533 * @param string $steps_heading The heading text (eg: 2 Steps)
534 * @param array $steps The achievement steps
535 * @param int $user_id The user's ID
536 * @param array $template_args The given template args
537 *
538 * @return string
539 */
540 $steps_heading = apply_filters( 'gamipress_steps_heading', $steps_heading, $steps, $user_id, $template_args );
541
542 $output .= '<' . $a['heading_size'] . ' class="gamipress-achievement-steps-heading">' . $steps_heading . '</' . $a['heading_size'] . '>';
543
544 }
545
546 $output .= '<' . $container .' class="gamipress-achievement-steps gamipress-required-achievements">';
547
548 // Concatenate our output
549 foreach ( $steps as $step ) {
550
551 $can_earn = true;
552
553 if( $user_id === 0 ) {
554 $earned_status = 'user-has-not-earned';
555 } else {
556 // Check if user can earn this step and add the earned class
557 $can_earn = gamipress_can_user_earn_requirement( $step->ID, $user_id );
558 $earned_status = $can_earn ? 'user-has-not-earned' : 'user-has-earned';
559 }
560
561 // Force earned class if user can't earn this step but has earned it in the past
562 if( ! $can_earn && gamipress_has_user_earned_achievement( $step->ID, $user_id ) ) {
563 $earned_status = 'user-has-earned';
564 }
565
566 $title = $step->post_title;
567
568 // If step doesn't have a title, then try to build one
569 if( empty( $title ) ) {
570 $title = gamipress_build_requirement_title( $step->ID );
571 }
572
573 /**
574 * Filters the step CSS class
575 *
576 * @since 1.0.0
577 *
578 * @param string $class CSS class based on earned status ('user-has-earned' | 'user-has-not-earned')
579 * @param stdClass $step Requirement object
580 * @param int $user_id User's ID
581 * @param array $template_args The given template args
582 *
583 * @return string
584 */
585 $class = apply_filters( 'gamipress_step_class', $earned_status, $step, $user_id, $template_args );
586
587 /**
588 * Filters the step HTML title to display
589 *
590 * @since 1.0.0
591 *
592 * @param string $title Title to display
593 * @param stdClass $step Requirement object
594 * @param int $user_id User's ID
595 * @param array $template_args The given template args
596 *
597 * @return string
598 */
599 $title = apply_filters( 'gamipress_step_title_display', $title, $step, $user_id, $template_args );
600
601 $output .= '<li class="'. esc_attr( $class ) .'">'. $title . '</li>';
602 }
603
604 $output .= '</'. $container .'><!-- .gamipress-required-achievements -->';
605
606 // Return our output
607 return $output;
608
609 }
610
611 /**
612 * Filter requirements titles to link to assigned post
613 *
614 * @since 1.0.0
615 * @updated 1.5.0 Improved checks to get correct post permalink and centralize all requirements link functions to this
616 * @updated 1.8.7 Added permalink to the 'earn-rank' trigger
617 *
618 * @param string $title The requirement title
619 * @param object $requirement The requirement object
620 *
621 * @return string Our potentially updated title
622 */
623 function gamipress_format_requirement_title_with_post_link( $title = '', $requirement = null ) {
624
625 // Grab our step requirements
626 $requirement = gamipress_get_requirement_object( $requirement->ID );
627
628 $url = $requirement['url'];
629
630 if( empty( $url ) ) {
631
632 $trigger = $requirement['trigger_type'];
633 $post_id = 0;
634 $site_id = get_current_blog_id();
635
636 if ( in_array( $trigger, array( 'earn-rank', 'revoke-rank' ) ) && ! empty( $requirement['rank_required'] ) ) {
637
638 // Set the post ID for the rank required to reach
639 $post_id = $requirement['rank_required'];
640 $site_id = ( gamipress_is_network_wide_active() ? get_main_site_id() : get_current_blog_id() );
641
642 } else if ( ! empty( $requirement['achievement_post'] ) ) {
643
644 // Set the post ID for the post assigned
645 $post_id = $requirement['achievement_post'];
646 $site_id = $requirement['achievement_post_site_id'];
647
648 }
649
650 // Set the URL to link to a specific post
651 $url = gamipress_get_specific_activity_trigger_permalink( $post_id, $trigger, $site_id );
652
653 }
654
655 // If we have a URL, update the title to link to it
656 if ( ! empty( $url ) ) {
657 $title = '<a href="' . esc_url( $url ) . '">' . $title . '</a>';
658 }
659
660 return $title;
661
662 }
663 add_filter( 'gamipress_step_title_display', 'gamipress_format_requirement_title_with_post_link', 10, 2 );
664 add_filter( 'gamipress_points_award_title_display', 'gamipress_format_requirement_title_with_post_link', 10, 2 );
665 add_filter( 'gamipress_points_deduct_title_display', 'gamipress_format_requirement_title_with_post_link', 10, 2 );
666 add_filter( 'gamipress_rank_requirement_title_display', 'gamipress_format_requirement_title_with_post_link', 10, 2 );
667
668 /**
669 * Generate markup for an achievement's times earned output
670 *
671 * @since 1.5.9
672 *
673 * @param int $achievement_id The given achievement's ID
674 * @param array $template_args Achievement template args
675 *
676 * @return string The HTML markup for our times earned output
677 */
678 function gamipress_achievement_times_earned_markup( $achievement_id = 0, $template_args = array() ) {
679
680 // Grab the current post ID if no achievement_id was specified
681 if ( ! $achievement_id ) {
682 global $post;
683 $achievement_id = $post->ID;
684 }
685
686 if( ! isset( $template_args['user_id'] ) ) {
687 $template_args['user_id'] = get_current_user_id();
688 }
689
690 $user_id = absint( $template_args['user_id'] );
691
692 // Guest not supported yet (basically because they has not earned this achievement)
693 if( $user_id === 0 ) {
694 return '';
695 }
696
697 $maximum_earnings = absint( gamipress_get_post_meta( $achievement_id, '_gamipress_maximum_earnings' ) );
698
699 // Return if maximum earnings configured is 1
700 if( $maximum_earnings === 1 ) {
701 return '';
702 }
703
704 $earned_times = gamipress_get_earnings_count( array( 'user_id' => $user_id, 'post_id' => $achievement_id ) );
705
706 /**
707 * Filter to override the minimum times required to show the achievement times earned text, by default 1
708 *
709 * @since 2.0.0
710 *
711 * @param int $minimum_earned_times Minimum times required to show the achievement times earned text, by default 1
712 * @param int $achievement_id The given achievement's ID
713 * @param int $user_id The user's ID
714 * @param int $earned_times The user's times earned this achievement
715 * @param int $maximum_earnings The achievement's maximum times to be earned
716 * @param array $template_args Achievement template args
717 */
718 $minimum_earned_times = apply_filters( 'gamipress_achievement_times_earned_minimum_times_to_show', 1, $achievement_id, $user_id, $earned_times, $maximum_earnings, $template_args );
719
720 // Return if user hasn't earned this achievement the minimum times required to show
721 if( $earned_times <= $minimum_earned_times ) {
722 return '';
723 }
724
725 // Setup the times earned text based on if achievements has unlimited times to be earned
726 if( $maximum_earnings === 0 ) {
727 $times_earned_pattern = __( '%d times earned', 'gamipress' );
728
729 $times_earned_text = sprintf( $times_earned_pattern, $earned_times );
730 } else {
731 $times_earned_pattern = __( '%d of %d times earned', 'gamipress' );
732
733 $times_earned_text = sprintf( $times_earned_pattern, $earned_times, $maximum_earnings );
734 }
735
736 /**
737 * Filter to override the achievement times earned text
738 *
739 * @since 1.5.9
740 *
741 * @param string $times_earned_text Times earned text, by default "X times earned"
742 * @param int $achievement_id The given achievement's ID
743 * @param int $user_id The user's ID
744 * @param int $earned_times The user's times earned this achievement
745 * @param int $maximum_earnings The achievement's maximum times to be earned
746 * @param array $template_args Achievement template args
747 */
748 $times_earned_text = apply_filters( 'gamipress_achievement_times_earned_text', $times_earned_text, $achievement_id, $user_id, $earned_times, $maximum_earnings, $template_args );
749
750 // The time earned markup
751 $output = '<div class="gamipress-achievement-times-earned">' . $times_earned_text . '</div>';
752
753 /**
754 * Filter to override the achievement times earned markup
755 *
756 * @since 1.5.9
757 *
758 * @param string $output Times earned markup
759 * @param int $achievement_id The given achievement's ID
760 * @param int $user_id The user's ID
761 * @param int $earned_times The user's times earned this achievement
762 * @param int $maximum_earnings The achievement's maximum times to be earned
763 * @param array $template_args Achievement template args
764 */
765 return apply_filters( 'gamipress_achievement_times_earned_markup', $output, $achievement_id, $user_id, $earned_times, $maximum_earnings, $template_args );
766
767 }
768
769 /**
770 * Generate markup for an achievement's times earned by all users output
771 *
772 * @since 2.0.0
773 *
774 * @param int $achievement_id The given achievement's ID
775 * @param array $template_args Achievement template args
776 *
777 * @return string The HTML markup for our times earned output
778 */
779 function gamipress_achievement_global_times_earned_markup( $achievement_id = 0, $template_args = array() ) {
780
781 // Grab the current post ID if no achievement_id was specified
782 if ( ! $achievement_id ) {
783 global $post;
784 $achievement_id = $post->ID;
785 }
786
787 $maximum_earnings = absint( gamipress_get_post_meta( $achievement_id, '_gamipress_global_maximum_earnings' ) );
788
789 $earned_times = gamipress_get_earnings_count( array( 'post_id' => $achievement_id ) );
790
791 /**
792 * Filter to override the minimum times required to show the achievement times earned by all users text, by default 1
793 *
794 * @since 2.0.0
795 *
796 * @param int $minimum_earned_times Minimum times required to show the achievement times earned text, by default 1
797 * @param int $achievement_id The given achievement's ID
798 * @param int $earned_times The user's times earned this achievement
799 * @param int $maximum_earnings The achievement's global maximum times to be earned
800 * @param array $template_args Achievement template args
801 */
802 $minimum_earned_times = apply_filters( 'gamipress_achievement_global_times_earned_minimum_times_to_show', 1, $achievement_id, $earned_times, $maximum_earnings, $template_args );
803
804 // Return if user hasn't earned this achievement the minimum times required to show
805 if( $earned_times <= $minimum_earned_times ) {
806 return '';
807 }
808
809 $post_type = gamipress_get_post_type( $achievement_id );
810 $achievement_type_singular = gamipress_get_achievement_type_singular( $post_type );
811
812 // Setup the times earned text based on if achievements has unlimited times to be earned
813 if( $maximum_earnings === 0 ) {
814 $times_earned_pattern = __( '%d users have earned this %s', 'gamipress' );
815
816 $times_earned_text = sprintf( $times_earned_pattern, $earned_times, $achievement_type_singular );
817 } else {
818 $times_earned_pattern = __( '%d of %d users have earned this %s', 'gamipress' );
819
820 $times_earned_text = sprintf( $times_earned_pattern, $earned_times, $maximum_earnings, $achievement_type_singular );
821 }
822
823 /**
824 * Filter to override the achievement times earned by all users text
825 *
826 * @since 1.5.9
827 *
828 * @param string $times_earned_text Times earned text, by default "X users have earned this Y"
829 * @param int $achievement_id The given achievement's ID
830 * @param int $earned_times The user's times earned this achievement
831 * @param int $maximum_earnings The achievement's maximum times to be earned
832 * @param array $template_args Achievement template args
833 */
834 $times_earned_text = apply_filters( 'gamipress_achievement_global_times_earned_text', $times_earned_text, $achievement_id, $earned_times, $maximum_earnings, $template_args );
835
836 // The time earned markup
837 $output = '<div class="gamipress-achievement-global-times-earned">' . $times_earned_text . '</div>';
838
839 /**
840 * Filter to override the achievement times earned by all users markup
841 *
842 * @since 1.5.9
843 *
844 * @param string $output Times earned markup
845 * @param int $achievement_id The given achievement's ID
846 * @param int $earned_times The user's times earned this achievement
847 * @param int $maximum_earnings The achievement's maximum times to be earned
848 * @param array $template_args Achievement template args
849 */
850 return apply_filters( 'gamipress_achievement_global_times_earned_markup', $output, $achievement_id, $earned_times, $maximum_earnings, $template_args );
851
852 }
853
854 /**
855 * Generate markup for an achievement's points output
856 *
857 * @since 1.0.0
858 * @updated 1.5.9 Added $template_args parameter
859 *
860 * @param int $achievement_id The given achievement's ID
861 * @param array $template_args Achievement template args
862 *
863 * @return string The HTML markup for our points
864 */
865 function gamipress_achievement_points_markup( $achievement_id = 0, $template_args = array() ) {
866
867 // Grab the current post ID if no achievement_id was specified
868 if ( ! $achievement_id ) {
869 global $post;
870 $achievement_id = $post->ID;
871 }
872
873 $points = absint( gamipress_get_post_meta( $achievement_id, '_gamipress_points' ) );
874
875 // Return if no points configured
876 if( $points === 0 ) {
877 return '';
878 }
879
880 $points_type = gamipress_get_post_meta( $achievement_id, '_gamipress_points_type' );
881
882 // Set the default value for the points awarded thumbnail
883 if( ! isset( $template_args['points_awarded_thumbnail'] ) ) {
884 $template_args['points_awarded_thumbnail'] = 'yes';
885 }
886
887 $output = '<div class="gamipress-achievement-points gamipress-achievement-points-type-' . $points_type . '">'
888 . ( $template_args['points_awarded_thumbnail'] === 'yes' ? gamipress_get_points_type_thumbnail( $points_type ) . ' ' : '' )
889 . gamipress_format_points( $points, $points_type )
890 . '</div>';
891
892 // Return the points awarded output
893 return apply_filters( 'gamipress_achievement_points_markup', $output, $achievement_id, $points, $points_type, $template_args );
894
895 }
896
897 /**
898 * Generate markup for an achievement's unlock with points output
899 *
900 * @since 1.3.7
901 *
902 * @param int $achievement_id The given achievement's ID
903 * @param array $template_args Achievement template args
904 *
905 * @return string The HTML markup for our points
906 */
907 function gamipress_achievement_unlock_with_points_markup( $achievement_id = 0, $template_args = array() ) {
908
909 // Grab the current post ID if no achievement_id was specified
910 if ( ! $achievement_id ) {
911 $achievement_id = get_the_ID();
912 }
913
914 $user_id = get_current_user_id();
915
916 // Guest not supported yet (basically because they have no points)
917 if( $user_id === 0 ) {
918 return '';
919 }
920
921 if( ! isset( $template_args['user_id'] ) ) {
922 $template_args['user_id'] = get_current_user_id();
923 }
924
925 // Return if user is displaying achievements of another user
926 if( $user_id !== absint( $template_args['user_id'] ) ) {
927 return '';
928 }
929
930 // Return if this option not was enabled
931 if( ! (bool) gamipress_get_post_meta( $achievement_id, '_gamipress_unlock_with_points' ) ) {
932 return '';
933 }
934
935 $points = absint( gamipress_get_post_meta( $achievement_id, '_gamipress_points_to_unlock' ) );
936
937 // Return if no points configured
938 if( $points === 0 ) {
939 return '';
940 }
941
942 $earned = gamipress_achievement_user_exceeded_max_earnings( $user_id, $achievement_id );
943
944 // Return if user has completely earned this achievement
945 if( $earned ) {
946 return '';
947 }
948
949 // Setup vars
950 $points_type = gamipress_get_post_meta( $achievement_id, '_gamipress_points_type_to_unlock' );
951
952 // Template vars
953 $achievement_title = gamipress_get_post_field( 'post_title', $achievement_id );
954 $points_formatted = gamipress_format_points( $points, $points_type );
955
956 /**
957 * Filters if achievement unlock with points requires confirmation, by default true
958 *
959 * @since 1.7.8.1
960 *
961 * @param bool $confirmation If the given achievement's ID requires confirmation on unlock using points
962 * @param int $achievement_id The given achievement's ID
963 * @param int $user_id The user's ID
964 * @param int $points Points amount to unlock
965 * @param string $points_type Points type of points amount to unlock
966 * @param array $template_args Achievement template args
967 *
968 * @return bool Whatever if achievement requires confirmation or not
969 */
970 $confirmation = apply_filters( 'gamipress_achievement_unlock_with_points_confirmation', true, $achievement_id, $user_id, $points, $points_type, $template_args );
971
972 $button_text = sprintf( __( 'Unlock using %s', 'gamipress' ), $points_formatted );
973
974 /**
975 * Filters the achievement unlock with points button text
976 *
977 * @since 1.8.6
978 *
979 * @param string $button_text The button text, by default "Unlock using 100 points"
980 * @param int $achievement_id The given achievement's ID
981 * @param int $user_id The user's ID
982 * @param int $points Points amount to unlock
983 * @param string $points_type Points type of points amount to unlock
984 * @param array $template_args Achievement template args
985 *
986 * @return string Whatever if achievement requires confirmation or not
987 */
988 $button_text = apply_filters( 'gamipress_achievement_unlock_with_points_button_text', $button_text, $achievement_id, $user_id, $points, $points_type, $template_args );
989
990 ob_start(); ?>
991 <div class="gamipress-achievement-unlock-with-points" data-id="<?php echo $achievement_id; ?>">
992 <button type="button" class="gamipress-achievement-unlock-with-points-button"><?php echo $button_text; ?></button>
993 <?php if( $confirmation ) : ?>
994 <div class="gamipress-achievement-unlock-with-points-confirmation" style="display: none;">
995 <p><?php echo sprintf( __( 'Do you want to unlock %s using %s?', 'gamipress' ), $achievement_title, $points_formatted ); ?></p>
996 <button type="button" class="gamipress-achievement-unlock-with-points-confirm-button"><?php echo __( 'Yes', 'gamipress' ); ?></button>
997 <button type="button" class="gamipress-achievement-unlock-with-points-cancel-button"><?php echo __( 'No', 'gamipress' ); ?></button>
998 </div>
999 <?php endif; ?>
1000 <div class="gamipress-spinner" style="display: none;"></div>
1001 </div>
1002 <?php $output = ob_get_clean();
1003
1004 /**
1005 * Filters the achievement unlock with points markup
1006 *
1007 * @since 1.3.7
1008 *
1009 * @param string $output The HTML markup for our points
1010 * @param int $achievement_id The given achievement's ID
1011 * @param int $user_id The user's ID
1012 * @param int $points Points amount to unlock
1013 * @param string $points_type Points type of points amount to unlock
1014 * @param array $template_args Achievement template args
1015 *
1016 * @return string The HTML markup for our points
1017 */
1018 $output = apply_filters( 'gamipress_achievement_unlock_with_points_markup', $output, $achievement_id, $user_id, $points, $points_type, $template_args );
1019
1020 // Return the unlock with points output
1021 return $output;
1022
1023 }
1024
1025 /**
1026 * Generate markup for an rank's unlock with points output
1027 *
1028 * @since 1.3.7
1029 *
1030 * @param int $rank_id The given rank's ID
1031 * @param array $template_args Rank template args
1032 *
1033 * @return string The HTML markup for our points
1034 */
1035 function gamipress_rank_unlock_with_points_markup( $rank_id = 0, $template_args = array() ) {
1036
1037 // Grab the current post ID if no rank_id was specified
1038 if ( ! $rank_id ) {
1039 $rank_id = get_the_ID();
1040 }
1041
1042 $rank_types = gamipress_get_rank_types();
1043 $rank_type = gamipress_get_post_type( $rank_id );
1044
1045 if( ! isset( $rank_types[$rank_type] ) ) {
1046 return '';
1047 }
1048
1049 $user_id = get_current_user_id();
1050
1051 // Guest not supported yet (basically because they have no points)
1052 if( $user_id === 0 ) {
1053 return '';
1054 }
1055
1056 if( ! isset( $template_args['user_id'] ) ) {
1057 $template_args['user_id'] = get_current_user_id();
1058 }
1059
1060 // Return if user is displaying ranks of another user
1061 if( $user_id !== absint( $template_args['user_id'] ) ) {
1062 return '';
1063 }
1064
1065 // Return if this option not was enabled
1066 if( ! (bool) gamipress_get_post_meta( $rank_id, '_gamipress_unlock_with_points' ) ) {
1067 return '';
1068 }
1069
1070 $points = absint( gamipress_get_post_meta( $rank_id, '_gamipress_points_to_unlock' ) );
1071
1072 // Return if no points configured
1073 if( $points === 0 ) {
1074 return '';
1075 }
1076
1077 // Bail if not is the next rank to unlock
1078 if( gamipress_get_next_user_rank_id( $user_id, $rank_type ) !== $rank_id ) {
1079 return '';
1080 }
1081
1082 $user_rank = gamipress_get_user_rank( $user_id, $rank_type );
1083
1084 // Return if user is in a higher rank
1085 if( gamipress_get_rank_priority( $rank_id ) <= gamipress_get_rank_priority( $user_rank ) ) {
1086 return '';
1087 }
1088
1089 // Setup vars
1090 $points_type = gamipress_get_post_meta( $rank_id, '_gamipress_points_type_to_unlock' );
1091
1092 // Template vars
1093 $rank_title = gamipress_get_post_field( 'post_title', $rank_id );
1094 $points_formatted = gamipress_format_points( $points, $points_type );
1095
1096 /**
1097 * Filters if rank unlock with points requires confirmation, by default true
1098 *
1099 * @since 1.7.8.1
1100 *
1101 * @param bool $confirmation If the given rank's ID requires confirmation on unlock using points
1102 * @param int $rank_id The given rank's ID
1103 * @param int $user_id The user's ID
1104 * @param int $points Points amount to unlock
1105 * @param string $points_type Points type of points amount to unlock
1106 * @param array $template_args Achievement template args
1107 *
1108 * @return bool Whatever if achievement requires confirmation or not
1109 */
1110 $confirmation = apply_filters( 'gamipress_rank_unlock_with_points_confirmation', true, $rank_id, $user_id, $points, $points_type, $template_args );
1111
1112 $button_text = sprintf( __( 'Unlock using %s', 'gamipress' ), $points_formatted );
1113
1114 /**
1115 * Filters the rank unlock with points button text
1116 *
1117 * @since 1.8.6
1118 *
1119 * @param string $button_text The button text, by default "Unlock using 100 points"
1120 * @param int $rank_id The given rank's ID
1121 * @param int $user_id The user's ID
1122 * @param int $points Points amount to unlock
1123 * @param string $points_type Points type of points amount to unlock
1124 * @param array $template_args Achievement template args
1125 *
1126 * @return string Whatever if achievement requires confirmation or not
1127 */
1128 $button_text = apply_filters( 'gamipress_rank_unlock_with_points_button_text', $button_text, $rank_id, $user_id, $points, $points_type, $template_args );
1129
1130 ob_start(); ?>
1131 <div class="gamipress-rank-unlock-with-points" data-id="<?php echo $rank_id; ?>">
1132 <button type="button" class="gamipress-rank-unlock-with-points-button"><?php echo $button_text; ?></button>
1133 <?php if( $confirmation ) : ?>
1134 <div class="gamipress-rank-unlock-with-points-confirmation" style="display: none;">
1135 <p><?php echo sprintf( __( 'Do you want to unlock %s using %s?', 'gamipress' ), $rank_title, $points_formatted ); ?></p>
1136 <button type="button" class="gamipress-rank-unlock-with-points-confirm-button"><?php echo __( 'Yes', 'gamipress' ); ?></button>
1137 <button type="button" class="gamipress-rank-unlock-with-points-cancel-button"><?php echo __( 'No', 'gamipress' ); ?></button>
1138 </div>
1139 <?php endif; ?>
1140 <div class="gamipress-spinner" style="display: none;"></div>
1141 </div>
1142 <?php $output = ob_get_clean();
1143
1144 /**
1145 * Filters the achievement unlock with points markup
1146 *
1147 * @since 1.3.7
1148 *
1149 * @param string $output The HTML markup for our points
1150 * @param int $rank_id The given rank's ID
1151 * @param int $user_id The user's ID
1152 * @param int $points Points amount to unlock
1153 * @param string $points_type Points type of points amount to unlock
1154 * @param array $template_args Achievement template args
1155 *
1156 * @return string The HTML markup for our points
1157 */
1158 $output = apply_filters( 'gamipress_rank_unlock_with_points_markup', $output, $rank_id, $user_id, $points, $points_type, $template_args );
1159
1160 // Return our markup
1161 return $output;
1162
1163 }
1164
1165 /**
1166 * Checks if should render open graph meta tags
1167 *
1168 * @since 1.8.6
1169 */
1170 function gamipress_maybe_render_open_graph_meta_tags() {
1171
1172 $allowed_types = array_merge( gamipress_get_achievement_types_slugs(), gamipress_get_rank_types_slugs() );
1173
1174 // Bail if not is a singular achievement type or rank type
1175 if( ! is_singular( $allowed_types ) ) {
1176 return;
1177 }
1178
1179 $enable_open_graph = (bool) gamipress_get_option( 'enable_open_graph_tags', false );
1180
1181 // Bail if open graph meta tags option is not enabled
1182 if( ! $enable_open_graph ) {
1183 return;
1184 }
1185
1186 gamipress_render_open_graph_meta_tags( get_the_ID() );
1187
1188 }
1189 add_action( 'wp_head', 'gamipress_maybe_render_open_graph_meta_tags' );
1190
1191
1192 /**
1193 * Render open graph meta tags for the received post ID
1194 *
1195 * @since 1.8.6
1196 *
1197 * @param int $post_id
1198 */
1199 function gamipress_render_open_graph_meta_tags( $post_id ) {
1200
1201 $post = get_post( $post_id );
1202
1203 // Get the post information
1204 $url = get_permalink( $post );
1205 $title = $post->post_title;
1206 $description = $post->post_content;
1207 $image = get_the_post_thumbnail_url( $post );
1208
1209 // Process the description
1210 $description = apply_filters( 'the_content', $description );
1211 $description = trim( strip_tags( wp_kses_no_null( $description ) ) );
1212 ?>
1213 <meta property="og:url" content="<?php echo esc_url( $url ); ?>"/>
1214 <meta property="og:title" content="<?php echo esc_attr( $title ); ?>"/>
1215 <meta property="og:description" content="<?php echo esc_attr( $description ); ?>"/>
1216 <meta property="og:image" content="<?php echo esc_url( $image ); ?>"/>
1217 <?php
1218 }
1219
1220 /**
1221 * Generate markup for share an achievement's output
1222 *
1223 * @since 1.8.6
1224 *
1225 * @param int $achievement_id The given achievement's ID
1226 * @param array $template_args Achievement template args
1227 *
1228 * @return string The HTML markup for our points
1229 */
1230 function gamipress_achievement_share_markup( $achievement_id = 0, $template_args = array() ) {
1231
1232 // Grab the current post ID if no achievement_id was specified
1233 if ( ! $achievement_id ) {
1234 $achievement_id = get_the_ID();
1235 }
1236
1237 $user_id = get_current_user_id();
1238
1239 // Guest not supported yet (basically because they has not earned this)
1240 if( $user_id === 0 ) {
1241 return '';
1242 }
1243
1244 if( ! isset( $template_args['user_id'] ) ) {
1245 $template_args['user_id'] = get_current_user_id();
1246 }
1247
1248 // Return if user is displaying achievements of another user
1249 if( $user_id !== absint( $template_args['user_id'] ) ) {
1250 return '';
1251 }
1252
1253 // Bail if share is not enable
1254 if( ! (bool) gamipress_get_option( 'enable_share', false ) ) {
1255 return '';
1256 }
1257
1258 $social_networks = gamipress_get_option( 'social_networks', false );
1259
1260 // Bail if not social networks enabled
1261 if( empty( $social_networks ) ) {
1262 return '';
1263 }
1264
1265 // Check if user has earned this achievement
1266 $earned = gamipress_has_user_earned_achievement( $achievement_id, $user_id );
1267
1268 if( ! $earned ) {
1269 return '';
1270 }
1271
1272 $button_style = gamipress_get_option( 'social_button_style', 'square' );
1273
1274 $titles = array(
1275 'facebook' => __( 'Share on Facebook', 'gamipress' ),
1276 'twitter' => __( 'Share on Twitter', 'gamipress' ),
1277 'linkedin' => __( 'Share on LinkedIn', 'gamipress' ),
1278 'pinterest' => __( 'Share on Pinterest', 'gamipress' ),
1279 );
1280
1281 $names = array(
1282 'facebook' => __( 'Facebook', 'gamipress' ),
1283 'twitter' => __( 'Twitter', 'gamipress' ),
1284 'linkedin' => __( 'LinkedIn', 'gamipress' ),
1285 'pinterest' => __( 'Pinterest', 'gamipress' ),
1286 );
1287
1288 // Setup vars
1289 $url = get_the_permalink( $achievement_id );
1290 $title = get_the_title( $achievement_id );
1291 $image = get_the_post_thumbnail_url( $achievement_id );
1292
1293 // Get the achievement twitter text
1294 $twitter_text = gamipress_get_option( 'twitter_achievement_text', __( 'I earned the {achievement_type} {achievement_title} on {site_title}', 'gamipress' ) );
1295
1296 // Parse the text
1297 $twitter_text = gamipress_parse_tags( 'achievement_earned', $achievement_id, $user_id, $twitter_text );
1298
1299 ob_start(); ?>
1300
1301 <p class="gamipress-share-buttons-label"><?php _e( 'Share:', 'gamipress' ); ?></p>
1302
1303 <div class="gamipress-share-buttons"
1304 data-url="<?php echo esc_attr( $url ); ?>"
1305 data-title="<?php echo esc_attr( $title ); ?>"
1306 data-image="<?php echo esc_attr( $image ); ?>"
1307 data-twitter-text="<?php echo esc_attr( $twitter_text ); ?>">
1308
1309 <?php
1310 /**
1311 * Action at top of the share buttons
1312 *
1313 * @since 1.8.6
1314 *
1315 * @param int $achievement_id The given achievement's ID
1316 * @param int $user_id The user's ID
1317 * @param array $template_args Achievement template args
1318 */
1319 do_action( 'gamipress_achievement_share_buttons_top', $achievement_id, $user_id, $template_args ) ?>
1320
1321 <?php foreach( $social_networks as $social_network ) : ?>
1322 <a href="#"
1323 title="<?php echo ( isset( $titles[$social_network] ) ? $titles[$social_network] : '' ); ?>"
1324 class="gamipress-share-button gamipress-share-button-<?php echo $button_style; ?> gamipress-share-button-<?php echo $social_network; ?>"
1325 data-network="<?php echo $social_network; ?>">
1326 <span><?php echo ( isset( $names[$social_network] ) ? $names[$social_network] : '' ); ?></span>
1327 </a>
1328 <?php endforeach; ?>
1329
1330 <?php
1331 /**
1332 * Action at bottom of the share buttons
1333 *
1334 * @since 1.8.6
1335 *
1336 * @param int $achievement_id The given achievement's ID
1337 * @param int $user_id The user's ID
1338 * @param array $template_args Achievement template args
1339 */
1340 do_action( 'gamipress_achievement_share_buttons_bottom', $achievement_id, $user_id, $template_args ) ?>
1341
1342 </div>
1343
1344 <?php $output = ob_get_clean();
1345
1346 /**
1347 * Filters the achievement share markup
1348 *
1349 * @since 1.8.6
1350 *
1351 * @param string $output The HTML markup for social sharing
1352 * @param int $achievement_id The given achievement's ID
1353 * @param int $user_id The user's ID
1354 * @param array $template_args Achievement template args
1355 *
1356 * @return string The HTML markup for our points
1357 */
1358 $output = apply_filters( 'gamipress_achievement_share_markup', $output, $achievement_id, $user_id, $template_args );
1359
1360 return $output;
1361
1362 }
1363
1364 /**
1365 * Generate markup for share an rank's output
1366 *
1367 * @since 1.8.6
1368 *
1369 * @param int $rank_id The given rank's ID
1370 * @param array $template_args Rank template args
1371 *
1372 * @return string The HTML markup for our points
1373 */
1374 function gamipress_rank_share_markup( $rank_id = 0, $template_args = array() ) {
1375
1376 // Grab the current post ID if no rank_id was specified
1377 if ( ! $rank_id ) {
1378 $rank_id = get_the_ID();
1379 }
1380
1381 $user_id = get_current_user_id();
1382
1383 // Guest not supported yet (basically because they has not earned this)
1384 if( $user_id === 0 ) {
1385 return '';
1386 }
1387
1388 if( ! isset( $template_args['user_id'] ) ) {
1389 $template_args['user_id'] = get_current_user_id();
1390 }
1391
1392 // Return if user is displaying ranks of another user
1393 if( $user_id !== absint( $template_args['user_id'] ) ) {
1394 return '';
1395 }
1396
1397 // Bail if share is not enable
1398 if( ! (bool) gamipress_get_option( 'enable_share', false ) ) {
1399 return '';
1400 }
1401
1402 $social_networks = gamipress_get_option( 'social_networks', false );
1403
1404 // Bail if not social networks enabled
1405 if( empty( $social_networks ) ) {
1406 return '';
1407 }
1408
1409 // Check if user has earned this achievement
1410 $earned = gamipress_has_user_earned_achievement( $rank_id, $user_id );
1411
1412 if( ! $earned ) {
1413 return '';
1414 }
1415
1416 $button_style = gamipress_get_option( 'social_button_style', 'square' );
1417
1418 $titles = array(
1419 'facebook' => __( 'Share on Facebook', 'gamipress' ),
1420 'twitter' => __( 'Share on Twitter', 'gamipress' ),
1421 'linkedin' => __( 'Share on LinkedIn', 'gamipress' ),
1422 'pinterest' => __( 'Share on Pinterest', 'gamipress' ),
1423 );
1424
1425 $names = array(
1426 'facebook' => __( 'Facebook', 'gamipress' ),
1427 'twitter' => __( 'Twitter', 'gamipress' ),
1428 'linkedin' => __( 'LinkedIn', 'gamipress' ),
1429 'pinterest' => __( 'Pinterest', 'gamipress' ),
1430 );
1431
1432 // Setup vars
1433 $url = get_the_permalink( $rank_id );
1434 $title = get_the_title( $rank_id );
1435 $image = get_the_post_thumbnail_url( $rank_id );
1436
1437 // Get the rank twitter text
1438 $twitter_text = gamipress_get_option( 'twitter_rank_text', __( 'I reached the {rank_type} {rank_title} on {site_title}', 'gamipress' ) );
1439
1440 // Parse the text
1441 $twitter_text = gamipress_parse_tags( 'rank_earned', $rank_id, $user_id, $twitter_text );
1442
1443 ob_start(); ?>
1444
1445 <p class="gamipress-share-buttons-label"><?php _e( 'Share:', 'gamipress' ); ?></p>
1446
1447 <div class="gamipress-share-buttons"
1448 data-url="<?php echo esc_attr( $url ); ?>"
1449 data-title="<?php echo esc_attr( $title ); ?>"
1450 data-image="<?php echo esc_attr( $image ); ?>"
1451 data-twitter-text="<?php echo esc_attr( $twitter_text ); ?>">
1452
1453 <?php
1454 /**
1455 * Action at top of the share buttons
1456 *
1457 * @since 1.8.6
1458 *
1459 * @param int $rank_id The given rank's ID
1460 * @param int $user_id The user's ID
1461 * @param array $template_args Rank template args
1462 */
1463 do_action( 'gamipress_rank_share_buttons_top', $rank_id, $user_id, $template_args ) ?>
1464
1465 <?php foreach( $social_networks as $social_network ) : ?>
1466 <a href="#"
1467 title="<?php echo ( isset( $titles[$social_network] ) ? $titles[$social_network] : '' ); ?>"
1468 class="gamipress-share-button gamipress-share-button-<?php echo $button_style; ?> gamipress-share-button-<?php echo $social_network; ?>"
1469 data-network="<?php echo $social_network; ?>">
1470 <span><?php echo ( isset( $names[$social_network] ) ? $names[$social_network] : '' ); ?></span>
1471 </a>
1472 <?php endforeach; ?>
1473
1474 <?php
1475 /**
1476 * Action at bottom of the share buttons
1477 *
1478 * @since 1.8.6
1479 *
1480 * @param int $rank_id The given rank's ID
1481 * @param int $user_id The user's ID
1482 * @param array $template_args Rank template args
1483 */
1484 do_action( 'gamipress_rank_share_buttons_bottom', $rank_id, $user_id, $template_args ) ?>
1485
1486 </div>
1487
1488 <?php $output = ob_get_clean();
1489
1490 /**
1491 * Filters the rank share markup
1492 *
1493 * @since 1.8.6
1494 *
1495 * @param string $output The HTML markup for social sharing
1496 * @param int $rank_id The given rank's ID
1497 * @param int $user_id The user's ID
1498 * @param array $template_args Rank template args
1499 *
1500 * @return string The HTML markup for our points
1501 */
1502 $output = apply_filters( 'gamipress_rank_share_markup', $output, $rank_id, $user_id, $template_args );
1503
1504 return $output;
1505
1506 }
1507
1508 /**
1509 * Adds "earned"/"not earned" post_class based on viewer's status
1510 *
1511 * @param array $classes Post classes
1512 *
1513 * @return array Updated post classes
1514 */
1515 function gamipress_add_earned_class_single( $classes = array() ) {
1516
1517 if( is_singular( gamipress_get_achievement_types_slugs() ) ) {
1518 // Single Achievement
1519
1520 // Check if current user has earned the achievement they're viewing
1521 $classes[] =gamipress_has_user_earned_achievement( get_the_ID(), get_current_user_id() ) ? 'user-has-earned' : 'user-has-not-earned';
1522
1523 } else if( is_singular( gamipress_get_rank_types_slugs() ) ) {
1524 // Single Rank
1525
1526 // Check if current user has earned the rank they're viewing, rank is earned by default if is the lowest priority of this type
1527 if( gamipress_is_lowest_priority_rank( get_the_ID() ) ) {
1528 $earned = true;
1529 } else {
1530 $earned = gamipress_has_user_earned_achievement( get_the_ID(), get_current_user_id() );
1531 }
1532
1533 $classes[] = $earned ? 'user-has-earned' : 'user-has-not-earned';
1534
1535 }
1536
1537 return $classes;
1538
1539 }
1540 add_filter( 'post_class', 'gamipress_add_earned_class_single' );
1541
1542 /**
1543 * Returns a message if user has earned the achievement.
1544 *
1545 * @since 1.0.0
1546 *
1547 * @param int $achievement_id Achievement ID.
1548 * @param int $user_id User ID.
1549 *
1550 * @return string HTML Markup.
1551 */
1552 function gamipress_render_earned_achievement_text( $achievement_id = 0, $user_id = 0 ) {
1553
1554 $earned_message = '';
1555
1556 if ( gamipress_has_user_earned_achievement( $achievement_id, $user_id ) ) {
1557
1558 $achievement_types = gamipress_get_achievement_types();
1559 $achievement_type = $achievement_types[gamipress_get_post_type( $achievement_id )];
1560
1561 $earned_message .= '<div class="gamipress-achievement-earned"><p>' . sprintf( __( 'You have earned this %s!', 'gamipress' ), $achievement_type['singular_name'] ) . '</p></div>';
1562
1563 if ( $congrats_text = gamipress_get_post_meta( $achievement_id, '_gamipress_congratulations_text' ) ) {
1564 $earned_message .= '<div class="gamipress-achievement-congratulations">' . wpautop( $congrats_text ) . '</div>';
1565 }
1566 }
1567
1568 /**
1569 * Available filter to override the message if user has earned the achievement
1570 *
1571 * @since 1.0.0
1572 *
1573 * @param string $earned_message The earned message HTML markup.
1574 * @param int $achievement_id Achievement ID.
1575 * @param int $user_id User ID.
1576 *
1577 * @return string HTML Markup.
1578 */
1579 return apply_filters( 'gamipress_earned_achievement_message', $earned_message, $achievement_id, $user_id );
1580 }
1581
1582 /**
1583 * Check if user has earned a given achievement.
1584 *
1585 * @since 1.0.0
1586 *
1587 * @param int $achievement_id Achievement ID.
1588 * @param int $user_id User ID.
1589 *
1590 * @return bool True if user has earned the achievement, otherwise false.
1591 */
1592 function gamipress_has_user_earned_achievement( $achievement_id = 0, $user_id = 0 ) {
1593
1594 $user_id = absint( $user_id );
1595 $achievement_id = absint( $achievement_id );
1596
1597 if( $user_id === 0 ) {
1598 $user_id = get_current_user_id();
1599 }
1600
1601 if( $user_id === 0 ) {
1602 return false;
1603 }
1604
1605 $earned = gamipress_get_earnings_count( array( 'user_id' => $user_id, 'post_id' => $achievement_id ) );
1606 $earned = $earned > 0;
1607
1608 /**
1609 * Available filter to override the has user earned achievement result.
1610 *
1611 * @since 1.0.0
1612 *
1613 * @param bool $earned Whatever if user has earned this achievement or not.
1614 * @param int $achievement_id Achievement ID.
1615 * @param int $user_id User ID.
1616 *
1617 * @return bool True if user has earned the achievement, otherwise false.
1618 */
1619 return apply_filters( 'gamipress_has_user_earned_achievement', $earned, $achievement_id, $user_id );
1620
1621 }
1622
1623
1624 /**
1625 * Hide the hidden achievement post link from next and previous post link
1626 *
1627 * @since 1.0.0
1628 *
1629 * @param string $output The adjacent post link.
1630 * @param string $format Link anchor format.
1631 * @param string $link Link permalink format.
1632 * @param WP_Post $post The adjacent post.
1633 * @param string $adjacent Whether the post is previous or next.
1634 *
1635 * @return string
1636 */
1637 function gamipress_hide_next_previous_hidden_achievement_link( $output, $format, $link, $post, $adjacent ) {
1638
1639 $post = get_post();
1640
1641 if( $post && gamipress_is_achievement( $post ) && $output ) {
1642
1643 // Get post link, without hidden achievement
1644 $output = gamipress_get_post_link_without_hidden_achievement( $post->ID, $adjacent );
1645
1646 }
1647
1648 return $output;
1649
1650 }
1651 add_filter( 'next_post_link', 'gamipress_hide_next_previous_hidden_achievement_link', 10, 5 );
1652 add_filter( 'previous_post_link', 'gamipress_hide_next_previous_hidden_achievement_link', 10, 5 );
1653
1654
1655 /**
1656 * Get post link without hidden achievement link
1657 *
1658 * @since 1.0.0
1659 *
1660 * @param int $achievement_id The achievement ID
1661 * @param string $rel 'next'|'previous'
1662 *
1663 * @return string
1664 */
1665 function gamipress_get_post_link_without_hidden_achievement( $achievement_id, $rel ) {
1666
1667 // Check if is an achievement
1668 if( ! gamipress_is_achievement( $achievement_id ) ) {
1669 return '';
1670 }
1671
1672 $link = '';
1673
1674 // Get next post id without hidden achievement id
1675 $next_post_id = gamipress_get_next_previous_achievement_id( $achievement_id, $rel );
1676
1677 if ( $next_post_id ) {
1678 // Generate post link
1679 $link = gamipress_generate_post_link_by_post_id( $next_post_id, $rel );
1680 }
1681
1682
1683 return $link;
1684
1685 }
1686
1687 /**
1688 * Get next or previous post id , without hidden achievement id
1689 *
1690 * @since 1.0.0
1691 *
1692 * @param int $achievement_id The achievement ID
1693 * @param string $rel 'next'|'previous'
1694 *
1695 * @return int|bool
1696 */
1697 function gamipress_get_next_previous_achievement_id( $achievement_id , $rel ) {
1698
1699 global $wpdb;
1700
1701 $posts = GamiPress()->db->posts;
1702
1703 // Redirecting user page based on achievements
1704 $post = gamipress_get_post( absint( $achievement_id ));
1705
1706 //Get hidden achievements ids
1707 $hidden = gamipress_get_hidden_achievement_ids( $post->post_type );
1708
1709 $operator = ( $rel === 'next' ? '>' : '<' );
1710 $order = ( $rel === 'next' ? 'ASC' : 'DESC' );
1711 $hidden_where = ( count( $hidden ) ? "AND ID NOT IN ( " . implode( ', ', $hidden ) . " )" : '' );
1712
1713 $next_id = absint( $wpdb->get_var( $wpdb->prepare(
1714 "SELECT ID
1715 FROM {$posts}
1716 WHERE post_type = %s
1717 AND post_status = %s
1718 {$hidden_where}
1719 AND ID {$operator} %d
1720 ORDER BY ID {$order}
1721 LIMIT 1",
1722 $post->post_type,
1723 'publish',
1724 $post->ID
1725 ) ) );
1726
1727 // Return next or previous achievement preventing hidden achievements
1728 return ( $next_id !== absint( $post->ID ) ? $next_id : false );
1729
1730 }
1731
1732 /**
1733 * Generate the post link based on custom post object
1734 *
1735 * @since 1.0.0
1736 *
1737 * @param int $post_id
1738 * @param string $rel
1739 *
1740 * @return string
1741 */
1742 function gamipress_generate_post_link_by_post_id( $post_id, $rel ) {
1743
1744 $post = gamipress_get_post( $post_id );
1745
1746 if( ! $post ) {
1747 return '';
1748 }
1749
1750 // Title of the post
1751 $title = get_the_title( $post->ID );
1752
1753 if ( empty( $post->post_title ) ) {
1754 if( $rel === 'next' ) {
1755 $title = __( 'Next Post' );
1756 } else {
1757 $title = __( 'Previous Post' );
1758 }
1759 }
1760
1761 $nav = ($rel == 'next') ? '%s <span class="meta-nav">→</span>' : '<span class="meta-nav">←</span> %s';
1762
1763 // Build link
1764 $link = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">' . sprintf( $nav, $title ) . '</a>';
1765
1766 return $link;
1767
1768 }
1769
1770 /**
1771 * Filters the log title.
1772 *
1773 * @param string $title The log title.
1774 * @param int $id The log ID.
1775 *
1776 * @return string The formatted title
1777 */
1778 function gamipress_log_title_format( $title, $id = null ) {
1779
1780 if( $id === null ) {
1781 return $title;
1782 }
1783
1784 if( empty( $title ) ) {
1785 $title = gamipress_get_parsed_log( $id );
1786 }
1787
1788 return $title;
1789 }
1790 add_filter( 'gamipress_render_log_title', 'gamipress_log_title_format', 10, 2 );
1791
1792 /**
1793 * Gets rank's required steps and returns HTML markup for these steps
1794 *
1795 * @since 1.0.0
1796 * @param int $rank_id The given rank's post ID
1797 * @param int $user_id A given user's ID
1798 *
1799 * @return string The markup for our list
1800 */
1801 function gamipress_get_rank_requirements_list( $rank_id = 0, $user_id = 0 ) {
1802
1803 // Grab the current post ID if no rank_id was specified
1804 if ( ! $rank_id ) {
1805 global $post;
1806 $rank_id = $post->ID;
1807 }
1808
1809 // Grab the current user's ID if none was specifed
1810 if ( ! $user_id )
1811 $user_id = wp_get_current_user()->ID;
1812
1813 // Grab our rank's required requirements
1814 $requirements = gamipress_get_rank_requirements( $rank_id );
1815
1816 // Return our markup output
1817 return gamipress_get_rank_requirements_list_markup( $requirements, $user_id );
1818
1819 }
1820
1821 /**
1822 * Generate HTML markup for an achievement's required steps
1823 *
1824 * This will generate an unorderd list (<ul>) if steps are non-sequential
1825 * and an ordered list (<ol>) if steps require sequentiality.
1826 *
1827 * @since 1.3.1
1828 *
1829 * @param array $requirements An rank's required requirements
1830 * @param int $rank_id The given rank's ID
1831 * @param int $user_id The given user's ID
1832 * @param array $template_args The given template args
1833 *
1834 * @return string The markup for our list
1835 */
1836 function gamipress_get_rank_requirements_list_markup( $requirements = array(), $rank_id = 0, $user_id = 0, $template_args = array() ) {
1837
1838 // If we don't have any steps, or our steps aren't an array, return nothing
1839 if ( ! $requirements || ! is_array( $requirements ) ) {
1840 return null;
1841 }
1842
1843 // Grab the current post ID if no achievement_id was specified
1844 if ( ! $rank_id ) {
1845 global $post;
1846 $rank_id = $post->ID;
1847 }
1848
1849 $count = count( $requirements );
1850
1851 // If we have no steps, return nothing
1852 if ( ! $count ) {
1853 return null;
1854 }
1855
1856 // Grab the current user's ID if none was specified
1857 if ( ! $user_id ) {
1858 $user_id = get_current_user_id();
1859 }
1860
1861 $a = wp_parse_args( $template_args, gamipress_rank_shortcode_defaults() );
1862
1863 // Setup our variables
1864 $output = '';
1865 $container = gamipress_is_achievement_sequential() ? 'ol' : 'ul';
1866
1867 if( $a['heading'] === 'yes' ) {
1868
1869 $requirements_heading = sprintf( '%1$d %2$s', $count, _n( 'Requirement', 'Requirements', $count, 'gamipress' ) );;
1870
1871 /**
1872 * Filters the steps heading text
1873 *
1874 * @since 1.0.0
1875 *
1876 * @param string $requirements_heading The heading text (eg: 2 Requirements)
1877 * @param array $requirements The rank requirements
1878 * @param int $user_id The user's ID
1879 * @param array $template_args The given template args
1880 *
1881 * @return string
1882 */
1883 $requirements_heading = apply_filters( 'gamipress_rank_requirements_heading', $requirements_heading, $requirements, $user_id, $template_args );
1884
1885 $output .= '<' . $a['heading_size'] . ' class="gamipress-rank-requirements-heading">' . $requirements_heading . '</' . $a['heading_size'] . '>';
1886
1887 }
1888
1889 $output .= '<' . $container .' class="gamipress-rank-requirements gamipress-required-requirements">';
1890
1891 // Concatenate our output
1892 foreach ( $requirements as $requirement ) {
1893
1894 if( $user_id === 0 ) {
1895 $earned_status = 'user-has-not-earned';
1896 } else {
1897 // Check if user can earn this requirement and add the earned class
1898 $can_earn = gamipress_can_user_earn_requirement( $requirement->ID, $user_id );
1899 $earned_status = $can_earn ? 'user-has-not-earned' : 'user-has-earned';
1900 }
1901
1902 $title = $requirement->post_title;
1903
1904 // If step doesn't have a title, then try to build one
1905 if( empty( $title ) ) {
1906 $title = gamipress_build_requirement_title( $requirement->ID );
1907 }
1908
1909 /**
1910 * Filters the rank requirement CSS class
1911 *
1912 * @since 1.3.1
1913 *
1914 * @param string $class CSS class based on earned status ('user-has-earned' | 'user-has-not-earned')
1915 * @param stdClass $requirement Requirement object
1916 * @param int $user_id User's ID
1917 * @param array $template_args The given template args
1918 *
1919 * @return string
1920 */
1921 $class = apply_filters( 'gamipress_rank_requirement_class', $earned_status, $requirement, $user_id, $template_args );
1922
1923 /**
1924 * Filters the rank requirement HTML title to display
1925 *
1926 * @since 1.3.1
1927 *
1928 * @param string $title Title to display
1929 * @param stdClass $requirement Requirement object
1930 * @param int $user_id User's ID
1931 * @param array $template_args The given template args
1932 *
1933 * @return string
1934 */
1935 $title = apply_filters( 'gamipress_rank_requirement_title_display', $title, $requirement, $user_id, $template_args );
1936
1937 $output .= '<li class="'. esc_attr( $class ) .'">'. $title . '</li>';
1938 }
1939
1940 $output .= '</'. $container .'><!-- .gamipress-requirements -->';
1941
1942 // Return our output
1943 return $output;
1944
1945 }
1946
1947 /**
1948 * Helper function tests that we're in the main loop
1949 *
1950 * @since 1.3.1
1951 *
1952 * @param bool|integer $id The page id
1953 *
1954 * @return boolean A boolean determining if the function is in the main loop
1955 */
1956 function gamipress_is_single_rank( $id = false ) {
1957
1958 $slugs = gamipress_get_rank_types_slugs();
1959
1960 // only run our filters on the rank singular page
1961 if ( is_admin() || empty( $slugs ) || ! is_singular( $slugs ) )
1962 return false;
1963 // w/o id, we're only checking template context
1964 if ( ! $id )
1965 return true;
1966
1967 // Checks several variables to be sure we're in the main loop (and won't effect things like post pagination titles)
1968 return ( ( $GLOBALS['post']->ID == $id ) && in_the_loop() && empty( $GLOBALS['gamipress_reformat_content'] ) );
1969
1970 }
1971
1972 /**
1973 * Returns a message if user has earned the rank.
1974 *
1975 * @since 1.3.1
1976 *
1977 * @param int $rank_id Rank ID.
1978 * @param int $user_id User ID.
1979 *
1980 * @return string HTML Markup.
1981 */
1982 function gamipress_render_earned_rank_text( $rank_id = 0, $user_id = 0 ) {
1983
1984 $earned_message = '';
1985
1986 if ( gamipress_has_user_earned_rank( $rank_id, $user_id ) ) {
1987
1988 $rank_types = gamipress_get_rank_types();
1989 $rank_type = $rank_types[gamipress_get_post_type( $rank_id )];
1990
1991 $earned_message .= '<div class="gamipress-rank-earned"><p>' . sprintf( __( 'You have reached this %s!', 'gamipress' ), $rank_type['singular_name']) . '</p></div>';
1992
1993 if ( $congrats_text = gamipress_get_post_meta( $rank_id, '_gamipress_congratulations_text' ) ) {
1994 $earned_message .= '<div class="gamipress-rank-congratulations">' . wpautop( $congrats_text ) . '</div>';
1995 }
1996 }
1997
1998 /**
1999 * Available filter to override the message if user has earned the rank
2000 *
2001 * @since 1.0.0
2002 *
2003 * @param string $earned_message The earned message HTML markup.
2004 * @param int $rank_id Rank ID.
2005 * @param int $user_id User ID.
2006 *
2007 * @return string HTML Markup.
2008 */
2009 return apply_filters( 'gamipress_earned_rank_message', $earned_message, $rank_id, $user_id );
2010
2011 }
2012
2013 /**
2014 * Check if user has earned a given achievement.
2015 *
2016 * @since 1.3.1
2017 *
2018 * @param int $rank_id Rank ID.
2019 * @param int $user_id User ID.
2020 *
2021 * @return bool True if user has earned the rank, otherwise false.
2022 */
2023 function gamipress_has_user_earned_rank( $rank_id = 0, $user_id = 0 ) {
2024
2025 $user_id = absint( $user_id );
2026 $rank_id = absint( $rank_id );
2027
2028 if( $user_id === 0 ) {
2029 $user_id = get_current_user_id();
2030 }
2031
2032 if( $user_id === 0 ) {
2033 return false;
2034 }
2035
2036 $earned = gamipress_get_earnings_count( array( 'user_id' => $user_id, 'post_id' => $rank_id ) );
2037 $earned = $earned > 0;
2038
2039 /**
2040 * Available filter to override the has user earned rank result.
2041 *
2042 * @since 1.0.0
2043 *
2044 * @param bool $earned Whatever if user has earned this rank or not.
2045 * @param int $rank_id Rank ID.
2046 * @param int $user_id User ID.
2047 *
2048 * @return bool True if user has earned the rank, otherwise false.
2049 */
2050 return apply_filters( 'gamipress_has_user_earned_rank', $earned, $rank_id, $user_id );
2051
2052 }
2053
2054 /**
2055 * Auto flush permalinks wth a soft flush when a 404 error is detected on an GamiPress page
2056 *
2057 * Taken from Easy Digital Downloads
2058 *
2059 * @since 1.3.3
2060 * @return string
2061 */
2062 function gamipress_refresh_permalinks_on_bad_404() {
2063
2064 global $wp;
2065
2066 if( ! is_404() ) {
2067 return;
2068 }
2069
2070 if( isset( $_GET['gamipress-flush'] ) ) {
2071 return;
2072 }
2073
2074 if( false === get_transient( 'gamipress_refresh_404_permalinks' ) ) {
2075
2076 $gamipress_slugs = array();
2077
2078 $gamipress_slugs = array_merge( $gamipress_slugs, gamipress_get_achievement_types() );
2079 $gamipress_slugs = array_merge( $gamipress_slugs, gamipress_get_points_types() );
2080 $gamipress_slugs = array_merge( $gamipress_slugs, gamipress_get_rank_types() );
2081
2082 $parts = explode( '/', $wp->request );
2083
2084 $is_a_gamipress_page = false;
2085
2086 foreach( $parts as $part ) {
2087
2088 if( isset( $gamipress_slugs[$part] ) ) {
2089 $is_a_gamipress_page = true;
2090 break;
2091 }
2092
2093 }
2094
2095 if( ! $is_a_gamipress_page ) {
2096 return;
2097 }
2098
2099 flush_rewrite_rules( false );
2100
2101 set_transient( 'gamipress_refresh_404_permalinks', 1, HOUR_IN_SECONDS * 12 );
2102
2103 wp_redirect( home_url( add_query_arg( array( 'gamipress-flush' => 1 ), $wp->request ) ) ); exit;
2104
2105 }
2106 }
2107 add_action( 'template_redirect', 'gamipress_refresh_permalinks_on_bad_404' );
2108
2109 /**
2110 * Render earnings column
2111 *
2112 * @since 1.0.0
2113 *
2114 * @param string $column_output Default column output
2115 * @param string $column_name The column name
2116 * @param stdClass $user_earning The column name
2117 * @param array $template_args Template received arguments
2118 *
2119 * @return string
2120 */
2121 function gamipress_earnings_render_column( $column_output, $column_name, $user_earning, $template_args ) {
2122
2123 // Setup vars
2124 $requirement_types = gamipress_get_requirement_types();
2125 $points_types = gamipress_get_points_types();
2126 $achievement_types = gamipress_get_achievement_types();
2127 $rank_types = gamipress_get_rank_types();
2128
2129 switch( $column_name ) {
2130 case 'user':
2131
2132 $user = get_userdata( $user_earning->user_id );
2133
2134 if( $user ) {
2135 $column_output = get_avatar( $user_earning->user_id ) . $user->display_name;
2136 }
2137
2138 break;
2139 case 'thumbnail':
2140
2141 if( in_array( $user_earning->post_type, gamipress_get_requirement_types_slugs() ) ) {
2142
2143 if( $user_earning->post_type === 'step' && $achievement = gamipress_get_step_achievement( $user_earning->post_id ) ) {
2144 // Step
2145
2146 // Get the achievement thumbnail and build a link to the achievement
2147 $column_output = gamipress_get_achievement_post_thumbnail( $achievement->ID );
2148
2149 } else if( ( $user_earning->post_type === 'points-award' || $user_earning->post_type === 'points-deduct' ) && $points_type = gamipress_get_points_award_points_type( $user_earning->post_id ) ) {
2150 // Points Award and Deduction
2151
2152 // Get the points type thumbnail
2153 $column_output = gamipress_get_points_type_thumbnail( $points_type->ID );
2154
2155 } else if( $user_earning->post_type === 'rank-requirement' && $rank = gamipress_get_rank_requirement_rank( $user_earning->post_id ) ) {
2156 // Rank requirement
2157
2158 // Get the rank thumbnail
2159 $column_output = gamipress_get_rank_post_thumbnail( $rank->ID );
2160 }
2161
2162 } else if( in_array( $user_earning->post_type, gamipress_get_achievement_types_slugs() ) ) {
2163 // Achievement
2164
2165 // Get the achievement thumbnail
2166 $column_output = gamipress_get_achievement_post_thumbnail( $user_earning->post_id );
2167
2168 } else if( in_array( $user_earning->post_type, gamipress_get_rank_types_slugs() ) ) {
2169 // Rank
2170
2171 // Get the rank thumbnail
2172 $column_output = gamipress_get_rank_post_thumbnail( $user_earning->post_id );
2173
2174 } else {
2175 // Default
2176
2177 // Get the points type thumbnail
2178 $column_output = gamipress_get_points_type_thumbnail( $user_earning->post_id );
2179 }
2180
2181 break;
2182 case 'description':
2183
2184 $earning_title = $user_earning->title;
2185 $earning_description = '';
2186
2187 if( in_array( $user_earning->post_type, gamipress_get_requirement_types_slugs() ) ) {
2188
2189 $earning_description = '';
2190
2191 if( $user_earning->post_type === 'step' && $achievement = gamipress_get_step_achievement( $user_earning->post_id ) ) {
2192 // Step
2193
2194 // Build a link to the achievement
2195 $earning_description = sprintf( '%s %s: <a href="%s">%s</a>',
2196 $achievement_types[$achievement->post_type]['singular_name'],
2197 __( 'Step', 'gamipress' ),
2198 get_post_permalink( $achievement->ID ),
2199 $user_earning->title
2200 );
2201
2202 } else if( ( $user_earning->post_type === 'points-award' || $user_earning->post_type === 'points-deduct' ) && $points_type = gamipress_get_points_award_points_type( $user_earning->post_id ) ) {
2203 // Points Award and Deduction
2204
2205 $earning_description = sprintf( '%s %s',
2206 $points_types[$points_type->post_name]['plural_name'],
2207 ( $user_earning->post_type === 'points-award' ? __( 'Award', 'gamipress' ) : __( 'Deduction', 'gamipress' ) )
2208 );
2209
2210 } else if( $user_earning->post_type === 'rank-requirement' && $rank = gamipress_get_rank_requirement_rank( $user_earning->post_id ) ) {
2211 // Rank requirement
2212
2213 // Build a link to the rank
2214 $earning_description = sprintf( '%s %s: <a href="%s">%s</a>',
2215 $rank_types[$rank->post_type]['singular_name'],
2216 __( 'Requirement', 'gamipress' ),
2217 get_post_permalink( $rank->ID ),
2218 $user_earning->title
2219 );
2220 }
2221
2222 } else if( in_array( $user_earning->post_type, gamipress_get_achievement_types_slugs() ) ) {
2223 // Achievement
2224
2225 // Build a link to the achievement
2226 $earning_title = sprintf( '<a href="%s">%s</a>',
2227 get_post_permalink( $user_earning->post_id ),
2228 gamipress_get_post_field( 'post_title', $user_earning->post_id )
2229 );
2230 $earning_description = $achievement_types[$user_earning->post_type]['singular_name'];
2231
2232 } else if( in_array( $user_earning->post_type, gamipress_get_rank_types_slugs() ) ) {
2233 // Rank
2234
2235 // Build a link to the rank
2236 $earning_title = sprintf( '<a href="%s">%s</a>',
2237 get_post_permalink( $user_earning->post_id ),
2238 gamipress_get_post_field( 'post_title', $user_earning->post_id )
2239 );
2240 $earning_description = $rank_types[$user_earning->post_type]['singular_name'];
2241
2242 }
2243
2244 // Default description for custom user earnings with points assigned
2245 if( empty( $earning_description ) ) {
2246
2247 $points = (int) $user_earning->points;
2248
2249 if( $points !== 0 && isset( $points_types[$user_earning->points_type] ) ) {
2250
2251 $earning_description = sprintf( '%s %s',
2252 $points_types[$user_earning->points_type]['plural_name'],
2253 ( $points > 0 ? __( 'Award', 'gamipress' ) : __( 'Deduction', 'gamipress' ) )
2254 );
2255
2256 }
2257
2258 }
2259
2260 $column_output = sprintf( '<strong class="gamipress-earning-title">%s</strong>'
2261 . '<br>'
2262 . '<span class="gamipress-earning-description">%s</span>',
2263 $earning_title,
2264 $earning_description
2265 );
2266
2267 break;
2268 case 'points':
2269
2270 $points = (int) $user_earning->points;
2271
2272 if( $points !== 0 && isset( $points_types[$user_earning->points_type] ) ) {
2273
2274 // Setup the output as %d point(s)
2275 $column_output = gamipress_format_points( $points, $user_earning->points_type );
2276
2277 // For points deducts turn amount to negative
2278 if( $user_earning->post_type === 'points-deduct' && $points > 0 ) {
2279 $negative_points = $points * -1;
2280 $column_output = gamipress_format_points( $negative_points, $user_earning->points_type );;
2281 }
2282
2283 }
2284
2285 break;
2286 case 'date':
2287
2288 $column_output = date_i18n( get_option( 'date_format' ), strtotime( $user_earning->date ) );
2289
2290 break;
2291 }
2292
2293 return $column_output;
2294
2295 }
2296 add_action( 'gamipress_earnings_render_column', 'gamipress_earnings_render_column', 10, 4 );