| @@ -1,0 +1,135 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Rules Engine | |
| 4 | + * | |
| 5 | + * @package GamiPress\BBForms\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.0.0 | |
| 15 | + * | |
| 16 | + * @param int $requirement_id | |
| 17 | + * @param string $trigger | |
| 18 | + * @param array $args | |
| 19 | + * | |
| 20 | + * @return bool | |
| 21 | + */ | |
| 22 | +function gamipress_bbforms_check_if_meets_requirements( $requirement_id, $trigger, $args ) { | |
| 23 | + | |
| 24 | + // Initialize the return value | |
| 25 | + $return = true; | |
| 26 | + | |
| 27 | + // If is specific category trigger | |
| 28 | + if( $trigger === 'gamipress_bbforms_form_specific_category_submission' ) { | |
| 29 | + | |
| 30 | + $category_id = $args[2]; | |
| 31 | + $required_category_id = get_post_meta( $requirement_id, '_gamipress_bbforms_category', true ); | |
| 32 | + | |
| 33 | + // True if there is a specific category, an attached form and both are equal | |
| 34 | + $return = (bool) ( $category_id === $required_category_id ); | |
| 35 | + } | |
| 36 | + | |
| 37 | + // If is specific tag trigger | |
| 38 | + if( $trigger === 'gamipress_bbforms_form_specific_tag_submission' ) { | |
| 39 | + | |
| 40 | + $tag_id = $args[2]; | |
| 41 | + $required_tag_id = get_post_meta( $requirement_id, '_gamipress_bbforms_tag', true ); | |
| 42 | + | |
| 43 | + // True if there is a specific tag, an attached form and both are equal | |
| 44 | + $return = (bool) ( $tag_id === $required_tag_id ); | |
| 45 | + } | |
| 46 | + | |
| 47 | + if( $return && ( $trigger === 'gamipress_bbforms_field_value_submission' | |
| 48 | + || $trigger === 'gamipress_bbforms_specific_field_value_submission' ) ) { | |
| 49 | + | |
| 50 | + $field_name = $args[2]; | |
| 51 | + $field_value = $args[3]; | |
| 52 | + | |
| 53 | + $required_field_name = gamipress_get_post_meta( $requirement_id, '_gamipress_bbforms_field_name', true ); | |
| 54 | + $required_field_value = gamipress_get_post_meta( $requirement_id, '_gamipress_bbforms_field_value', true ); | |
| 55 | + | |
| 56 | + // First, check if field name matches the required one | |
| 57 | + $return = (bool) ( $field_name === $required_field_name ); | |
| 58 | + | |
| 59 | + if( $return ) { | |
| 60 | + // Check if field value matches the required one (with support for arrays) | |
| 61 | + if( is_array( $field_value ) ) | |
| 62 | + $return = (bool) ( in_array( $required_field_value, $field_value ) ); | |
| 63 | + else | |
| 64 | + $return = (bool) ( $field_value === $required_field_value ); | |
| 65 | + } | |
| 66 | + } | |
| 67 | + | |
| 68 | + // Send back our eligibility | |
| 69 | + return $return; | |
| 70 | +} | |
| 71 | +add_filter( 'user_has_access_to_achievement', 'gamipress_bbforms_user_has_access_to_achievement', 10, 6 ); | |
| 72 | + | |
| 73 | +/** | |
| 74 | + * Filter triggered requirements to reduce the number of requirements to check by the awards engine | |
| 75 | + * | |
| 76 | + * @since 1.0.0 | |
| 77 | + * | |
| 78 | + * @param array $triggered_requirements | |
| 79 | + * @param integer $user_id | |
| 80 | + * @param string $trigger | |
| 81 | + * @param integer $site_id | |
| 82 | + * @param array $args | |
| 83 | + * | |
| 84 | + * @return array | |
| 85 | + */ | |
| 86 | +function gamipress_bbforms_filter_triggered_requirements( $triggered_requirements, $user_id, $trigger, $site_id, $args ) { | |
| 87 | + | |
| 88 | + $new_requirements = array(); | |
| 89 | + | |
| 90 | + foreach( $triggered_requirements as $i => $requirement ) { | |
| 91 | + | |
| 92 | + // Skip item | |
| 93 | + if( ! gamipress_bbforms_check_if_meets_requirements( $requirement->ID, $trigger, $args ) ) { | |
| 94 | + continue; | |
| 95 | + } | |
| 96 | + | |
| 97 | + // Keep the requirement on the list of requirements to check by the awards engine | |
| 98 | + $new_requirements[] = $requirement; | |
| 99 | + | |
| 100 | + } | |
| 101 | + | |
| 102 | + return $new_requirements; | |
| 103 | + | |
| 104 | +} | |
| 105 | +add_filter( 'gamipress_get_triggered_requirements', 'gamipress_bbforms_filter_triggered_requirements', 20, 5 ); | |
| 106 | + | |
| 107 | +/** | |
| 108 | + * Checks if an user is allowed to work on a given requirement related to a minimum of score | |
| 109 | + * | |
| 110 | + * @since 1.0.0 | |
| 111 | + * | |
| 112 | + * @param bool $return The default return value | |
| 113 | + * @param int $user_id The given user's ID | |
| 114 | + * @param int $requirement_id The given requirement's post ID | |
| 115 | + * @param string $trigger The trigger triggered | |
| 116 | + * @param int $site_id The site id | |
| 117 | + * @param array $args Arguments of this trigger | |
| 118 | + * | |
| 119 | + * @return bool True if user has access to the requirement, false otherwise | |
| 120 | + */ | |
| 121 | +function gamipress_bbforms_user_has_access_to_achievement( $return = false, $user_id = 0, $requirement_id = 0, $trigger = '', $site_id = 0, $args = array() ) { | |
| 122 | + | |
| 123 | + // If we're not working with a requirement, bail here | |
| 124 | + if ( ! in_array( get_post_type( $requirement_id ), gamipress_get_requirement_types_slugs() ) ) | |
| 125 | + return $return; | |
| 126 | + | |
| 127 | + // Check if user has access to the achievement ($return will be false if user has exceed the limit or achievement is not published yet) | |
| 128 | + if( ! $return ) | |
| 129 | + return $return; | |
| 130 | + | |
| 131 | + // Send back our eligibility | |
| 132 | + return gamipress_bbforms_check_if_meets_requirements( $requirement_id, $trigger, $args ); | |
| 133 | + | |
| 134 | +} | |
| 135 | +add_filter( 'user_has_access_to_achievement', 'gamipress_bbforms_user_has_access_to_achievement', 10, 6 ); | |