This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FeatureWithAssets.class.php
89 lines (81 loc) · 2.23 KB
/
FeatureWithAssets.class.php
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
<?php
/**
* Contains the FeatureWithAssets class
*/
/**
* Manages the assets loading for a feature.
*
* WARNING: this method doesn't register the asset:
* You have to take care of it by yourself (tipically in assets.php)
* @author etessore
* @version 1.0.1
* @package classes
* @deprecated uses ThemeHelpers::load_css() and ThemeHelpers::load_js() instead
*/
/*
* Changelog:
* 1.0.1
* Fixed Notice: wp_enqueue_script was called incorrectly
* 1.0.0
* Initial release
*/
class FeatureWithAssets{
/**
* @var array the list of assets to be loaded array('js'=>array(...), 'css'=>array(...));
*/
public $assets = array('js' => array(), 'css' => array());
/**
* Set the assets list to the given one
* @param array $assets the list of assets: array('js'=>array(...), 'css'=>array(...));
* @return FeatureWithAssets $this for chainability
*/
public function set_assets($assets){
if(
is_array($assets)
&& is_array($assets['js'])
&& is_array($assets['css'])
){
$this->assets = $assets;
}
return $this;
}
/**
* Adds an asset. You cannot define an asset here,
* to do it please use wp_register_script or wp_register_style
* in functions.php or config/assets.php
* WARNING: this method doesn't register the asset:
* You have to take care of it by yourself (tipically in assets.php)
* @param string $handle the handle for the asset.
* @param string $type the asset type: js|css
* @return FeatureWithAssets $this for chainability
*/
public function add_asset($handle, $type){
if($type == 'js' || $type == 'css'){
$this->assets[$type][] = $handle;
}
return $this;
}
/**
* Loads needed scripts and css
* @return FeatureWithAssets $this for chainability
*/
public function load_assets(){
if(!is_admin()){
add_action('wp_enqueue_scripts', array($this, 'load_assets_callback'));
}
return $this;
}
/**
* Loads needed scripts and css
* @return FeatureWithAssets $this for chainability
*/
public function load_assets_callback(){
foreach($this->assets['js'] as $js){
wp_enqueue_script($js);
}
foreach($this->assets['css'] as $css){
wp_enqueue_script($css);
}
return $this;
}
}