Completed
Push — master ( 854e15...fefbc6 )
by Andrew
04:57 queued 02:25
created

Classy::define_constants()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 1
Metric Value
cc 1
eloc 9
c 7
b 0
f 1
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 386.

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' ) );
60
61
	}
62
63
	/**
64
	 * Defines plugin constants
65
	 *
66
	 * @since    1.0.0
67
	 * @access   private
68
	 */
69
	private function define_constants() {
70
71
		$theme = wp_get_theme();
72
73
		define( 'CLASSY_THEME', $theme->template );
74
		define( 'CLASSY_THEME_NAME', $theme->get( 'Name' ) );
75
		define( 'CLASSY_THEME_PATH', get_template_directory() . '/' );
76
		define( 'CLASSY_THEME_DIR', get_template_directory_uri() . '/' );
77
		define( 'CLASSY_THEME_VERSION', $theme->get( 'Version' ) );
78
		define( 'CLASSY_THEME_FRAMEWORK_PATH', CLASSY_THEME_PATH . 'app/' );
79
		define( 'CLASSY_THEME_FRAMEWORK_DIR', CLASSY_THEME_DIR . 'app/' );
80
81
	}
82
83
	/**
84
	 * Include core files that are responsible for theme render
85
	 */
86
	private function include_core_files() {
87
88
		require_once CLASSY_THEME_PATH . 'vendor/autoload.php';
89
90
		// Basis Class
91
		require_once CLASSY_THEME_FRAMEWORK_PATH . 'classy/classy-basis.php';
92
93
		// Hierarchy
94
		require_once CLASSY_THEME_FRAMEWORK_PATH . 'classy/classy-hierarchy.php';
95
96
		// Theme Config
97
		require_once CLASSY_THEME_FRAMEWORK_PATH . 'classy/classy-config.php';
98
99
		// Scope
100
		require_once CLASSY_THEME_FRAMEWORK_PATH . 'classy/classy-scope.php';
101
102
		// View Loader
103
		require_once CLASSY_THEME_FRAMEWORK_PATH . 'classy/classy-view.php';
104
105
		// Helper functions
106
		require_once CLASSY_THEME_FRAMEWORK_PATH . 'classy/classy-helper.php';
107
108
		// Query Helper
109
		require_once CLASSY_THEME_FRAMEWORK_PATH . 'classy/classy-query-helper.php';
110
111
		// Menu
112
		require_once CLASSY_THEME_FRAMEWORK_PATH . 'classy/classy-menu.php';
113
114
		// Menu Item
115
		require_once CLASSY_THEME_FRAMEWORK_PATH . 'classy/classy-menu-item.php';
116
117
		// Comment
118
		require_once CLASSY_THEME_FRAMEWORK_PATH . 'classy/classy-comment.php';
119
120
		// Appearance
121
		require_once CLASSY_THEME_FRAMEWORK_PATH . 'appearance.php';
122
123
	}
124
125
	/**
126
	 * Include theme Object-Orienter models
127
	 */
128
	private function include_models() {
129
130
		$files = (array) glob( CLASSY_THEME_FRAMEWORK_PATH . '/models/*.php' );
131
132
		foreach ( $files as $filename ) {
133
134
			if ( ! empty( $filename ) ) {
135
136
				require_once $filename;
137
138
			}
139
		}
140
141
	}
142
143
	/**
144
	 * Init Theme Configuration
145
	 */
146
	private function init_config() {
147
148
		$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...
149
150
	}
151
152
	/**
153
	 * Filters registered templates and adds custom theme templates.
154
	 *
155
	 * @param array $page_templates Available WordPress templates.
156
	 *
157
	 * @return array
158
	 */
159
	public function filter_templates( $page_templates = array() ) {
160
161
		$custom_templates = ClassyView::get_page_templates_list();
162
163
		return array_merge( $page_templates, $custom_templates );
164
165
	}
166
167
	/**
168
	 * Returns theme config variable.
169
	 *
170
	 * @param string $name Variable's name.
171
	 *
172
	 * @return mixed|bool Return false if variable not found.
173
	 */
174
	public static function get_config_var( $name ) {
175
176
		$vars = ClassyConfig::get_vars();
177
178
		return ( isset( $vars[ $name ] ) ) ? $vars[ $name ] : false;
179
180
	}
181
182
	/**
183
	 * Returns theme textdomain
184
	 *
185
	 * @return string
186
	 */
187
	public static function textdomain() {
188
189
		$textdomain = Classy::get_config_var( 'textdomain' );
190
191
		return $textdomain ? $textdomain : CLASSY_THEME;
192
193
	}
194
195
	/**
196
	 * Performs view render.
197
	 * If there is $view attribute presented, it will render requested view.
198
	 * If it's not it will try to find necessary view based on $wp_query
199
	 *
200
	 * @param  string|null $view view path in blade format, ex: single, layout.default, single.partials.slider and etc
201
	 * @param  array|null  $data     Additional params
202
	 * @return void
203
	 */
204
	public static function render( $view = null, $data = null ) {
205
206
		$views = CLASSY_THEME_PATH . ClassyView::$folder;
207
		$cache = WP_CONTENT_DIR . '/viewcache';
208
		$common_scope = ClassyScope::get_common_scope();
209
210
		if ( null !== $view && is_string( $view ) ) {
211
212
			if ( $data && is_array( $data ) ) {
213
214
				$scope = array_merge( $common_scope, $data );
215
216
			} else {
217
218
				$scope = $common_scope;
219
220
			}
221
		} else {
222
223
			$view = ClassyView::get_view();
224
225
			$scope = ClassyScope::get_scope();
226
227
		}
228
229
		$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...
230
231
		echo $renderer->render( $view, $scope ); // XSS: xss ok.
232
233
	}
234
235
	/**
236
	 * Alias for ClassyHelper::get_archives_title()
237
	 * Returns page title for archive page.
238
	 * Example: Archives, Author: John Doe, Tag: Lorem Ipsum
239
	 *
240
	 * @return string
241
	 */
242
	public static function archives_title() {
243
244
		return ClassyHelper::get_archives_title();
245
246
	}
247
248
249
	/**
250
	 * Returns posts
251
	 *
252
	 * @param  mixed   $args   Array of query args
253
	 * @param  string  $return object/id/ClassyPost
254
	 * @return mixed
255
	 */
256
	public static function get_posts( $args = false, $return = 'ClassyPost' ) {
257
258
		$_return = array();
259
260
		$query = ClassyQueryHelper::find_query( $args );
261
262
		if ( isset( $query->posts ) ) {
263
264
			foreach ( $query->posts as $post ) {
265
266
				if ( 'id' === $return ) {
267
268
					$_return[] = $post->id;
269
270
				} elseif ( 'object' === $return ) {
271
272
					$_return[] = $post;
273
274
				} elseif ( class_exists( $return ) ) {
275
276
					$_return[] = new $return( $post );
277
278
				}
279
			}
280
		}
281
282
		return $_return;
283
	}
284
285
286
	/**
287
	 * Returns post
288
	 *
289
	 * @param  mixed $args Array of query args
290
	 * @param  string  $return_type ClassyPost/object/id
291
	 * @return mixed
292
	 */
293
	public static function get_post( $args = false, $return_type = 'ClassyPost' ) {
294
295
		$posts = self::get_posts( $args, $return_type );
296
297
		if ( $post = reset( $posts ) ) {
298
			return $post;
299
		}
300
301
	}
302
303
	/**
304
	 * @param array   $prefs
305
	 * @return array mixed
306
	 */
307
	public static function get_pagination( $prefs = array() ) {
308
309
		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...
310
		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...
311
		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...
312
313
		$args = array();
314
		$args['total'] = ceil( $wp_query->found_posts / $wp_query->query_vars['posts_per_page'] );
315
316
		if ( $wp_rewrite->using_permalinks() ) {
317
318
			$url = explode( '?', get_pagenum_link( 0 ) );
319
320
			if ( isset( $url[1] ) ) {
321
				parse_str( $url[1], $query );
322
				$args['add_args'] = $query;
323
			}
324
325
			$args['format'] = 'page/%#%';
326
			$args['base'] = trailingslashit( $url[0] ).'%_%';
327
328
		} else {
329
			$big = 999999999;
330
			$args['base'] = str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) );
331
		}
332
333
		$args['type'] = 'array';
334
		$args['current'] = max( 1, get_query_var( 'paged' ) );
335
		$args['mid_size'] = max( 9 - $args['current'], 3 );
336
		$args['prev_next'] = false;
337
338
		if ( is_int( $prefs ) ) {
339
			$args['mid_size'] = $prefs - 2;
340
		} else {
341
			$args = array_merge( $args, $prefs );
342
		}
343
344
		$data = array();
345
		$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...
346
		$next = get_next_posts_page_link( $args['total'] );
347
348
		if ( $next ) {
349
			$data['next'] = array( 'link' => untrailingslashit( $next ), 'class' => 'page-numbers next' );
350
		}
351
352
		$prev = previous_posts( false );
353
354
		if ( $prev ) {
355
			$data['prev'] = array( 'link' => untrailingslashit( $prev ), 'class' => 'page-numbers prev' );
356
		}
357
358
		if ( $paged < 2 ) {
359
			$data['prev'] = null;
360
		}
361
362
		return ClassyHelper::array_to_object( $data );
363
364
	}
365
}
366
367
368
/**
369
 * Grab the Classy object and return it.
370
 * Wrapper for Classy::get_instance()
371
 *
372
 * @since  0.1.0
373
 * @return Classy  Singleton instance of plugin class.
374
 */
375
function get_theme_framework() {
376
377
	return Classy::get_instance();
378
379
}
380
381
/**
382
 * Get Instance
383
 *
384
 * @var classy
385
 */
386
$classy = get_theme_framework();
387