Completed
Push — master ( 75c234...bb087c )
by Andrew
03:53
created

ClassyTemplate::is_classy_template()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 2
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 4
rs 10
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 {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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' ):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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' ):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 ) {
0 ignored issues
show
Bug introduced by
The class WP_User does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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' ):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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' ):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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];
0 ignored issues
show
Bug Best Practice introduced by
The expression $matches of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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') ) :
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
310
311
		elseif ( is_search() && $template = self::get_available_template('search') ) :
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
312
313
		elseif ( is_front_page() && $template = self::get_available_template('front-page') ) :
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
314
315
		elseif ( is_home() && $template = self::get_available_template('home') ) :
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
316
317
		elseif ( is_post_type_archive() && $template = self::get_available_template('post_type_archive') ) :
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
318
319
		elseif ( is_tax() && $template = self::get_available_template('taxonomy') ) :
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
320
321
		elseif ( is_attachment() && $template = self::get_available_template('attachment') ) :
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
322
323
		elseif ( is_single() && $template = self::get_available_template('single') ) :
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
324
325
		elseif ( is_page() && $template = self::get_available_template('page') ) :
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
326
327
		elseif ( is_singular() && $template = self::get_available_template('singular') ) :
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
328
329
		elseif ( is_category() && $template = self::get_available_template('category') ) :
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
330
331
		elseif ( is_tag() && $template = self::get_available_template('tag') ) :
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
332
333
		elseif ( is_author() && $template = self::get_available_template('author') ) :
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
334
335
		elseif ( is_date() && $template = self::get_available_template('date') ) :
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
336
337
		elseif ( is_archive() && $template = self::get_available_template('archive') ) :
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
338
339
		elseif ( is_paged() && $template = self::get_available_template('paged') ) :
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $template of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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));
0 ignored issues
show
Documentation introduced by
$views is of type string, but the function expects a object<SplPriorityQueue>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
434
435
		echo $renderer->render($template, $scope);
0 ignored issues
show
Security Bug introduced by
It seems like $template can also be of type false; however, Windwalker\Renderer\BladeRenderer::render() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
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
}