Register::enqueue_script()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 13
loc 13
ccs 0
cts 10
cp 0
rs 9.8333
c 0
b 0
f 0
cc 4
nc 2
nop 2
crap 20
1
<?php
2
namespace Intraxia\Jaxion\Assets;
3
4
use Intraxia\Jaxion\Contract\Assets\Register as RegisterContract;
5
6
/**
7
 * Class Register
8
 *
9
 * Provides a consistent interface for registering static assets with WordPress.
10
 *
11
 * @package    Intraxia\Jaxion
12
 * @subpackage Register
13
 */
14
class Register implements RegisterContract {
15
	/**
16
	 * Url to the plugin directory.
17
	 *
18
	 * @var string
19
	 */
20
	protected $url;
21
22
	/**
23
	 * Script/plugin version.
24
	 *
25
	 * @var string
26
	 */
27
	protected $version;
28
29
	/**
30
	 * Array of script definition arrays.
31
	 *
32
	 * @var array
33
	 */
34
	private $scripts = array();
35
36
	/**
37
	 * Array of style definition arrays.
38
	 *
39
	 * @var array
40
	 */
41
	private $styles = array();
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
	}
59
60
	/**
61
	 * {@inheritdoc}
62 45
	 *
63 45
	 * @param array $script
64 45
	 */
65 45
	public function register_script( $script ) {
66
		$this->scripts[] = $script;
67
	}
68
69
	/**
70
	 * {@inheritdoc}
71
	 *
72 42
	 * @param array $style
73 42
	 */
74 42
	public function register_style( $style ) {
75 28
		$this->styles[] = $style;
76 3
	}
77
78 42
	/**
79
	 * {@inheritDoc}
80
	 */
81 View Code Duplication
	public function enqueue_web_scripts() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
		foreach ( $this->scripts as $script ) {
83
			if ( in_array( $script['type'], array( 'web', 'shared' ) ) ) {
84
				$this->enqueue_script( $script );
85 21
			}
86 21
		}
87 21
	}
88
89
	/**
90
	 * {@inheritDoc}
91
	 */
92 View Code Duplication
	public function enqueue_web_styles() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
		foreach ( $this->styles as $style ) {
94 18
			if ( in_array( $style['type'], array( 'web', 'shared' ) ) ) {
95 18
				$this->enqueue_style( $style );
96 18
			}
97
		}
98
	}
99
100
	/**
101 15
	 * {@inheritDoc}
102 15
	 *
103 15
	 * @param string $hook Passes a string representing the current page.
104 15
	 */
105 10 View Code Duplication
	public function enqueue_admin_scripts( $hook ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106 10
		foreach ( $this->scripts as $script ) {
107 15
			if ( in_array( $script['type'], array( 'admin', 'shared' ) ) ) {
108
				$this->enqueue_script( $script, $hook );
109
			}
110
		}
111
	}
112 12
113 12
	/**
114 12
	 * {@inheritDoc}
115 12
	 *
116 8
	 * @param string $hook Passes a string representing the current page.
117 8
	 */
118 12 View Code Duplication
	public function enqueue_admin_styles( $hook ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 6
126 6
	/**
127 6
	 * {@inheritDoc}
128 6
	 */
129 4
	public function register_blocks() {
130 4
		$blocks = array();
131 6
132 View Code Duplication
		foreach ( $this->styles as $style ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 18
					$this->version,
139 18
					isset( $style['media'] ) ? $style['media'] : 'all'
140 12
				);
141 12
142 8
				if ( ! isset( $blocks[ $style['block'] ] ) ) {
143 12
					$blocks[ $style['block'] ] = array();
144 18
				}
145
146
				$blocks[ $style['block'] ]['editor_style'] = $style['handle'];
147
			}
148
		}
149
150 View Code Duplication
		foreach ( $this->scripts as $script ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
160
				$this->localize_script( $script );
161
162
				if ( ! isset( $blocks[ $script['block'] ] ) ) {
163
					$blocks[ $script['block'] ] = array();
164
				}
165
166
				$blocks[ $script['block'] ]['editor_script'] = $script['handle'];
167
			}
168
		}
169
170
		foreach ( $blocks as $slug => $opts ) {
171
			register_block_type( $slug, $opts );
172
		}
173
	}
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
	}
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 View Code Duplication
	protected function enqueue_script( $script, $hook = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
221
			$this->localize_script( $script );
222
		}
223
	}
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 18 View Code Duplication
	protected function enqueue_style( $style, $hook = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
232 18
		if ( $style['condition']( $hook ) ) {
233 12
			wp_enqueue_style(
234 12
				$style['handle'],
235 12
				$this->url . $style['src'],
236 12
				isset( $style['deps'] ) ? $style['deps'] : array(),
237 12
				$this->version,
238 12
				isset( $style['media'] ) ? $style['media'] : 'all'
239 8
			);
240
		}
241 12
	}
242 8
243 18
	/**
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 18
		}
252 18
253 9
		if ( is_callable( $script['localize'] ) ) { // @todo make all properties callables
254 9
			$script['localize'] = call_user_func( $script['localize'] );
255 9
		}
256 9
257 9
		wp_localize_script(
258 9
			$script['handle'],
259 6
			$script['localize']['name'],
260 6
			$script['localize']['data']
261 18
		);
262
	}
263
}
264