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