← All changes
|
integrations/wishlist-member/includes/rules-engine.php
+113
-0
7.9.2
→
8.0.5
View file →
| @@ -1,0 +1,113 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Rules Engine | |
| 4 | + * | |
| 5 | + * @package GamiPress\WishList_Member\Rules_Engine | |
| 6 | + * @since 1.0.0 | |
| 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.1.2 | |
| 15 | + * | |
| 16 | + * @param int $requirement_id | |
| 17 | + * @param string $trigger | |
| 18 | + * @param array $args | |
| 19 | + * | |
| 20 | + * @return bool | |
| 21 | + */ | |
| 22 | +function gamipress_wishlist_member_check_if_meets_requirements( $requirement_id, $trigger, $args ) { | |
| 23 | + | |
| 24 | + // Initialize the return value | |
| 25 | + $return = true; | |
| 26 | + | |
| 27 | + // Check if given id match with specific id | |
| 28 | + if( in_array( $trigger, array( | |
| 29 | + 'gamipress_wishlist_member_add_specific_level', | |
| 30 | + 'gamipress_wishlist_member_remove_specific_level', | |
| 31 | + 'gamipress_wishlist_member_approve_specific_level', | |
| 32 | + 'gamipress_wishlist_member_unapprove_specific_level', | |
| 33 | + 'gamipress_wishlist_member_cancel_specific_level', | |
| 34 | + 'gamipress_wishlist_member_uncancel_specific_level', | |
| 35 | + ) ) ) { | |
| 36 | + | |
| 37 | + $id = $args[0]; | |
| 38 | + $required_id = gamipress_get_post_meta( $requirement_id, '_gamipress_wishlist_member_level_id', true ); | |
| 39 | + | |
| 40 | + $return = (bool) ( | |
| 41 | + $id !== '' | |
| 42 | + && $required_id !== '' | |
| 43 | + && $id === $required_id ); | |
| 44 | + } | |
| 45 | + | |
| 46 | + return $return; | |
| 47 | + | |
| 48 | +} | |
| 49 | + | |
| 50 | +/** | |
| 51 | + * Filter triggered requirements to reduce the number of requirements to check by the awards engine | |
| 52 | + * | |
| 53 | + * @since 1.1.2 | |
| 54 | + * | |
| 55 | + * @param array $triggered_requirements | |
| 56 | + * @param integer $user_id | |
| 57 | + * @param string $trigger | |
| 58 | + * @param integer $site_id | |
| 59 | + * @param array $args | |
| 60 | + * | |
| 61 | + * @return array | |
| 62 | + */ | |
| 63 | +function gamipress_wishlist_member_filter_triggered_requirements( $triggered_requirements, $user_id, $trigger, $site_id, $args ) { | |
| 64 | + | |
| 65 | + $new_requirements = array(); | |
| 66 | + | |
| 67 | + foreach( $triggered_requirements as $i => $requirement ) { | |
| 68 | + | |
| 69 | + // Skip item | |
| 70 | + if( ! gamipress_wishlist_member_check_if_meets_requirements( $requirement->ID, $trigger, $args ) ) { | |
| 71 | + continue; | |
| 72 | + } | |
| 73 | + | |
| 74 | + // Keep the requirement on the list of requirements to check by the awards engine | |
| 75 | + $new_requirements[] = $requirement; | |
| 76 | + | |
| 77 | + } | |
| 78 | + | |
| 79 | + return $new_requirements; | |
| 80 | + | |
| 81 | +} | |
| 82 | +add_filter( 'gamipress_get_triggered_requirements', 'gamipress_wishlist_member_filter_triggered_requirements', 20, 5 ); | |
| 83 | + | |
| 84 | +/** | |
| 85 | + * Checks if an user is allowed to work on a given requirement related to a specific link click | |
| 86 | + * | |
| 87 | + * @since 1.0.0 | |
| 88 | + * | |
| 89 | + * @param bool $return The default return value | |
| 90 | + * @param int $user_id The given user's ID | |
| 91 | + * @param int $requirement_id The given requirement's post ID | |
| 92 | + * @param string $trigger The trigger triggered | |
| 93 | + * @param int $site_id The site id | |
| 94 | + * @param array $args Arguments of this trigger | |
| 95 | + * | |
| 96 | + * @return bool True if user has access to the requirement, false otherwise | |
| 97 | + */ | |
| 98 | +function gamipress_wishlist_member_user_has_access_to_achievement( $return = false, $user_id = 0, $requirement_id = 0, $trigger = '', $site_id = 0, $args = array() ) { | |
| 99 | + | |
| 100 | + // If we're not working with a requirement, bail here | |
| 101 | + if ( ! in_array( gamipress_get_post_type( $requirement_id ), gamipress_get_requirement_types_slugs() ) ) { | |
| 102 | + return $return; | |
| 103 | + } | |
| 104 | + | |
| 105 | + // Check if user has access to the achievement ($return will be false if user has exceed the limit or achievement is not published yet) | |
| 106 | + if( ! $return ) { | |
| 107 | + return $return; | |
| 108 | + } | |
| 109 | + | |
| 110 | + // Send back our eligibility | |
| 111 | + return gamipress_wishlist_member_check_if_meets_requirements( $requirement_id, $trigger, $args ); | |
| 112 | +} | |
| 113 | +add_filter( 'user_has_access_to_achievement', 'gamipress_wishlist_member_user_has_access_to_achievement', 10, 6 ); | |