| 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 |
return $return; |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Filter triggered requirements to reduce the number of requirements to check by the awards engine |
| 92 |
* |
| 93 |
* @since 1.2.5 |
| 94 |
* |
| 95 |
* @param array $triggered_requirements |
| 96 |
* @param integer $user_id |
| 97 |
* @param string $trigger |
| 98 |
* @param integer $site_id |
| 99 |
* @param array $args |
| 100 |
* |
| 101 |
* @return array |
| 102 |
*/ |
| 103 |
function gamipress_wc_filter_triggered_requirements( $triggered_requirements, $user_id, $trigger, $site_id, $args ) { |
| 104 |
|
| 105 |
$new_requirements = array(); |
| 106 |
|
| 107 |
foreach( $triggered_requirements as $i => $requirement ) { |
| 108 |
|
| 109 |
// Skip item |
| 110 |
if( ! gamipress_wc_check_if_meets_requirements( $requirement->ID, $trigger, $args ) ) { |
| 111 |
continue; |
| 112 |
} |
| 113 |
|
| 114 |
// Keep the requirement on the list of requirements to check by the awards engine |
| 115 |
$new_requirements[] = $requirement; |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
return $new_requirements; |
| 120 |
|
| 121 |
} |
| 122 |
add_filter( 'gamipress_get_triggered_requirements', 'gamipress_wc_filter_triggered_requirements', 20, 5 ); |
| 123 |
|
| 124 |
|
| 125 |
/** |
| 126 |
* Checks if an user is allowed to work on a given requirement |
| 127 |
* |
| 128 |
* @since 1.1.3 |
| 129 |
* |
| 130 |
* @param bool $return The default return value |
| 131 |
* @param int $user_id The given user's ID |
| 132 |
* @param int $requirement_id The given requirement's post ID |
| 133 |
* @param string $trigger The trigger triggered |
| 134 |
* @param int $site_id The site id |
| 135 |
* @param array $args Arguments of this trigger |
| 136 |
* |
| 137 |
* @return bool True if user has access to the requirement, false otherwise |
| 138 |
*/ |
| 139 |
function gamipress_wc_user_has_access_to_achievement( $return = false, $user_id = 0, $requirement_id = 0, $trigger = '', $site_id = 0, $args = array() ) { |
| 140 |
|
| 141 |
// If we're not working with a requirement, bail here |
| 142 |
if ( ! in_array( get_post_type( $requirement_id ), gamipress_get_requirement_types_slugs() ) ) |
| 143 |
return $return; |
| 144 |
|
| 145 |
// Check if user has access to the achievement ($return will be false if user has exceed the limit or achievement is not published yet) |
| 146 |
if( ! $return ) |
| 147 |
return $return; |
| 148 |
|
| 149 |
// Send back our eligibility |
| 150 |
return gamipress_wc_check_if_meets_requirements( $requirement_id, $trigger, $args ); |
| 151 |
} |
| 152 |
add_filter( 'user_has_access_to_achievement', 'gamipress_wc_user_has_access_to_achievement', 10, 6 ); |