Completed
Push — master ( 50ebea...f30043 )
by Andrew
02:27
created

Classy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 2
Metric Value
cc 1
eloc 6
c 5
b 0
f 2
nc 1
nop 0
dl 0
loc 13
rs 9.4285
1
<?php 
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 406.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
use Windwalker\Renderer\BladeRenderer;
4
5
6
/**
7
 * The core theme class.
8
 *
9
 *
10
 * @since      1.0.0
11
 * @package    Classy
12
 * @author     Andrew Tolochka <[email protected]>
13
 */
14
class Classy {
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...
15
16
	/**
17
	 * Singleton instance of plugin
18
	 *
19
	 * @var Classy
20
	 * @since  0.1.0
21
	 */
22
	protected static $single_instance = null;
23
24
	/**
25
	 * Creates or returns an instance of this class.
26
	 *
27
	 * @since  0.1.0
28
	 * @return Classy A single instance of this class.
29
	 */
30
	public static function get_instance() {
31
		
32
		if ( null === self::$single_instance ) {
33
		
34
			self::$single_instance = new self();
35
		
36
		}
37
38
		return self::$single_instance;
39
40
	}
41
42
	/**
43
	 * Define the core functionality of the them.
44
	 *
45
	 * Set the theme name and the theme version that can be used throughout the theme.
46
	 *
47
	 * @since    1.0.0
48
	 */
49
	protected function __construct() {
50
		
51
		$this->define_constants();
52
53
		$this->include_core_files();
54
55
		$this->include_models();
56
57
		$this->init_config();
58
59
		add_filter('theme_page_templates', array($this, 'filter_templates'), 3);
60
61
	}
62
63
	/**
64
	 * Function to define constants
65
	 * 
66
	 * @param  string
67
	 * @param  string
68
	 */
69
	private function define( $name, $value ) {
70
		
71
		if ( !defined($name) ) {
72
			
73
			define( $name, $value );
74
75
		}
76
77
	}
78
79
	/**
80
	 * Defines plugin constants
81
	 * 
82
	 * @since    1.0.0
83
	 * @access   private
84
	 */
85
	private function define_constants() {
86
87
		$theme = wp_get_theme();
88
89
		$this->define( 'THEME', $theme->template );
90
		$this->define( 'THEME_NAME', $theme->get('Name') );
91
		$this->define( 'THEME_PATH', get_template_directory() . '/' );
92
		$this->define( 'THEME_DIR', get_template_directory_uri() . '/' );
93
		$this->define( 'THEME_VERSION', $theme->get('Version') );
94
		$this->define( 'THEME_FRAMEWORK_PATH', THEME_PATH . 'app/' );
95
		$this->define( 'THEME_FRAMEWORK_DIR', THEME_DIR . 'app/' );
96
97
	}
98
99
	/**
100
	 * Include core files that are responsible for theme render
101
	 */
102
	private function include_core_files() {
103
104
		require_once THEME_PATH . 'vendor/autoload.php';
105
106
		// Basis Class
107
		require_once THEME_FRAMEWORK_PATH . 'classy/classy-basis.php';
108
109
		// Hierarchy
110
		require_once THEME_FRAMEWORK_PATH . 'classy/classy-hierarchy.php';
111
112
		// Theme Config
113
		require_once THEME_FRAMEWORK_PATH . 'classy/classy-config.php';
114
	
115
		// Scope
116
		require_once THEME_FRAMEWORK_PATH . 'classy/classy-scope.php';
117
118
		// Template Loader
119
		require_once THEME_FRAMEWORK_PATH . 'classy/classy-template.php';
120
121
		// Helper functions
122
		require_once THEME_FRAMEWORK_PATH . 'classy/classy-helper.php';
123
124
		// Query Helper
125
		require_once THEME_FRAMEWORK_PATH . 'classy/classy-query-helper.php';
126
127
		// Menu
128
		require_once THEME_FRAMEWORK_PATH . 'classy/classy-menu.php';
129
130
		// Menu Item
131
		require_once THEME_FRAMEWORK_PATH . 'classy/classy-menu-item.php';
132
133
		// Comment
134
		require_once THEME_FRAMEWORK_PATH . 'classy/classy-comment.php';
135
136
		// Appearance
137
		require_once THEME_FRAMEWORK_PATH . 'appearance.php';
138
139
	}
140
141
	/**
142
	 * Include theme Object-Orienter models
143
	 */
144
	private function include_models() {
145
146
		$files = (array) glob( THEME_FRAMEWORK_PATH . '/models/*.php' );
147
148
		foreach ( $files as $filename ) {
149
150
			if ( !empty($filename) ) {
151
152
				require_once $filename;
153
154
			}
155
156
		}
157
158
	}
159
160
	/**
161
	 * Init Theme Configuration
162
	 */
163
	private function init_config() {
164
165
		$this->config = ClassyConfig::init();
0 ignored issues
show
Bug introduced by
The property config does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
Are you sure the assignment to $this->config is correct as \ClassyConfig::init() (which targets ClassyConfig::init()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
166
167
	}
168
169
	/**
170
	 * Filters registed templates and ads custom theme templates
171
	 * 
172
	 * @return array
173
	 */
174
	
175
	public function filter_templates($page_templates = array(), $object = null, $post = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $post is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
176
177
		$custom_templates = ClassyTemplate::get_page_templates_list(); 
178
179
		return array_merge($page_templates, $custom_templates);
180
181
	}
182
183
	/**
184
	 * Returns theme config variable
185
	 * 
186
	 * @param  string $name
187
	 * @return any
188
	 */
189
	public static function get_config_var($name) {
190
191
		$vars = ClassyConfig::get_vars();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $vars is correct as \ClassyConfig::get_vars() (which targets ClassyConfig::get_vars()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
192
193
		if (isset($vars[$name])) return $vars[$name];
194
195
		return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Classy::get_config_var of type any.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
196
197
	}
198
199
	/**
200
	 * Returns theme textdomain
201
	 * 
202
	 * @return string
203
	 */
204
	public static function textdomain() {
205
206
		$textdomain = Classy::get_config_var('textdomain');
207
208
		return $textdomain ? $textdomain : THEME;
209
210
	}
211
212
	/**
213
	 * Performs template render. 
214
	 * If there is $template attribute presented, it will render requested template. 
215
	 * If it's not it will try to find necessary template based on $wp_query
216
	 * 
217
	 * @param  string|null $template template path in blade format, ex: single, layout.default, single.partials.slider and etc
218
	 * @param  array|null  $data     Additional params
219
	 * @return void                
220
	 */
221
	public static function render($template = null, $data = null) {
222
		
223
		$views = THEME_PATH . ClassyTemplate::$folder;
224
		$cache = WP_CONTENT_DIR . '/templatecache';
225
		$common_scope = ClassyScope::get_common_scope();
226
227
		if ($template !== null && is_string($template)) {
228
229
			if ($data && is_array($data)) {
230
231
				$scope = array_merge($common_scope, $data);
232
233
			} else {
234
235
				$scope = $common_scope;
236
237
			}
238
239
		} else {
240
241
			$template = ClassyTemplate::get_template();
242
243
			$scope = ClassyScope::get_scope();
244
245
		}
246
247
		$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...
248
249
		echo $renderer->render($template, $scope);
250
251
	}
252
253
	/**
254
	 * Alias for ClassyHelper::get_archives_title()
255
	 * Returns page title for archive page. 
256
	 * Example: Archives, Author: John Doe, Tag: Lorem Ipsum
257
	 * 
258
	 * @return string
259
	 */
260
	public static function archives_title() {
261
		
262
		return ClassyHelper::get_archives_title();
263
264
	}
265
266
267
	/**
268
	 * Returns posts
269
	 * 
270
	 * @param  mixed $args        Array of query args
271
	 * @param  string  $return_type ClassyPost/object/id
272
	 * @return mixed               
273
	 */
274
	public static function get_posts($args = false, $return_type = 'ClassyPost') {
275
276
		$_return = array();
277
278
		$query = ClassyQueryHelper::find_query($args);
279
280
		if (isset($query->posts)) {
281
282
			foreach ($query->posts as $post_id) {
283
				
284
				if ($return_type == 'ClassyPost') {
285
				
286
					$_return[] = new ClassyPost($post_id);
287
				
288
				} elseif($return_type == 'object') {
289
290
					$_return[] = get_post($post_id);
291
292
				} else {
293
294
					$_return[] = $post_id;
295
296
				}
297
298
			}
299
300
		}
301
302
		return $_return;
303
	}
304
305
	/**
306
	 * Returns post
307
	 * 
308
	 * @param  mixed $args Array of query args
309
	 * @param  string  $return_type ClassyPost/object/id
310
	 * @return mixed               
311
	 */
312
	public static function get_post($args = false, $return_type = 'ClassyPost') {
313
314
		$posts = self::get_posts($args, $return_type);
315
316
		if ( $post = reset($posts ) ) {
317
			return $post;
318
		}
319
320
	}
321
322
	/**
323
	 * @param array   $prefs
324
	 * @return array mixed
325
	 */
326
	public static function get_pagination( $prefs = array() ) {
327
328
		global $wp_query;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
329
		global $paged;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
330
		global $wp_rewrite;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
331
332
		$args = array();
333
		$args['total'] = ceil( $wp_query->found_posts / $wp_query->query_vars['posts_per_page'] );
334
		
335
		if ( $wp_rewrite->using_permalinks() ) {
336
			
337
			$url = explode( '?', get_pagenum_link( 0 ) );
338
			
339
			if ( isset( $url[1] ) ) {
340
				parse_str( $url[1], $query );
341
				$args['add_args'] = $query;
342
			}
343
			
344
			$args['format'] = 'page/%#%';
345
			$args['base'] = trailingslashit( $url[0] ).'%_%';
346
347
		} else {
348
			$big = 999999999;
349
			$args['base'] = str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) );
350
		}
351
352
		$args['type'] = 'array';
353
		$args['current'] = max( 1, get_query_var( 'paged' ) );
354
		$args['mid_size'] = max( 9 - $args['current'], 3 );
355
		$args['prev_next'] = false;
356
		
357
		if ( is_int( $prefs ) ) {
358
			$args['mid_size'] = $prefs - 2;
359
		} else {
360
			$args = array_merge( $args, $prefs );
361
		}
362
363
		$data = array();
364
		$data['pages'] = ClassyHelper::paginate_links( $args );
0 ignored issues
show
Documentation introduced by
$args is of type array, but the function expects a string.

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...
365
		$next = get_next_posts_page_link( $args['total'] );
366
		
367
		if ( $next ) {
368
			$data['next'] = array( 'link' => untrailingslashit( $next ), 'class' => 'page-numbers next' );
369
		}
370
371
		$prev = previous_posts( false );
372
		
373
		if ( $prev ) {
374
			$data['prev'] = array( 'link' => untrailingslashit( $prev ), 'class' => 'page-numbers prev' );
375
		}
376
		
377
		if ( $paged < 2 ) {
378
			$data['prev'] = null;
379
		}
380
		
381
		return ClassyHelper::array_to_object($data);
382
383
	}
384
385
}
386
387
388
/**
389
 * Grab the Classy object and return it.
390
 * Wrapper for Classy::get_instance()
391
 *
392
 * @since  0.1.0
393
 * @return Classy  Singleton instance of plugin class.
394
 */
395
function get_theme_framework() {
396
397
	return Classy::get_instance();
398
	
399
}
400
401
/**
402
 * Get Instance
403
 * 
404
 * @var classy
405
 */
406
$classy = get_theme_framework();