Completed
Push — master ( e75b66...257029 )
by Andrew
06:07 queued 03:34
created

Classy   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 356
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 356
rs 8.8
wmc 36
lcom 2
cbo 7

16 Methods

Rating   Name   Duplication   Size   Complexity  
A get_instance() 0 11 2
A __construct() 0 11 1
A init_appearance() 0 3 1
A load_template_function() 0 3 1
A define_constants() 0 13 1
A init_config() 0 3 1
A filter_templates() 0 7 1
A get_config_var() 0 7 2
A textdomain() 0 7 2
B render() 0 32 5
A maybe_minify() 0 13 2
A minify_html() 0 21 1
A archives_title() 0 5 1
B get_posts() 0 28 6
A get_post() 0 9 2
B get_pagination() 0 58 7
1
<?php
2
/**
3
 * The core theme class.
4
 *
5
 * @since 	1.0.0
6
 * @package Classy
7
 * @author 	Andrew Tolochka <[email protected]>
8
 */
9
10
namespace Classy;
11
12
use Windwalker\Renderer\BladeRenderer;
13
14
/**
15
 * Class Classy.
16
 */
17
class Classy {
18
19
	/**
20
	 * Singleton instance of plugin
21
	 *
22
	 * @var Classy
23
	 * @since  0.1.0
24
	 */
25
	protected static $single_instance = null;
26
27
	/**
28
	 * Creates or returns an instance of this class.
29
	 *
30
	 * @since  0.1.0
31
	 * @return Classy A single instance of this class.
32
	 */
33
	public static function get_instance() {
34
35
		if ( null === self::$single_instance ) {
36
37
			self::$single_instance = new self();
38
39
		}
40
41
		return self::$single_instance;
42
43
	}
44
45
	/**
46
	 * Define the core functionality of the them.
47
	 *
48
	 * Set the theme name and the theme version that can be used throughout the theme.
49
	 *
50
	 * @since    1.0.0
51
	 */
52
	protected function __construct() {
53
		$this->define_constants();
54
55
		$this->init_appearance();
56
57
		$this->load_template_function();
58
59
		$this->init_config();
60
61
		add_filter( 'theme_page_templates', array( $this, 'filter_templates' ) );
62
	}
63
64
	/**
65
	 * Init Appearance class.
66
	 */
67
	private function init_appearance() {
68
		new Appearance();
69
	}
70
71
	/**
72
	 * Load template functions.
73
	 */
74
	private function load_template_function() {
75
		require_once( CLASSY_THEME_PATH . 'app/functions/template-functions.php' );
76
	}
77
78
	/**
79
	 * Defines plugin constants
80
	 *
81
	 * @since    1.0.0
82
	 * @access   private
83
	 */
84
	private function define_constants() {
85
86
		$theme = wp_get_theme();
87
88
		define( 'CLASSY_THEME', $theme->template );
89
		define( 'CLASSY_THEME_NAME', $theme->get( 'Name' ) );
90
		define( 'CLASSY_THEME_PATH', get_template_directory() . '/' );
91
		define( 'CLASSY_THEME_DIR', get_template_directory_uri() . '/' );
92
		define( 'CLASSY_THEME_VERSION', $theme->get( 'Version' ) );
93
		define( 'CLASSY_THEME_FRAMEWORK_PATH', CLASSY_THEME_PATH . 'app/' );
94
		define( 'CLASSY_THEME_FRAMEWORK_DIR', CLASSY_THEME_DIR . 'app/' );
95
96
	}
97
98
	/**
99
	 * Init Theme Configuration
100
	 */
101
	private function init_config() {
102
		Config::init();
103
	}
104
105
	/**
106
	 * Filters registered templates and adds custom theme templates.
107
	 *
108
	 * @param array $page_templates Available WordPress templates.
109
	 *
110
	 * @return array
111
	 */
112
	public function filter_templates( $page_templates = array() ) {
113
114
		$custom_templates = View::get_page_templates_list();
115
116
		return array_merge( $page_templates, $custom_templates );
117
118
	}
119
120
	/**
121
	 * Returns theme config variable.
122
	 *
123
	 * @param string $name Variable's name.
124
	 *
125
	 * @return mixed|bool Return false if variable not found.
126
	 */
127
	public static function get_config_var( $name ) {
128
129
		$vars = Config::get_vars();
130
131
		return ( isset( $vars[ $name ] ) ) ? $vars[ $name ] : false;
132
133
	}
134
135
	/**
136
	 * Returns theme textdomain
137
	 *
138
	 * @return string
139
	 */
140
	public static function textdomain() {
141
142
		$textdomain = Classy::get_config_var( 'textdomain' );
143
144
		return $textdomain ? $textdomain : CLASSY_THEME;
145
146
	}
147
148
	/**
149
	 * Performs view render.
150
	 * If there is $view attribute presented, it will render requested view.
151
	 * If it's not it will try to find necessary view based on $wp_query
152
	 *
153
	 * @param  string|null $view View path in blade format, ex: single, layout.default, single.partials.slider and etc.
154
	 * @param  array|null  $data Additional params.
155
	 * @return void
156
	 */
157
	public static function render( $view = null, $data = null ) {
158
159
		$views = CLASSY_THEME_PATH . View::$folder;
160
		$cache = WP_CONTENT_DIR . '/viewcache';
161
		$common_scope = Scope::get_common_scope();
162
163
		if ( null !== $view && is_string( $view ) ) {
164
165
			if ( $data && is_array( $data ) ) {
166
167
				$scope = array_merge( $common_scope, $data );
168
169
			} else {
170
171
				$scope = $common_scope;
172
173
			}
174
		} else {
175
176
			$view = View::get_view();
177
178
			$scope = Scope::get_scope();
179
180
		}
181
182
		$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...
183
184
		$html = $renderer->render( $view, $scope );
185
186
		echo self::maybe_minify( $html );
187
188
	}
189
190
	/**
191
	 * Minifies html in case the minify_html option is set to true.
192
	 *
193
	 * @param  string $html HTML string.
194
	 * @return string
195
	 */
196
	private static function maybe_minify( $html ) {
197
198
		$minify_html = self::get_config_var( 'minify_html' );
199
200
		if ( true === $minify_html ) {
201
202
			$html = self::minify_html( $html );
203
204
		}
205
206
		return $html;
207
208
	}
209
210
	/**
211
	 * Returns minified version of string with removed whitespaces and empty strings.
212
	 *
213
	 * @param  string $html HTML string.
214
	 * @return string
215
	 */
216
	private static function minify_html( $html ) {
217
218
		$search = array(
219
			"/\n/s",
220
			'/\>[^\S ]+/s',  // Strip whitespaces after tags, except space.
221
			'/[^\S ]+\</s',  // Strip whitespaces before tags, except space.
222
			'/(\s)+/s',       // Shorten multiple whitespace sequences.
223
			'/<!--(.|\s)*?-->/',
224
		);
225
226
		$replace = array(
227
			'',
228
			'>',
229
			'<',
230
			'\\1',
231
			'',
232
		);
233
234
		return preg_replace( $search, $replace, $html );
235
236
	}
237
238
	/**
239
	 * Alias for Helper::get_archives_title()
240
	 * Returns page title for archive page.
241
	 * Example: Archives, Author: John Doe, Tag: Lorem Ipsum
242
	 *
243
	 * @return string
244
	 */
245
	public static function archives_title() {
246
247
		return Helper::get_archives_title();
248
249
	}
250
251
	/**
252
	 * Returns posts
253
	 *
254
	 * @param  mixed  $args   Array of query args.
255
	 * @param  string $return object/id/Post.
256
	 *
257
	 * @return array
258
	 */
259
	public static function get_posts( $args = false, $return = '\Classy\Models\Post' ) {
260
261
		$_return = array();
262
263
		$query = Query_Helper::find_query( $args );
264
265
		if ( isset( $query->posts ) ) {
266
267
			foreach ( $query->posts as $post ) {
268
269
				if ( 'id' === $return ) {
270
271
					$_return[] = $post->id;
272
273
				} elseif ( 'object' === $return ) {
274
275
					$_return[] = $post;
276
277
				} elseif ( class_exists( $return ) ) {
278
279
					$_return[] = new $return( $post );
280
281
				}
282
			}
283
		}
284
285
		return $_return;
286
	}
287
288
289
	/**
290
	 * Returns post.
291
	 *
292
	 * @param  mixed  $args 		Array of query args.
293
	 * @param  string $return_type 	Post/object/id.
294
	 *
295
	 * @return mixed
296
	 */
297
	public static function get_post( $args = false, $return_type = '\Classy\Models\Post' ) {
298
299
		$posts = self::get_posts( $args, $return_type );
300
301
		if ( $post = reset( $posts ) ) {
302
			return $post;
303
		}
304
305
	}
306
307
	/**
308
	 * @todo: Write description here.
309
	 *
310
	 * @param array $prefs Args for paginate_links.
311
	 *
312
	 * @return array mixed
313
	 */
314
	public static function get_pagination( $prefs = array() ) {
315
316
		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...
317
		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...
318
		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...
319
320
		$args = array();
321
		$args['total'] = ceil( $wp_query->found_posts / $wp_query->query_vars['posts_per_page'] );
322
323
		if ( $wp_rewrite->using_permalinks() ) {
324
325
			$url = explode( '?', get_pagenum_link( 0 ) );
326
327
			if ( isset( $url[1] ) ) {
328
				parse_str( $url[1], $query );
329
				$args['add_args'] = $query;
330
			}
331
332
			$args['format'] = 'page/%#%';
333
			$args['base'] = trailingslashit( $url[0] ).'%_%';
334
335
		} else {
336
			$big = 999999999;
337
			$args['base'] = str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) );
338
		}
339
340
		$args['type'] = 'array';
341
		$args['current'] = max( 1, get_query_var( 'paged' ) );
342
		$args['mid_size'] = max( 9 - $args['current'], 3 );
343
		$args['prev_next'] = false;
344
345
		if ( is_int( $prefs ) ) {
346
			$args['mid_size'] = $prefs - 2;
347
		} else {
348
			$args = array_merge( $args, $prefs );
349
		}
350
351
		$data = array();
352
		$data['pages'] = Helper::paginate_links( $args );
353
		$next = get_next_posts_page_link( $args['total'] );
354
355
		if ( $next ) {
356
			$data['next'] = array( 'link' => untrailingslashit( $next ), 'class' => 'page-numbers next' );
357
		}
358
359
		$prev = previous_posts( false );
360
361
		if ( $prev ) {
362
			$data['prev'] = array( 'link' => untrailingslashit( $prev ), 'class' => 'page-numbers prev' );
363
		}
364
365
		if ( $paged < 2 ) {
366
			$data['prev'] = null;
367
		}
368
369
		return Helper::array_to_object( $data );
370
371
	}
372
}
373