1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
<?php
namespace Objectiv\Plugins\Checkout\Model;
use Objectiv\Plugins\Checkout\Managers\SettingsManager; use Symfony\Component\Finder\Finder;
/** * Template handler for associated template piece. Typically there should only be 3 of these in total (header, footer, * content) * * @link checkoutwc.com * @since 2.0.0 * @package Objectiv\Plugins\Checkout\Core * @author Clifton Griffin <[email protected]> */
class Template { private $_stylesheet_file_name = 'style.min.css'; private $_basepath; private $_baseuri; private $_name; private $_description; private $_author; private $_author_uri; private $_version; private $_supports = array(); private $_templates = array(); private $_slug;
/** * @since 2.0.0 * @access private * @static * @var array $default_headers */ public static $default_headers = array( 'Name' => 'Template Name', 'Description' => 'Description', 'Author' => 'Author', 'AuthorURI' => 'Author URI', 'Version' => 'Version', 'Supports' => 'Supports', );
/** * Template constructor. * * @param string $slug */ public function __construct( string $slug ) { /** * Locate the template * * Search WordPress theme template folder first, then plugin */ if ( is_dir( trailingslashit( CFW_PATH_THEME_TEMPLATE ) . $slug ) ) { $this->_basepath = trailingslashit( CFW_PATH_THEME_TEMPLATE ) . $slug; $this->_baseuri = trailingslashit( get_stylesheet_directory_uri() ) . 'checkout-wc/' . $slug; } elseif ( is_dir( trailingslashit( CFW_PATH_PLUGIN_TEMPLATE ) . $slug ) ) { $this->_basepath = trailingslashit( CFW_PATH_PLUGIN_TEMPLATE ) . $slug; $this->_baseuri = trailingslashit( CFW_PATH_URL_BASE ) . 'templates/' . $slug; } else { // Otherwise, load the default template return new Template( 'default' ); }
$this->_slug = $slug;
$this->_load(); }
/** * Load template information for given path * * @param $basepath */ private function _load() { /** * Template Information */ $stylesheet_path = $this->get_stylesheet_path();
if ( $stylesheet_path ) { $data = get_file_data( $stylesheet_path, self::$default_headers );
$data['Name'] = ( '' === $data['Name'] ) ? ucfirst( basename( $this->get_basepath() ) ) : $data['Name']; $data['Supports'] = isset( $data['Supports'] ) ? explode( ', ', $data['Supports'] ) : array();
foreach ( $data as $key => $value ) { $key = str_replace( ' ', '_', $key ); $key = sanitize_key( $key ); $this->{"_$key"} = $value; } }
/** * Template Files */ $finder = new Finder(); $finder->files()->depth( 0 )->in( $this->get_basepath() )->name( '*.php' )->notName( 'functions.php' )->notName( 'header.php' )->notName( 'footer.php' );
foreach ( $finder as $template_file ) { $this->_templates[] = $template_file->getFilename(); } }
/** * Load the theme template functions file * * @since 2.0.0 * @return void */ public function load_functions() { $functions_path = trailingslashit( $this->get_basepath() ) . 'functions.php';
if ( file_exists( $functions_path ) ) { require_once $functions_path; } }
/** * Load the template init settings file * * @since 2.0.0 * @return void */ public function init() { $init_path = trailingslashit( $this->get_basepath() ) . 'init.php';
$defaults = $this->get_default_settings();
$settings_manager = SettingsManager::instance();
foreach ( $defaults as $setting => $value ) { if ( defined( 'CFW_FORCE_TEMPLATE_RESET' ) ) { $settings_manager->update_setting( $setting, $value, true, array( $this->get_slug() ) ); } else { $settings_manager->add_setting( $setting, $value, array( $this->get_slug() ) ); } }
if ( file_exists( $init_path ) ) { require_once $init_path; } }
/** * Get the default settings array * * @return array */ function get_default_settings(): array { $default_path = trailingslashit( $this->get_basepath() ) . 'defaults.php';
if ( file_exists( $default_path ) ) { require $default_path;
return $defaults; }
return $this->get_standard_default_settings(); }
/** * @param $setting * * @return mixed|string */ function get_default_setting( $setting ) { $defaults = $this->get_default_settings();
return ! empty( $defaults[ $setting ] ) ? $defaults[ $setting ] : ''; }
/** * @return string[] */ function get_standard_default_settings(): array { return array( 'body_background_color' => '#ffffff', 'body_text_color' => '#333333', 'header_background_color' => '#ffffff', 'footer_background_color' => '#ffffff', 'header_text_color' => '#2b2b2b', 'footer_color' => '#999999', 'link_color' => '#0073aa', 'button_color' => '#333333', 'button_text_color' => '#ffffff', 'button_hover_color' => '#555555', 'button_text_hover_color' => '#ffffff', 'secondary_button_color' => '#999999', 'secondary_button_text_color' => '#ffffff', 'secondary_button_hover_color' => '#666666', 'secondary_button_text_hover_color' => '#ffffff', 'summary_background_color' => '#fafafa', 'summary_mobile_background_color' => '#fafafa', 'summary_text_color' => '#333333', 'cart_item_quantity_color' => '#7f7f7f', 'cart_item_quantity_text_color' => '#ffffff', 'breadcrumb_completed_text_color' => '#7f7f7f', 'breadcrumb_current_text_color' => '#333333', 'breadcrumb_next_text_color' => '#7f7f7f', 'breadcrumb_completed_accent_color' => '#333333', 'breadcrumb_current_accent_color' => '#333333', 'breadcrumb_next_accent_color' => '#333333', ); }
public function view( $filename, $parameters = array() ) { $filename_with_basepath = trailingslashit( $this->get_basepath() ) . $filename; $template_name = $this->get_slug(); $template_piece_name = basename( $filename, '.php' );
if ( file_exists( $filename_with_basepath ) ) { /** * Fires before template is output * * @since 3.0.0 */ do_action( "cfw_template_load_before_{$template_name}_{$template_piece_name}" );
// Extract any parameters for use in the template extract( $parameters ); // phpcs:ignore
// Pass the parameters to the view require $filename_with_basepath;
/** * Fires after template has been echoed out * * @since 3.0.0 */ do_action( "cfw_template_load_after_{$template_name}_{$template_piece_name}" ); } }
/** * @param $capability * * @return bool */ public function supports( $capability ): bool { return in_array( $capability, $this->get_supports(), true ); }
/** * @return string */ public function get_template_uri(): string { return $this->_baseuri; }
/** * Return fully qualified path to stylesheet * * @return string|bool $stylesheet */ public function get_stylesheet_path() { $stylesheet = trailingslashit( $this->get_basepath() ) . $this->get_stylesheet_filename();
return file_exists( $stylesheet ) ? $stylesheet : false; }
/** * @return string */ public function get_stylesheet_filename(): string { if ( defined( 'CFW_DEV_MODE' ) && CFW_DEV_MODE ) { return 'style.css'; }
return $this->_stylesheet_file_name; }
/** * @param string $_stylesheet_file_name */ public function set_stylesheet_filename( string $_stylesheet_file_name ) { $this->_stylesheet_file_name = $_stylesheet_file_name; }
/** * @return string */ public function get_basepath(): string { return $this->_basepath; }
/** * @param string $basepath */ public function set_basepath( string $basepath ) { $this->_basepath = $basepath; }
/** * @return mixed */ public function get_name() { return $this->_name; }
/** * @param mixed $name */ public function set_name( $name ) { $this->_name = $name; }
/** * @return mixed */ public function get_description() { return $this->_description; }
/** * @param mixed $description */ public function set_description( $description ) { $this->_description = $description; }
/** * @return mixed */ public function get_author() { return $this->_author; }
/** * @param mixed $author */ public function set_author( $author ) { $this->_author = $author; }
/** * @return mixed */ public function get_author_uri() { return $this->_author_uri; }
/** * @param mixed $author_uri */ public function set_author_uri( $author_uri ) { $this->_author_uri = $author_uri; }
/** * @return mixed */ public function get_version() { return $this->_version; }
/** * @param mixed $version */ public function set_version( $version ) { $this->_version = $version; }
/** * @return array */ public function get_supports(): array { return (array) $this->_supports; }
/** * @param mixed $supports */ public function set_supports( $supports ) { $this->_supports = $supports; }
/** * @return array */ public function get_templates(): array { return $this->_templates; }
/** * @return string */ public function get_slug(): string { return $this->_slug; }
public function run_on_plugin_activation() { $this->init(); }
public static function get_all_available(): array { $templates = array(); $finder = new Finder();
$finder->directories()->depth( 0 )->in( cfw_get_plugin_template_path() );
foreach ( $finder as $template ) { $templates[ $template->getBasename() ] = new self( $template->getBasename() ); }
if ( is_dir( CFW_PATH_THEME_TEMPLATE ) ) { $finder = new Finder(); $finder->directories()->depth( 0 )->in( CFW_PATH_THEME_TEMPLATE );
foreach ( $finder as $template ) { $templates[ $template->getBasename() ] = new self( $template->getBasename() ); } }
return $templates; }
public static function init_active_template( self $template ) { add_action( 'cfw_load_template_assets', function() use ( $template ) { $min = ( ! CFW_DEV_MODE ) ? '.min' : '';
wp_enqueue_style( 'cfw_front_template_css', $template->get_template_uri() . "/style{$min}.css", array(), CFW_VERSION ); wp_enqueue_script( 'wc-checkout', $template->get_template_uri() . "/theme{$min}.js", array( 'jquery' ), CFW_VERSION, true ); } ); $template->load_functions(); } }
|