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 / includes / functions / helpers.php

helpers.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 7.9.8, at includes/functions/helpers.php

371 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Helpers Functions
4 *
5 * @package GamiPress\Helpers_Functions
6 * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]>
7 * @since 1.7.1
8 */
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 /**
13 * Utility to check whether function is disabled.
14 *
15 * @since 1.3.7
16 *
17 * @param string $function Name of the function.
18 * @return bool Whether or not function is disabled.
19 */
20 function gamipress_is_function_disabled( $function ) {
21 $disabled = explode( ',', ini_get( 'disable_functions' ) );
22
23 return in_array( $function, $disabled );
24 }
25
26 /**
27 * Helper function to recursively check if something is in a multidimensional array
28 *
29 * @since 1.4.1
30 *
31 * @param mixed $needle
32 * @param array $haystack
33 * @param bool $strict
34 *
35 * @return bool
36 */
37 function gamipress_in_array( $needle, $haystack, $strict = false ) {
38
39 foreach( $haystack as $item ) {
40 if (
41 ( $strict ? $item === $needle : $item == $needle )
42 || ( is_array( $item ) && gamipress_in_array( $needle, $item, $strict ) )
43 ) {
44 return true;
45 }
46 }
47
48 return false;
49 }
50
51 /**
52 * Helper function to check if a string starts by needle string given
53 *
54 * @since 1.7.0
55 *
56 * @param string $haystack
57 * @param string $needle
58 *
59 * @return bool
60 */
61 function gamipress_starts_with( $haystack, $needle ) {
62 return strncmp( $haystack, $needle, strlen( $needle ) ) === 0;
63 }
64
65 /**
66 * Helper function to check if a string ends by needle string given
67 *
68 * @since 1.7.0
69 *
70 * @param string $haystack
71 * @param string $needle
72 *
73 * @return bool
74 */
75 function gamipress_ends_with( $haystack, $needle ) {
76 return $needle === '' || substr_compare( $haystack, $needle, -strlen( $needle ) ) === 0;
77 }
78
79 /**
80 * Helper function to get all editable roles included if get_editable_roles() doesn't exists
81 *
82 * @since 1.8.8
83 *
84 * @return array[]|mixed|void
85 */
86 function gamipress_get_editable_roles() {
87
88 if( function_exists('get_editable_roles' ) ) {
89 $roles = get_editable_roles();
90 } else {
91 $roles = wp_roles()->roles;
92
93 $roles = apply_filters( 'editable_roles', $roles );
94 }
95
96 return $roles;
97
98 }
99
100 /**
101 * Utility function to get the number condition options
102 *
103 * @since 2.4.2
104 *
105 * @return array
106 */
107 function gamipress_number_condition_options() {
108
109 return array(
110 'equal' => __( 'equal to', 'gamipress'),
111 'not_equal' => __( 'not equal to', 'gamipress'),
112 'less_than' => __( 'less than', 'gamipress' ),
113 'greater_than' => __( 'greater than', 'gamipress' ),
114 'less_or_equal' => __( 'less or equal to', 'gamipress' ),
115 'greater_or_equal' => __( 'greater or equal to', 'gamipress' ),
116 );
117
118 }
119
120 /**
121 * Utility function to check the condition option parameter
122 *
123 * @since 2.0.0
124 *
125 * @param int|float $to_match Number to match
126 * @param int|float $to_compare Number to compare
127 * @param string $condition The condition to compare numbers
128 *
129 * @return bool
130 */
131 function gamipress_number_condition_matches( $to_match, $to_compare, $condition ) {
132
133 if( empty( $condition ) ) {
134 $condition = 'equal';
135 }
136
137 $matches = false;
138
139 switch( $condition ) {
140 case 'equal':
141 case '=':
142 case '==':
143 case '===':
144 $matches = ( $to_match == $to_compare );
145 break;
146 case 'not_equal':
147 case '!=':
148 case '!==':
149 $matches = ( $to_match != $to_compare );
150 break;
151 case 'less_than':
152 case '<':
153 $matches = ( $to_match < $to_compare );
154 break;
155 case 'greater_than':
156 case '>':
157 $matches = ( $to_match > $to_compare );
158 break;
159 case 'less_or_equal':
160 case '<=':
161 $matches = ( $to_match <= $to_compare );
162 break;
163 case 'greater_or_equal':
164 case '>=':
165 $matches = ( $to_match >= $to_compare );
166 break;
167 }
168
169 return $matches;
170
171 }
172
173 /**
174 * Utility function to get the condition options
175 * Note: For numbers, use the gamipress_number_condition_options()
176 *
177 * @since 2.4.2
178 *
179 * @return array
180 */
181 function gamipress_condition_options() {
182
183 return array(
184 // String
185 'equal' => __( 'is equal to', 'gamipress' ),
186 'not_equal' => __( 'is not equal to', 'gamipress' ),
187 'contains' => __( 'contains', 'gamipress' ),
188 'not_contains' => __( 'does not contains', 'gamipress' ),
189 'start_with' => __( 'starts with', 'gamipress' ),
190 'not_start_with' => __( 'does not starts with', 'gamipress' ),
191 'ends_with' => __( 'ends with', 'gamipress' ),
192 'not_ends_with' => __( 'does not ends with', 'gamipress' ),
193 // Number
194 'less_than' => __( 'is less than', 'gamipress' ),
195 'greater_than' => __( 'is greater than', 'gamipress' ),
196 'less_or_equal' => __( 'is less or equal to', 'gamipress' ),
197 'greater_or_equal' => __( 'is greater or equal to', 'gamipress' ),
198 );
199
200 }
201
202 /**
203 * Utility function to get the condition option parameter
204 *
205 * @since 2.4.2
206 *
207 * @param mixed $a Element to match
208 * @param mixed $b Element to compare
209 * @param string $condition The condition to compare elements
210 *
211 * @return bool
212 */
213 function gamipress_condition_matches( $a, $b, $condition ) {
214
215 if( empty( $condition ) ) {
216 $condition = 'equal';
217 }
218
219 $matches = false;
220
221 // Ensure that the element to compare is a string
222 if( is_array( $b ) ) {
223 $b = implode( ',', $b );
224 }
225
226 $a = strval( $a );
227 $b = strval( $b );
228
229 // If not is a string condition and elements to compare are numerics, turn them to float
230 if( ! gamipress_is_string_condition( $condition ) ) {
231 if( is_numeric( $a ) ) {
232 $a = (float) $a;
233 }
234
235 if( is_numeric( $b ) ) {
236 $b = (float) $b;
237 }
238 }
239
240 switch( $condition ) {
241 case 'equal':
242 case '=':
243 case '==':
244 case '===':
245 $matches = ( $a == $b );
246 break;
247 case 'not_equal':
248 case '!=':
249 case '!==':
250 $matches = ( $a != $b );
251 break;
252 case 'less_than':
253 case '<':
254 $matches = ( $a < $b );
255 break;
256 case 'greater_than':
257 case '>':
258 $matches = ( $a > $b );
259 break;
260 case 'less_or_equal':
261 case '<=':
262 $matches = ( $a <= $b );
263 break;
264 case 'greater_or_equal':
265 case '>=':
266 $matches = ( $a >= $b );
267 break;
268 case 'contains':
269 $matches = ( strpos( $a, strval( $b ) ) !== false );
270 break;
271 case 'not_contains':
272 $matches = ( strpos( $a, strval( $b ) ) === false );
273 break;
274 case 'start_with':
275 $matches = ( gamipress_starts_with( $a, $b ) );
276 break;
277 case 'not_start_with':
278 $matches = ( ! gamipress_starts_with( $a, $b ) );
279 break;
280 case 'ends_with':
281 $matches = ( gamipress_ends_with( $a, $b ) );
282 break;
283 case 'not_ends_with':
284 $matches = ( ! gamipress_ends_with( $a, $b ) );
285 break;
286 }
287
288 return $matches;
289
290 }
291
292 /**
293 * Utility function to meet if condition is related to string
294 *
295 * @since 2.4.2
296 *
297 * @param string $condition The condition to check
298 *
299 * @return bool
300 */
301 function gamipress_is_string_condition( $condition ) {
302
303 $return = false;
304
305 switch( $condition ) {
306 case 'contains':
307 case 'not_contains':
308 case 'start_with':
309 case 'not_start_with':
310 case 'ends_with':
311 case 'not_ends_with':
312 $return = true;
313 break;
314 }
315
316 return $return;
317
318 }
319
320 /**
321 * Retrieves the post thumbnail.
322 *
323 * WordPress does not check if the thumbnail could be a WP Error so here is our version to prevent PHP errors.
324 *
325 * @since 7.5.6
326 *
327 * @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global `$post`.
328 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of
329 * width and height values in pixels (in that order). Default 'post-thumbnail'.
330 * @param string|array $attr Optional. Query string or array of attributes. Default empty.
331 * @return string The post thumbnail image tag.
332 */
333 function gamipress_get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
334
335 if( ! gamipress_has_post_thumbnail( $post ) ) {
336 return '';
337 }
338
339 return get_the_post_thumbnail( $post, $size, $attr );
340
341 }
342
343 /**
344 * Determines whether a post has an image attached.
345 *
346 * WordPress does not check if the thumbnail could be a WP Error so here is our version to prevent PHP errors.
347 *
348 * @since 7.5.6
349 *
350 * @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global `$post`.
351 * @return bool Whether the post has an image attached.
352 */
353 function gamipress_has_post_thumbnail( $post ) {
354
355 $post = get_post( $post );
356
357 if ( ! $post ) {
358 return false;
359 }
360
361 $thumbnail_id = get_post_meta( $post->ID, '_thumbnail_id', true );
362
363 if( ! is_wp_error( $thumbnail_id ) ) {
364 $thumbnail_id = absint( $thumbnail_id );
365 }
366
367 $thumbnail_id = apply_filters( 'post_thumbnail_id', $thumbnail_id, $post );
368
369 return ( is_wp_error( $thumbnail_id ) ? false : absint( $thumbnail_id ) );
370
371 }