| @@ -1,0 +1,61 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Rules Engine | |
| 4 | + * | |
| 5 | + * @package GamiPress\WP_Forms\Rules_Engine | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | +// Exit if accessed directly | |
| 9 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * Checks if an user is allowed to work on a given requirement | |
| 13 | + * | |
| 14 | + * @since 1.0.0 | |
| 15 | + * | |
| 16 | + * @param bool $return The default return value | |
| 17 | + * @param int $user_id The given user's ID | |
| 18 | + * @param int $requirement_id The given requirement's post ID | |
| 19 | + * @param string $trigger The trigger triggered | |
| 20 | + * @param int $site_id The site id | |
| 21 | + * @param array $args Arguments of this trigger | |
| 22 | + * | |
| 23 | + * @return bool True if user has access to the requirement, false otherwise | |
| 24 | + */ | |
| 25 | +function gamipress_wp_forms_user_has_access_to_achievement( $return = false, $user_id = 0, $requirement_id = 0, $trigger = '', $site_id = 0, $args = array() ) { | |
| 26 | + | |
| 27 | + // If we're not working with a requirement, bail here | |
| 28 | + if ( ! in_array( get_post_type( $requirement_id ), gamipress_get_requirement_types_slugs() ) ) | |
| 29 | + return $return; | |
| 30 | + | |
| 31 | + // Check if user has access to the achievement ($return will be false if user has exceed the limit or achievement is not published yet) | |
| 32 | + if( ! $return ) | |
| 33 | + return $return; | |
| 34 | + | |
| 35 | + // If is between score trigger, rules engine needs to check if field name and values matches required ones | |
| 36 | + if( $return && ( $trigger === 'gamipress_wp_forms_field_value_submission' | |
| 37 | + || $trigger === 'gamipress_wp_forms_specific_field_value_submission' ) ) { | |
| 38 | + | |
| 39 | + $field_name = $args[2]; | |
| 40 | + $field_value = $args[3]; | |
| 41 | + | |
| 42 | + $required_field_name = get_post_meta( $requirement_id, '_gamipress_wp_forms_field_name', true ); | |
| 43 | + $required_field_value = get_post_meta( $requirement_id, '_gamipress_wp_forms_field_value', true ); | |
| 44 | + | |
| 45 | + // First, check if field name matches the required one | |
| 46 | + $return = (bool) ( $field_name === $required_field_name ); | |
| 47 | + | |
| 48 | + if( $return ) { | |
| 49 | + // Check if field value matches the required one (with support for arrays) | |
| 50 | + if( is_array( $field_value ) ) | |
| 51 | + $return = (bool) ( in_array( $required_field_value, $field_value ) ); | |
| 52 | + else | |
| 53 | + $return = (bool) ( $field_value === $required_field_value ); | |
| 54 | + } | |
| 55 | + } | |
| 56 | + | |
| 57 | + // Send back our eligibility | |
| 58 | + return $return; | |
| 59 | + | |
| 60 | +} | |
| 61 | +add_filter( 'user_has_access_to_achievement', 'gamipress_wp_forms_user_has_access_to_achievement', 10, 6 ); | |