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 / divi / 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/divi/includes/rules-engine.php

127 lines 4.2 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\Divi\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_divi_check_if_meets_requirements( $requirement_id, $trigger, $args ) {
23
24 // Initialize the return value
25 $return = true;
26
27 // Check form name
28 if( $trigger === 'gamipress_divi_specific_new_form_submission'
29 || $trigger === 'gamipress_divi_specific_field_value_submission' ) {
30
31 $form_id = $args[0];
32
33 $required_form_id = get_post_meta( $requirement_id, '_gamipress_divi_form_id', true );
34
35 // Check if form name matches the required one
36 $return = (bool) ( $form_id === $required_form_id );
37 }
38
39 // Check field name and value
40 if( $return && $trigger === 'gamipress_divi_field_value_submission'
41 || $trigger === 'gamipress_divi_specific_field_value_submission' ) {
42
43 $field_name = $args[2];
44 $field_value = $args[3];
45
46 $required_field_name = get_post_meta( $requirement_id, '_gamipress_divi_field_name', true );
47 $required_field_value = get_post_meta( $requirement_id, '_gamipress_divi_field_value', true );
48
49 // First, check if field name matches the required one
50 $return = (bool) ( $field_name === $required_field_name );
51
52 if( $return ) {
53 // Check if field value matches the required one (with support for arrays)
54 if( is_array( $field_value ) )
55 $return = (bool) ( in_array( $required_field_value, $field_value ) );
56 else
57 $return = (bool) ( $field_value === $required_field_value );
58 }
59 }
60
61 return $return;
62
63 }
64
65 /**
66 * Filter triggered requirements to reduce the number of requirements to check by the awards engine
67 *
68 * @since 1.0.0
69 *
70 * @param array $triggered_requirements
71 * @param integer $user_id
72 * @param string $trigger
73 * @param integer $site_id
74 * @param array $args
75 *
76 * @return array
77 */
78 function gamipress_divi_filter_triggered_requirements( $triggered_requirements, $user_id, $trigger, $site_id, $args ) {
79
80 $new_requirements = array();
81
82 foreach( $triggered_requirements as $i => $requirement ) {
83
84 // Skip item
85 if( ! gamipress_divi_check_if_meets_requirements( $requirement->ID, $trigger, $args ) ) {
86 continue;
87 }
88
89 // Keep the requirement on the list of requirements to check by the awards engine
90 $new_requirements[] = $requirement;
91
92 }
93
94 return $new_requirements;
95
96 }
97 add_filter( 'gamipress_get_triggered_requirements', 'gamipress_divi_filter_triggered_requirements', 20, 5 );
98
99 /**
100 * Checks if an user is allowed to work on a given requirement related to a minimum of score
101 *
102 * @since 1.0.0
103 *
104 * @param bool $return The default return value
105 * @param int $user_id The given user's ID
106 * @param int $requirement_id The given requirement's post ID
107 * @param string $trigger The trigger triggered
108 * @param int $site_id The site id
109 * @param array $args Arguments of this trigger
110 *
111 * @return bool True if user has access to the requirement, false otherwise
112 */
113 function gamipress_divi_user_has_access_to_achievement( $return = false, $user_id = 0, $requirement_id = 0, $trigger = '', $site_id = 0, $args = array() ) {
114
115 // If we're not working with a requirement, bail here
116 if ( ! in_array( get_post_type( $requirement_id ), gamipress_get_requirement_types_slugs() ) )
117 return $return;
118
119 // Check if user has access to the achievement ($return will be false if user has exceed the limit or achievement is not published yet)
120 if( ! $return )
121 return $return;
122
123 // Send back our eligibility
124 return gamipress_divi_check_if_meets_requirements( $requirement_id, $trigger, $args );
125
126 }
127 add_filter( 'user_has_access_to_achievement', 'gamipress_divi_user_has_access_to_achievement', 10, 6 );