forked from zeen101/issuem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
issuem-templates.php
145 lines (125 loc) · 4.12 KB
/
issuem-templates.php
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
<?php
/**
* Renders overridable templates
*
* @package IssueM
* @since 1.2.4
*/
/**
* Credits to: Stephen Harris
* Src: https://github.com/stephenharris/Event-Organiser/blob/1.7.3/includes/event-organiser-templates.php
*/
if ( ! function_exists( 'issuem_render' ) ) {
/**
* Find a template by $file_name, allowing for overrides in the child and
* parent themes, then return the rendered result
*
* @since 1.2.4
*
* @param string $file_name Name of template to render
* @param array $data A hash array of variables to make available when
* rendering the template
* @return string the rendered template
*/
function issuem_render( $file_name, $data ) {
$template_path = issuem_get_template_path( $file_name );
return issuem_process_render( $template_path, $data );
}
}
if ( ! function_exists( 'issuem_get_template_path' ) ) {
/**
* Return the path to a template allowing overrides by child and parent
* themes
*
* @since 1.2.4
*
* @param string $file_name Name of template to find
* @return string path to template found
*/
function issuem_get_template_path( $file_name ) {
$stack = array(
ISSUEM_PATH_CHILD_THEME . '/issuem',
ISSUEM_PATH_PARENT_THEME . '/issuem',
ISSUEM_PATH . 'templates',
);
return issuem_search_file_path( $file_name, $stack );
}
}
if ( ! function_exists( 'issuem_get_asset_path' ) ) {
/**
* Return the URL to an asset allowing overrides by child and parent themes
*
* @since 1.2.4
*
* @param string $file_name Name of template to find
* @return string path to template found
*/
function issuem_get_asset_path( $file_name ) {
$stack = array(
ISSUEM_PATH_CHILD_THEME => ISSUEM_URL_CHILD_THEME,
ISSUEM_PATH_PARENT_THEME => ISSUEM_URL_PARENT_THEME,
ISSUEM_PATH => ISSUEM_URL,
);
return issuem_search_url_path( $file_name, $stack );
}
}
if ( ! function_exists( 'issuem_search_url_path' ) ) {
/**
* Search through a path stack (dir, url) for the first location to match
* the dir + file name and return the url + file name
*
* When only file paths are needed, use issuem_search_file_path()
*
* @since 1.2.4
*
* @param string $file_name The file to search for
* @param array $stack A hash array of dir, url locations to search
* @return string path to first file found
*/
function issuem_search_url_path( $file_name, $stack ) {
foreach ( $stack as $dir => $url ) {
if ( file_exists( trailingslashit( $dir ) . $file_name ) )
return trailingslashit( $url ) . $file_name;
}
}
}
if ( ! function_exists( 'issuem_search_file_path' ) ) {
/**
* Search through a path array for the first location to contain the file
* name and return dir + filename
*
* @since 1.2.4
*
* @param string $file_name The file to search for
* @param array $shallow_stack An ordered array of file locations to search
* @return string path to first file found
*/
function issuem_search_file_path( $file_name, $shallow_stack ) {
$stack = array();
foreach( $shallow_stack as $dir ) {
$stack[ $dir ] = $dir;
}
return issuem_search_url_path( $file_name, $stack );
}
}
if ( ! function_exists( 'issuem_render' ) ) {
/**
* Template rendering is done by extracting provided variables and simply
* including a PHP file
*
* @since 1.2.4
*
* @param string $file_path The PHP file to include
* @param array $view_data A hash array of of variabled to be
* accessable locally
* @return string rendered file contents
*/
function issuem_process_render( $file_path, $view_data = null ) {
( $view_data ) ? extract( $view_data ) : null;
ob_start();
include ( $file_path );
$template = ob_get_contents();
ob_end_clean();
return $template;
}
}