PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.2
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.2
8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 7.8.7 7.8.6 7.8.5 All 44 releases
gamipress / integrations / formidable-forms / includes / rules-engine.php

rules-engine.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 8.0.2, at integrations/formidable-forms/includes/rules-engine.php

115 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Rules Engine
4 *
5 * @package GamiPress\Formidable_Forms\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_frm_check_if_meets_requirements( $requirement_id, $trigger, $args ) {
23
24 // Initialize the return value
25 $return = true;
26
27 // If is between score trigger, rules engine needs to check if field name and values matches required ones
28 if( $return && ( $trigger === 'gamipress_frm_field_value_submission'
29 || $trigger === 'gamipress_frm_specific_field_value_submission' ) ) {
30
31 $field_name = $args[2];
32 $field_value = $args[3];
33
34 $required_field_name = get_post_meta( $requirement_id, '_gamipress_frm_field_name', true );
35 $required_field_value = get_post_meta( $requirement_id, '_gamipress_frm_field_value', true );
36
37 // First, check if field name matches the required one
38 $return = (bool) ( $field_name === $required_field_name );
39
40 if( $return ) {
41 // Check if field value matches the required one (with support for arrays)
42 if( is_array( $field_value ) )
43 $return = (bool) ( in_array( $required_field_value, $field_value ) );
44 else
45 $return = (bool) ( $field_value === $required_field_value );
46 }
47 }
48
49 return $return;
50
51 }
52
53 /**
54 * Filter triggered requirements to reduce the number of requirements to check by the awards engine
55 *
56 * @since 1.2.5
57 *
58 * @param array $triggered_requirements
59 * @param integer $user_id
60 * @param string $trigger
61 * @param integer $site_id
62 * @param array $args
63 *
64 * @return array
65 */
66 function gamipress_frm_filter_triggered_requirements( $triggered_requirements, $user_id, $trigger, $site_id, $args ) {
67
68 $new_requirements = array();
69
70 foreach( $triggered_requirements as $i => $requirement ) {
71
72 // Skip item
73 if( ! gamipress_frm_check_if_meets_requirements( $requirement->ID, $trigger, $args ) ) {
74 continue;
75 }
76
77 // Keep the requirement on the list of requirements to check by the awards engine
78 $new_requirements[] = $requirement;
79
80 }
81
82 return $new_requirements;
83
84 }
85 add_filter( 'gamipress_get_triggered_requirements', 'gamipress_frm_filter_triggered_requirements', 20, 5 );
86
87 /**
88 * Checks if an user is allowed to work on a given requirement related to a minimum of score
89 *
90 * @since 1.0.0
91 *
92 * @param bool $return The default return value
93 * @param int $user_id The given user's ID
94 * @param int $requirement_id The given requirement's post ID
95 * @param string $trigger The trigger triggered
96 * @param int $site_id The site id
97 * @param array $args Arguments of this trigger
98 *
99 * @return bool True if user has access to the requirement, false otherwise
100 */
101 function gamipress_frm_user_has_access_to_achievement( $return = false, $user_id = 0, $requirement_id = 0, $trigger = '', $site_id = 0, $args = array() ) {
102
103 // If we're not working with a requirement, bail here
104 if ( ! in_array( get_post_type( $requirement_id ), gamipress_get_requirement_types_slugs() ) )
105 return $return;
106
107 // Check if user has access to the achievement ($return will be false if user has exceed the limit or achievement is not published yet)
108 if( ! $return )
109 return $return;
110
111 // Send back our eligibility
112 return gamipress_frm_check_if_meets_requirements( $requirement_id, $trigger, $args );
113
114 }
115 add_filter( 'user_has_access_to_achievement', 'gamipress_frm_user_has_access_to_achievement', 10, 6 );