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 ) ); |
|
|
|
|
183
|
|
|
|
184
|
|
|
echo $renderer->render( $view, $scope ); // XSS: xss ok. |
185
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Alias for Helper::get_archives_title() |
190
|
|
|
* Returns page title for archive page. |
191
|
|
|
* Example: Archives, Author: John Doe, Tag: Lorem Ipsum |
192
|
|
|
* |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
|
|
public static function archives_title() { |
196
|
|
|
|
197
|
|
|
return Helper::get_archives_title(); |
198
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Returns posts |
203
|
|
|
* |
204
|
|
|
* @param mixed $args Array of query args. |
205
|
|
|
* @param string $return object/id/Post. |
206
|
|
|
* |
207
|
|
|
* @return array |
208
|
|
|
*/ |
209
|
|
|
public static function get_posts( $args = false, $return = '\Classy\Models\Post' ) { |
210
|
|
|
|
211
|
|
|
$_return = array(); |
212
|
|
|
|
213
|
|
|
$query = Query_Helper::find_query( $args ); |
214
|
|
|
|
215
|
|
|
if ( isset( $query->posts ) ) { |
216
|
|
|
|
217
|
|
|
foreach ( $query->posts as $post ) { |
218
|
|
|
|
219
|
|
|
if ( 'id' === $return ) { |
220
|
|
|
|
221
|
|
|
$_return[] = $post->id; |
222
|
|
|
|
223
|
|
|
} elseif ( 'object' === $return ) { |
224
|
|
|
|
225
|
|
|
$_return[] = $post; |
226
|
|
|
|
227
|
|
|
} elseif ( class_exists( $return ) ) { |
228
|
|
|
|
229
|
|
|
$_return[] = new $return( $post ); |
230
|
|
|
|
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $_return; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Returns post. |
241
|
|
|
* |
242
|
|
|
* @param mixed $args Array of query args. |
243
|
|
|
* @param string $return_type Post/object/id. |
244
|
|
|
* |
245
|
|
|
* @return mixed |
246
|
|
|
*/ |
247
|
|
|
public static function get_post( $args = false, $return_type = '\Classy\Models\Post' ) { |
248
|
|
|
|
249
|
|
|
$posts = self::get_posts( $args, $return_type ); |
250
|
|
|
|
251
|
|
|
if ( $post = reset( $posts ) ) { |
252
|
|
|
return $post; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @todo: Write description here. |
259
|
|
|
* |
260
|
|
|
* @param array $prefs Args for paginate_links. |
261
|
|
|
* |
262
|
|
|
* @return array mixed |
263
|
|
|
*/ |
264
|
|
|
public static function get_pagination( $prefs = array() ) { |
265
|
|
|
|
266
|
|
|
global $wp_query; |
|
|
|
|
267
|
|
|
global $paged; |
|
|
|
|
268
|
|
|
global $wp_rewrite; |
|
|
|
|
269
|
|
|
|
270
|
|
|
$args = array(); |
271
|
|
|
$args['total'] = ceil( $wp_query->found_posts / $wp_query->query_vars['posts_per_page'] ); |
272
|
|
|
|
273
|
|
|
if ( $wp_rewrite->using_permalinks() ) { |
274
|
|
|
|
275
|
|
|
$url = explode( '?', get_pagenum_link( 0 ) ); |
276
|
|
|
|
277
|
|
|
if ( isset( $url[1] ) ) { |
278
|
|
|
parse_str( $url[1], $query ); |
279
|
|
|
$args['add_args'] = $query; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
$args['format'] = 'page/%#%'; |
283
|
|
|
$args['base'] = trailingslashit( $url[0] ).'%_%'; |
284
|
|
|
|
285
|
|
|
} else { |
286
|
|
|
$big = 999999999; |
287
|
|
|
$args['base'] = str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
$args['type'] = 'array'; |
291
|
|
|
$args['current'] = max( 1, get_query_var( 'paged' ) ); |
292
|
|
|
$args['mid_size'] = max( 9 - $args['current'], 3 ); |
293
|
|
|
$args['prev_next'] = false; |
294
|
|
|
|
295
|
|
|
if ( is_int( $prefs ) ) { |
296
|
|
|
$args['mid_size'] = $prefs - 2; |
297
|
|
|
} else { |
298
|
|
|
$args = array_merge( $args, $prefs ); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
$data = array(); |
302
|
|
|
$data['pages'] = Helper::paginate_links( $args ); |
|
|
|
|
303
|
|
|
$next = get_next_posts_page_link( $args['total'] ); |
304
|
|
|
|
305
|
|
|
if ( $next ) { |
306
|
|
|
$data['next'] = array( 'link' => untrailingslashit( $next ), 'class' => 'page-numbers next' ); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
$prev = previous_posts( false ); |
310
|
|
|
|
311
|
|
|
if ( $prev ) { |
312
|
|
|
$data['prev'] = array( 'link' => untrailingslashit( $prev ), 'class' => 'page-numbers prev' ); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
if ( $paged < 2 ) { |
316
|
|
|
$data['prev'] = null; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
return Helper::array_to_object( $data ); |
320
|
|
|
|
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|