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
|
|
|
// Classy template? |
60
|
|
|
|
61
|
|
|
if ( $template = self::get_classy_template() ) { |
62
|
|
|
|
63
|
|
|
return $template; |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Else try to find template based on request |
68
|
|
|
|
69
|
|
|
$templates = self::get_request_templates_list($type); |
70
|
|
|
|
71
|
|
|
foreach ($templates as $template) { |
72
|
|
|
|
73
|
|
|
if ( self::template_exists($template) ): |
74
|
|
|
|
75
|
|
|
return $template; |
76
|
|
|
|
77
|
|
|
endif; |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return false; |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns list of templates to check, based on type of request |
88
|
|
|
* |
89
|
|
|
* @param string $type |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
private static function get_request_templates_list($type) { |
93
|
|
|
|
94
|
|
|
$templates = array(); |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
// Home |
98
|
|
|
|
99
|
|
|
if ( $type == 'home' ) : |
100
|
|
|
|
101
|
|
|
$templates[] = 'home'; |
102
|
|
|
$templates[] = 'index'; |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
// Single |
106
|
|
|
|
107
|
|
|
elseif ( $type == 'single' ) : |
108
|
|
|
|
109
|
|
|
$post_type = get_query_var( 'post_type' ); |
110
|
|
|
|
111
|
|
|
$templates[] = 'single-' . $post_type; |
112
|
|
|
|
113
|
|
|
$templates[] = 'single'; |
114
|
|
|
|
115
|
|
|
// Post type |
116
|
|
|
|
117
|
|
|
elseif ( $type == 'post_type_archive' ) : |
118
|
|
|
|
119
|
|
|
$post_type = get_query_var( 'post_type' ); |
120
|
|
|
|
121
|
|
|
$templates[] = 'archive-' . $post_type; |
122
|
|
|
|
123
|
|
|
$templates[] = 'archive'; |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
// Taxonomy |
127
|
|
|
|
128
|
|
|
elseif ( $type == 'taxonomy' ): |
129
|
|
|
|
130
|
|
|
$term = get_queried_object(); |
131
|
|
|
|
132
|
|
|
if ( ! empty( $term->slug ) ) { |
133
|
|
|
|
134
|
|
|
$taxonomy = $term->taxonomy; |
135
|
|
|
|
136
|
|
|
$templates[] = "taxonomy-$taxonomy-{$term->slug}"; |
137
|
|
|
$templates[] = "taxonomy-$taxonomy"; |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$templates[] = 'taxonomy'; |
142
|
|
|
|
143
|
|
|
// Category |
144
|
|
|
|
145
|
|
View Code Duplication |
elseif ( $type == 'category' ): |
|
|
|
|
146
|
|
|
|
147
|
|
|
$category = get_queried_object(); |
148
|
|
|
|
149
|
|
|
if ( ! empty( $category->slug ) ) { |
150
|
|
|
$templates[] = "category-{$category->slug}"; |
151
|
|
|
$templates[] = "category-{$category->term_id}"; |
152
|
|
|
} |
153
|
|
|
$templates[] = 'category'; |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
// Attachment |
157
|
|
|
|
158
|
|
|
elseif ( $type == 'attachment' ): |
159
|
|
|
|
160
|
|
|
$attachment = get_queried_object(); |
161
|
|
|
|
162
|
|
|
if ( $attachment ) { |
163
|
|
|
|
164
|
|
|
if ( false !== strpos( $attachment->post_mime_type, '/' ) ) { |
165
|
|
|
|
166
|
|
|
list( $type, $subtype ) = explode( '/', $attachment->post_mime_type ); |
167
|
|
|
|
168
|
|
|
} else { |
169
|
|
|
|
170
|
|
|
list( $type, $subtype ) = array( $attachment->post_mime_type, '' ); |
171
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if ( ! empty( $subtype ) ) { |
175
|
|
|
$templates[] = "{$type}-{$subtype}"; |
176
|
|
|
$templates[] = "{$subtype}"; |
177
|
|
|
} |
178
|
|
|
$templates[] = "{$type}"; |
179
|
|
|
|
180
|
|
|
} |
181
|
|
|
$templates[] = 'attachment'; |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
// Tag |
185
|
|
|
|
186
|
|
View Code Duplication |
elseif ( $type == 'tag' ): |
|
|
|
|
187
|
|
|
|
188
|
|
|
$tag = get_queried_object(); |
189
|
|
|
|
190
|
|
|
if ( ! empty( $tag->slug ) ) { |
191
|
|
|
$templates[] = "tag-{$tag->slug}"; |
192
|
|
|
$templates[] = "tag-{$tag->term_id}"; |
193
|
|
|
} |
194
|
|
|
$templates[] = 'tag'; |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
// Author |
198
|
|
|
|
199
|
|
|
elseif ( $type == 'author' ): |
200
|
|
|
|
201
|
|
|
$author = get_queried_object(); |
202
|
|
|
|
203
|
|
|
if ( $author instanceof WP_User ) { |
|
|
|
|
204
|
|
|
$templates[] = "author-{$author->user_nicename}"; |
205
|
|
|
$templates[] = "author-{$author->ID}"; |
206
|
|
|
} |
207
|
|
|
$templates[] = 'author'; |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
// Front Page |
211
|
|
|
|
212
|
|
View Code Duplication |
elseif ( $type == 'front-page' ): |
|
|
|
|
213
|
|
|
|
214
|
|
|
$id = get_queried_object_id(); |
215
|
|
|
|
216
|
|
|
$pagename = get_query_var('pagename'); |
217
|
|
|
|
218
|
|
|
if ( ! $pagename && $id ) { |
219
|
|
|
// If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object |
220
|
|
|
$post = get_queried_object(); |
221
|
|
|
if ( $post ) |
222
|
|
|
$pagename = $post->post_name; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$template = get_post_meta('theme-page-template', $id); |
226
|
|
|
|
227
|
|
|
if ( $template != 'index' ) |
228
|
|
|
$templates[] = $template; |
229
|
|
|
if ( $pagename ) |
230
|
|
|
$templates[] = "page-$pagename"; |
231
|
|
|
if ( $id ) |
232
|
|
|
$templates[] = "page-$id"; |
233
|
|
|
$templates[] = ''; |
234
|
|
|
|
235
|
|
|
// Page |
236
|
|
|
|
237
|
|
View Code Duplication |
elseif ( $type == 'page' ): |
|
|
|
|
238
|
|
|
|
239
|
|
|
$id = get_queried_object_id(); |
240
|
|
|
|
241
|
|
|
$template = get_post_meta('theme-page-template', $id); |
242
|
|
|
|
243
|
|
|
$pagename = get_query_var('pagename'); |
244
|
|
|
|
245
|
|
|
if ( ! $pagename && $id ) { |
246
|
|
|
// If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object |
247
|
|
|
$post = get_queried_object(); |
248
|
|
|
if ( $post ) |
249
|
|
|
$pagename = $post->post_name; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
if ( $template != 'index' ) |
253
|
|
|
$templates[] = $template; |
254
|
|
|
if ( $pagename ) |
255
|
|
|
$templates[] = "page-$pagename"; |
256
|
|
|
if ( $id ) |
257
|
|
|
$templates[] = "page-$id"; |
258
|
|
|
$templates[] = 'page'; |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
// Default |
262
|
|
|
|
263
|
|
|
else: |
264
|
|
|
|
265
|
|
|
$templates[] = $type; |
266
|
|
|
|
267
|
|
|
endif; |
268
|
|
|
|
269
|
|
|
|
270
|
|
|
return $templates; |
271
|
|
|
|
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Checks if this is classy custom template |
276
|
|
|
* |
277
|
|
|
* @return boolean |
278
|
|
|
*/ |
279
|
|
|
public static function is_classy_template() { |
280
|
|
|
|
281
|
|
|
return self::get_classy_template() ? true : false; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Returns classy template name or boolean if this is not classy template |
286
|
|
|
* |
287
|
|
|
* @return mixed |
288
|
|
|
*/ |
289
|
|
|
public static function get_classy_template() { |
290
|
|
|
|
291
|
|
|
$template_slug = get_page_template_slug(); |
292
|
|
|
|
293
|
|
|
preg_match('/classy\-(.*)/', $template_slug, $matches); |
294
|
|
|
|
295
|
|
|
if ( $matches && isset($matches[1]) ) return $matches[1]; |
|
|
|
|
296
|
|
|
|
297
|
|
|
return false; |
298
|
|
|
|
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Returns current page template slug |
304
|
|
|
* |
305
|
|
|
* @return string |
306
|
|
|
*/ |
307
|
|
|
public static function get_current_page() { |
308
|
|
|
|
309
|
|
|
if ( is_404() && $template = self::get_available_template('404') ) : |
|
|
|
|
310
|
|
|
|
311
|
|
|
elseif ( is_search() && $template = self::get_available_template('search') ) : |
|
|
|
|
312
|
|
|
|
313
|
|
|
elseif ( is_front_page() && $template = self::get_available_template('front-page') ) : |
|
|
|
|
314
|
|
|
|
315
|
|
|
elseif ( is_home() && $template = self::get_available_template('home') ) : |
|
|
|
|
316
|
|
|
|
317
|
|
|
elseif ( is_post_type_archive() && $template = self::get_available_template('post_type_archive') ) : |
|
|
|
|
318
|
|
|
|
319
|
|
|
elseif ( is_tax() && $template = self::get_available_template('taxonomy') ) : |
|
|
|
|
320
|
|
|
|
321
|
|
|
elseif ( is_attachment() && $template = self::get_available_template('attachment') ) : |
|
|
|
|
322
|
|
|
|
323
|
|
|
elseif ( is_single() && $template = self::get_available_template('single') ) : |
|
|
|
|
324
|
|
|
|
325
|
|
|
elseif ( is_page() && $template = self::get_available_template('page') ) : |
|
|
|
|
326
|
|
|
|
327
|
|
|
elseif ( is_singular() && $template = self::get_available_template('singular') ) : |
|
|
|
|
328
|
|
|
|
329
|
|
|
elseif ( is_category() && $template = self::get_available_template('category') ) : |
|
|
|
|
330
|
|
|
|
331
|
|
|
elseif ( is_tag() && $template = self::get_available_template('tag') ) : |
|
|
|
|
332
|
|
|
|
333
|
|
|
elseif ( is_author() && $template = self::get_available_template('author') ) : |
|
|
|
|
334
|
|
|
|
335
|
|
|
elseif ( is_date() && $template = self::get_available_template('date') ) : |
|
|
|
|
336
|
|
|
|
337
|
|
|
elseif ( is_archive() && $template = self::get_available_template('archive') ) : |
|
|
|
|
338
|
|
|
|
339
|
|
|
elseif ( is_paged() && $template = self::get_available_template('paged') ) : |
|
|
|
|
340
|
|
|
|
341
|
|
|
else : |
342
|
|
|
|
343
|
|
|
$template = 'index'; |
344
|
|
|
|
345
|
|
|
endif; |
346
|
|
|
|
347
|
|
|
|
348
|
|
|
return $template; |
349
|
|
|
|
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Modifies template path to nested that we use for our template structuring |
354
|
|
|
* |
355
|
|
|
* @param string $template ex: single |
356
|
|
|
* @return string single.single (it will look at "single" folder and will find "single.blade.php" template) |
357
|
|
|
*/ |
358
|
|
|
public static function get_nested_blade_path($template) { |
359
|
|
|
|
360
|
|
|
if (preg_match('/\./', $template)) { |
361
|
|
|
|
362
|
|
|
return $template; |
363
|
|
|
|
364
|
|
|
} else { |
365
|
|
|
|
366
|
|
|
return $template . '.' . $template; |
367
|
|
|
|
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Returns available template, based on page argument |
375
|
|
|
* |
376
|
|
|
* @return string |
377
|
|
|
*/ |
378
|
|
|
public static function get_blade_template($page = null) { |
379
|
|
|
|
380
|
|
|
if (!$page) { |
381
|
|
|
$page = self::get_current_page(); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
$template = self::get_available_template($page); |
385
|
|
|
|
386
|
|
|
if ($template) { |
|
|
|
|
387
|
|
|
|
388
|
|
|
return self::get_nested_blade_path($template); |
389
|
|
|
|
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
return false; |
393
|
|
|
|
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Performs template render. |
398
|
|
|
* If there is $template attribute presented, it will render requested template. |
399
|
|
|
* If it's not it will try to find necessary template based on $wp_query |
400
|
|
|
* |
401
|
|
|
* @param string|null $template template path in blade format, ex: single, base.default, single.partials.slider and etc |
402
|
|
|
* @param array|null $data Additional params |
403
|
|
|
* @return void |
404
|
|
|
*/ |
405
|
|
|
public static function render($template = null, $data = null) { |
406
|
|
|
|
407
|
|
|
$views = THEME_PATH . self::$theme_templates_folder; |
408
|
|
|
$cache = WP_CONTENT_DIR . '/templatecache'; |
409
|
|
|
$common_scope = ClassyScope::get_common_scope(); |
410
|
|
|
|
411
|
|
|
if ($template !== null && is_string($template)) { |
412
|
|
|
|
413
|
|
|
if ($data && is_array($data)) { |
414
|
|
|
|
415
|
|
|
$scope = array_merge($common_scope, $data); |
416
|
|
|
|
417
|
|
|
} else { |
418
|
|
|
|
419
|
|
|
$scope = $common_scope; |
420
|
|
|
|
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
} else { |
424
|
|
|
|
425
|
|
|
$current_page = self::get_current_page(); |
426
|
|
|
|
427
|
|
|
$template = self::get_blade_template($current_page); |
428
|
|
|
|
429
|
|
|
$scope = ClassyScope::get_scope($current_page); |
430
|
|
|
|
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
$renderer = new BladeRenderer($views, array('cache_path' => $cache)); |
|
|
|
|
434
|
|
|
|
435
|
|
|
echo $renderer->render($template, $scope); |
|
|
|
|
436
|
|
|
|
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Returns list of theme page templates |
441
|
|
|
* |
442
|
|
|
* @return array |
443
|
|
|
*/ |
444
|
|
|
public static function get_page_templates_list() { |
445
|
|
|
|
446
|
|
|
$templates = array(); |
447
|
|
|
|
448
|
|
|
$files = (array) glob( THEME_PATH . '/' . self::$theme_templates_folder . '/*/*.blade.php' ); |
449
|
|
|
|
450
|
|
|
foreach ( $files as $filename ) { |
451
|
|
|
|
452
|
|
|
if ( !empty($filename) ) { |
453
|
|
|
|
454
|
|
|
if ( ! preg_match( '/\{\{\-\-\s*Template Name:(.*)\s*\-\-\}\}/mi', file_get_contents( $filename ), $header ) ) continue; |
455
|
|
|
|
456
|
|
|
$template_name = trim($header[1]); |
457
|
|
|
|
458
|
|
|
preg_match('/\/([^\/]*)\.blade.php$/is', $filename, $filename_match); |
459
|
|
|
|
460
|
|
|
$template_file = 'classy-' . $filename_match[1]; |
461
|
|
|
|
462
|
|
|
$templates[$template_file] = $template_name; |
463
|
|
|
|
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
return $templates; |
469
|
|
|
|
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
} |
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.