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.5.1.php

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

1,036 lines 30.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * GamiPress 1.5.1 compatibility functions
4 *
5 * @package GamiPress\1.5.1
6 * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]>
7 * @since 1.5.1
8 */
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 function gamipress_pre_init_151() {
13
14 global $wpdb;
15
16 // Setup the P2P tables for backward compatibility
17 GamiPress()->db->p2p = ( property_exists( $wpdb, 'p2p' ) ? $wpdb->p2p : $wpdb->prefix . 'p2p' );
18 GamiPress()->db->p2pmeta = ( property_exists( $wpdb, 'p2pmeta' ) ? $wpdb->p2pmeta : $wpdb->prefix . 'p2pmeta' );
19
20 // Multisite support
21 if( gamipress_is_network_wide_active() ) {
22
23 GamiPress()->db->p2p = $wpdb->base_prefix . 'p2p';
24 GamiPress()->db->p2pmeta = $wpdb->base_prefix . 'p2pmeta';
25
26 }
27
28 }
29 add_action( 'init', 'gamipress_pre_init_151', 6 );
30
31 /**
32 * Replace per site queries to root site when GamiPress is active network wide
33 *
34 * @since 1.4.0
35 *
36 * @param string $request
37 * @param WP_Query $wp_query
38 *
39 * @return string
40 */
41 function gamipress_network_wide_post_request_151( $request, $wp_query ) {
42
43 global $wpdb;
44
45 if( is_gamipress_upgraded_to( '1.5.1' ) ) {
46 return $request;
47 }
48
49 // If GamiPress is active network wide and we are not in main site, then filter all queries to our post types
50 if(
51 gamipress_is_network_wide_active()
52 && ! is_main_site()
53 && isset( $wp_query->query_vars['post_type'] )
54 ) {
55
56 $post_type = $wp_query->query_vars['post_type'];
57
58 if( is_array( $post_type ) ) {
59 $post_type = $post_type[0];
60 }
61
62 if(
63 in_array( $post_type, array( 'points-type', 'achievement-type', 'rank-type' ) )
64 || in_array( $post_type, gamipress_get_requirement_types_slugs() )
65 || in_array( $post_type, gamipress_get_achievement_types_slugs() )
66 || in_array( $post_type, gamipress_get_rank_types_slugs() )
67 ) {
68
69 $p2p = ( property_exists( $wpdb, 'p2p' ) ? $wpdb->p2p : $wpdb->prefix . 'p2p' );
70 $p2pmeta = ( property_exists( $wpdb, 'p2pmeta' ) ? $wpdb->p2pmeta : $wpdb->prefix . 'p2pmeta' );
71
72 // Replace {prefix}{site}p2p to {prefix}p2p
73 $request = str_replace( $p2p, "{$wpdb->base_prefix}p2p", $request );
74
75 // Replace {prefix}{site}p2pmeta to {prefix}p2pmeta
76 $request = str_replace( $p2pmeta, "{$wpdb->base_prefix}p2pmeta", $request );
77 }
78
79 }
80
81 return $request;
82
83 }
84 add_filter( 'posts_request', 'gamipress_network_wide_post_request_151', 10, 2 );
85
86 /**
87 * Listener for daily visits
88 *
89 * Triggers: gamipress_site_visit, gamipress_specific_post_visit
90 *
91 * @deprecated
92 *
93 * @since 1.0.0
94 *
95 * @return void
96 */
97 function gamipress_site_visit_listener_old() {
98
99 // Bail if is an ajax request
100 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
101 return;
102 }
103
104 // Bail if is admin area
105 if( is_admin() ) {
106 return;
107 }
108
109 // Bail if not logged in
110 if( ! is_user_logged_in() ) {
111 return;
112 }
113
114 // Current User ID
115 $user_id = get_current_user_id();
116 $now = strtotime( date( 'Y-m-d', current_time( 'timestamp' ) ) );
117
118 // Website daily visit
119 $count = gamipress_get_user_trigger_count( $user_id, 'gamipress_site_visit', $now );
120
121 // Trigger daily visit action if not triggered today
122 if( $count === 0 ) {
123 do_action( 'gamipress_site_visit', $user_id );
124 }
125
126 global $post;
127
128 if( $post ) {
129
130 // Post daily visit
131 $count = gamipress_get_user_trigger_count( $user_id, 'gamipress_specific_post_visit', $now, 0, array( $post->ID, $user_id, $post ) );
132
133 // Trigger daily post visit action if not triggered today
134 if( $count === 0 ) {
135
136 // Trigger any post visit
137 do_action( 'gamipress_post_visit', $post->ID, $user_id, $post );
138
139 // Trigger specific post visit
140 do_action( 'gamipress_specific_post_visit', $post->ID, $user_id, $post );
141
142 }
143
144 }
145 }
146 // Since 1.5.1 visits has been tracked through ajax
147 //add_action( 'wp_head', 'gamipress_site_visit_listener_old' );
148
149 /**
150 * Listener for user post visits
151 *
152 * Triggers: gamipress_user_post_visit, gamipress_user_specific_post_visit
153 *
154 * @deprecated
155 *
156 * @since 1.2.9
157 *
158 * @return void
159 */
160 function gamipress_user_post_visit_listener() {
161
162 // Bail if is an ajax request
163 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
164 return;
165 }
166
167 // Bail if is admin area
168 if( is_admin() ) {
169 return;
170 }
171
172 // Current User ID
173 $user_id = get_current_user_id();
174
175 global $post;
176
177 if( $post ) {
178
179 $post_author = absint( $post->post_author );
180
181 // Trigger user post visit action to the author if visitor not is the author
182 if( $post_author && $post_author !== $user_id ) {
183 do_action( 'gamipress_user_post_visit', $post->ID, $post_author, $user_id, $post );
184 do_action( 'gamipress_user_specific_post_visit', $post->ID, $post_author, $user_id, $post );
185 }
186
187 }
188
189 }
190 // Since 1.5.1 visits has been tracked through ajax
191 //add_action( 'wp_head', 'gamipress_user_post_visit_listener' );
192
193 /* --------------------------
194 * Requirement functions
195 -------------------------- */
196
197 /**
198 * Get the sort order for a given requirement
199 *
200 * @since 1.0.5
201 *
202 * @param integer $requirement_id The given requirement's post ID
203 *
204 * @return integer The requirement's sort order
205 */
206 function gamipress_get_requirement_menu_order_old( $requirement_id = 0 ) {
207
208 global $wpdb;
209
210 $p2p = GamiPress()->db->p2p;
211 $p2pmeta = GamiPress()->db->p2pmeta;
212
213 $p2p_id = $wpdb->get_var( $wpdb->prepare( "SELECT p2p_id FROM $p2p WHERE p2p_from = %d", $requirement_id ) );
214
215 $menu_order = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM $p2pmeta WHERE p2p_id=%d AND meta_key='order'", $p2p_id ) );
216
217 if ( ! $menu_order || $menu_order === 'NaN' ) {
218 $menu_order = '0';
219 }
220
221 return $menu_order;
222
223 }
224
225 /**
226 * Helper function for comparing our requirement sort order (used in uasort() in gamipress_create_points_awards_meta_box())
227 *
228 * @since 1.0.5
229 *
230 * @param integer $requirement_x The order number of our given requirement
231 * @param integer $requirement_y The order number of the requirement we're comparing against
232 *
233 * @return integer 0 if the order matches, -1 if it's lower, 1 if it's higher
234 */
235 function gamipress_compare_requirements_order_old( $requirement_x = 0, $requirement_y = 0 ) {
236
237 if( ! property_exists( $requirement_x, 'order' ) ) {
238 return 0;
239 }
240
241 if ( $requirement_x->order == $requirement_y->order ) {
242 return 0;
243 }
244
245 return ( $requirement_x->order < $requirement_y->order ) ? -1 : 1;
246
247 }
248
249 /**
250 * Get the the ID of a P2P connection (p2p_id) to a given requirement ID (p2p_from)
251 *
252 * @deprecated
253 *
254 * @since 1.0.6
255 *
256 * @param integer $requirement_id The given post ID
257 *
258 * @return integer The resulting connection
259 */
260 function gamipress_get_requirement_connection_id( $requirement_id = 0 ) {
261
262 global $wpdb;
263
264 $p2p = GamiPress()->db->p2p;
265
266 $p2p_id = $wpdb->get_var( $wpdb->prepare( "SELECT p2p_id FROM $p2p WHERE p2p_from = %d ", $requirement_id ) );
267
268 return $p2p_id;
269
270 }
271
272 /**
273 * Get the the ID of a P2P connected object (p2p_to) to a given requirement ID (p2p_from)
274 *
275 * @deprecated Just used on 1.2.7 upgrade
276 *
277 * @since 1.0.6
278 *
279 * @param integer $requirement_id The given post ID
280 *
281 * @return integer The resulting connected post ID
282 */
283 function gamipress_get_requirement_connected_id( $requirement_id = 0 ) {
284
285 global $wpdb;
286
287 $p2p = GamiPress()->db->p2p;
288
289 if( ! gamipress_database_table_exists( $p2p ) ) {
290 return 0;
291 }
292
293 $p2p_to = $wpdb->get_var( $wpdb->prepare( "SELECT p2p_to FROM $p2p WHERE p2p_from = %d ", $requirement_id ) );
294
295 return $p2p_to;
296
297 }
298
299 /**
300 * Return post assigned requirements
301 *
302 * Note: This function has been reworked in order to search on p2p tables to retrieve requirements of the given post ID
303 *
304 * @since 1.0.0
305 *
306 * @param null $post_id
307 * @param $requirement_type
308 * @param $post_status
309 *
310 * @return array|bool
311 */
312 function gamipress_get_assigned_requirements_old( $post_id = null, $requirement_type = '', $post_status = 'publish' ) {
313
314 global $post, $wpdb;
315
316 if( $post_id === null ) {
317 $post_id = $post->ID;
318 }
319
320 // For full backward compatibility, get the new requirements to check them with old requirements
321 $new_requirements = get_posts( array(
322 'post_type' => $requirement_type,
323 'post_parent' => $post_id,
324 'post_status' => $post_status,
325 'orderby' => 'menu_order',
326 'order' => 'ASC',
327 'posts_per_page' => -1,
328 'suppress_filters' => false,
329 ) );
330
331 // Build an array just with IDs to being compared with old requirements
332 $new_requirements_ids = array();
333
334 foreach( $new_requirements as $new_requirement ) {
335 $new_requirements_ids[] = $new_requirement->ID;
336 }
337
338 // Perform the old function
339 $post_type = gamipress_get_post_type( $post_id );
340
341 $p2p = GamiPress()->db->p2p;
342 $p2pmeta = GamiPress()->db->p2pmeta;
343
344 $requirements = $wpdb->get_results( $wpdb->prepare( "SELECT p2p.p2p_from AS ID FROM {$p2p} AS p2p WHERE p2p.p2p_to = {$post_id} AND p2p.p2p_type = '{$requirement_type}-to-{$post_type}'" ) );
345
346 foreach( $requirements as $key => $requirement ) {
347
348 // Just add not in new requiremetns array
349 if( ! in_array( $requirement->ID, $new_requirements_ids ) ) {
350 $new_requirements[] = get_post( $requirement->ID );
351 }
352
353 //$requirements[$key] = get_post( $requirement->ID );
354
355 }
356
357 // Return new requirements instead
358 //return $requirements;
359 return $new_requirements;
360
361 }
362
363 /* --------------------------
364 * Points functions
365 -------------------------- */
366
367 /**
368 * Get Points Award Points Type
369 *
370 * @since 1.0.0
371 *
372 * @param integer $points_award_id The given points award's post ID
373 * @return object|bool The post object of the points type, or false if none
374 */
375 function gamipress_get_points_award_points_type_old( $points_award_id = 0 ) {
376
377 global $wpdb;
378
379 // Grab the current post ID if no points_award_id was specified
380 if ( ! $points_award_id ) {
381 global $post;
382 $points_award_id = $post->ID;
383 }
384
385 // Requirements UI is storing post parents without get 1.5.1 upgrade so first check if post_parent has been assigned yet
386 $points_type_id = absint( gamipress_get_post_field( 'post_parent', $points_award_id ) );
387
388 if( $points_type_id !== 0 ) {
389 // If has parent, return his post object
390 return get_post( $points_type_id );
391 }
392
393 $p2p = GamiPress()->db->p2p;
394
395 $points_type_id = absint( $wpdb->get_var( $wpdb->prepare( "SELECT p2p.p2p_to FROM {$p2p} AS p2p WHERE p2p.p2p_from = {$points_award_id} AND p2p.p2p_type = 'points-award-to-points-type'" ) ) );
396
397 if( $points_type_id !== 0 ) {
398 // If has parent, return his post object
399 return get_post( $points_type_id );
400 } else {
401 return false;
402 }
403
404 }
405
406 /**
407 * Get Points Deduction Points Type
408 *
409 * @since 1.3.7
410 *
411 * @param integer $points_deduct_id The given points deduct's post ID
412 * @return object|bool The post object of the points type, or false if none
413 */
414 function gamipress_get_points_deduct_points_type_old( $points_deduct_id = 0 ) {
415
416 global $wpdb;
417
418 // Grab the current post ID if no points_award_id was specified
419 if ( ! $points_deduct_id ) {
420 global $post;
421 $points_deduct_id = $post->ID;
422 }
423
424 // Requirements UI is storing post parents without get 1.5.1 upgrade so first check if post_parent has been assigned yet
425 $points_type_id = absint( gamipress_get_post_field( 'post_parent', $points_deduct_id ) );
426
427 if( $points_type_id !== 0 ) {
428 // If has parent, return his post object
429 return get_post( $points_type_id );
430 }
431
432 $p2p = GamiPress()->db->p2p;
433
434 $points_type_id = absint( $wpdb->get_var( $wpdb->prepare( "SELECT p2p.p2p_to FROM {$p2p} AS p2p WHERE p2p.p2p_from = {$points_deduct_id} AND p2p.p2p_type = 'points-award-to-points-type'" ) ) );
435
436 if( $points_type_id !== 0 ) {
437 // If has parent, return his post object
438 return get_post( $points_type_id );
439 } else {
440 return false;
441 }
442
443 }
444
445 /* --------------------------
446 * Achievement functions
447 -------------------------- */
448
449 /**
450 * Get an array of achievements
451 *
452 * @since 1.0.0
453 *
454 * @param array $args An array of our relevant arguments
455 * @return array An array of the queried achievements
456 */
457 function gamipress_get_achievements_old( $args = array() ) {
458
459 // Setup our defaults
460 $defaults = array(
461 'post_type' => array_merge( gamipress_get_achievement_types_slugs(), gamipress_get_requirement_types_slugs() ),
462 'numberposts' => -1,
463 'suppress_filters' => false,
464 'achievement_relationship' => 'any',
465 );
466
467 $args = wp_parse_args( $args, $defaults );
468
469 // Hook join functions for joining to P2P table to retrieve the parent of an achievement
470 if ( isset( $args['parent_of'] ) ) {
471 add_filter( 'posts_join', 'gamipress_get_achievements_parents_join' );
472 add_filter( 'posts_where', 'gamipress_get_achievements_parents_where', 10, 2 );
473 }
474
475 // Hook join functions for joining to P2P table to retrieve the children of an achievement
476 if ( isset( $args['children_of'] ) ) {
477 add_filter( 'posts_join', 'gamipress_get_achievements_children_join', 10, 2 );
478 add_filter( 'posts_where', 'gamipress_get_achievements_children_where', 10, 2 );
479 add_filter( 'posts_orderby', 'gamipress_get_achievements_children_orderby' );
480 }
481
482 // Get our achievement posts
483 $achievements = get_posts( $args );
484
485 // Remove all our filters
486 remove_filter( 'posts_join', 'gamipress_get_achievements_parents_join' );
487 remove_filter( 'posts_where', 'gamipress_get_achievements_parents_where' );
488 remove_filter( 'posts_join', 'gamipress_get_achievements_children_join' );
489 remove_filter( 'posts_where', 'gamipress_get_achievements_children_where' );
490 remove_filter( 'posts_orderby', 'gamipress_get_achievements_children_orderby' );
491
492 return $achievements;
493
494 }
495
496 /**
497 * Modify the WP_Query Join filter for achievement children
498 *
499 * @deprecated
500 *
501 * @since 1.0.0
502 *
503 * @param string $join The query "join" string
504 * @param object $query_object The complete query object
505 * @return string The updated "join" string
506 */
507 function gamipress_get_achievements_children_join( $join = '', $query_object = null ) {
508
509 $posts = GamiPress()->db->posts;
510 $p2p = GamiPress()->db->p2p;
511 $p2pmeta = GamiPress()->db->p2pmeta;
512
513 $join .= " LEFT JOIN {$p2p} AS p2p ON p2p.p2p_from = {$posts}.ID";
514
515 //if ( isset( $query_object->query_vars['achievement_relationship'] ) && $query_object->query_vars['achievement_relationship'] !== 'any' ) {
516 //$join .= " LEFT JOIN {$p2pmeta} AS p2pm1 ON p2pm1.p2p_id = p2p.p2p_id";
517 //}
518
519 $join .= " LEFT JOIN {$p2pmeta} AS p2pm2 ON p2pm2.p2p_id = p2p.p2p_id";
520
521 return $join;
522
523 }
524
525 /**
526 * Modify the WP_Query Where filter for achievement children
527 *
528 * @deprecated
529 *
530 * @since 1.0.0
531 *
532 * @param string $where The query "where" string
533 * @param object $query_object The complete query object
534 * @return string The updated query "where" string
535 */
536 function gamipress_get_achievements_children_where( $where = '', $query_object = null ) {
537
538 global $wpdb;
539
540 // ^^ add required and optional. right now just returns all achievements.
541
542 // Check for required relationship
543 //if ( isset( $query_object->query_vars['achievement_relationship'] ) && $query_object->query_vars['achievement_relationship'] === 'required' )
544 //$where .= " AND p2pm1.meta_key = 'Required'";
545
546 // Check for optional relationship
547 //if ( isset( $query_object->query_vars['achievement_relationship'] ) && $query_object->query_vars['achievement_relationship'] === 'optional' )
548 //$where .= " AND p2pm1.meta_key = 'Optional'";
549
550 // Filter by order meta
551 $where .= " AND p2pm2.meta_key ='order'";
552
553 $where .= $wpdb->prepare( ' AND p2p.p2p_to = %d', $query_object->query_vars['children_of'] );
554
555 return $where;
556
557 }
558
559 /**
560 * Modify the WP_Query OrderBy filter for achievement children
561 *
562 * @deprecated
563 *
564 * @since 1.0.0
565 *
566 * @param string $orderby The query "orderby" string
567 * @return string The updated "orderby" string
568 */
569 function gamipress_get_achievements_children_orderby( $orderby = '' ) {
570
571 return $orderby = 'p2pm2.meta_value ASC';
572
573 }
574
575 /**
576 * Modify the WP_Query Join filter for achievement parents
577 *
578 * @deprecated
579 *
580 * @since 1.0.0
581 *
582 * @param string $join The query "join" string
583 * @return string The updated "join" string
584 */
585 function gamipress_get_achievements_parents_join( $join = '' ) {
586
587 $posts = GamiPress()->db->posts;
588 $p2p = GamiPress()->db->p2p;
589
590 $join .= " LEFT JOIN {$p2p} AS p2p ON p2p.p2p_to = {$posts}.ID";
591
592 return $join;
593
594 }
595
596 /**
597 * Modify the WP_Query Where filter for achievement parents
598 *
599 * @deprecated
600 *
601 * @since 1.0.0
602 *
603 * @param string $where The query "where" string
604 * @param object $query_object The complete query object
605 *
606 * @return string appended sql where statement
607 */
608 function gamipress_get_achievements_parents_where( $where = '', $query_object = null ) {
609
610 global $wpdb;
611
612 $where .= $wpdb->prepare( ' AND p2p.p2p_from = %d', $query_object->query_vars['parent_of'] );
613
614 return $where;
615
616 }
617
618 function gamipress_update_achievement_type_151( $original_type = '', $new_type = '' ) {
619
620 // Bail if properly upgrade to required version
621 if( is_gamipress_upgraded_to( '1.5.1' ) ) {
622 return;
623 }
624
625 gamipress_update_p2p_achievement_types( $original_type, $new_type );
626
627 }
628 add_action( 'gamipress_update_achievement_type', 'gamipress_update_achievement_type_151', 10, 2 );
629
630 /**
631 * Change all p2p connections of one achievement type to a new type.
632 *
633 * @deprecated
634 *
635 * @since 1.0.0
636 *
637 * @param string $original_type Original achievement type.
638 * @param string $new_type New achievement type.
639 */
640 function gamipress_update_p2p_achievement_types( $original_type = '', $new_type = '' ) {
641
642 global $wpdb;
643
644 $p2p = GamiPress()->db->p2p;
645
646 $p2p_relationships = array(
647 "step-to-{$original_type}" => "step-to-{$new_type}",
648 "{$original_type}-to-step" => "{$new_type}-to-step",
649 "{$original_type}-to-points-award" => "{$new_type}-to-points-award",
650 "{$original_type}-to-points-deduct" => "{$new_type}-to-points-deduct",
651 "{$original_type}-to-rank-requirement" => "{$new_type}-to-rank-requirement",
652 );
653
654 foreach ( $p2p_relationships as $old => $new ) {
655 $wpdb->query( $wpdb->prepare( "UPDATE {$p2p} SET p2p_type = %s WHERE p2p_type = %s", $new, $old ) );
656 }
657
658 }
659
660 /**
661 * Returns achievements that may be earned when the given achievement is earned.
662 *
663 * @since 1.0.0
664 * @param integer $achievement_id The given achievement's post ID
665 * @return array An array of achievements that are dependent on the given achievement
666 */
667 function gamipress_get_dependent_achievements_old( $achievement_id = 0 ) {
668
669 global $wpdb;
670
671 // Grab the current achievement ID if none specified
672 if ( ! $achievement_id ) {
673 global $post;
674 $achievement_id = $post->ID;
675 }
676
677 $posts = GamiPress()->db->posts;
678 $postmeta = GamiPress()->db->postmeta;
679 $p2p = GamiPress()->db->p2p;
680
681 // Grab posts that can be earned by unlocking the given achievement
682 $specific_achievements = $wpdb->get_results( $wpdb->prepare(
683 "
684 SELECT *
685 FROM {$posts} as posts,
686 {$p2p} as p2p
687 WHERE posts.ID = p2p.p2p_to
688 AND p2p.p2p_from = %d
689 ",
690 $achievement_id
691 ) );
692
693 // Grab posts triggered by unlocking any/all of the given achievement's type
694 $type_achievements = $wpdb->get_results( $wpdb->prepare(
695 "
696 SELECT *
697 FROM {$posts} as posts,
698 {$postmeta} as meta
699 WHERE posts.ID = meta.post_id
700 AND meta.meta_key = '_gamipress_achievement_type'
701 AND meta.meta_value = %s
702 ",
703 gamipress_get_post_type( $achievement_id )
704 ) );
705
706 // Merge our dependent achievements together
707 $achievements = array_merge( $specific_achievements, $type_achievements );
708
709 // Available filter to modify an achievement's dependents
710 return apply_filters( 'gamipress_dependent_achievements', $achievements, $achievement_id );
711 }
712
713 /**
714 * Returns achievements that must be earned to earn given achievement.
715 *
716 * @since 1.0.0
717 * @param integer $achievement_id The given achievement's post ID
718 * @return array An array of achievements that are dependent on the given achievement
719 */
720 function gamipress_get_required_achievements_for_achievement_old( $achievement_id = 0 ) {
721
722 global $wpdb;
723
724 // Grab the current achievement ID if none specified
725 if ( ! $achievement_id ) {
726 global $post;
727 $achievement_id = $post->ID;
728 }
729
730 // Don't retrieve requirements if achievement is not earned by steps
731 if ( gamipress_get_post_meta( $achievement_id, '_gamipress_earned_by' ) !== 'triggers' )
732 return false;
733
734 $posts = GamiPress()->db->posts;
735 $p2p = GamiPress()->db->p2p;
736 $p2pmeta = GamiPress()->db->p2pmeta;
737
738 // Grab our requirements for this achievement
739 $requirements = $wpdb->get_results( $wpdb->prepare(
740 "
741 SELECT *
742 FROM $posts as posts
743 LEFT JOIN $p2p as p2p
744 ON p2p.p2p_from = posts.ID
745 LEFT JOIN $p2pmeta AS p2pmeta
746 ON p2p.p2p_id = p2pmeta.p2p_id
747 WHERE p2p.p2p_to = %d
748 AND p2pmeta.meta_key = %s
749 ORDER BY CAST( p2pmeta.meta_value as SIGNED ) ASC
750 ",
751 $achievement_id,
752 'order'
753 ) );
754
755 return $requirements;
756
757 }
758
759 /**
760 * Get requirements that by some way have been unassigned from their achievement (normally it happens if achievement has been removed but not the requirements)
761 *
762 * @deprecated Since 1.5.1, requirements has their assigned item in the post_parent field
763 *
764 * @since 1.0.6
765 *
766 * @return array|bool Array of the requirements, or false if none
767 */
768 function gamipress_get_unassigned_requirements() {
769
770 global $wpdb;
771
772 $posts = GamiPress()->db->posts;
773 $p2p = GamiPress()->db->p2p;
774
775 $requirements = $wpdb->get_results( $wpdb->prepare( "
776 SELECT p.ID
777 FROM {$posts} AS p
778 LEFT JOIN {$p2p} AS p2p
779 ON p2p.p2p_from = p.ID
780 WHERE p.post_type IN( 'points-award', 'points-deduct', 'step', 'rank-requirement' )
781 AND p2p.p2p_from IS NULL
782 ", ARRAY_A ) );
783
784 // If it has results, return them, otherwise return false
785 if ( ! empty( $requirements ) )
786 return $requirements;
787 else
788 return false;
789
790 }
791
792 /* --------------------------
793 * Rank functions
794 -------------------------- */
795
796 function gamipress_update_rank_types_151( $original_type = '', $new_type = '' ) {
797
798 // Bail if properly upgrade to required version
799 if( is_gamipress_upgraded_to( '1.5.1' ) ) {
800 return;
801 }
802
803 gamipress_update_p2p_rank_types( $original_type, $new_type );
804
805 }
806 add_action( 'gamipress_update_rank_type', 'gamipress_update_rank_types_151', 10, 2 );
807
808 /**
809 * Change all p2p connections of one rank type to a new type.
810 *
811 * @deprecated
812 *
813 * @since 1.3.1
814 *
815 * @param string $original_type Original rank type.
816 * @param string $new_type New rank type.
817 */
818 function gamipress_update_p2p_rank_types( $original_type = '', $new_type = '' ) {
819
820 global $wpdb;
821
822 $p2p = GamiPress()->db->p2p;
823
824 $p2p_relationships = array(
825 "rank-requirement-to-{$original_type}" => "rank-requirement-to-{$new_type}",
826 "{$original_type}-to-rank-requirement" => "{$new_type}-to-rank-requirement",
827 "{$original_type}-to-points-award" => "{$new_type}-to-points-award",
828 "{$original_type}-to-points-deduct" => "{$new_type}-to-points-deduct",
829 );
830
831 foreach ( $p2p_relationships as $old => $new ) {
832 $wpdb->query( $wpdb->prepare( "UPDATE $p2p SET p2p_type = %s WHERE p2p_type = %s", $new, $old ) );
833 }
834
835 }
836
837 /**
838 * Get rank requirement's rank
839 *
840 * @since 1.3.1
841 *
842 * @param integer $rank_requirement_id The given rank requirement's post ID
843 *
844 * @return object|bool The post object of the rank, or false if none
845 */
846 function gamipress_get_rank_requirement_rank_old( $rank_requirement_id = 0 ) {
847
848 // Grab the current post ID if no rank_requirement_id was specified
849 if ( ! $rank_requirement_id ) {
850 global $post;
851 $rank_requirement_id = $post->ID;
852 }
853
854 // Grab our requirement's rank
855 $ranks = gamipress_get_ranks( array( 'parent_of' => $rank_requirement_id ) );
856
857 // If it has a rank, return it, otherwise return false
858 if ( ! empty( $ranks ) )
859 return $ranks[0];
860 else
861 return false;
862
863 }
864
865 /**
866 * Return registered ranks
867 *
868 * @since 1.3.1
869 *
870 * @return array Array of ranks as WP_Post
871 */
872 function gamipress_get_ranks_old( $args = array() ) {
873
874 // Setup our defaults
875 $defaults = array(
876 'post_type' => array_merge( gamipress_get_rank_types_slugs(), gamipress_get_requirement_types_slugs() ),
877 'numberposts' => -1,
878 'orderby' => 'menu_order',
879 'suppress_filters' => false,
880 'rank_relationship' => 'any',
881 );
882
883 $args = wp_parse_args( $args, $defaults );
884
885 // Hook join functions for joining to P2P table to retrieve the parent of a rank
886 if ( isset( $args['parent_of'] ) ) {
887 add_filter( 'posts_join', 'gamipress_get_ranks_parents_join' );
888 add_filter( 'posts_where', 'gamipress_get_ranks_parents_where', 10, 2 );
889 }
890
891 // Hook join functions for joining to P2P table to retrieve the children of a rank
892 if ( isset( $args['children_of'] ) ) {
893 add_filter( 'posts_join', 'gamipress_get_ranks_children_join', 10, 2 );
894 add_filter( 'posts_where', 'gamipress_get_ranks_children_where', 10, 2 );
895 add_filter( 'posts_orderby', 'gamipress_get_ranks_children_orderby' );
896 }
897
898 // Get our ranks posts
899 $ranks = get_posts( $args );
900
901 // Remove all our filters
902 remove_filter( 'posts_join', 'gamipress_get_ranks_parents_join' );
903 remove_filter( 'posts_where', 'gamipress_get_ranks_parents_where' );
904 remove_filter( 'posts_join', 'gamipress_get_ranks_children_join' );
905 remove_filter( 'posts_where', 'gamipress_get_ranks_children_where' );
906 remove_filter( 'posts_orderby', 'gamipress_get_ranks_children_orderby' );
907
908 return $ranks;
909
910 }
911
912 /**
913 * Modify the WP_Query Join filter for rank children
914 *
915 * @deprecated
916 *
917 * @since 1.3.1
918 *
919 * @param string $join The query "join" string
920 * @param object $query_object The complete query object
921 *
922 * @return string The updated "join" string
923 */
924 function gamipress_get_ranks_children_join( $join = '', $query_object = null ) {
925
926 $posts = GamiPress()->db->posts;
927 $p2p = GamiPress()->db->p2p;
928 $p2pmeta = GamiPress()->db->p2pmeta;
929
930 $join .= " LEFT JOIN {$p2p} AS p2p ON p2p.p2p_from = {$posts}.ID";
931
932 //if ( isset( $query_object->query_vars['rank_relationship'] ) && $query_object->query_vars['rank_relationship'] != 'any' )
933 //$join .= " LEFT JOIN {$p2pmeta} AS p2pm1 ON p2pm1.p2p_id = p2p.p2p_id";
934
935 $join .= " LEFT JOIN {$p2pmeta} AS p2pm2 ON p2pm2.p2p_id = p2p.p2p_id";
936
937 return $join;
938
939 }
940
941 /**
942 * Modify the WP_Query Where filter for rank children
943 *
944 * @deprecated
945 *
946 * @since 1.3.1
947 *
948 * @param string $where The query "where" string
949 * @param object $query_object The complete query object
950 *
951 * @return string The updated query "where" string
952 */
953 function gamipress_get_ranks_children_where( $where = '', $query_object = null ) {
954
955 global $wpdb;
956
957 // ^^ add required and optional. right now just returns all ranks.
958
959 // Check for required relationship
960 //if ( isset( $query_object->query_vars['rank_relationship'] ) && $query_object->query_vars['rank_relationship'] == 'required' )
961 //$where .= " AND p2pm1.meta_key ='Required'";
962
963 // Check for optional relationship
964 //if ( isset( $query_object->query_vars['rank_relationship'] ) && $query_object->query_vars['rank_relationship'] == 'optional' )
965 //$where .= " AND p2pm1.meta_key ='Optional'";
966
967
968 // Filter by order meta
969 $where .= " AND p2pm2.meta_key ='order'";
970
971 $where .= $wpdb->prepare( ' AND p2p.p2p_to = %d', $query_object->query_vars['children_of'] );
972
973 return $where;
974
975 }
976
977 /**
978 * Modify the WP_Query OrderBy filter for rank children
979 *
980 * @deprecated
981 *
982 * @since 1.3.1
983 *
984 * @param string $orderby The query "orderby" string
985 *
986 * @return string The updated "orderby" string
987 */
988 function gamipress_get_ranks_children_orderby( $orderby = '' ) {
989
990 return $orderby = 'p2pm2.meta_value ASC';
991
992 }
993
994 /**
995 * Modify the WP_Query Join filter for rank parents
996 *
997 * @deprecated
998 *
999 * @since 1.3.1
1000 *
1001 * @param string $join The query "join" string
1002 *
1003 * @return string The updated "join" string
1004 */
1005 function gamipress_get_ranks_parents_join( $join = '' ) {
1006
1007 $posts = GamiPress()->db->posts;
1008 $p2p = GamiPress()->db->p2p;
1009
1010 $join .= " LEFT JOIN {$p2p} AS p2p ON p2p.p2p_to = {$posts}.ID";
1011
1012 return $join;
1013
1014 }
1015
1016 /**
1017 * Modify the WP_Query Where filter for rank parents
1018 *
1019 * @deprecated
1020 *
1021 * @since 1.3.1
1022 *
1023 * @param string $where The query "where" string
1024 * @param object $query_object The complete query object
1025 *
1026 * @return string appended sql where statement
1027 */
1028 function gamipress_get_ranks_parents_where( $where = '', $query_object = null ) {
1029
1030 global $wpdb;
1031
1032 $where .= $wpdb->prepare( ' AND p2p.p2p_from = %d', $query_object->query_vars['parent_of'] );
1033
1034 return $where;
1035
1036 }