1 | <?php |
||
7 | class Asset |
||
8 | { |
||
9 | const DEFAULT_OPTIONS = [ |
||
10 | 'dependencies' => [], |
||
11 | 'version' => null, |
||
12 | 'inFooter' => true, |
||
13 | 'media' => 'all', |
||
14 | 'cdn' => [] |
||
15 | ]; |
||
16 | |||
17 | protected static $assetManifest; |
||
18 | protected static $loadFromCdn = false; |
||
19 | |||
20 | /** |
||
21 | * Gets the asset's url. |
||
22 | * |
||
23 | * @since 0.1.0 |
||
24 | * |
||
25 | * @param string $asset The filename of the required asset. |
||
26 | * |
||
27 | * @return string|boolean Returns the url or false if the asset is not found. |
||
28 | */ |
||
29 | public static function requireUrl($asset) |
||
33 | |||
34 | /** |
||
35 | * Gets the asset's absolute path. |
||
36 | * |
||
37 | * @since 0.1.0 |
||
38 | * |
||
39 | * @param string $asset The filename of the required asset. |
||
40 | * |
||
41 | * @return string|boolean Returns the absolute path or false if the asset is not found. |
||
42 | */ |
||
43 | public static function requirePath($asset) |
||
47 | |||
48 | /** |
||
49 | * Registers an asset. |
||
50 | * It can register a script or a style. |
||
51 | * |
||
52 | * If the Asset utility has `loadFromCdn` set to true, you can use the cdn option to load an asset from the CDN, |
||
53 | * the path option will then become the local fallback file, using the 'check' in the 'cdn' array. |
||
54 | * |
||
55 | * @since 0.1.0 |
||
56 | * @since 0.2.0 Supports CDN parameter. |
||
57 | * |
||
58 | * @param array $options Options must specify a type, a name and a path. |
||
59 | * $options = [ |
||
60 | * 'type' => (string) The type of asset to register (script|style). |
||
61 | * 'name' => (string) Should be unique as it is used to identify the script in the whole system. |
||
62 | * 'path' => (string) The path. |
||
63 | * 'dependencies' => (array) An array of registered script handles this script depends on. |
||
64 | * 'version' => (string/boolean) String specifying script version number, if it has one. |
||
65 | * 'inFooter' => (boolean) If the type is 'script', whether to enqueue the script before </body> instead of in the <head>. Default 'false'. |
||
66 | * 'media' => (string) If the type is 'style', string specifying the media for which this stylesheet has been defined (all/screen/handheld/print). |
||
67 | * 'cdn' => [ |
||
68 | * 'url' => (string) The CDN url. |
||
69 | * 'check' => (string/boolean) Javascript boolean value that loads the local fallback if it is false. |
||
70 | * ] |
||
71 | * ] |
||
72 | * |
||
73 | * @return boolean |
||
74 | */ |
||
75 | public static function register($options) |
||
79 | |||
80 | /** |
||
81 | * Enqueues an asset. |
||
82 | * It can enqueue a script or a style. |
||
83 | * |
||
84 | * @since 0.1.0 |
||
85 | * @since 0.2.0 Supports CDN parameter. |
||
86 | * |
||
87 | * @param array $options Options must specify a type, a name and a path. |
||
88 | * $options = [ |
||
89 | * 'type' => (string) The type of asset to register (script|style). |
||
90 | * 'name' => (string) Should be unique as it is used to identify the script in the whole system. |
||
91 | * 'path' => (string) The path. |
||
92 | * 'dependencies' => (array) An array of registered script handles this script depends on. |
||
93 | * 'version' => (string/boolean) String specifying script version number, if it has one. |
||
94 | * 'inFooter' => (boolean) If the type is 'script', whether to enqueue the script before </body> instead of in the <head>. Default 'false'. |
||
95 | * 'media' => (string) If the type is 'style', string specifying the media for which this stylesheet has been defined (all/screen/handheld/print). |
||
96 | * 'cdn' => [ |
||
97 | * 'url' => (string) The CDN url. |
||
98 | * 'check' => (string/boolean) Javascript boolean value that loads the local fallback if it is false. |
||
99 | * ] |
||
100 | * ] |
||
101 | * |
||
102 | * @return boolean |
||
103 | */ |
||
104 | public static function enqueue($options) |
||
108 | |||
109 | /** |
||
110 | * Getter and setter for the loadFromCdn setting. |
||
111 | * |
||
112 | * @param boolean $load (optional) Value to set the parameter to. |
||
113 | * |
||
114 | * @since 0.2.0 |
||
115 | */ |
||
116 | public static function loadFromCdn($load = null) |
||
123 | |||
124 | /** |
||
125 | * Gets the contents of an asset. |
||
126 | * |
||
127 | * Useful for loading SVGs or other files inline. |
||
128 | * |
||
129 | * @since 0.2.0 |
||
130 | * |
||
131 | * @param string $asset Asset path (relative to the theme directory). |
||
132 | * @return string|boolean Returns the file contents or false in case of failure. |
||
133 | */ |
||
134 | public static function getContents($asset) |
||
144 | |||
145 | protected static function get($returnType, $asset) |
||
173 | |||
174 | protected static function add($funcType, $options) |
||
248 | } |
||
249 |