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:  * Check if the page type should be displayed or not.
  5:  *
  6:  * @param  string|object $page_type
  7:  *
  8:  * @return bool
  9:  */
 10: function papi_display_page_type( $page_type ) {
 11:     $post_type = papi_get_post_type();
 12: 
 13:     if ( empty( $post_type ) ) {
 14:         return false;
 15:     }
 16: 
 17:     if ( is_string( $page_type ) ) {
 18:         $page_type = papi_get_page_type_by_id( $page_type );
 19:     }
 20: 
 21:     if ( ! is_object( $page_type ) ) {
 22:         return false;
 23:     }
 24: 
 25:     if ( ! in_array( $post_type, $page_type->post_type ) ) {
 26:         return false;
 27:     }
 28: 
 29:     $display = $page_type->display( $post_type );
 30: 
 31:     if ( ! is_bool( $display ) || $display === false ) {
 32:         return false;
 33:     }
 34: 
 35:     if ( preg_match( '/papi\-standard\-\w+\-type/', $page_type->get_id() ) ) {
 36:         return true;
 37:     }
 38: 
 39:     $parent_page_type = papi_get_page_type_by_post_id( papi_get_parent_post_id() );
 40: 
 41:     if ( papi_is_page_type( $parent_page_type ) ) {
 42:         $child_types = $parent_page_type->get_child_types();
 43: 
 44:         if ( ! empty( $child_types ) ) {
 45:             return in_array( $page_type, $parent_page_type->get_child_types() );
 46:         }
 47:     }
 48: 
 49:     // Run show page type filter.
 50:     return papi_filter_settings_show_page_type( $post_type, $page_type );
 51: }
 52: 
 53: /**
 54:  * Get all page types that exists.
 55:  *
 56:  * @param  bool   $all
 57:  * @param  string $post_type
 58:  * @param  bool   $fake_post_types
 59:  *
 60:  * @return array
 61:  */
 62: function papi_get_all_page_types( $all = false, $post_type = null, $fake_post_types = false ) {
 63:     if ( empty( $post_type ) ) {
 64:         $post_type  = papi_get_post_type();
 65:     }
 66: 
 67:     $cache_key   = papi_cache_key( sprintf( '%s_%s', $all, $post_type ), $fake_post_types );
 68:     $page_types  = wp_cache_get( $cache_key );
 69:     $load_once   = papi_filter_core_load_one_type_on();
 70: 
 71:     if ( empty( $page_types ) ) {
 72:         $files = papi_get_all_page_type_files();
 73: 
 74:         foreach ( $files as $file ) {
 75:             $page_type = papi_get_page_type( $file );
 76: 
 77:             if ( is_null( $page_type ) ) {
 78:                 continue;
 79:             }
 80: 
 81:             if ( $page_type instanceof Papi_Page_Type === false ) {
 82:                 continue;
 83:             }
 84: 
 85:             if ( papi()->exists( 'core.page_type.' . $page_type->post_type[0] ) ) {
 86:                 if ( ! empty( $page_types ) ) {
 87:                     continue;
 88:                 }
 89:             } else if ( in_array( $page_type->post_type[0], $load_once ) ) {
 90:                 papi()->singleton( 'core.page_type.' . $page_type->post_type[0], $page_type->get_id() );
 91:             }
 92: 
 93:             if ( $fake_post_types ) {
 94:                 if ( isset( $page_type->post_type[0] ) && ! post_type_exists( $page_type->post_type[0] ) ) {
 95:                     // Boot page type.
 96:                     $page_type->boot();
 97: 
 98:                     // Add it to the page types array.
 99:                     $page_types[] = $page_type;
100:                 }
101:                 continue;
102:             } else if ( $page_type instanceof Papi_Option_Type ) {
103:                 continue;
104:             }
105: 
106:             // Add the page type if the post types is allowed.
107:             if ( ! is_null( $page_type ) && papi_current_user_is_allowed( $page_type->capabilities ) && ( $all || in_array( $post_type, $page_type->post_type ) ) ) {
108:                 // Boot page type.
109:                 $page_type->boot();
110: 
111:                 // Add it to the page types array.
112:                 $page_types[] = $page_type;
113:             }
114:         }
115: 
116:         if ( is_array( $page_types ) ) {
117:             usort( $page_types, function ( $a, $b ) {
118:                 return strcmp( $a->name, $b->name );
119:             } );
120: 
121:             wp_cache_set( $cache_key, $page_types );
122:         }
123:     }
124: 
125:     if ( ! is_array( $page_types ) ) {
126:         return [];
127:     }
128: 
129:     return papi_sort_order( array_reverse( $page_types ) );
130: }
131: 
132: /**
133:  * Get number of how many pages uses the given page type.
134:  * This will also work with only page type.
135:  *
136:  * @param  string|object $page_type
137:  *
138:  * @return int
139:  */
140: function papi_get_number_of_pages( $page_type ) {
141:     global $wpdb;
142: 
143:     if ( empty( $page_type ) || ( ! is_string( $page_type ) && ( ! is_object( $page_type ) ) ) ) {
144:         return 0;
145:     }
146: 
147:     if ( is_object( $page_type ) && method_exists( $page_type, 'get_id' ) ) {
148:         $page_type = $page_type->get_id();
149:     }
150: 
151:     if ( ! is_string( $page_type ) ) {
152:         return 0;
153:     }
154: 
155:     $value = papi_cache_get( 'page_type', $page_type );
156: 
157:     if ( $value === false ) {
158:         $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}postmeta WHERE `meta_key` = '%s' AND `meta_value` = '%s'";
159:         $sql = $wpdb->prepare( $sql, papi_get_page_type_key(), $page_type );
160: 
161:         $value = intval( $wpdb->get_var( $sql ) );
162:         papi_cache_set( 'page_type', $page_type, $value );
163:     }
164: 
165:     return $value;
166: }
167: 
168: /**
169:  * Get the data page.
170:  *
171:  * @param  int    $post_id
172:  * @param  string $type
173:  *
174:  * @return Papi_Core_Page|null
175:  */
176: function papi_get_page( $post_id = 0, $type = 'post' ) {
177:     return Papi_Core_Page::factory( $post_id, $type );
178: }
179: 
180: /**
181:  * Get a page type by file path.
182:  *
183:  * @param  string $file_path
184:  *
185:  * @return Papi_Page_Type
186:  */
187: function papi_get_page_type( $file_path ) {
188:     if ( ! is_file( $file_path ) || ! is_string( $file_path ) ) {
189:         return;
190:     }
191: 
192:     $class_name = papi_get_class_name( $file_path );
193: 
194:     if ( empty( $class_name ) ) {
195:         return;
196:     }
197: 
198:     // Try to add the page type to the container.
199:     if ( ! papi()->exists( $class_name ) ) {
200: 
201:         // @codeCoverageIgnoreStart
202:         if ( ! class_exists( $class_name ) ) {
203:             require_once $file_path;
204:         }
205:         // @codeCoverageIgnoreEnd
206: 
207:         $rc         = new ReflectionClass( $class_name );
208:         $page_type  = $rc->newInstanceArgs( [$file_path] );
209: 
210:         // If the page type don't have a name we can't use it.
211:         if ( ! $page_type->has_name() ) {
212:             return;
213:         }
214: 
215:         papi()->singleton( $class_name, $page_type );
216:     }
217: 
218:     return papi()->make( $class_name );
219: }
220: 
221: /**
222:  * Get page type by identifier.
223:  *
224:  * @param  string $id
225:  *
226:  * @return Papi_Page_Type
227:  */
228: function papi_get_page_type_by_id( $id ) {
229:     if ( ! is_string( $id ) || empty( $id ) ) {
230:         return;
231:     }
232: 
233:     $result     = null;
234:     $page_types = papi_get_all_page_types( true );
235: 
236:     foreach ( $page_types as $page_type ) {
237:         if ( $page_type->match_id( $id ) ) {
238:             $result = $page_type;
239:             break;
240:         }
241:     }
242: 
243:     if ( is_null( $result ) ) {
244:         $path   = papi_get_file_path( $id );
245:         $result = papi_get_page_type( $path );
246:     }
247: 
248:     return $result;
249: }
250: 
251: /**
252:  * Get page type from post id.
253:  *
254:  * @param  int $post_id
255:  *
256:  * @return Papi_Page_Type
257:  */
258: function papi_get_page_type_by_post_id( $post_id = 0 ) {
259:     if ( ! is_numeric( $post_id ) ) {
260:         return;
261:     }
262: 
263:     $post_id = papi_get_post_id( $post_id );
264: 
265:     if ( $post_id === 0 ) {
266:         return;
267:     }
268: 
269:     if ( $page_type = papi_get_page_type_id( $post_id ) ) {
270:         return papi_get_page_type_by_id( $page_type );
271:     }
272: }
273: 
274: /**
275:  * Get page type id.
276:  *
277:  * @param  int $post_id
278:  *
279:  * @return string
280:  */
281: function papi_get_page_type_id( $post_id = 0 ) {
282:     $post_id   = papi_get_post_id( $post_id );
283:     $key       = papi_get_page_type_key();
284:     $page_type = '';
285: 
286:     if ( $post_id !== 0 ) {
287:         $meta_value = get_post_meta( $post_id, $key, true );
288:         $page_type  = empty( $meta_value ) ? '' : $meta_value;
289:     }
290: 
291:     if ( empty( $page_type ) ) {
292:         $page_type = str_replace( 'papi/', '', papi_get_qs( 'page_type' ) );
293:     }
294: 
295:     if ( empty( $page_type ) ) {
296:         $page_type = papi_get_sanitized_post( papi_get_page_type_key() );
297:     }
298: 
299:     // Load right page type from a post query string
300:     if ( empty( $page_type ) ) {
301:         $meta_value = get_post_meta( papi_get_parent_post_id(), $key, true );
302:         $page_type  = empty( $meta_value ) ? '' : $meta_value;
303:     }
304: 
305:     // Load page type id from the container if it exists or
306:     // load it from `papi_get_all_page_types`.
307:     if ( empty( $page_type ) ) {
308:         $post_type      = papi_get_post_type();
309:         $load_once      = papi_filter_core_load_one_type_on();
310:         $collection_key = 'core.page_type.' . $post_type;
311: 
312:         if ( in_array( $post_type, $load_once ) ) {
313:             if ( papi()->exists( $collection_key )  ) {
314:                 return papi()->make( $collection_key );
315:             }
316: 
317:             if ( $page_types = papi_get_all_page_types( false, $post_type ) ) {
318:                 return $page_types[0]->get_id();
319:             }
320:         }
321:     }
322: 
323:     return $page_type;
324: }
325: 
326: /**
327:  * Get the page type key that is used for each post.
328:  *
329:  * @return string
330:  */
331: function papi_get_page_type_key() {
332:     return defined( 'PAPI_PAGE_TYPE_KEY' ) ? PAPI_PAGE_TYPE_KEY : '_papi_page_type';
333: }
334: 
335: /**
336:  * Get the Page type name.
337:  *
338:  * @param  int $post_id
339:  *
340:  * @return string
341:  */
342: function papi_get_page_type_name( $post_id = 0 ) {
343:     $post_id = papi_get_post_id( $post_id );
344: 
345:     if ( empty( $post_id ) ) {
346:         return '';
347:     }
348: 
349:     $page_type_id = papi_get_page_type_id( $post_id );
350: 
351:     if ( empty( $page_type_id ) ) {
352:         return '';
353:     }
354: 
355:     $page_type = papi_get_page_type_by_id( $page_type_id );
356: 
357:     if ( empty( $page_type ) ) {
358:         return '';
359:     }
360: 
361:     return $page_type->name;
362: }
363: 
364: /**
365:  * Get template file from post id.
366:  *
367:  * @param  int|string $post_id
368:  *
369:  * @return null|string
370:  */
371: function papi_get_page_type_template( $post_id = 0 ) {
372:     if ( empty( $post_id ) && ! is_numeric( $post_id ) ) {
373:         return;
374:     }
375: 
376:     $data = papi_get_page_type_by_post_id( $post_id );
377: 
378:     if ( isset( $data ) && isset( $data->template ) ) {
379:         $template  = $data->template;
380:         $extension = '.php';
381:         $ext_reg   = '/(' . $extension . ')+$/';
382: 
383:         if ( preg_match( '/\.\w+$/', $template, $matches ) && preg_match( $ext_reg, $matches[0] ) ) {
384:             return str_replace( '.', '/', preg_replace( '/' . $matches[0] . '$/', '', $template ) ) . $matches[0];
385:         } else {
386:             $template = str_replace( '.', '/', $template );
387:             return substr( $template, -strlen( $extension ) ) === $extension
388:                 ? $template : $template . $extension;
389:         }
390:     }
391: }
392: 
393: /**
394:  * Get all post types Papi should work with.
395:  *
396:  * @return array
397:  */
398: function papi_get_post_types() {
399:     $page_types = papi_get_all_page_types( true );
400:     $post_types = [];
401: 
402:     foreach ( $page_types as $page_type ) {
403:         $post_types = array_merge(
404:             $post_types,
405:             papi_to_array( $page_type->post_type )
406:         );
407:     }
408: 
409:     if ( empty( $post_types ) ) {
410:         return ['page'];
411:     }
412: 
413:     return array_unique( $post_types );
414: }
415: 
416: /**
417:  * Get boxes with properties slug for a page.
418:  *
419:  * @param  int $post_id
420:  *
421:  * @return array
422:  */
423: function papi_get_slugs( $post_id = 0 ) {
424:     $page = papi_get_page( $post_id );
425: 
426:     if ( $page instanceof Papi_Post_Page === false ) {
427:         return [];
428:     }
429: 
430:     $page_type = $page->get_page_type();
431: 
432:     if ( empty( $page_type ) ) {
433:         return [];
434:     }
435: 
436:     $value = [];
437:     $boxes = $page_type->get_boxes();
438: 
439:     foreach ( $boxes as $box ) {
440:         if ( count( $box ) < 2 || empty( $box[0]['title'] ) || ! is_array( $box[1] ) ) {
441:             continue;
442:         }
443: 
444:         if ( ! isset( $value[$box[0]['title']] ) ) {
445:             $value[$box[0]['title']] = [];
446:         }
447: 
448:         foreach ( $box[1] as $property ) {
449:             $value[$box[0]['title']][] = $property->get_slug( true );
450:         }
451:     }
452: 
453:     return $value;
454: }
455: 
456: /**
457:  * Check if `$obj` is a instanceof `Papi_Option_Type`.
458:  *
459:  * @param  mixed $obj
460:  *
461:  * @return bool
462:  */
463: function papi_is_option_type( $obj ) {
464:     return $obj instanceof Papi_Option_Type && $obj->get_post_type() === '_papi_option_type';
465: }
466: 
467: /**
468:  * Check if `$obj` is a instanceof `Papi_Page_Type`.
469:  *
470:  * @param  mixed $obj
471:  *
472:  * @return bool
473:  */
474: function papi_is_page_type( $obj ) {
475:     return $obj instanceof Papi_Page_Type && ! papi_is_option_type( $obj );
476: }
477: 
478: /**
479:  * Check if page type exists.
480:  *
481:  * @param  string $id
482:  *
483:  * @return bool
484:  */
485: function papi_page_type_exists( $id ) {
486:     $exists     = false;
487:     $page_types = papi_get_all_page_types( true );
488: 
489:     foreach ( $page_types as $page_type ) {
490:         if ( $page_type->match_id( $id ) ) {
491:             $exists = true;
492:             break;
493:         }
494:     }
495: 
496:     return $exists;
497: }
498: 
499: /**
500:  * Set page type to a post.
501:  *
502:  * @param  mixed $post_id
503:  * @param  string $page_type
504:  *
505:  * @return bool
506:  */
507: function papi_set_page_type_id( $post_id, $page_type ) {
508:     return papi_page_type_exists( $page_type ) && update_post_meta(
509:         papi_get_post_id( $post_id ),
510:         papi_get_page_type_key(),
511:         $page_type
512:     );
513: }
514: 
515: /**
516:  * Echo the Page type name.
517:  *
518:  * @param  int $post_id
519:  *
520:  * @return string
521:  */
522: function the_papi_page_type_name( $post_id = 0 ) {
523:     echo papi_get_page_type_name( $post_id );
524: }
525: 
API documentation generated by ApiGen