1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
<?php
function papi_body_class( array $classes ) {
$class = papi_get_entry_type_css_class();
if ( empty( $class ) ) {
return $classes;
}
$classes[] = $class;
return $classes;
}
add_filter( 'body_class', 'papi_body_class' );
function papi_get_template_file_name( $template ) {
if ( ! is_string( $template ) ) {
return;
}
$extension = '.php';
$ext_reg = '/(' . $extension . ')+$/';
if ( preg_match( '/\.\w+$/', $template, $matches ) && preg_match( $ext_reg, $matches[0] ) ) {
return str_replace( '.', '/', preg_replace( '/' . $matches[0] . '$/', '', $template ) ) . $matches[0];
}
$template = str_replace( '.', '/', $template );
$template = substr( $template, -strlen( $extension ) ) === $extension ? $template : $template . $extension;
return $template === $extension ? null : $template;
}
function papi_include_template( $file, array $vars = [] ) {
if ( ! is_string( $file ) || empty( $file ) ) {
return;
}
$path = PAPI_PLUGIN_DIR;
$path = rtrim( $path, '/' ) . '/';
if ( file_exists( $path . $file ) ) {
require $path . $file;
}
}
function papi_template( $file, array $values = [], $convert_to_object = false ) {
if ( ! is_string( $file ) || empty( $file ) ) {
return [];
}
$filepath = papi_get_file_path( $file );
if ( empty( $filepath ) && is_file( $file ) ) {
$filepath = $file;
}
if ( empty( $filepath ) || ! file_exists( $filepath ) || is_dir( $filepath ) ) {
return [];
}
$template = require $filepath;
if ( papi_is_property( $template ) ) {
foreach ( $values as $key => $value ) {
$template->set_option( $key, $value );
}
$result = $template;
} else {
$result = array_merge( (array) $template, $values );
}
if ( $convert_to_object ) {
return (object) $result;
}
return $result;
}
function papi_template_include( $original_template ) {
global $post;
if ( ! is_single() && ! is_page() && ! is_tag() ) {
return $original_template;
}
$id = is_tag() ? get_queried_object()->term_id : $post->ID;
if ( $page_template = papi_get_entry_type_template( $id ) ) {
if ( $template = locate_template( $page_template ) ) {
return apply_filters( 'papi/template_include', $template );
}
}
return $original_template;
}
add_filter( 'template_include', 'papi_template_include' );