forked from johnbillion/extended-template-parts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextended-template-parts.php
193 lines (163 loc) · 5.26 KB
/
extended-template-parts.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/**
* Extended template parts for WordPress.
*
* @package ExtendedTemplateParts
* @version 1.1.0
* @author John Blackbourn <https://johnblackbourn.com>
* @link https://github.com/johnbillion/extended-template-parts
* @copyright 2012-2018 John Blackbourn
* @license GPL v2 or later
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/**
* Output a template part.
*
* This function is functionally identical to `get_template_part()`. In addition, it allows you to pass in
* variables for use in the template part using the `$vars` argument, which will then be available in the
* `$this->vars` property from within the template part.
*
* @param string $slug The slug name for the generic template.
* @param string $name The name of the specialised template.
* @param array $vars Variables for use within the template part.
* @param array $args {
* Arguments for the template part.
*
* @type int|false $cache The number of seconds this template part should be cached for, or boolean false
* for no caching. Default false.
* @type string $dir The theme subdirectory to look in for template parts. Default 'template-parts'.
* }
*/
function get_extended_template_part( $slug, $name = '', array $vars = [], array $args = [] ) {
$template = new Extended_Template_Part( $slug, $name, $vars, $args );
$dir = $template->args['dir'];
/* This action is documented in WordPress core: wp-includes/general-template.php */
do_action( "get_template_part_{$dir}/{$slug}", "{$dir}/{$slug}", $name );
echo $template->get_output(); // WPCS: XSS ok.
}
class Extended_Template_Part {
public $slug = '';
public $name = '';
public $args = [];
public $vars = [];
protected $template = null;
/**
* Class constructor.
*
* @see get_extended_template_part()
*
* @param string $slug The slug name for the generic template.
* @param string $name The name of the specialised template.
* @param array $vars Variables for use within the template part.
* @param array $args Arguments for the template part.
*/
public function __construct( $slug, $name = '', array $vars = [], array $args = [] ) {
$args = wp_parse_args( $args, array(
'cache' => false,
'dir' => 'template-parts',
) );
$this->slug = $slug;
$this->name = $name;
$this->args = $args;
$this->set_vars( $vars );
}
/**
* Get the output of the template part.
*
* @return string The template part output.
*/
public function get_output() {
if ( false === $this->args['cache'] || ! $output = $this->get_cache() ) {
ob_start();
if ( $this->has_template() ) {
$this->load_template( $this->locate_template() );
}
$output = ob_get_clean();
if ( false !== $this->args['cache'] ) {
$this->set_cache( $output );
}
}
return $output;
}
/**
* Is the requested template part available?
*
* @return boolean Whether the template part is available.
*/
public function has_template() {
return !! $this->locate_template();
}
/**
* Set the variables available to this template part.
*
* @param array $vars Template variables.
*/
public function set_vars( array $vars ) {
$this->vars = array_merge( $this->vars, $vars );
}
/**
* Locate the template part file according to the slug and name.
*
* @return string The template part file name. Empty string if none is found.
*/
protected function locate_template() {
if ( isset( $this->template ) ) {
return $this->template;
}
$templates = [];
if ( ! empty( $this->name ) ) {
$templates[] = "{$this->args['dir']}/{$this->slug}-{$this->name}.php";
}
$templates[] = "{$this->args['dir']}/{$this->slug}.php";
$this->template = locate_template( $templates );
if ( 0 !== validate_file( $this->template ) ) {
$this->template = '';
}
return $this->template;
}
/**
* Load the template part.
*
* @param string $template_file The template part file path.
*/
protected function load_template( $template_file ) {
global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
if ( 0 !== validate_file( $template_file ) ) {
return;
}
require $template_file;
}
/**
* Get the cached version of the template part output.
*
* @return string|false The cached output, or boolean false if there is no cached version.
*/
protected function get_cache() {
return get_transient( $this->cache_key() );
}
/**
* Cache the template part output.
*
* @param string $output The template part output.
*/
protected function set_cache( $output ) {
return set_transient( $this->cache_key(), $output, $this->args['cache'] );
}
/**
* Get the template part cache key.
*
* @return string The cache key.
*/
protected function cache_key() {
return 'part_' . md5( $this->locate_template() . '/' . wp_json_encode( $this->args ) );
}
}