@@ -12,208 +12,208 @@ |
||
12 | 12 | * @subpackage Register |
13 | 13 | */ |
14 | 14 | class Register implements RegisterContract { |
15 | - /** |
|
16 | - * Minification string for enqueued assets. |
|
17 | - * |
|
18 | - * @var string |
|
19 | - */ |
|
20 | - private $min = '.min'; |
|
21 | - |
|
22 | - /** |
|
23 | - * Url to the plugin directory. |
|
24 | - * |
|
25 | - * @var string |
|
26 | - */ |
|
27 | - protected $url; |
|
28 | - |
|
29 | - /** |
|
30 | - * Script/plugin version. |
|
31 | - * |
|
32 | - * @var string |
|
33 | - */ |
|
34 | - protected $version; |
|
35 | - |
|
36 | - /** |
|
37 | - * Array of script definition arrays. |
|
38 | - * |
|
39 | - * @var array |
|
40 | - */ |
|
41 | - private $scripts = array(); |
|
42 | - |
|
43 | - /** |
|
44 | - * Array of style definition arrays. |
|
45 | - * |
|
46 | - * @var array |
|
47 | - */ |
|
48 | - private $styles = array(); |
|
49 | - |
|
50 | - /** |
|
51 | - * Instantiates a new instance of the Register class. |
|
52 | - * |
|
53 | - * The URL param should be relative to the plugin directory. The URL |
|
54 | - * form should always end with a '/'. All asset location definitions |
|
55 | - * should not begin with a slash and should be relative to the plugin's |
|
56 | - * root directory. The URL provided by default from the Application |
|
57 | - * class is compatible. |
|
58 | - * |
|
59 | - * @param string $url |
|
60 | - * @param string $version |
|
61 | - */ |
|
62 | - public function __construct( $url, $version = null ) { |
|
63 | - $this->url = $url; |
|
64 | - $this->version = $version ?: null; // Empty string should remain null. |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * {@inheritdoc} |
|
69 | - * |
|
70 | - * @param bool $debug |
|
71 | - */ |
|
72 | - public function set_debug( $debug ) { |
|
73 | - if ( $debug ) { |
|
74 | - $this->min = ''; |
|
75 | - } else { |
|
76 | - $this->min = '.min'; |
|
77 | - } |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * {@inheritdoc} |
|
82 | - * |
|
83 | - * @param array $script |
|
84 | - */ |
|
85 | - public function register_script( $script ) { |
|
86 | - $this->scripts[] = $script; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * {@inheritdoc} |
|
91 | - * |
|
92 | - * @param array $style |
|
93 | - */ |
|
94 | - public function register_style( $style ) { |
|
95 | - $this->styles[] = $style; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * {@inheritDoc} |
|
100 | - */ |
|
101 | - public function enqueue_web_scripts() { |
|
102 | - foreach ( $this->scripts as $script ) { |
|
103 | - if ( in_array( $script['type'], array( 'web', 'shared' ) ) ) { |
|
104 | - $this->enqueue_script( $script ); |
|
105 | - } |
|
106 | - } |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * {@inheritDoc} |
|
111 | - */ |
|
112 | - public function enqueue_web_styles() { |
|
113 | - foreach ( $this->styles as $style ) { |
|
114 | - if ( in_array( $style['type'], array( 'web', 'shared' ) ) ) { |
|
115 | - $this->enqueue_style( $style ); |
|
116 | - } |
|
117 | - } |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * {@inheritDoc} |
|
122 | - * |
|
123 | - * @param string $hook Passes a string representing the current page. |
|
124 | - */ |
|
125 | - public function enqueue_admin_scripts( $hook ) { |
|
126 | - foreach ( $this->scripts as $script ) { |
|
127 | - if ( in_array( $script['type'], array( 'admin', 'shared' ) ) ) { |
|
128 | - $this->enqueue_script( $script, $hook ); |
|
129 | - } |
|
130 | - } |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * {@inheritDoc} |
|
135 | - * |
|
136 | - * @param string $hook Passes a string representing the current page. |
|
137 | - */ |
|
138 | - public function enqueue_admin_styles( $hook ) { |
|
139 | - foreach ( $this->styles as $style ) { |
|
140 | - if ( in_array( $style['type'], array( 'admin', 'shared' ) ) ) { |
|
141 | - $this->enqueue_style( $style, $hook ); |
|
142 | - } |
|
143 | - } |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * {@inheritDoc} |
|
148 | - * |
|
149 | - * @return array[] |
|
150 | - */ |
|
151 | - public function action_hooks() { |
|
152 | - return array( |
|
153 | - array( |
|
154 | - 'hook' => 'wp_enqueue_scripts', |
|
155 | - 'method' => 'enqueue_web_scripts', |
|
156 | - ), |
|
157 | - array( |
|
158 | - 'hook' => 'wp_enqueue_scripts', |
|
159 | - 'method' => 'enqueue_web_styles', |
|
160 | - ), |
|
161 | - array( |
|
162 | - 'hook' => 'admin_enqueue_scripts', |
|
163 | - 'method' => 'enqueue_admin_scripts', |
|
164 | - ), |
|
165 | - array( |
|
166 | - 'hook' => 'admin_enqueue_scripts', |
|
167 | - 'method' => 'enqueue_admin_styles', |
|
168 | - ), |
|
169 | - ); |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Enqueues an individual script if the style's condition is met. |
|
174 | - * |
|
175 | - * @param array $script The script attachment callback. |
|
176 | - * @param string $hook The location hook. Only passed on admin side. |
|
177 | - */ |
|
178 | - protected function enqueue_script( $script, $hook = null ) { |
|
179 | - if ( $script['condition']( $hook ) ) { |
|
180 | - wp_enqueue_script( |
|
181 | - $script['handle'], |
|
182 | - $this->url . $script['src'] . $this->min . '.js', |
|
183 | - isset( $script['deps'] ) ? $script['deps'] : array(), |
|
184 | - $this->version, |
|
185 | - isset( $script['footer'] ) ? $script['footer'] : false |
|
186 | - ); |
|
187 | - |
|
188 | - if ( isset( $script['localize'] ) ) { |
|
189 | - if ( is_callable( $script['localize'] ) ) { // @todo make all properties callables |
|
190 | - $script['localize'] = call_user_func( $script['localize'] ); |
|
191 | - } |
|
192 | - |
|
193 | - wp_localize_script( |
|
194 | - $script['handle'], |
|
195 | - $script['localize']['name'], |
|
196 | - $script['localize']['data'] |
|
197 | - ); |
|
198 | - } |
|
199 | - } |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Enqueues an individual stylesheet if the style's condition is met. |
|
204 | - * |
|
205 | - * @param array $style The style attachment callback. |
|
206 | - * @param string $hook The location hook. |
|
207 | - */ |
|
208 | - protected function enqueue_style( $style, $hook = null ) { |
|
209 | - if ( $style['condition']( $hook ) ) { |
|
210 | - wp_enqueue_style( |
|
211 | - $style['handle'], |
|
212 | - $this->url . $style['src'] . $this->min . '.css', |
|
213 | - isset( $style['deps'] ) ? $style['deps'] : array(), |
|
214 | - $this->version, |
|
215 | - isset( $style['media'] ) ? $style['media'] : 'all' |
|
216 | - ); |
|
217 | - } |
|
218 | - } |
|
15 | + /** |
|
16 | + * Minification string for enqueued assets. |
|
17 | + * |
|
18 | + * @var string |
|
19 | + */ |
|
20 | + private $min = '.min'; |
|
21 | + |
|
22 | + /** |
|
23 | + * Url to the plugin directory. |
|
24 | + * |
|
25 | + * @var string |
|
26 | + */ |
|
27 | + protected $url; |
|
28 | + |
|
29 | + /** |
|
30 | + * Script/plugin version. |
|
31 | + * |
|
32 | + * @var string |
|
33 | + */ |
|
34 | + protected $version; |
|
35 | + |
|
36 | + /** |
|
37 | + * Array of script definition arrays. |
|
38 | + * |
|
39 | + * @var array |
|
40 | + */ |
|
41 | + private $scripts = array(); |
|
42 | + |
|
43 | + /** |
|
44 | + * Array of style definition arrays. |
|
45 | + * |
|
46 | + * @var array |
|
47 | + */ |
|
48 | + private $styles = array(); |
|
49 | + |
|
50 | + /** |
|
51 | + * Instantiates a new instance of the Register class. |
|
52 | + * |
|
53 | + * The URL param should be relative to the plugin directory. The URL |
|
54 | + * form should always end with a '/'. All asset location definitions |
|
55 | + * should not begin with a slash and should be relative to the plugin's |
|
56 | + * root directory. The URL provided by default from the Application |
|
57 | + * class is compatible. |
|
58 | + * |
|
59 | + * @param string $url |
|
60 | + * @param string $version |
|
61 | + */ |
|
62 | + public function __construct( $url, $version = null ) { |
|
63 | + $this->url = $url; |
|
64 | + $this->version = $version ?: null; // Empty string should remain null. |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * {@inheritdoc} |
|
69 | + * |
|
70 | + * @param bool $debug |
|
71 | + */ |
|
72 | + public function set_debug( $debug ) { |
|
73 | + if ( $debug ) { |
|
74 | + $this->min = ''; |
|
75 | + } else { |
|
76 | + $this->min = '.min'; |
|
77 | + } |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * {@inheritdoc} |
|
82 | + * |
|
83 | + * @param array $script |
|
84 | + */ |
|
85 | + public function register_script( $script ) { |
|
86 | + $this->scripts[] = $script; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * {@inheritdoc} |
|
91 | + * |
|
92 | + * @param array $style |
|
93 | + */ |
|
94 | + public function register_style( $style ) { |
|
95 | + $this->styles[] = $style; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * {@inheritDoc} |
|
100 | + */ |
|
101 | + public function enqueue_web_scripts() { |
|
102 | + foreach ( $this->scripts as $script ) { |
|
103 | + if ( in_array( $script['type'], array( 'web', 'shared' ) ) ) { |
|
104 | + $this->enqueue_script( $script ); |
|
105 | + } |
|
106 | + } |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * {@inheritDoc} |
|
111 | + */ |
|
112 | + public function enqueue_web_styles() { |
|
113 | + foreach ( $this->styles as $style ) { |
|
114 | + if ( in_array( $style['type'], array( 'web', 'shared' ) ) ) { |
|
115 | + $this->enqueue_style( $style ); |
|
116 | + } |
|
117 | + } |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * {@inheritDoc} |
|
122 | + * |
|
123 | + * @param string $hook Passes a string representing the current page. |
|
124 | + */ |
|
125 | + public function enqueue_admin_scripts( $hook ) { |
|
126 | + foreach ( $this->scripts as $script ) { |
|
127 | + if ( in_array( $script['type'], array( 'admin', 'shared' ) ) ) { |
|
128 | + $this->enqueue_script( $script, $hook ); |
|
129 | + } |
|
130 | + } |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * {@inheritDoc} |
|
135 | + * |
|
136 | + * @param string $hook Passes a string representing the current page. |
|
137 | + */ |
|
138 | + public function enqueue_admin_styles( $hook ) { |
|
139 | + foreach ( $this->styles as $style ) { |
|
140 | + if ( in_array( $style['type'], array( 'admin', 'shared' ) ) ) { |
|
141 | + $this->enqueue_style( $style, $hook ); |
|
142 | + } |
|
143 | + } |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * {@inheritDoc} |
|
148 | + * |
|
149 | + * @return array[] |
|
150 | + */ |
|
151 | + public function action_hooks() { |
|
152 | + return array( |
|
153 | + array( |
|
154 | + 'hook' => 'wp_enqueue_scripts', |
|
155 | + 'method' => 'enqueue_web_scripts', |
|
156 | + ), |
|
157 | + array( |
|
158 | + 'hook' => 'wp_enqueue_scripts', |
|
159 | + 'method' => 'enqueue_web_styles', |
|
160 | + ), |
|
161 | + array( |
|
162 | + 'hook' => 'admin_enqueue_scripts', |
|
163 | + 'method' => 'enqueue_admin_scripts', |
|
164 | + ), |
|
165 | + array( |
|
166 | + 'hook' => 'admin_enqueue_scripts', |
|
167 | + 'method' => 'enqueue_admin_styles', |
|
168 | + ), |
|
169 | + ); |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Enqueues an individual script if the style's condition is met. |
|
174 | + * |
|
175 | + * @param array $script The script attachment callback. |
|
176 | + * @param string $hook The location hook. Only passed on admin side. |
|
177 | + */ |
|
178 | + protected function enqueue_script( $script, $hook = null ) { |
|
179 | + if ( $script['condition']( $hook ) ) { |
|
180 | + wp_enqueue_script( |
|
181 | + $script['handle'], |
|
182 | + $this->url . $script['src'] . $this->min . '.js', |
|
183 | + isset( $script['deps'] ) ? $script['deps'] : array(), |
|
184 | + $this->version, |
|
185 | + isset( $script['footer'] ) ? $script['footer'] : false |
|
186 | + ); |
|
187 | + |
|
188 | + if ( isset( $script['localize'] ) ) { |
|
189 | + if ( is_callable( $script['localize'] ) ) { // @todo make all properties callables |
|
190 | + $script['localize'] = call_user_func( $script['localize'] ); |
|
191 | + } |
|
192 | + |
|
193 | + wp_localize_script( |
|
194 | + $script['handle'], |
|
195 | + $script['localize']['name'], |
|
196 | + $script['localize']['data'] |
|
197 | + ); |
|
198 | + } |
|
199 | + } |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Enqueues an individual stylesheet if the style's condition is met. |
|
204 | + * |
|
205 | + * @param array $style The style attachment callback. |
|
206 | + * @param string $hook The location hook. |
|
207 | + */ |
|
208 | + protected function enqueue_style( $style, $hook = null ) { |
|
209 | + if ( $style['condition']( $hook ) ) { |
|
210 | + wp_enqueue_style( |
|
211 | + $style['handle'], |
|
212 | + $this->url . $style['src'] . $this->min . '.css', |
|
213 | + isset( $style['deps'] ) ? $style['deps'] : array(), |
|
214 | + $this->version, |
|
215 | + isset( $style['media'] ) ? $style['media'] : 'all' |
|
216 | + ); |
|
217 | + } |
|
218 | + } |
|
219 | 219 | } |