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:  * Delete property value from database.
  5:  * If it's on a option page it will fetch the value from the
  6:  * option table instead of the postmeta table.
  7:  *
  8:  * @param int    $post_id
  9:  * @param string $slug
 10:  * @param string $type
 11:  */
 12: function papi_delete_property_meta_value( $post_id, $slug, $type = 'post' ) {
 13:     if ( $type === Papi_Core_Page::TYPE_OPTION || papi_is_option_page() ) {
 14:         return delete_option( papi_remove_papi( $slug ) );
 15:     }
 16: 
 17:     return delete_post_meta( $post_id, papi_remove_papi( $slug ) );
 18: }
 19: 
 20: /**
 21:  * Convert array of slugs to array with arrays in.
 22:  *
 23:  * @param  array  $values
 24:  * @param  string $slug
 25:  *
 26:  * @return array
 27:  */
 28: function papi_from_property_array_slugs( array $values, $slug ) {
 29:     $results = [];
 30: 
 31:     if ( empty( $values ) ) {
 32:         return $results;
 33:     }
 34: 
 35:     for ( $i = 0; $i < $values[$slug]; $i++ ) {
 36:         $item      = [];
 37:         $item_slug = $slug . '_' . $i . '_';
 38:         $keys      = preg_grep( '/' . preg_quote( $item_slug ). '/' , array_keys( $values ) );
 39: 
 40:         foreach ( $keys as $key ) {
 41:             $arr_key = str_replace( $item_slug, '', $key );
 42:             $item[$arr_key] = $values[$key];
 43:         }
 44: 
 45:         $results[] = $item;
 46:     }
 47: 
 48:     return $results;
 49: }
 50: 
 51: /**
 52:  * Check if the given value is a instance of a property or not.
 53:  *
 54:  * @param  mixed $value
 55:  *
 56:  * @return bool
 57:  */
 58: function papi_is_property( $value ) {
 59:     return $value instanceof Papi_Property;
 60: }
 61: 
 62: /**
 63:  * Get box property.
 64:  *
 65:  * @param  array $properties
 66:  *
 67:  * @return array
 68:  */
 69: function papi_get_box_property( array $properties ) {
 70:     $box_property = array_filter( $properties, function ( $property ) {
 71:         return ! is_object( $property );
 72:     } );
 73: 
 74:     if ( ! empty( $box_property ) && ! isset( $box_property[0] ) && ! isset( $box_property[0]['tab'] ) ) {
 75:         $property = papi_property( $properties );
 76: 
 77:         if ( ! $property->disabled() ) {
 78:             $property->_box_property = true;
 79:             $properties = [$property];
 80:         }
 81:     }
 82: 
 83:     return $properties;
 84: }
 85: 
 86: /**
 87:  * Get options and properties.
 88:  *
 89:  * @param  mixed $file_or_options
 90:  * @param  array $properties
 91:  * @param  bool  $is_box
 92:  *
 93:  * @return array
 94:  */
 95: function papi_get_options_and_properties( $file_or_options = [], $properties = [], $is_box = true ) {
 96:     $options = [];
 97: 
 98:     if ( is_array( $file_or_options ) ) {
 99:         if ( empty( $properties ) && $is_box ) {
100:             // Check if we have a title or not.
101:             if ( isset( $file_or_options['title'] ) ) {
102:                 $options['title'] = $file_or_options['title'];
103:             } else if ( isset( $file_or_options[0]->title ) ) {
104:                 $options['title'] = $file_or_options[0]->title;
105:                 if ( $file_or_options[0]->sidebar === false && $file_or_options[0]->required ) {
106:                     $options['_required'] = true;
107:                 }
108:             } else if ( isset( $file_or_options[0]->options ) && isset( $file_or_options[0]->options['title'] ) ) {
109:                 $options['title'] = $file_or_options[0]->options['title'];
110:             } else {
111:                 $options['title'] = '';
112:             }
113:             $properties  = $file_or_options;
114:         } else {
115:             $options = array_merge( $options, $file_or_options );
116: 
117:             if ( ! $is_box ) {
118:                 // Add all non string keys to the properties array
119:                 foreach ( $options as $key => $value ) {
120:                     if ( ! is_string( $key ) ) {
121:                         $properties[] = $value;
122:                         unset( $options[$key] );
123:                     }
124:                 }
125:             }
126:         }
127:     } else if ( is_string( $file_or_options ) ) {
128:         // If it's a template we need to load it the right way
129:         // and add all properties the right way.
130:         if ( preg_match( '/\.php$/', $file_or_options ) === 1 ) {
131:             $values = $properties;
132:             $template = papi_template( $file_or_options, $values );
133: 
134:             // Create the property array from existing property array or a new.
135:             $properties = [];
136:             $options = $template;
137: 
138:             // Add all non string keys to the properties array
139:             foreach ( $options as $key => $value ) {
140:                 if ( ! is_string( $key ) ) {
141:                     $properties[] = $value;
142:                     unset( $options[$key] );
143:                 }
144:             }
145:         } else {
146:             // The first parameter is used as the title.
147:             $options['title'] = $file_or_options;
148:         }
149:     }
150: 
151:     return [$options, $properties];
152: }
153: 
154: /**
155:  * Get property class name.
156:  *
157:  * @param  string $type
158:  *
159:  * @return string
160:  */
161: function papi_get_property_class_name( $type ) {
162:     if ( ! is_string( $type ) || empty( $type ) ) {
163:         return;
164:     }
165: 
166:     return 'Papi_Property_' . ucfirst( preg_replace( '/^Property/', '', $type ) );
167: }
168: 
169: /**
170:  * Get property value from database.
171:  * If it's on a option page it will fetch the value from the
172:  * option table instead of the postmeta table.
173:  *
174:  * @param int    $post_id
175:  * @param string $slug
176:  * @param string $type
177:  */
178: function papi_get_property_meta_value( $post_id, $slug, $type = 'post' ) {
179:     if ( $type === Papi_Core_Page::TYPE_OPTION || papi_is_option_page() ) {
180:         $value = get_option( $slug, null );
181:     } else {
182:         $value = get_post_meta( $post_id, $slug, true );
183:     }
184: 
185:     if ( papi_is_empty( $value ) ) {
186:         return;
187:     }
188: 
189:     return $value;
190: }
191: 
192: /**
193:  * Get property options.
194:  *
195:  * @param  array $options
196:  *
197:  * @return stdClass
198:  */
199: function papi_get_property_options( $options ) {
200:     if ( ! is_array( $options ) ) {
201:         if ( is_object( $options ) ) {
202:             return $options;
203:         }
204: 
205:         return;
206:     }
207: 
208:     $property = Papi_Property::create( $options );
209: 
210:     return $property->get_options();
211: }
212: 
213: /**
214:  * Get property type by the given type.
215:  *
216:  * @param  object|string $type
217:  *
218:  * @return null|Papi_Property
219:  */
220: function papi_get_property_type( $type ) {
221:     if ( papi_is_empty( $type ) ) {
222:         return;
223:     }
224: 
225:     return Papi_Property::factory( $type );
226: }
227: 
228: /**
229:  * Get the right key for a property type.
230:  *
231:  * @param  string $str
232:  *
233:  * @return string
234:  */
235: function papi_get_property_type_key( $str = '' ) {
236:     $suffix = '_property';
237: 
238:     if ( ! is_string( $str ) || strlen( $str ) === 0 ) {
239:         return $suffix;
240:     }
241: 
242:     $len = strlen( $str );
243: 
244:     if ( isset( $str[$len - 1] ) && $str[$len - 1] === ']' ) {
245:         $str = substr( $str, 0, $len - 1 );
246:         return papi_get_property_type_key( $str ) . ']';
247:     }
248: 
249:     return papi_remove_papi( $str . $suffix );
250: }
251: 
252: /**
253:  * Get the right key for a property type with a underscore as the first character.
254:  *
255:  * @param  string $str
256:  *
257:  * @return string
258:  */
259: function papi_get_property_type_key_f( $str ) {
260:     return papi_f( papi_get_property_type_key( $str ) );
261: }
262: 
263: /**
264:  * Check if it's ends with '_property'.
265:  *
266:  * @param  string $str
267:  *
268:  * @return bool
269:  */
270: function papi_is_property_type_key( $str = '' ) {
271:     $pattern = '_property';
272:     $pattern = str_replace( '_', '\_', $pattern );
273:     $pattern = str_replace( '-', '\-', $pattern );
274:     $pattern = '/' . $pattern . '$/';
275: 
276:     return preg_match( $pattern, $str ) === 1;
277: }
278: 
279: /**
280:  * Create a new property array or rendering a template property file.
281:  *
282:  * @param  mixed $file_or_options
283:  * @param  array $values
284:  *
285:  * @return null|Papi_Property
286:  */
287: function papi_property( $file_or_options, $values = [] ) {
288:     if ( papi_is_empty( $file_or_options ) ) {
289:         return;
290:     }
291: 
292:     if ( is_array( $file_or_options ) ) {
293:         $file_or_options = papi_get_property_options( $file_or_options );
294:     }
295: 
296:     if ( is_string( $file_or_options ) && is_array( $values ) ) {
297:         $file_or_options = papi_template( $file_or_options, $values );
298:     }
299: 
300:     if ( is_object( $file_or_options ) ) {
301:         return papi_get_property_type( $file_or_options );
302:     }
303: }
304: 
305: /**
306:  * Render the given property.
307:  *
308:  * @param Papi_Property $property
309:  */
310: function papi_render_property( $property ) {
311:     $property = Papi_Property::factory( $property );
312: 
313:     if ( is_null( $property ) ) {
314:         return;
315:     }
316: 
317:     $property->render();
318: }
319: 
320: /**
321:  * Render properties the right way.
322:  *
323:  * @param array $properties
324:  */
325: function papi_render_properties( array $properties ) {
326:     if ( empty( $properties ) ) {
327:         return;
328:     }
329: 
330:     // If it's a tab the tabs class will
331:     // handle the rendering of the properties.
332:     if ( isset( $properties[0]->tab ) && $properties[0]->tab ) {
333:         new Papi_Admin_Meta_Box_Tabs( $properties );
334:     } else {
335:         ?>
336: 
337:         <table class="papi-table">
338:             <tbody>
339:             <?php
340:             foreach ( $properties as $property ) {
341:                 papi_render_property( $property );
342:             }
343:             ?>
344:             </tbody>
345:         </table>
346: 
347:     <?php
348:     }
349: }
350: 
351: /**
352:  * Get require text for property.
353:  *
354:  * @param  Papi_Property $property
355:  *
356:  * @return string
357:  */
358: function papi_require_text( $property ) {
359:     if ( ! is_object( $property ) || ! $property->required ) {
360:         return '';
361:     }
362: 
363:     return esc_html__( '(required field)', 'papi' );
364: }
365: 
366: /**
367:  * Get require tag for property.
368:  *
369:  * @param  Papi_Property $property
370:  * @param  bool $text
371:  *
372:  * @return string
373:  */
374: function papi_required_html( $property, $text = false ) {
375:     if ( ! is_object( $property ) || ! $property->required ) {
376:         return '';
377:     }
378: 
379:     return ' <span class="papi-rq" data-property-name="' . $property->title . '" data-property-id="' . $property->slug . '">' . ( $text ? papi_require_text( $property ) : '*' ) . '</span>';
380: }
381: 
382: /**
383:  * Populate properties array.
384:  *
385:  * @param  array|object $properties
386:  *
387:  * @return array
388:  */
389: function papi_populate_properties( $properties ) {
390:     if ( ! is_array( $properties ) && ! is_object( $properties ) || empty( $properties ) ) {
391:         return [];
392:     }
393: 
394:     if ( is_object( $properties )  ) {
395:         return [$properties];
396:     }
397: 
398:     $results = [];
399: 
400:     // Get the box property (when you only put a array in the box method) if it exists.
401:     $properties = papi_get_box_property( $properties );
402: 
403:     // Convert all non property objects to property objects.
404:     $properties = array_map( function ( $property ) {
405:         if ( ! is_object( $property ) && is_array( $property ) && ! isset( $property['tab'] ) ) {
406:             return papi_get_property_options( $property );
407:         }
408: 
409:         return $property;
410:     }, $properties );
411: 
412:     // Fix so the properties array will have the right order.
413:     $properties = array_reverse( $properties );
414: 
415:     foreach ( $properties as $property ) {
416:         if ( isset( $property->tab ) && $property->tab ) {
417:             $results[] = $property;
418:             continue;
419:         }
420: 
421:         $results[] = $property;
422:     }
423: 
424:     if ( empty( $results ) || ( isset( $results[0]->tab ) && $results[0]->tab ) ) {
425:         return $results;
426:     }
427: 
428:     return papi_sort_order( $results );
429: }
430: 
431: /**
432:  * Update property values on the post with the given post id
433:  * or update property values on the option page.
434:  *
435:  * @param  array $meta
436:  *
437:  * @return bool
438:  */
439: function papi_update_property_meta_value( array $meta = [] ) {
440:     $meta       = (object) $meta;
441:     $option     = papi_is_option_page();
442:     $save_value = true;
443: 
444:     foreach ( papi_to_array( $meta->value ) as $key => $value ) {
445:         if ( is_string( $key ) ) {
446:             $save_value = false;
447:             break;
448:         }
449:     }
450: 
451:     if ( ! isset( $meta->post_id ) ) {
452:         $meta->post_id = 0;
453:     }
454: 
455:     if ( ! $save_value && is_array( $meta->value ) ) {
456:         $meta->value = [$meta->value];
457:     }
458: 
459:     if ( papi_is_empty( $meta->value ) ) {
460:         papi_cache_delete( $meta->slug, $meta->post_id );
461: 
462:         if ( $option ) {
463:             return delete_option( papi_remove_papi( $meta->slug ) );
464:         } else {
465:             return delete_post_meta( $meta->post_id, papi_remove_papi( $meta->slug ) );
466:         }
467:     }
468: 
469:     $result = true;
470: 
471:     foreach ( papi_to_array( $meta->value ) as $key => $value ) {
472:         papi_cache_delete( $meta->slug, $meta->post_id );
473: 
474:         if ( ! is_array( $value ) ) {
475:             if ( $save_value ) {
476:                 $value = $meta->value;
477:             }
478: 
479:             if ( $option ) {
480:                 $out = update_option( papi_remove_papi( $meta->slug ), $value );
481:                 $result = $out ? $result : $out;
482:             } else {
483:                 $out = update_post_meta( $meta->post_id, papi_remove_papi( $meta->slug ), $value );
484:                 $result = $out ? $result : $out;
485:             }
486: 
487:             continue;
488:         }
489: 
490:         foreach ( $value as $child_key => $child_value ) {
491:             if ( papi_is_empty( $child_value ) ) {
492:                 if ( $option ) {
493:                     delete_option( papi_remove_papi( $child_key ) );
494:                 } else {
495:                     delete_post_meta( $meta->post_id, papi_remove_papi( $child_key ) );
496:                 }
497:             } else {
498:                 if ( $option ) {
499:                     update_option( papi_remove_papi( $child_key ), $child_value );
500:                 } else {
501:                     update_post_meta( $meta->post_id, papi_remove_papi( $child_key ), $child_value );
502:                 }
503:             }
504:         }
505:     }
506: 
507:     return $result;
508: }
509: 
510: /**
511:  * Convert array of arrays to array of slugs.
512:  * The given slug will match a key with the number of properties.
513:  *
514:  * @param  array  $value
515:  * @param  string $slug
516:  *
517:  * @return array
518:  */
519: function papi_to_property_array_slugs( array $value, $slug ) {
520:     $results = [];
521:     $counter = [];
522: 
523:     foreach ( $value as $index => $arr ) {
524: 
525:         if ( ! is_array( $arr ) ) {
526:             continue;
527:         }
528: 
529:         $counter[] = $arr;
530: 
531:         foreach ( $arr as $key => $val ) {
532: 
533:             if ( ! is_string( $key ) || empty( $key ) ) {
534:                 continue;
535:             }
536: 
537:             if ( $key[0] !== '_' ) {
538:                 $key = '_' . $key;
539:             }
540: 
541:             $item_slug = $slug . '_' . $index . $key;
542: 
543:             if ( papi_is_property_type_key( $item_slug ) ) {
544:                 $item_slug = papi_f( $item_slug );
545:             }
546: 
547:             $results[$item_slug] = $val;
548:         }
549:     }
550: 
551:     $results[$slug] = count( $counter );
552: 
553:     return $results;
554: }
555: 
API documentation generated by ApiGen