Completed
Pull Request — master (#2094)
by Justin
07:22
created

WPSC_Template_Engine::get_asset_paths()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Template engine main class.
5
 *
6
 * @since 4.0
7
 */
8
class WPSC_Template_Engine {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
9
	/**
10
	 * Singleton instance
11
	 * @since 4.0
12
	 * @var WPSC_Template_Engine
13
	 */
14
	private static $instance;
15
16
	/**
17
	 * Return the singleton instance
18
	 * @since  0.1
19
	 * @return WPSC_Template_Engine
20
	 */
21
	public static function get_instance() {
22
23
		if ( ! self::$instance ) {
24
			self::$instance = new WPSC_Template_Engine();
25
		}
26
27
		return self::$instance;
28
	}
29
30
	/**
31
	 * Paths where asset files can be found
32
	 *
33
	 * @since 4.0
34
	 * @var array
35
	 */
36
	private $asset_paths = array();
0 ignored issues
show
Unused Code introduced by
The property $asset_paths is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
37
38
	/**
39
	 * Paths where template parts can be found.
40
	 *
41
	 * @since 4.0
42
	 * @var array
43
	 */
44
	private $template_part_paths = array();
0 ignored issues
show
Unused Code introduced by
The property $template_part_paths is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
45
46
	/**
47
	 * Paths where view wrappers can be found.
48
	 *
49
	 * @since 4.0
50
	 * @var array
51
	 */
52
	private $view_wrapper_paths = array();
0 ignored issues
show
Unused Code introduced by
The property $view_wrapper_paths is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
53
54
	/**
55
	 * Core scripts ready to be enqueued during the wp_enqueue_scripts hook.
56
	 *
57
	 * @since 4.0
58
	 * @var array
59
	 */
60
	private $queued_scripts = array();
61
62
	/**
63
	 * Core scripts to register during wp_enqueue_scripts hook.
64
	 *
65
	 * @since 4.0
66
	 * @var array
67
	 */
68
	private $core_scripts = array(
69
		'wpsc-select-autocomplete' => array(
70
			'path'         => 'js/jquery.select-to-autocomplete.js',
71
			'dependencies' => array( 'jquery-ui-autocomplete' ),
72
			'version'      => '1.0.5',
73
		),
74
		'wpsc-country-region' => array(
75
			'path'         => 'js/country-region.js',
76
			'dependencies' => array( 'wpsc-select-autocomplete', 'jquery' ),
77
			'version'      => WPSC_VERSION,
78
		),
79
		'wpsc-copy-billing-info' => array(
80
			'path'         => 'js/copy-billing-info.js',
81
			'dependencies' => array( 'jquery' ),
82
			'version'      => WPSC_VERSION,
83
		),
84
		'wpsc-shipping-price-simulator' => array(
85
			'path'         => 'js/shipping-price-simulator.js',
86
			'dependencies' => array( 'jquery' ),
87
			'version'      => WPSC_VERSION,
88
		),
89
		'wpsc-checkout-payment' => array(
90
			'path'         => 'js/checkout-payment.js',
91
			'dependencies' => array( 'jquery' ),
92
			'version'      => WPSC_VERSION,
93
		),
94
		'wpsc-cart-notifications' => array(
95
			'path'         => 'js/cart-notifications.js',
96
			'dependencies' => array( 'jquery' ),
97
			'version'      => WPSC_VERSION,
98
		),
99
	);
100
101
	/**
102
	 * Constructor
103
	 *
104
	 * @since 4.0
105
	 * @access private
106
	 */
107
	private function __construct() {
108
		$this->register_default_asset_paths();
109
		$this->register_default_template_part_paths();
110
		$this->register_default_view_wrapper_paths();
111
	}
112
113
	/**
114
	 * Register default paths to assets
115
	 *
116
	 * @since 4.0
117
	 */
118
	private function register_default_asset_paths() {
119
		// First, search in wp-e-commerce/assets under current theme
120
		$this->register_asset_path( STYLESHEETPATH . '/wp-e-commerce/assets', 10 );
121
122
		// Then, if this is a child theme, search in wp-e-commerce/assets under the parent theme
123
		if ( is_child_theme() ) {
124
			$this->register_asset_path( TEMPLATEPATH . '/wp-e-commerce/assets', 20 );
125
		}
126
127
		// Finally, fall back to the default asset path in theme engine's folder
128
		$this->register_asset_path( WPSC_TE_V2_ASSETS_PATH, 30 );
129
	}
130
131
	/**
132
	 * Register default paths to template parts
133
	 *
134
	 * @since 4.0
135
	 */
136
	private function register_default_template_part_paths() {
137
138
		// First, search in wp-e-commerce/template-parts under the current theme
139
		$this->register_template_part_path( STYLESHEETPATH . '/wp-e-commerce/template-parts', 10 );
140
141
		// Then, if this is a child theme, search in wp-e-commerce/template-parts under the parent theme
142
		if ( is_child_theme() ) {
143
			$this->register_template_part_path( TEMPLATEPATH . '/wp-e-commerce/template-parts', 20 );
144
		}
145
146
		// Finally, fall back to the default template part path in theme engine's folder
147
		$this->register_template_part_path( WPSC_TE_V2_TEMPLATE_PARTS_PATH, 30 );
148
	}
149
150
	/**
151
	 * Register default view wrapper paths
152
	 *
153
	 * @since 4.0
154
	 */
155
	private function register_default_view_wrapper_paths() {
156
		// First, search in wp-e-commerce subfolder inside the current theme
157
		$this->register_view_wrapper_path( STYLESHEETPATH . '/wp-e-commerce', 10 );
158
159
		// Then, if this is a child theme, search in wp-e-commerce subfolder inside the parent theme
160
		if ( is_child_theme() ) {
161
			$this->register_view_wrapper_path( TEMPLATEPATH . '/wp-e-commerce', 20 );
162
		}
163
	}
164
165
	/**
166
	 * Get all the registered core scripts data.
167
	 *
168
	 * @since  4.0
169
	 * @return array
170
	 */
171
	public function get_core_scripts_data() {
172
		return $this->core_scripts;
173
	}
174
175
	/**
176
	 * Register a path where template engine can look for a certain asset file
177
	 *
178
	 * @since 4.0
179
	 * @uses  WPSC_Template_Engine::register_thing()
180
	 * @param  string  $path     Path to the assets
181
	 * @param  integer $priority Optional. Priority of this path (smaller = higher priority). Defaults to 50.
182
	 */
183
	public function register_asset_path( $path, $priority = 50 ) {
184
		$this->register_thing( 'asset_paths', $path, $priority );
185
	}
186
187
	/**
188
	 * Register a path where template engine can look for a certain template part
189
	 *
190
	 * @since  0.1
191
	 * @param  string  $path     Path to the template parts
192
	 * @param  integer $priority Optional. Priority of this path (smaller = higher priority). Defaults to 50.
193
	 */
194
	public function register_template_part_path( $path, $priority = 50 ) {
195
		$this->register_thing( 'template_part_paths', $path, $priority );
196
	}
197
198
	/**
199
	 * Register a path where template engine can look for a certain view wraper
200
	 *
201
	 * @since  0.1
202
	 * @param  string  $path     Path to the view wrapper
203
	 * @param  integer $priority Optional. Priority of this path (smaller = higher priority). Defaults to 50.
204
	 */
205
	public function register_view_wrapper_path( $path, $priority = 50 ) {
206
		$this->register_thing( 'view_wrapper_paths', $path, $priority );
207
	}
208
209
	/**
210
	 * Register a path where template engine can look for a certain view wraper
211
	 *
212
	 * @since 4.0
213
	 * @param string $handle      Name of the core script to queue.
214
	 * @param array  $script_data (Optional) data to send to wp_localize_script under the WPSC namespace.
215
	 */
216
	public function register_queued_script( $handle, $script_data = array() ) {
217
		$this->queued_scripts[ $handle ] = $script_data;
218
	}
219
220
	/**
221
	 * Deregister a path where template engine can look for a certain asset.
222
	 *
223
	 * The priority has to be the same as when this path was registered.
224
	 *
225
	 * @since  0.1
226
	 * @param  string  $path     Path to remove
227
	 * @param  integer $priority Optional. Priority of this path (smaller = higher priority). Defaults to 50.
228
	 */
229
	public function deregister_asset_path( $path, $priority = 50 ) {
230
		$this->deregister_thing( 'asset_paths', $path, $priority );
231
	}
232
233
	/**
234
	 * Deregister a path where template engine can look for a certain template part
235
	 *
236
	 * The priority has to be the same as when this path was registered.
237
	 *
238
	 * @since  0.1
239
	 * @param  string  $path     Path to remove
240
	 * @param  integer $priority Optional. Priority of this path (smaller = higher priority). Defaults to 50.
241
	 */
242
	public function deregister_template_path( $path, $priority = 50 ) {
243
		$this->deregister_thing( 'template_part_paths', $path, $priority );
244
	}
245
246
	/**
247
	 * Deregister a path where template engine can look for a certain view wrapper
248
	 *
249
	 * The priority has to be the same as when this path was registered.
250
	 *
251
	 * @since  0.1
252
	 * @param  string  $path     Path to remove
253
	 * @param  integer $priority Optional. Priority of this path (smaller = higher priority). Defaults to 50.
254
	 */
255
	public function deregister_view_wrapper_path( $path, $priority = 50 ) {
256
		$this->deregister_thing( 'view_wrapper_paths', $path, $priority );
257
	}
258
259
	/**
260
	 * Register a path to the $var private variable.
261
	 *
262
	 * This is a private shortcut which is meant to be used internally.
263
	 *
264
	 * @since  0.1
265
	 * @param  string  $var      Variable name
266
	 * @param  string  $path     Path
267
	 * @param  integer $priority Priority
268
	 */
269
	private function register_thing( $var, $path, $priority = 50 ) {
270
		$arr = &$this->$var;
271
272
		if ( empty( $arr[ $priority ] ) ) {
273
			$arr[ $priority ] = array();
274
		}
275
276
		$arr[ $priority ][] = $path;
277
	}
278
279
	/**
280
	 * Deregister a path from the $var private variable.
281
	 *
282
	 * This is a private shortcut which is meant to be used internally.
283
	 *
284
	 * @since  0.1
285
	 * @param  string  $var      Variable name
286
	 * @param  string  $path     Path
287
	 * @param  integer $priority Priority
288
	 */
289
	private function deregister_thing( $var, $path, $priority = 50 ) {
290
		$arr = &$this->$var;
291
292
		if ( ! isset( $arr[ $priority ] ) ) {
293
			return;
294
		}
295
296
		$key = array_search( $path, $arr[ $priority ] );
297
298
		if ( $key !== false ) {
299
			unset( $arr[ $priority ][ $key ] );
300
		}
301
302
		return;
303
	}
304
305
	/**
306
	 * Get all the registered asset paths, ordered by priority
307
	 *
308
	 * @since  0.1
309
	 * @uses   WPSC_Template_Engine::get_paths()
310
	 * @return array
311
	 */
312
	public function get_asset_paths() {
313
		return $this->get_paths( 'asset_paths' );
314
	}
315
316
	/**
317
	 * Get all the registered template part paths, ordered by priority
318
	 *
319
	 * @since  0.1
320
	 * @uses   WPSC_Template_Engine::get_paths()
321
	 * @return array
322
	 */
323
	public function get_template_part_paths() {
324
		return $this->get_paths( 'template_part_paths' );
325
	}
326
327
	/**
328
	 * Get all the registered view wrapper paths, ordered by priority
329
	 *
330
	 * @since  0.1
331
	 * @uses   WPSC_Template_Engine::get_paths()
332
	 * @return array
333
	 */
334
	public function get_view_wrapper_paths() {
335
		return $this->get_paths( 'view_wrapper_paths' );
336
	}
337
338
	/**
339
	 * Get all the registered queued scripts.
340
	 *
341
	 * @since  4.0
342
	 * @return array
343
	 */
344
	public function get_queued_scripts() {
345
		return $this->queued_scripts;
346
	}
347
348
	/**
349
	 * Get all the registered paths from a private variable, ordered by priority.
350
	 *
351
	 * This is meant to be used privately.
352
	 *
353
	 * @since  0.1
354
	 * @uses   WPSC_Template_Engine::get_paths()
355
	 * @return array
356
	 */
357
	private function get_paths( $var ) {
358
		$return = array();
359
360
		foreach ( $this->$var as $paths ) {
361
			$return = array_merge( $return, $paths );
362
		}
363
364
		return $return;
365
	}
366
}
367