1: <?php
2:
3: 4: 5: 6: 7: 8: 9:
10: function papi_body_class( array $classes ) {
11: global $post;
12:
13:
14: if ( ! is_single() && ! is_page() ) {
15: return $classes;
16: }
17:
18: $page_type = get_post_meta( $post->ID, papi_get_page_type_key(), true );
19:
20: if ( empty( $page_type ) ) {
21: return $classes;
22: }
23:
24: $parts = explode( '/', $page_type );
25:
26: if ( empty( $parts ) || empty( $parts[0] ) ) {
27: return $classes;
28: }
29:
30: $classes[] = array_pop( $parts );
31:
32: return $classes;
33: }
34:
35: add_filter( 'body_class', 'papi_body_class' );
36:
37: 38: 39: 40: 41: 42:
43: function papi_include_template( $file, $vars = [] ) {
44: if ( ! is_string( $file ) || empty( $file ) ) {
45: return;
46: }
47:
48: $path = PAPI_PLUGIN_DIR;
49: $path = rtrim( $path, '/' ) . '/';
50:
51: if ( file_exists( $path . $file ) ) {
52: require $path . $file;
53: }
54: }
55:
56: 57: 58: 59: 60: 61: 62: 63: 64:
65: function papi_template( $file, $values = [], $convert_to_object = false ) {
66: if ( ! is_string( $file ) || empty( $file ) ) {
67: return [];
68: }
69:
70: $filepath = papi_get_file_path( $file );
71:
72: if ( empty( $filepath ) && is_file( $file ) ) {
73: $filepath = $file;
74: }
75:
76: if ( empty( $filepath ) || ! file_exists( $filepath ) || is_dir( $filepath ) ) {
77: return [];
78: }
79:
80: $template = require $filepath;
81:
82: if ( papi_is_property( $template ) ) {
83: foreach ( $values as $key => $value ) {
84: $template->set_option( $key, $value );
85: }
86:
87: $result = $template;
88: } else {
89: $result = array_merge( (array) $template, $values );
90: }
91:
92: if ( $convert_to_object ) {
93: return (object) $result;
94: }
95:
96: return $result;
97: }
98:
99: 100: 101: 102: 103: 104: 105:
106: function papi_template_include( $original_template ) {
107: global $post;
108:
109:
110: if ( ! is_single() && ! is_page() ) {
111: return $original_template;
112: }
113:
114: $page_template = papi_get_page_type_template( $post->ID );
115:
116: if ( ! empty( $page_template ) ) {
117: $path = get_template_directory();
118: $path = trailingslashit( $path );
119: $file = $path . $page_template;
120:
121: if ( file_exists( $file ) && ! is_dir( $file ) ) {
122: return $file;
123: }
124: }
125:
126: return $original_template;
127: }
128:
129: add_filter( 'template_include', 'papi_template_include' );
130: