1: <?php
2:
3: 4: 5: 6: 7: 8: 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:
50: return papi_filter_settings_show_page_type( $post_type, $page_type );
51: }
52:
53: 54: 55: 56: 57: 58: 59: 60: 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:
96: $page_type->boot();
97:
98:
99: $page_types[] = $page_type;
100: }
101: continue;
102: } else if ( $page_type instanceof Papi_Option_Type ) {
103: continue;
104: }
105:
106:
107: if ( ! is_null( $page_type ) && papi_current_user_is_allowed( $page_type->capabilities ) && ( $all || in_array( $post_type, $page_type->post_type ) ) ) {
108:
109: $page_type->boot();
110:
111:
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: 134: 135: 136: 137: 138: 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: 170: 171: 172: 173: 174: 175:
176: function papi_get_page( $post_id = 0, $type = 'post' ) {
177: return Papi_Core_Page::factory( $post_id, $type );
178: }
179:
180: 181: 182: 183: 184: 185: 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:
199: if ( ! papi()->exists( $class_name ) ) {
200:
201:
202: if ( ! class_exists( $class_name ) ) {
203: require_once $file_path;
204: }
205:
206:
207: $rc = new ReflectionClass( $class_name );
208: $page_type = $rc->newInstanceArgs( [$file_path] );
209:
210:
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: 223: 224: 225: 226: 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: 253: 254: 255: 256: 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: 276: 277: 278: 279: 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:
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:
306:
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: 328: 329: 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: 337: 338: 339: 340: 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: 366: 367: 368: 369: 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: 395: 396: 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: 418: 419: 420: 421: 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: 458: 459: 460: 461: 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: 469: 470: 471: 472: 473:
474: function papi_is_page_type( $obj ) {
475: return $obj instanceof Papi_Page_Type && ! papi_is_option_type( $obj );
476: }
477:
478: 479: 480: 481: 482: 483: 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: 501: 502: 503: 504: 505: 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: 517: 518: 519: 520: 521:
522: function the_papi_page_type_name( $post_id = 0 ) {
523: echo papi_get_page_type_name( $post_id );
524: }
525: