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