Completed
Push — master ( ab4944...534e9c )
by Andrew
02:30
created

ClassyTemplate::render()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 33
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 5
eloc 15
c 3
b 0
f 1
nc 3
nop 2
dl 0
loc 33
rs 8.439
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
		$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' ):
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...
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' ):
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...
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 ) {
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...
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' ):
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...
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' ):
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...
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') ) :
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...
273
274
		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...
275
276
		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...
277
278
		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...
279
280
		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...
281
282
		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...
283
284
		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...
285
286
		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...
287
288
		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...
289
290
		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...
291
292
		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...
293
294
		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...
295
296
		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...
297
298
		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...
299
300
		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...
301
302
		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...
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) {
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...
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));
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...
398
399
		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...
400
401
	}
402
	
403
}