1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Windwalker\Renderer\BladeRenderer; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Template Loader |
7
|
|
|
* |
8
|
|
|
* Loads the corresponding template based on request |
9
|
|
|
*/ |
10
|
|
|
class ClassyTemplate { |
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Theme twig templates folder |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
public static $theme_templates_folder = 'views'; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Return theme template absolute path |
22
|
|
|
* |
23
|
|
|
* @param string $template in blade path format, ex: base.header |
24
|
|
|
* @return string full path to template file |
25
|
|
|
*/ |
26
|
|
|
public static function get_theme_template_path($template) { |
27
|
|
|
|
28
|
|
|
$template = self::get_nested_blade_path($template); |
29
|
|
|
|
30
|
|
|
$template = str_replace('.', '/', $template); |
31
|
|
|
|
32
|
|
|
return THEME_PATH . self::$theme_templates_folder . '/' . $template . '.blade.php'; |
33
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Checks if template exists |
38
|
|
|
* |
39
|
|
|
* @param string $template in blade path format, ex: base.header |
40
|
|
|
* @return boolean true/false |
41
|
|
|
*/ |
42
|
|
|
public static function template_exists($template) { |
43
|
|
|
|
44
|
|
|
$template_path = self::get_theme_template_path($template); |
45
|
|
|
|
46
|
|
|
return file_exists($template_path); |
47
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Returns template name for render, based on type of request |
53
|
|
|
* |
54
|
|
|
* @param string $type |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public static function get_available_template($type) { |
58
|
|
|
|
59
|
|
|
$templates = self::get_request_templates_list($type); |
60
|
|
|
|
61
|
|
|
foreach ($templates as $template) { |
62
|
|
|
|
63
|
|
|
if ( self::template_exists($template) ): |
64
|
|
|
|
65
|
|
|
return $template; |
66
|
|
|
|
67
|
|
|
endif; |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return false; |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns list of templates to check, based on type of request |
78
|
|
|
* |
79
|
|
|
* @param string $type |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
private static function get_request_templates_list($type) { |
83
|
|
|
|
84
|
|
|
$templates = array(); |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
// Home |
88
|
|
|
|
89
|
|
|
if ( $type == 'home' ) : |
90
|
|
|
|
91
|
|
|
$templates[] = 'home'; |
92
|
|
|
$templates[] = 'index'; |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
// Single |
96
|
|
|
|
97
|
|
|
elseif ( $type == 'single' ) : |
98
|
|
|
|
99
|
|
|
$post_type = get_query_var( 'post_type' ); |
100
|
|
|
|
101
|
|
|
$templates[] = 'single-' . $post_type; |
102
|
|
|
|
103
|
|
|
$templates[] = 'single'; |
104
|
|
|
|
105
|
|
|
// Post type |
106
|
|
|
|
107
|
|
|
elseif ( $type == 'post_type_archive' ) : |
108
|
|
|
|
109
|
|
|
$post_type = get_query_var( 'post_type' ); |
110
|
|
|
|
111
|
|
|
$templates[] = 'archive-' . $post_type; |
112
|
|
|
|
113
|
|
|
$templates[] = 'archive'; |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
// Taxonomy |
117
|
|
|
|
118
|
|
|
elseif ( $type == 'taxonomy' ): |
119
|
|
|
|
120
|
|
|
$term = get_queried_object(); |
121
|
|
|
|
122
|
|
|
if ( ! empty( $term->slug ) ) { |
123
|
|
|
|
124
|
|
|
$taxonomy = $term->taxonomy; |
125
|
|
|
|
126
|
|
|
$templates[] = "taxonomy-$taxonomy-{$term->slug}"; |
127
|
|
|
$templates[] = "taxonomy-$taxonomy"; |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$templates[] = 'taxonomy'; |
132
|
|
|
|
133
|
|
|
// Category |
134
|
|
|
|
135
|
|
View Code Duplication |
elseif ( $type == 'category' ): |
|
|
|
|
136
|
|
|
|
137
|
|
|
$category = get_queried_object(); |
138
|
|
|
|
139
|
|
|
if ( ! empty( $category->slug ) ) { |
140
|
|
|
$templates[] = "category-{$category->slug}"; |
141
|
|
|
$templates[] = "category-{$category->term_id}"; |
142
|
|
|
} |
143
|
|
|
$templates[] = 'category'; |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
// Attachment |
147
|
|
|
|
148
|
|
|
elseif ( $type == 'attachment' ): |
149
|
|
|
|
150
|
|
|
$attachment = get_queried_object(); |
151
|
|
|
|
152
|
|
|
if ( $attachment ) { |
153
|
|
|
|
154
|
|
|
if ( false !== strpos( $attachment->post_mime_type, '/' ) ) { |
155
|
|
|
|
156
|
|
|
list( $type, $subtype ) = explode( '/', $attachment->post_mime_type ); |
157
|
|
|
|
158
|
|
|
} else { |
159
|
|
|
|
160
|
|
|
list( $type, $subtype ) = array( $attachment->post_mime_type, '' ); |
161
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if ( ! empty( $subtype ) ) { |
165
|
|
|
$templates[] = "{$type}-{$subtype}"; |
166
|
|
|
$templates[] = "{$subtype}"; |
167
|
|
|
} |
168
|
|
|
$templates[] = "{$type}"; |
169
|
|
|
|
170
|
|
|
} |
171
|
|
|
$templates[] = 'attachment'; |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
// Tag |
175
|
|
|
|
176
|
|
View Code Duplication |
elseif ( $type == 'tag' ): |
|
|
|
|
177
|
|
|
|
178
|
|
|
$tag = get_queried_object(); |
179
|
|
|
|
180
|
|
|
if ( ! empty( $tag->slug ) ) { |
181
|
|
|
$templates[] = "tag-{$tag->slug}"; |
182
|
|
|
$templates[] = "tag-{$tag->term_id}"; |
183
|
|
|
} |
184
|
|
|
$templates[] = 'tag'; |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
// Author |
188
|
|
|
|
189
|
|
|
elseif ( $type == 'author' ): |
190
|
|
|
|
191
|
|
|
$author = get_queried_object(); |
192
|
|
|
|
193
|
|
|
if ( $author instanceof WP_User ) { |
|
|
|
|
194
|
|
|
$templates[] = "author-{$author->user_nicename}"; |
195
|
|
|
$templates[] = "author-{$author->ID}"; |
196
|
|
|
} |
197
|
|
|
$templates[] = 'author'; |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
// Front Page |
201
|
|
|
|
202
|
|
View Code Duplication |
elseif ( $type == 'front-page' ): |
|
|
|
|
203
|
|
|
|
204
|
|
|
$id = get_queried_object_id(); |
205
|
|
|
|
206
|
|
|
$pagename = get_query_var('pagename'); |
207
|
|
|
|
208
|
|
|
if ( ! $pagename && $id ) { |
209
|
|
|
// If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object |
210
|
|
|
$post = get_queried_object(); |
211
|
|
|
if ( $post ) |
212
|
|
|
$pagename = $post->post_name; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$template = get_post_meta('theme-page-template', $id); |
216
|
|
|
|
217
|
|
|
if ( $template != 'index' ) |
218
|
|
|
$templates[] = $template; |
219
|
|
|
if ( $pagename ) |
220
|
|
|
$templates[] = "page-$pagename"; |
221
|
|
|
if ( $id ) |
222
|
|
|
$templates[] = "page-$id"; |
223
|
|
|
$templates[] = ''; |
224
|
|
|
|
225
|
|
|
// Page |
226
|
|
|
|
227
|
|
View Code Duplication |
elseif ( $type == 'page' ): |
|
|
|
|
228
|
|
|
|
229
|
|
|
$id = get_queried_object_id(); |
230
|
|
|
|
231
|
|
|
$template = get_post_meta('theme-page-template', $id); |
232
|
|
|
|
233
|
|
|
$pagename = get_query_var('pagename'); |
234
|
|
|
|
235
|
|
|
if ( ! $pagename && $id ) { |
236
|
|
|
// If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object |
237
|
|
|
$post = get_queried_object(); |
238
|
|
|
if ( $post ) |
239
|
|
|
$pagename = $post->post_name; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
if ( $template != 'index' ) |
243
|
|
|
$templates[] = $template; |
244
|
|
|
if ( $pagename ) |
245
|
|
|
$templates[] = "page-$pagename"; |
246
|
|
|
if ( $id ) |
247
|
|
|
$templates[] = "page-$id"; |
248
|
|
|
$templates[] = 'page'; |
249
|
|
|
|
250
|
|
|
|
251
|
|
|
// Default |
252
|
|
|
|
253
|
|
|
else: |
254
|
|
|
|
255
|
|
|
$templates[] = $type; |
256
|
|
|
|
257
|
|
|
endif; |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
return $templates; |
261
|
|
|
|
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Returns current page template slug |
267
|
|
|
* |
268
|
|
|
* @return string |
269
|
|
|
*/ |
270
|
|
|
public static function get_current_page() { |
271
|
|
|
|
272
|
|
|
if ( is_404() && $template = self::get_available_template('404') ) : |
|
|
|
|
273
|
|
|
|
274
|
|
|
elseif ( is_search() && $template = self::get_available_template('search') ) : |
|
|
|
|
275
|
|
|
|
276
|
|
|
elseif ( is_front_page() && $template = self::get_available_template('front-page') ) : |
|
|
|
|
277
|
|
|
|
278
|
|
|
elseif ( is_home() && $template = self::get_available_template('home') ) : |
|
|
|
|
279
|
|
|
|
280
|
|
|
elseif ( is_post_type_archive() && $template = self::get_available_template('post_type_archive') ) : |
|
|
|
|
281
|
|
|
|
282
|
|
|
elseif ( is_tax() && $template = self::get_available_template('taxonomy') ) : |
|
|
|
|
283
|
|
|
|
284
|
|
|
elseif ( is_attachment() && $template = self::get_available_template('attachment') ) : |
|
|
|
|
285
|
|
|
|
286
|
|
|
elseif ( is_single() && $template = self::get_available_template('single') ) : |
|
|
|
|
287
|
|
|
|
288
|
|
|
elseif ( is_page() && $template = self::get_available_template('page') ) : |
|
|
|
|
289
|
|
|
|
290
|
|
|
elseif ( is_singular() && $template = self::get_available_template('singular') ) : |
|
|
|
|
291
|
|
|
|
292
|
|
|
elseif ( is_category() && $template = self::get_available_template('category') ) : |
|
|
|
|
293
|
|
|
|
294
|
|
|
elseif ( is_tag() && $template = self::get_available_template('tag') ) : |
|
|
|
|
295
|
|
|
|
296
|
|
|
elseif ( is_author() && $template = self::get_available_template('author') ) : |
|
|
|
|
297
|
|
|
|
298
|
|
|
elseif ( is_date() && $template = self::get_available_template('date') ) : |
|
|
|
|
299
|
|
|
|
300
|
|
|
elseif ( is_archive() && $template = self::get_available_template('archive') ) : |
|
|
|
|
301
|
|
|
|
302
|
|
|
elseif ( is_paged() && $template = self::get_available_template('paged') ) : |
|
|
|
|
303
|
|
|
|
304
|
|
|
else : |
305
|
|
|
|
306
|
|
|
$template = 'index'; |
307
|
|
|
|
308
|
|
|
endif; |
309
|
|
|
|
310
|
|
|
|
311
|
|
|
return $template; |
312
|
|
|
|
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Modifies template path to nested that we use for our template structuring |
317
|
|
|
* |
318
|
|
|
* @param string $template ex: single |
319
|
|
|
* @return string single.single (it will look at "single" folder and will find "single.blade.php" template) |
320
|
|
|
*/ |
321
|
|
|
public static function get_nested_blade_path($template) { |
322
|
|
|
|
323
|
|
|
if (preg_match('/\./', $template)) { |
324
|
|
|
|
325
|
|
|
return $template; |
326
|
|
|
|
327
|
|
|
} else { |
328
|
|
|
|
329
|
|
|
return $template . '.' . $template; |
330
|
|
|
|
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Returns available template, based on page argument |
338
|
|
|
* |
339
|
|
|
* @return string |
340
|
|
|
*/ |
341
|
|
|
public static function get_blade_template($page = null) { |
342
|
|
|
|
343
|
|
|
if (!$page) { |
344
|
|
|
$page = self::get_current_page(); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
|
348
|
|
|
$template = self::get_available_template($page); |
349
|
|
|
|
350
|
|
|
if ($template) { |
|
|
|
|
351
|
|
|
|
352
|
|
|
return self::get_nested_blade_path($template); |
353
|
|
|
|
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
return false; |
357
|
|
|
|
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Performs template render. |
362
|
|
|
* If there is $template attribute presented, it will render requested template. |
363
|
|
|
* If it's not it will try to find necessary template based on $wp_query |
364
|
|
|
* |
365
|
|
|
* @param string|null $template template path in blade format, ex: single, base.default, single.partials.slider and etc |
366
|
|
|
* @param array|null $data Additional params |
367
|
|
|
* @return void |
368
|
|
|
*/ |
369
|
|
|
public static function render($template = null, $data = null) { |
370
|
|
|
|
371
|
|
|
$views = THEME_PATH . self::$theme_templates_folder; |
372
|
|
|
$cache = WP_CONTENT_DIR . '/templatecache'; |
373
|
|
|
$common_scope = ClassyScope::get_common_scope(); |
374
|
|
|
|
375
|
|
|
if ($template !== null && is_string($template)) { |
376
|
|
|
|
377
|
|
|
if ($data && is_array($data)) { |
378
|
|
|
|
379
|
|
|
$scope = array_merge($common_scope, $data); |
380
|
|
|
|
381
|
|
|
} else { |
382
|
|
|
|
383
|
|
|
$scope = $common_scope; |
384
|
|
|
|
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
} else { |
388
|
|
|
|
389
|
|
|
$current_page = self::get_current_page(); |
390
|
|
|
|
391
|
|
|
$template = self::get_blade_template($current_page); |
392
|
|
|
|
393
|
|
|
$scope = ClassyScope::get_scope($current_page); |
394
|
|
|
|
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
$renderer = new BladeRenderer($views, array('cache_path' => $cache)); |
|
|
|
|
398
|
|
|
|
399
|
|
|
echo $renderer->render($template, $scope); |
|
|
|
|
400
|
|
|
|
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.