1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12: function papi_camel_case( $str ) {
13: if ( ! is_string( $str ) ) {
14: throw new InvalidArgumentException( 'Invalid argument. Must be string.' );
15: }
16:
17: return lcfirst( str_replace( ' ', '', ucwords( str_replace( ['-', '_'], ' ', $str ) ) ) );
18: }
19:
20: 21: 22: 23: 24: 25: 26:
27: function papi_cast_string_value( $str ) {
28: if ( ! is_string( $str ) ) {
29: return $str;
30: }
31:
32: if ( is_numeric( $str ) ) {
33: return $str == (int) $str ? (int) $str : (float) $str;
34: }
35:
36: if ( $str === 'true' || $str === 'false' ) {
37: return $str === 'true';
38: }
39:
40: return papi_maybe_json_decode(
41: maybe_unserialize( $str )
42: );
43: }
44:
45: 46: 47: 48: 49: 50: 51:
52: function papi_convert_to_string( $obj ) {
53: if ( $obj === true ) {
54: return 'true';
55: }
56:
57: if ( $obj === false ) {
58: return 'false';
59: }
60:
61: if ( ! is_array( $obj ) && ( ( ! is_object( $obj ) && settype( $obj, 'string' ) !== false ) || ( is_object( $obj ) && method_exists( $obj, '__toString' ) ) ) ) {
62: return (string) $obj;
63: }
64:
65: return '';
66: }
67:
68: 69: 70: 71: 72: 73: 74:
75: function papi_current_user_is_allowed( $capabilities = [] ) {
76: if ( ! is_array( $capabilities ) && ! is_string( $capabilities ) || empty( $capabilities ) ) {
77: return true;
78: }
79:
80: foreach ( papi_to_array( $capabilities ) as $capability ) {
81: if ( ! current_user_can( $capability ) ) {
82: return false;
83: }
84: }
85:
86: return true;
87: }
88:
89: 90: 91: 92: 93:
94: function papi_doing_ajax() {
95: return defined( 'DOING_PAPI_AJAX' ) && DOING_PAPI_AJAX;
96: }
97:
98: 99: 100: 101: 102: 103: 104: 105:
106: function papi_esc_html( $obj, $keys = [] ) {
107: $object = is_object( $obj ) && get_class( $obj ) === 'stdClass';
108:
109: if ( $object ) {
110: $obj = (array) $obj;
111: }
112:
113: if ( is_array( $obj ) ) {
114: foreach ( $obj as $key => $value ) {
115: if ( in_array( $key, $keys ) ) {
116: continue;
117: }
118:
119: if ( is_string( $key ) ) {
120: unset( $obj[$key] );
121: $key = papi_esc_html( $key );
122: }
123:
124: if ( is_string( $value ) || is_object( $value ) || is_array( $obj ) ) {
125: $obj[$key] = papi_esc_html( $value, $keys );
126: }
127: }
128:
129: if ( $object ) {
130: return (object) $obj;
131: }
132:
133: return $obj;
134: } else if ( is_string( $obj ) ) {
135: return esc_html( $obj );
136: } else {
137: return $obj;
138: }
139: }
140:
141: 142: 143: 144: 145: 146: 147: 148:
149: function papi_dashify( $str ) {
150: if ( ! is_string( $str ) ) {
151: return '';
152: }
153:
154: return str_replace( ' ', '-', str_replace( '_', '-', $str ) );
155: }
156:
157: 158: 159: 160: 161: 162: 163: 164:
165: function papi_f( $str = '', $len = 1 ) {
166: if ( ! is_string( $str ) ) {
167: return '';
168: }
169:
170: $prefix = '';
171:
172: for ( $i = 0; $i < $len; $i++ ) {
173: $prefix .= '_';
174: }
175:
176: if ( strpos( $str, $prefix ) === 0 ) {
177: return $str;
178: }
179:
180: return $prefix . preg_replace( '/^\_/', '', $str );
181: }
182:
183: 184: 185: 186: 187: 188: 189:
190: function papi_get_class_name( $file ) {
191: if ( ! is_string( $file ) ) {
192: return '';
193: }
194:
195: $content = file_get_contents( $file );
196: $tokens = token_get_all( $content );
197: $class_name = '';
198: $namespace_name = '';
199: $i = 0;
200: $len = count( $tokens );
201:
202: for ( ; $i < $len; $i++ ) {
203: if ( $tokens[$i][0] === T_NAMESPACE ) {
204: for ( $j = $i + 1; $j < $len; $j++ ) {
205: if ( $tokens[$j][0] === T_STRING ) {
206: $namespace_name .= '\\' . $tokens[$j][1];
207: } else if ( $tokens[$j] === '{' || $tokens[$j] === ';' ) {
208: break;
209: }
210: }
211: }
212:
213: if ( $tokens[$i][0] === T_CLASS ) {
214: for ( $j = $i + 1; $j < $len; $j++ ) {
215: if ( $tokens[$j] === '{' ) {
216: $class_name = $tokens[$i + 2][1];
217: }
218: }
219: }
220: }
221:
222: if ( empty( $class_name ) ) {
223: return '';
224: }
225:
226: if ( empty( $namespace_name ) ) {
227: return $class_name;
228: }
229:
230: return $namespace_name . '\\' . $class_name;
231: }
232:
233: 234: 235: 236: 237: 238: 239:
240: function papi_get_only_objects( array $arr ) {
241: return array_filter( $arr, function ( $item ) {
242: return is_object( $item );
243: } );
244: }
245:
246: 247: 248: 249: 250: 251: 252:
253: function papi_get_or_post( $key ) {
254: if ( ! is_string( $key ) ) {
255: return;
256: }
257:
258: if ( $value = papi_get_qs( $key ) ) {
259: return $value;
260: }
261:
262: if ( $value = papi_get_sanitized_post( $key ) ) {
263: return $value;
264: }
265: }
266:
267: 268: 269: 270: 271: 272: 273: 274:
275: function papi_get_qs( $qs, $keep_keys = false ) {
276: if ( ! is_string( $qs ) && ! is_array( $qs ) ) {
277: return;
278: }
279:
280: if ( is_array( $qs ) ) {
281: if ( $keep_keys ) {
282: $results = [];
283:
284: foreach ( $qs as $key ) {
285: $value = papi_get_qs( $key );
286:
287: if ( ! papi_is_empty( $value ) ) {
288: $results[$key] = $value;
289: }
290: }
291:
292: return $results;
293: } else {
294: return array_map( 'papi_get_qs', $qs );
295: }
296: }
297:
298: if ( isset( $_GET[$qs] ) && ! empty( $_GET[$qs] ) ) {
299: $value = $_GET[$qs];
300:
301: if ( is_string( $value ) ) {
302: $value = sanitize_text_field( $value );
303: }
304:
305: if ( $value === 'false' ) {
306: $value = false;
307: }
308:
309: if ( $value === 'true' ) {
310: $value = true;
311: }
312:
313: return $value;
314: }
315: }
316:
317: 318: 319: 320: 321: 322: 323:
324: function papi_get_sanitized_post( $key ) {
325: if ( ! isset( $_POST[$key] ) ) {
326: return;
327: }
328:
329: return sanitize_text_field( $_POST[$key] );
330: }
331:
332: 333: 334: 335: 336: 337: 338:
339: function papi_html_name( $name ) {
340: if ( ! is_string( $name ) ) {
341: return '';
342: }
343:
344: if ( ! preg_match( '/^\_\_papi|^\_papi/', $name ) ) {
345: $name = papify( $name );
346:
347: if ( ! preg_match( '/\[.*\]/', $name ) ) {
348: $name = papi_slugify( $name );
349: }
350:
351: return papi_underscorify( $name );
352: }
353:
354: return $name;
355: }
356:
357: 358: 359: 360: 361: 362: 363: 364:
365: function papi_html_tag( $tag, $attr = [] ) {
366: $attributes = [];
367: $content = [];
368:
369: if ( ! is_array( $attr ) ) {
370: $attr = [$attr];
371: }
372:
373: foreach ( $attr as $key => $value ) {
374: if ( is_numeric( $key ) ) {
375: if ( is_array( $value ) ) {
376: $content[] = implode( ' ', $value );
377: } else {
378: $content[] = $value;
379: }
380:
381: continue;
382: }
383:
384: if ( is_array( $value ) || is_object( $value ) ) {
385: $value = json_encode( $value );
386: } else if ( is_bool( $value ) ) {
387: $value = $value ? 'true' : 'false';
388: } else if ( is_string( $value ) ) {
389: $value = trim( $value );
390: }
391:
392: if ( is_null( $value ) ) {
393: continue;
394: }
395:
396: $attributes[] = sprintf( '%s="%s"', $key, esc_attr( $value ) );
397: }
398:
399: if ( papi_is_empty( $content ) ) {
400: $end = '/>';
401: } else {
402: $end = sprintf( '>%s</%s>', implode( ' ', $content ), $tag );
403: }
404:
405: if ( ! empty( $attributes ) ) {
406: $attributes = ' ' . implode( ' ', $attributes );
407: } else {
408: $attributes = '';
409: }
410:
411: return sprintf( '<%s%s%s', $tag, $attributes, $end );
412: }
413:
414: 415: 416: 417: 418: 419: 420: 421:
422: function papi_is_empty( $obj ) {
423: if ( is_string( $obj ) ) {
424: return empty( $obj ) && ! is_numeric( $obj );
425: }
426:
427: if ( is_bool( $obj ) || is_numeric( $obj ) ) {
428: return false;
429: }
430:
431: return empty( $obj );
432: }
433:
434: 435: 436: 437: 438: 439: 440:
441: function papi_is_json( $obj ) {
442: return is_string( $obj )
443: && is_array( json_decode( $obj, true ) )
444: && json_last_error() === JSON_ERROR_NONE;
445: }
446:
447: 448: 449: 450: 451: 452: 453:
454: function papi_is_method( $method ) {
455: if ( ! isset( $_SERVER['REQUEST_METHOD'] ) || ! is_string( $method ) ) {
456: return false;
457: }
458:
459: return strtoupper( $_SERVER ['REQUEST_METHOD'] ) === strtoupper( $method );
460: }
461:
462: 463: 464: 465: 466: 467: 468: 469:
470: function papi_maybe_json_decode( $str, $assoc = false ) {
471: return papi_is_json( $str ) ? json_decode( $str, $assoc ) : $str;
472: }
473:
474: 475: 476: 477: 478: 479: 480:
481: function papi_maybe_json_encode( $obj ) {
482: if ( is_array( $obj ) || is_object( $obj ) ) {
483: return function_exists( 'wp_json_encode' ) ?
484: wp_json_encode( $obj ) :
485: json_encode( $obj );
486: }
487:
488: return $obj;
489: }
490:
491: 492: 493: 494: 495: 496: 497:
498: function papi_maybe_convert_to_array( $obj ) {
499: return is_object( $obj ) ? (array) $obj : $obj;
500: }
501:
502: 503: 504: 505: 506: 507: 508:
509: function papi_maybe_convert_to_object( $obj ) {
510: return is_array( $obj ) ? (object) $obj : $obj;
511: }
512:
513: 514: 515: 516: 517: 518: 519: 520:
521: function papi_maybe_get_callable_value( $callable, $args = [] ) {
522: if ( is_callable( $callable ) ) {
523: $ob_level = ob_get_level();
524:
525: ob_start();
526:
527: try {
528: call_user_func_array( $callable, papi_to_array( $args ) );
529: } catch ( Exception $e ) {
530: while ( ob_get_level() > $ob_level ) {
531: ob_end_clean();
532: }
533:
534: return $callable;
535: }
536:
537: return ltrim( ob_get_clean() );
538: }
539:
540: return $callable;
541: }
542:
543: 544: 545: 546: 547: 548: 549:
550: function papi_nl2br( $str ) {
551: return str_replace( '\n', '<br />', nl2br( $str ) );
552: }
553:
554: 555: 556: 557: 558: 559: 560:
561: function papi_remove_papi( $str ) {
562: if ( ! is_string( $str ) ) {
563: return '';
564: }
565:
566: return str_replace( 'papi-', '', str_replace( 'papi_', '', $str ) );
567: }
568:
569: 570: 571: 572: 573: 574: 575: 576:
577: function papi_remove_trailing_quotes( $str ) {
578: if ( ! is_string( $str ) ) {
579: return '';
580: }
581:
582: return str_replace( "\'", "'", str_replace( '\"', '"', $str ) );
583: }
584:
585: 586: 587: 588: 589: 590: 591: 592:
593: function papi_render_html_tag( $tag, $attr = [] ) {
594: echo papi_html_tag( $tag, $attr );
595: }
596:
597: 598: 599: 600: 601: 602: 603:
604: function papi_santize_data( $obj ) {
605: if ( is_array( $obj ) ) {
606: foreach ( $obj as $k => $v ) {
607: if ( is_string( $v ) ) {
608: $obj[ $k ] = papi_santize_data( $v );
609: }
610: }
611: } else if ( is_string( $obj ) ) {
612: $obj = papi_remove_trailing_quotes( $obj );
613: }
614:
615: return $obj;
616: }
617:
618: 619: 620: 621: 622: 623: 624: 625:
626: function papi_sort_order( $array, $key = 'sort_order' ) {
627: if ( empty( $array ) || ! is_array( $array ) && ! is_object( $array ) ) {
628: return [];
629: }
630:
631: if ( is_object( $array ) ) {
632: $array = papi_to_array( $array );
633: }
634:
635: $sorter = [];
636:
637: foreach ( $array as $k => $value ) {
638: if ( is_object( $value ) ) {
639: if ( isset( $value->$key ) ) {
640: $sorter[$k] = $value->$key;
641: } else if ( isset( $value->options->$key ) ) {
642: $sorter[$k] = $value->options->$key;
643: }
644: } else if ( is_array( $value ) && isset( $value[$key] ) ) {
645: $sorter[$k] = $value[$key];
646: }
647: }
648:
649: $i = 0;
650: $default_sort = papi_filter_settings_sort_order();
651:
652: foreach ( $sorter as $k => $v ) {
653: if ( $default_sort === $v ) {
654: $sorter[$k] = $v - $i;
655: $i++;
656: }
657: }
658:
659: asort( $sorter, SORT_NUMERIC );
660:
661: $result = [];
662: $rest = [];
663:
664: foreach ( $sorter as $k => $v ) {
665: $value = $array[ $k ];
666:
667: if ( ( is_object( $value ) && ( ! isset( $value->options ) && ! isset( $value->options->$key ) || ! isset( $value->$key ) ) ) || ( is_array( $value ) && ! isset( $value[$key] ) ) ) {
668: $rest[] = $value;
669: } else {
670: $result[$k] = $array[$k];
671: }
672: }
673:
674: $result = array_values( $result );
675:
676: foreach ( $rest as $key => $value ) {
677: $result[] = $value;
678: }
679:
680: return $result;
681: }
682:
683: 684: 685: 686: 687: 688: 689: 690: 691:
692: function papi_slugify( $str, $replace = [], $delimiter = '-' ) {
693: if ( ! is_string( $str ) ) {
694: return '';
695: }
696:
697: setlocale( LC_ALL, 'en_US.UTF8' );
698:
699: if ( ! empty( $replace ) ) {
700: $str = str_replace( (array) $replace, ' ', $str );
701: }
702:
703: $clean = iconv( 'UTF-8', 'ASCII//TRANSLIT', $str );
704: $clean = preg_replace( '/[^a-zA-Z0-9\/_|+ -]/', '', $clean );
705: $clean = strtolower( trim( $clean, '-' ) );
706: $clean = preg_replace( '/[\/_|+ -]+/', $delimiter, $clean );
707:
708: return trim( $clean );
709: }
710:
711: 712: 713: 714: 715: 716: 717:
718: function papi_to_array( $obj ) {
719: if ( ! is_array( $obj ) ) {
720: $obj = [$obj];
721: }
722:
723: return $obj;
724: }
725:
726: 727: 728: 729: 730: 731: 732: 733:
734: function papi_translate_keys( array $arr, $domain ) {
735: foreach ( $arr as $key => $value ) {
736: if ( ! is_string( $key ) ) {
737: continue;
738: }
739:
740: unset( $arr[$key] );
741: $key = __( $key, $domain );
742: $arr[$key] = $value;
743: }
744:
745: return $arr;
746: }
747:
748: 749: 750: 751: 752: 753: 754: 755:
756: function papi_underscorify( $str ) {
757: if ( ! is_string( $str ) ) {
758: return '';
759: }
760:
761: return str_replace( ' ', '_', str_replace( '-', '_', $str ) );
762: }
763:
764: 765: 766: 767: 768: 769: 770:
771: function papify( $str = '' ) {
772: if ( ! is_string( $str ) ) {
773: return '';
774: }
775:
776: if ( ! preg_match( '/^\_\_papi|^\_papi|^papi\_/', $str ) ) {
777: if ( ! empty( $str ) && $str[0] === '_' ) {
778: return 'papi' . $str;
779: }
780:
781: return 'papi_' . $str;
782: }
783:
784: return $str;
785: }
786:
787: 788: 789: 790: 791: 792: 793:
794: function papi_with( $obj ) {
795: return $obj;
796: }
797: