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

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

441 lines 11.4 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 <contact@gamipress.com>, Ruben Garcia <rubengcdev@gmail.com>
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 $matches = false;
219
220 // Ensure that the element to compare is a string
221 if( is_array( $b ) ) {
222 $b = implode( ',', $b );
223 }
224
225 $a = strval( $a );
226 $b = strval( $b );
227
228 // If not is a string condition and elements to compare are numerics, turn them to float
229 if( ! gamipress_is_string_condition( $condition ) ) {
230 if( is_numeric( $a ) ) {
231 $a = (float) $a;
232 }
233
234 if( is_numeric( $b ) ) {
235 $b = (float) $b;
236 }
237 }
238
239 switch( $condition ) {
240 case 'equal':
241 case '=':
242 case '==':
243 case '===':
244 $matches = ( $a == $b );
245 break;
246 case 'not_equal':
247 case '!=':
248 case '!==':
249 $matches = ( $a != $b );
250 break;
251 case 'less_than':
252 case '<':
253 $matches = ( $a < $b );
254 break;
255 case 'greater_than':
256 case '>':
257 $matches = ( $a > $b );
258 break;
259 case 'less_or_equal':
260 case '<=':
261 $matches = ( $a <= $b );
262 break;
263 case 'greater_or_equal':
264 case '>=':
265 $matches = ( $a >= $b );
266 break;
267 case 'contains':
268 $matches = ( strpos( $a, strval( $b ) ) !== false );
269 break;
270 case 'not_contains':
271 $matches = ( strpos( $a, strval( $b ) ) === false );
272 break;
273 case 'start_with':
274 $matches = ( gamipress_starts_with( $a, $b ) );
275 break;
276 case 'not_start_with':
277 $matches = ( ! gamipress_starts_with( $a, $b ) );
278 break;
279 case 'ends_with':
280 $matches = ( gamipress_ends_with( $a, $b ) );
281 break;
282 case 'not_ends_with':
283 $matches = ( ! gamipress_ends_with( $a, $b ) );
284 break;
285 }
286
287 return $matches;
288
289 }
290
291 /**
292 * Utility function to meet if condition is related to string
293 *
294 * @since 2.4.2
295 *
296 * @param string $condition The condition to check
297 *
298 * @return bool
299 */
300 function gamipress_is_string_condition( $condition ) {
301
302 $return = false;
303
304 switch( $condition ) {
305 case 'contains':
306 case 'not_contains':
307 case 'start_with':
308 case 'not_start_with':
309 case 'ends_with':
310 case 'not_ends_with':
311 $return = true;
312 break;
313 }
314
315 return $return;
316
317 }
318
319 /**
320 * Retrieves the post thumbnail.
321 *
322 * WordPress does not check if the thumbnail could be a WP Error so here is our version to prevent PHP errors.
323 *
324 * @since 7.5.6
325 *
326 * @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global `$post`.
327 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of
328 * width and height values in pixels (in that order). Default 'post-thumbnail'.
329 * @param string|array $attr Optional. Query string or array of attributes. Default empty.
330 * @return string The post thumbnail image tag.
331 */
332 function gamipress_get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
333
334 if( ! gamipress_has_post_thumbnail( $post ) )
335 return '';
336
337 return get_the_post_thumbnail( $post, $size, $attr );
338
339 }
340
341 /**
342 * Determines whether a post has an image attached.
343 *
344 * WordPress does not check if the thumbnail could be a WP Error so here is our version to prevent PHP errors.
345 *
346 * @since 7.5.6
347 *
348 * @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global `$post`.
349 * @return bool Whether the post has an image attached.
350 */
351 function gamipress_has_post_thumbnail( $post ) {
352
353 $post = get_post( $post );
354 if ( ! $post ) return false;
355
356 $thumbnail_id = get_post_meta( $post->ID, '_thumbnail_id', true );
357
358 if( ! is_wp_error( $thumbnail_id ) )
359 $thumbnail_id = absint( $thumbnail_id );
360
361 $thumbnail_id = apply_filters( 'post_thumbnail_id', $thumbnail_id, $post );
362
363 return ( is_wp_error( $thumbnail_id ) ? false : absint( $thumbnail_id ) );
364
365 }
366
367 /**
368 * Get all public post types slugs
369 *
370 * @since 8.0.0
371 *
372 * @return array
373 */
374 function gamipress_get_post_types_slugs() {
375 $post_types = get_post_types( array( 'public' => true ), 'objects' );
376
377 return is_array( $post_types ) ? array_keys( $post_types ) : array();
378 }
379
380 /**
381 * Validates if value is in array ensuring to return a valid entry from the array
382 *
383 * @since 8.0.0
384 *
385 * @param string $value
386 * @param array $array
387 * @param mixed $default
388 *
389 * @return mixed
390 */
391 function gamipress_validate_from_array( $value, $array, $default = '' ) {
392 return is_array( $array ) && in_array( $value, $array ) ? $value : $default;
393 }
394
395 /**
396 * Get a random array entry for associative arrays
397 * For arrays without keys, use PHP array_rand() instead
398 *
399 * @since 8.0.0
400 *
401 * @param array $array
402 * @param mixed $default
403 *
404 * @return mixed
405 */
406 function gamipress_assoc_array_rand( $array, $default = false ) {
407
408 if( is_array( $array ) && count( $array ) ) {
409 $keys = array_keys( $array );
410
411 if( count( $keys ) === 1 ) {
412 return $array[ $keys[0] ];
413 } else {
414 return $array[ $keys[ array_rand( $keys ) ] ];
415 }
416 }
417
418 return $default;
419 }
420
421 /**
422 * Join a string with a natural language conjunction at the end (one, two, three or four).
423 *
424 * @since 8.0.0
425 *
426 * @param array $array Array of words
427 * @param string $join and|or
428 *
429 * @return string
430 */
431 function gamipress_join_words( $array, $join = '' ) {
432
433 if( $join === '' ) $join = __( 'and', 'gamipress' );
434
435 $last = array_pop($array);
436
437 if ($array) return implode(', ', $array) . ' ' . $join . ' ' . $last;
438
439 return $last;
440
441 }