PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.5
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.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
← All changes | integrations/easy-digital-downloads/includes/rules-engine.php +133 -0 7.9.2 → 8.0.5 View file →
@@ -1,0 +1,133 @@
1 +<?php
2 +/**
3 + * Rules Engine
4 + *
5 + * @package GamiPress\Easy_Digital_Downloads\Rules_Engine
6 + * @since 1.1.2
7 + */
8 +// Exit if accessed directly
9 +if( !defined( 'ABSPATH' ) ) exit;
10 +/**
11 + * Checks if given requirement meets the requirements of triggered event
12 + *
13 + * @since 1.2.5
14 + *
15 + * @param int $requirement_id
16 + * @param string $trigger
17 + * @param array $args
18 + *
19 + * @return bool
20 + */
21 +function gamipress_edd_check_if_meets_requirements( $requirement_id, $trigger, $args ) {
22 +
23 + // Initialize the return value
24 + $return = true;
25 +
26 + // If is minimum score trigger, rules engine needs to check the minimum score
27 + if( $trigger === 'gamipress_edd_lifetime_value' ) {
28 +
29 + $lifetime = absint( $args[0] );
30 +
31 + $condition = get_post_meta( $requirement_id, '_gamipress_edd_lifetime_condition', true );
32 + $required_value = absint( get_post_meta( $requirement_id, '_gamipress_edd_lifetime', true ) );
33 +
34 + // True if there is score is bigger than required score
35 + $return = (bool) ( gamipress_number_condition_matches( $lifetime, $required_value, $condition ) );
36 + }
37 +
38 + return $return;
39 +
40 +}
41 +
42 +/**
43 + * Filter triggered requirements to reduce the number of requirements to check by the awards engine
44 + *
45 + * @since 1.2.5
46 + *
47 + * @param array $triggered_requirements
48 + * @param integer $user_id
49 + * @param string $trigger
50 + * @param integer $site_id
51 + * @param array $args
52 + *
53 + * @return array
54 + */
55 +function gamipress_edd_filter_triggered_requirements( $triggered_requirements, $user_id, $trigger, $site_id, $args ) {
56 +
57 + $new_requirements = array();
58 +
59 + foreach( $triggered_requirements as $i => $requirement ) {
60 +
61 + // Skip item
62 + if( ! gamipress_edd_check_if_meets_requirements( $requirement->ID, $trigger, $args ) ) {
63 + continue;
64 + }
65 +
66 + // Keep the requirement on the list of requirements to check by the awards engine
67 + $new_requirements[] = $requirement;
68 +
69 + }
70 +
71 + return $new_requirements;
72 +
73 +}
74 +add_filter( 'gamipress_get_triggered_requirements', 'gamipress_edd_filter_triggered_requirements', 20, 5 );
75 +
76 +/**
77 + * Checks if an user is allowed to work on a given requirement
78 + *
79 + * @since 1.1.2
80 + *
81 + * @param bool $return The default return value
82 + * @param int $user_id The given user's ID
83 + * @param int $requirement_id The given requirement's post ID
84 + * @param string $trigger The trigger triggered
85 + * @param int $site_id The site id
86 + * @param array $args Arguments of this trigger
87 + *
88 + * @return bool True if user has access to the requirement, false otherwise
89 + */
90 +function gamipress_edd_user_has_access_to_achievement( $return = false, $user_id = 0, $requirement_id = 0, $trigger = '', $site_id = 0, $args = array() ) {
91 +
92 + // If we're not working with a requirement, bail here
93 + if ( ! in_array( get_post_type( $requirement_id ), gamipress_get_requirement_types_slugs() ) )
94 + return $return;
95 +
96 + // Check if user has access to the achievement ($return will be false if user has exceed the limit or achievement is not published yet)
97 + if( ! $return )
98 + return $return;
99 +
100 + // If is download variation trigger, rules engine needs to check the variation ID
101 + if( $trigger === 'gamipress_edd_download_variation_purchase' || $trigger === 'gamipress_edd_download_variation_refund' ) {
102 +
103 + $variation_id = absint( $args[2] );
104 + $required_variation_id = absint( get_post_meta( $requirement_id, '_gamipress_edd_variation_id', true ) );
105 +
106 + // True if is the correct variation ID
107 + $return = (bool) ( $variation_id === $required_variation_id );
108 + }
109 +
110 + // If is download category trigger, rules engine needs to check the category ID
111 + if( $trigger === 'gamipress_edd_download_category_purchase' || $trigger === 'gamipress_edd_download_category_refund' ) {
112 +
113 + $category_id = absint( $args[2] );
114 + $required_category_id = absint( get_post_meta( $requirement_id, '_gamipress_edd_category_id', true ) );
115 +
116 + // True if is the correct category ID
117 + $return = (bool) ( $required_category_id === $category_id );
118 + }
119 +
120 + // If is download tag trigger, rules engine needs to check the tag ID
121 + if( $trigger === 'gamipress_edd_download_tag_purchase' || $trigger === 'gamipress_edd_download_tag_refund' ) {
122 +
123 + $tag_id = absint( $args[2] );
124 + $required_tag_id = absint( get_post_meta( $requirement_id, '_gamipress_edd_tag_id', true ) );
125 +
126 + // True if is the correct tag ID
127 + $return = (bool) ( $required_tag_id === $tag_id );
128 + }
129 +
130 + // Send back our eligibility
131 + return $return;
132 +}
133 +add_filter( 'user_has_access_to_achievement', 'gamipress_edd_user_has_access_to_achievement', 10, 6 );