PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 7.9.8
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v7.9.8
8.0.4 8.0.3 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 All 46 releases
gamipress / integrations / meta-box / includes / rules-engine.php

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

152 lines 5.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\Meta_Box\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_meta_box_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 // Rules engine needs to check if field name matches required ones
36 if( $return && ( $trigger === 'gamipress_meta_box_update_specific_post_field_any_value'
37 || $trigger === 'gamipress_meta_box_update_specific_user_field_any_value' ) ) {
38
39 $return = gamipress_meta_box_fields_conditions( $trigger, $requirement_id, $args );
40
41 }
42
43 // Rules engine needs to check if value matches required ones
44 if( $return && ( $trigger === 'gamipress_meta_box_update_any_post_field_specific_value'
45 || $trigger === 'gamipress_meta_box_update_any_user_field_specific_value' ) ) {
46
47 $field_value = $args[3];
48
49 $condition = gamipress_get_post_meta( $requirement_id, '_gamipress_meta_box_field_value_condition', true );
50 $required_field_value = gamipress_get_post_meta( $requirement_id, '_gamipress_meta_box_field_value', true );
51 $return = (bool) ( gamipress_meta_box_condition_matches( $field_value, $required_field_value, $condition ) );
52
53 }
54
55 // Rules engine needs to check if field name and values matches required ones
56 if( $return && ( $trigger === 'gamipress_meta_box_update_specific_post_field_specific_value'
57 || $trigger === 'gamipress_meta_box_update_specific_user_field_specific_value' ) ) {
58
59 $return = gamipress_meta_box_fields_values_conditions( $trigger, $requirement_id, $args );
60
61 }
62
63 // Send back our eligibility
64 return $return;
65
66 }
67 add_filter( 'user_has_access_to_achievement', 'gamipress_meta_box_user_has_access_to_achievement', 10, 6 );
68
69 /**
70 * Checks if field name matches condition
71 *
72 * @since 1.0.0
73 *
74 * @param string $trigger The trigger triggered
75 * @param int $requirement_id The given requirement's post ID
76 * @param array $args Arguments of this trigger
77 *
78 * @return bool True if field name matches condition, false otherwise
79 */
80 function gamipress_meta_box_fields_conditions( $trigger, $requirement_id, $args ) {
81
82 $field_name = $args[2];
83
84 if ( $trigger === 'gamipress_meta_box_update_specific_post_field_any_value' ) {
85 $required_field_name = gamipress_get_post_meta( $requirement_id, '_gamipress_meta_box_field_name', true );
86 }
87
88 if ( $trigger === 'gamipress_meta_box_update_specific_user_field_any_value' ) {
89 $required_field_name = gamipress_get_post_meta( $requirement_id, '_gamipress_meta_box_field_name_user', true );
90 }
91
92 // First, check if field name matches the required one
93 $return = (bool) ( $field_name === $required_field_name );
94
95 if( $return ) {
96 // Check if field name matches the required one (with support for arrays)
97 if( is_array( $field_name ) )
98 $return = (bool) ( in_array( $required_field_name, $field_name ) );
99 else
100 $return = (bool) ( $field_name === $required_field_name );
101 }
102
103 return $return;
104
105 }
106
107 /**
108 * Checks if field name and value match condition
109 *
110 * @since 1.0.0
111 *
112 * @param string $trigger The trigger triggered
113 * @param int $requirement_id The given requirement's post ID
114 * @param array $args Arguments of this trigger
115 *
116 * @return bool True if field name and value match condition, false otherwise
117 */
118 function gamipress_meta_box_fields_values_conditions( $trigger, $requirement_id, $args ) {
119
120 $field_value = $args[3];
121 $field_name = $args[2];
122
123 if ( $trigger === 'gamipress_meta_box_update_specific_post_field_specific_value' ) {
124 $required_field_name = gamipress_get_post_meta( $requirement_id, '_gamipress_meta_box_field_name', true );
125 }
126
127 if ( $trigger === 'gamipress_meta_box_update_specific_user_field_specific_value' ) {
128 $required_field_name = gamipress_get_post_meta( $requirement_id, '_gamipress_meta_box_field_name_user', true );
129 }
130
131 $condition = gamipress_get_post_meta( $requirement_id, '_gamipress_meta_box_field_value_condition', true );
132 $required_field_value = gamipress_get_post_meta( $requirement_id, '_gamipress_meta_box_field_value', true );
133
134 // First, check if field name matches the required one
135 $return = (bool) ( $field_name === $required_field_name );
136
137 if( $return ) {
138 // Check if field value matches the required one (with support for arrays)
139 if( is_array( $field_name ) )
140 $return = (bool) ( in_array( $required_field_name, $field_name ) );
141 else
142 $return = (bool) ( $field_name === $required_field_name );
143 }
144
145 if ( $return ) {
146 $return = (bool) ( gamipress_meta_box_condition_matches( $field_value, $required_field_value, $condition ) );
147 }
148
149 return $return;
150
151 }
152