Overview

Classes

  • Papi_Admin
  • Papi_Admin_Ajax
  • Papi_Admin_Assets
  • Papi_Admin_Management_Pages
  • Papi_Admin_Menu
  • Papi_Admin_Meta_Box
  • Papi_Admin_Meta_Box_Tabs
  • Papi_Admin_Option_Handler
  • Papi_Admin_Post_Handler
  • Papi_Admin_View
  • Papi_Attachment_Type
  • Papi_Conditional_Rules
  • Papi_Container
  • Papi_Core_Autoload
  • Papi_Core_Conditional
  • Papi_Core_Conditional_Rule
  • Papi_Core_Data_Handler
  • Papi_Core_Page
  • Papi_Core_Property
  • Papi_Core_Type
  • Papi_Loader
  • Papi_Option_Page
  • Papi_Option_Type
  • Papi_Page_Type
  • Papi_Page_Type_Meta
  • Papi_Porter
  • Papi_Porter_Driver
  • Papi_Porter_Driver_Core
  • Papi_Post_Page
  • Papi_Property
  • Papi_Property_Bool
  • Papi_Property_Checkbox
  • Papi_Property_Color
  • Papi_Property_Datetime
  • Papi_Property_Divider
  • Papi_Property_Dropdown
  • Papi_Property_Editor
  • Papi_Property_Email
  • Papi_Property_File
  • Papi_Property_Flexible
  • Papi_Property_Gallery
  • Papi_Property_Hidden
  • Papi_Property_Html
  • Papi_Property_Image
  • Papi_Property_Link
  • Papi_Property_Number
  • Papi_Property_Post
  • Papi_Property_Radio
  • Papi_Property_Reference
  • Papi_Property_Relationship
  • Papi_Property_Repeater
  • Papi_Property_String
  • Papi_Property_Term
  • Papi_Property_Text
  • Papi_Property_Url
  • Papi_Property_User

Functions

  • current_page
  • papi
  • papi_action_delete_value
  • papi_action_include
  • papi_append_post_type_query
  • papi_body_class
  • papi_cache_delete
  • papi_cache_get
  • papi_cache_key
  • papi_cache_set
  • papi_camel_case
  • papi_cast_string_value
  • papi_convert_to_string
  • papi_current_user_is_allowed
  • papi_dashify
  • papi_delete_field
  • papi_delete_option
  • papi_delete_property_meta_value
  • papi_display_page_type
  • papi_doing_ajax
  • papi_esc_html
  • papi_f
  • papi_field
  • papi_field_shortcode
  • papi_field_value
  • papi_fields
  • papi_filter_conditional_rule_allowed
  • papi_filter_core_load_one_type_on
  • papi_filter_format_value
  • papi_filter_load_value
  • papi_filter_settings_directories
  • papi_filter_settings_only_page_type
  • papi_filter_settings_page_type_column_title
  • papi_filter_settings_show_page_type
  • papi_filter_settings_show_standard_page_type
  • papi_filter_settings_show_standard_page_type_in_filter
  • papi_filter_settings_sort_order
  • papi_filter_settings_standard_page_description
  • papi_filter_settings_standard_page_name
  • papi_filter_settings_standard_page_thumbnail
  • papi_filter_update_value
  • papi_from_property_array_slugs
  • papi_get_all_files_in_directory
  • papi_get_all_page_type_files
  • papi_get_all_page_types
  • papi_get_box_property
  • papi_get_class_name
  • papi_get_field
  • papi_get_file_path
  • papi_get_number_of_pages
  • papi_get_only_objects
  • papi_get_option
  • papi_get_options_and_properties
  • papi_get_or_post
  • papi_get_page
  • papi_get_page_new_url
  • papi_get_page_query_strings
  • papi_get_page_type
  • papi_get_page_type_base_path
  • papi_get_page_type_by_id
  • papi_get_page_type_by_post_id
  • papi_get_page_type_id
  • papi_get_page_type_key
  • papi_get_page_type_meta_value
  • papi_get_page_type_name
  • papi_get_page_type_template
  • papi_get_parent_post_id
  • papi_get_post_id
  • papi_get_post_type
  • papi_get_post_type_label
  • papi_get_post_types
  • papi_get_property_class_name
  • papi_get_property_meta_value
  • papi_get_property_options
  • papi_get_property_type
  • papi_get_property_type_key
  • papi_get_property_type_key_f
  • papi_get_qs
  • papi_get_sanitized_post
  • papi_get_slugs
  • papi_get_tab_options
  • papi_html_name
  • papi_html_tag
  • papi_include_template
  • papi_is_empty
  • papi_is_json
  • papi_is_method
  • papi_is_option_page
  • papi_is_option_type
  • papi_is_page_type
  • papi_is_property
  • papi_is_property_type_key
  • papi_is_rule
  • papi_management_page_type_render_box
  • papi_maybe_convert_to_array
  • papi_maybe_convert_to_object
  • papi_maybe_get_callable_value
  • papi_maybe_json_decode
  • papi_maybe_json_encode
  • papi_nl2br
  • papi_option_shortcode
  • papi_option_type_exists
  • papi_page_type_exists
  • papi_populate_properties
  • papi_property
  • papi_remove_papi
  • papi_remove_trailing_quotes
  • papi_render_html_tag
  • papi_render_properties
  • papi_render_property
  • papi_require_text
  • papi_required_html
  • papi_rule
  • papi_santize_data
  • papi_set_page_type_id
  • papi_setup_tabs
  • papi_slugify
  • papi_sort_order
  • papi_tab
  • papi_template
  • papi_template_include
  • papi_to_array
  • papi_to_property_array_slugs
  • papi_translate_keys
  • papi_underscorify
  • papi_update_field
  • papi_update_option
  • papi_update_property_meta_value
  • papi_with
  • papify
  • the_papi_field
  • the_papi_option
  • the_papi_page_type_name
  • Overview
  • Class
  1: <?php
  2: 
  3: /**
  4:  * Convert a value to camel case.
  5:  *
  6:  * @param  string $str
  7:  *
  8:  * @throws InvalidArgumentException if an argument is not of the expected type.
  9:  *
 10:  * @return string
 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:  * Cast string value to right value type.
 22:  *
 23:  * @param  string $str
 24:  *
 25:  * @return mixed
 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:  * Try convert to string if is possible else return empty string.
 47:  *
 48:  * @param  mixed $obj
 49:  *
 50:  * @return string
 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:  * Check if current is allowed the given capabilities.
 70:  *
 71:  * @param  array|string $capabilities
 72:  *
 73:  * @return bool
 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:  * Check if Papi is doing a AJAX request or not.
 91:  *
 92:  * @return bool
 93:  */
 94: function papi_doing_ajax() {
 95:     return defined( 'DOING_PAPI_AJAX' ) && DOING_PAPI_AJAX;
 96: }
 97: 
 98: /**
 99:  * Papi escape html.
100:  *
101:  * @param  mixed $obj
102:  * @param  array $keys
103:  *
104:  * @return mixed
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:  * Dashify the given string.
143:  * Replacing whitespace and underscore with a dash.
144:  *
145:  * @param  string $str
146:  *
147:  * @return string
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:  * Add underscores at the start of the string.
159:  *
160:  * @param  string $str
161:  * @param  int    $len
162:  *
163:  * @return string
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:  * Get namespace name and/or class name from page type file.
185:  *
186:  * @param  string $file
187:  *
188:  * @return string
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:  * Get only objects from the value.
235:  *
236:  * @param  array $arr
237:  *
238:  * @return array
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:  * Get value from $_GET or $_POST with the given key.
248:  *
249:  * @param  string $key
250:  *
251:  * @return string
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:  * Get query string if it exists and is not empty.
269:  *
270:  * @param  array|string $qs
271:  * @param  bool         $keep_keys
272:  *
273:  * @return array|string
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:  * Get sanitized value from global `$_POST`.
319:  *
320:  * @param  string $key
321:  *
322:  * @return string
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:  * Get a php friendly name.
334:  *
335:  * @param  string $name
336:  *
337:  * @return string
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:  * Get html tag from tag name and array of attributes.
359:  *
360:  * @param  string $tag
361:  * @param  array  $attr
362:  *
363:  * @return string
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:  * Check if the given object is empty or not.
416:  * Values like "0", 0 and false should not return true.
417:  *
418:  * @param  mixed $obj
419:  *
420:  * @return bool
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:  * Test if given object is a JSON string or not.
436:  *
437:  * @param  mixed  $obj
438:  *
439:  * @return bool
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:  * Check which http method it is.
449:  *
450:  * @param  string $method
451:  *
452:  * @return bool
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:  * Maybe JSON decode the given string.
464:  *
465:  * @param  string $str
466:  * @param  bool   $assoc
467:  *
468:  * @return mixed
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:  * Maybe JSON encode the given object.
476:  *
477:  * @param  mixed $obj
478:  *
479:  * @return mixed
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:  * Maybe convert value to array if array or return the value.
493:  *
494:  * @param  mixed $obj
495:  *
496:  * @return mixed
497:  */
498: function papi_maybe_convert_to_array( $obj ) {
499:     return is_object( $obj ) ? (array) $obj : $obj;
500: }
501: 
502: /**
503:  * Maybe convert value to object if array or return the value.
504:  *
505:  * @param  mixed $obj
506:  *
507:  * @return mixed
508:  */
509: function papi_maybe_convert_to_object( $obj ) {
510:     return is_array( $obj ) ? (object) $obj : $obj;
511: }
512: 
513: /**
514:  * Papi get callable value if is it callable.
515:  *
516:  * @param  mixed $callable
517:  * @param  array $args
518:  *
519:  * @return mixed
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:  * Replace '\n' with '<br />'.
545:  *
546:  * @param  string $str
547:  *
548:  * @return string
549:  */
550: function papi_nl2br( $str ) {
551:     return str_replace( '\n', '<br />', nl2br( $str ) );
552: }
553: 
554: /**
555:  * Remove `papi-` or `papi_` from the given string.
556:  *
557:  * @param  string $str
558:  *
559:  * @return string
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:  * Remove trailing dobule quote.
571:  * PHP's $_POST object adds this automatic.
572:  *
573:  * @param  string $str The string to check.
574:  *
575:  * @return string
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:  * Render html tag from tag name and array of attributes.
587:  *
588:  * @param  string $tag
589:  * @param  array  $attr
590:  *
591:  * @return string
592:  */
593: function papi_render_html_tag( $tag, $attr = [] ) {
594:     echo papi_html_tag( $tag, $attr );
595: }
596: 
597: /**
598:  * Santize data.
599:  *
600:  * @param  mixed $obj
601:  *
602:  * @return mixed
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:  * Sort array based on given key and numeric value.
620:  *
621:  * @param  array  $array
622:  * @param  string $key
623:  *
624:  * @return array
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:  * Slugify the given string.
685:  *
686:  * @param  string $str
687:  * @param  array  $replace
688:  * @param  string $delimiter
689:  *
690:  * @return string
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:  * Get a array of $obj.
713:  *
714:  * @param  mixed $obj
715:  *
716:  * @return mixed
717:  */
718: function papi_to_array( $obj ) {
719:     if ( ! is_array( $obj ) ) {
720:         $obj = [$obj];
721:     }
722: 
723:     return $obj;
724: }
725: 
726: /**
727:  * Translate array keys.
728:  *
729:  * @param  array  $arr
730:  * @param  array $domain
731:  *
732:  * @return array
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:  * Underscorify the given string.
750:  * Replacing whitespace and dash with a underscore.
751:  *
752:  * @param  string $str
753:  *
754:  * @return string
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:  * Add `papi_` to the given string ad the start of the string.
766:  *
767:  * @param  string $str
768:  *
769:  * @return string
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:  * Return the given object. Useful for chaining.
789:  *
790:  * @param  mixed $obj
791:  *
792:  * @return mixed
793:  */
794: function papi_with( $obj ) {
795:     return $obj;
796: }
797: 
API documentation generated by ApiGen