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
gamipress / integrations / woocommerce / includes / rules-engine.php

rules-engine.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 8.0.5, at integrations/woocommerce/includes/rules-engine.php

163 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Rules Engine
4 *
5 * @package GamiPress\WooCommerce\Rules_Engine
6 * @since 1.1.3
7 */
8 // Exit if accessed directly
9 if( !defined( 'ABSPATH' ) ) exit;
10
11 /**
12 * Checks if given requirement meets the requirements of triggered event
13 *
14 * @since 1.2.5
15 *
16 * @param int $requirement_id
17 * @param string $trigger
18 * @param array $args
19 *
20 * @return bool
21 */
22 function gamipress_wc_check_if_meets_requirements( $requirement_id, $trigger, $args ) {
23
24 // Initialize the return value
25 $return = true;
26
27 // If is purchase total trigger, rules engine needs to check the condition
28 if( $trigger === 'gamipress_wc_new_purchase_total' ) {
29
30 $order_total = floatval( $args[2] );
31
32 $condition = gamipress_get_post_meta( $requirement_id, '_gamipress_wc_purchase_total_condition', true );
33 $required_value = floatval( gamipress_get_post_meta( $requirement_id, '_gamipress_wc_purchase_total', true ) );
34
35 // True if purchase total matches the condition
36 $return = (bool) ( gamipress_number_condition_matches( $order_total, $required_value, $condition ) );
37 }
38
39 // If is product variation trigger, rules engine needs to check the variation ID
40 if( $trigger === 'gamipress_wc_product_variation_purchase'
41 || $trigger === 'gamipress_wc_product_variation_refund'
42 || $trigger === 'gamipress_wc_specific_subscription_variation_purchase'
43 || $trigger === 'gamipress_wc_specific_subscription_variation_renewal'
44 || $trigger === 'gamipress_wc_specific_subscription_variation_cancelled'
45 || $trigger === 'gamipress_wc_specific_subscription_variation_expired' ) {
46
47 $variation_id = absint( $args[2] );
48 $required_variation_id = absint( gamipress_get_post_meta( $requirement_id, '_gamipress_wc_variation_id', true ) );
49
50 // True if is the correct variation ID
51 $return = (bool) ( $variation_id === $required_variation_id );
52 }
53
54 // If is product category trigger, rules engine needs to check the category ID
55 if( $trigger === 'gamipress_wc_product_category_purchase' || $trigger === 'gamipress_wc_product_category_refund' ) {
56
57 $category_id = absint( $args[2] );
58 $required_category_id = absint( gamipress_get_post_meta( $requirement_id, '_gamipress_wc_category_id', true ) );
59
60 // True if is the correct category ID
61 $return = (bool) ( $required_category_id === $category_id );
62 }
63
64 // If is product tag trigger, rules engine needs to check the tag ID
65 if( $trigger === 'gamipress_wc_product_tag_purchase' || $trigger === 'gamipress_wc_product_tag_refund' ) {
66
67 $tag_id = absint( $args[2] );
68 $required_tag_id = absint( gamipress_get_post_meta( $requirement_id, '_gamipress_wc_tag_id', true ) );
69
70 // True if is the correct tag ID
71 $return = (bool) ( $required_tag_id === $tag_id );
72 }
73
74 // If is lifetime value trigger, rules engine needs to check the condition
75 if( $trigger === 'gamipress_wc_lifetime_value' ) {
76
77 $lifetime = floatval( $args[0] );
78
79 $condition = gamipress_get_post_meta( $requirement_id, '_gamipress_wc_lifetime_condition', true );
80 $required_value = floatval( gamipress_get_post_meta( $requirement_id, '_gamipress_wc_lifetime', true ) );
81
82 // True if lifetime value matches condition
83 $return = (bool) ( gamipress_number_condition_matches( $lifetime, $required_value, $condition ) );
84 }
85
86 // Subscription months active
87 if( $trigger === 'gamipress_wc_subscription_months_active' || $trigger === 'gamipress_wc_specific_subscription_months_active' ) {
88
89 $months_old = absint( $args[3] );
90
91 $required_months_old = absint( gamipress_get_post_meta( $requirement_id, '_gamipress_wc_subscription_months', true ) );
92
93 // True if there is months old is equal to required months old
94 $return = (bool) ( $months_old >= $required_months_old );
95 }
96
97 return $return;
98
99 }
100
101 /**
102 * Filter triggered requirements to reduce the number of requirements to check by the awards engine
103 *
104 * @since 1.2.5
105 *
106 * @param array $triggered_requirements
107 * @param integer $user_id
108 * @param string $trigger
109 * @param integer $site_id
110 * @param array $args
111 *
112 * @return array
113 */
114 function gamipress_wc_filter_triggered_requirements( $triggered_requirements, $user_id, $trigger, $site_id, $args ) {
115
116 $new_requirements = array();
117
118 foreach( $triggered_requirements as $i => $requirement ) {
119
120 // Skip item
121 if( ! gamipress_wc_check_if_meets_requirements( $requirement->ID, $trigger, $args ) ) {
122 continue;
123 }
124
125 // Keep the requirement on the list of requirements to check by the awards engine
126 $new_requirements[] = $requirement;
127
128 }
129
130 return $new_requirements;
131
132 }
133 add_filter( 'gamipress_get_triggered_requirements', 'gamipress_wc_filter_triggered_requirements', 20, 5 );
134
135
136 /**
137 * Checks if an user is allowed to work on a given requirement
138 *
139 * @since 1.1.3
140 *
141 * @param bool $return The default return value
142 * @param int $user_id The given user's ID
143 * @param int $requirement_id The given requirement's post ID
144 * @param string $trigger The trigger triggered
145 * @param int $site_id The site id
146 * @param array $args Arguments of this trigger
147 *
148 * @return bool True if user has access to the requirement, false otherwise
149 */
150 function gamipress_wc_user_has_access_to_achievement( $return = false, $user_id = 0, $requirement_id = 0, $trigger = '', $site_id = 0, $args = array() ) {
151
152 // If we're not working with a requirement, bail here
153 if ( ! in_array( get_post_type( $requirement_id ), gamipress_get_requirement_types_slugs() ) )
154 return $return;
155
156 // Check if user has access to the achievement ($return will be false if user has exceed the limit or achievement is not published yet)
157 if( ! $return )
158 return $return;
159
160 // Send back our eligibility
161 return gamipress_wc_check_if_meets_requirements( $requirement_id, $trigger, $args );
162 }
163 add_filter( 'user_has_access_to_achievement', 'gamipress_wc_user_has_access_to_achievement', 10, 6 );