Completed
Pull Request — staging (#840)
by
unknown
16:56
created

ScriptAsset::add_localization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * YIKES Inc. Easy Mailchimp Forms Plugin.
4
 *
5
 * @package   YIKES\EasyForms
6
 * @author    Freddie Mixell
7
 * @license   GPL2
8
 */
9
10
namespace YIKES\EasyForms\Assets;
11
12
use YIKES\EasyForms\Exception\FailedToRegister;
13
use YIKES\EasyForms\Plugin;
14
use Closure;
15
16
/**
17
 * Class ScriptAsset.
18
 *
19
 * @since   %VERSION%
20
 *
21
 * @package YIKES\EasyForms\Assets
22
 * @author  Freddie Mixell
23
 */
24
class ScriptAsset extends BaseAsset {
25
26
	const ENQUEUE_HEADER = false;
27
	const ENQUEUE_FOOTER = true;
28
29
	const DEFAULT_EXTENSION = 'js';
30
31
	const VERSION = Plugin::VERSION;
32
33
	/**
34
	 * Source location of the asset.
35
	 *
36
	 * @since %VERSION%
37
	 *
38
	 * @var string
39
	 */
40
	protected $source;
41
42
	/**
43
	 * Dependencies of the asset.
44
	 *
45
	 * @since %VERSION%
46
	 *
47
	 * @var string[]
48
	 */
49
	protected $dependencies;
50
51
	/**
52
	 * Version of the asset.
53
	 *
54
	 * @since %VERSION%
55
	 *
56
	 * @var string|bool|null
57
	 */
58
	protected $version;
59
60
	/**
61
	 * Whether to enqueue the script in the footer.
62
	 *
63
	 * @since %VERSION%
64
	 *
65
	 * @var bool
66
	 */
67
	protected $in_footer;
68
69
	/**
70
	 * Localization data that is added to the JS space.
71
	 *
72
	 * @since %VERSION%
73
	 *
74
	 * @var array
75
	 */
76
	protected $localizations = [];
77
78
	/**
79
	 * Instantiate a ScriptAsset object.
80
	 *
81
	 * @since %VERSION%
82
	 *
83
	 * @param string           $handle       Handle of the asset.
84
	 * @param string           $source       Source location of the asset.
85
	 * @param array            $dependencies Optional. Dependencies of the
86
	 *                                       asset.
87
	 * @param string|bool|null $version      Optional. Version of the asset.
88
	 * @param bool             $in_footer    Whether to enqueue the asset in
89
	 *                                       the footer.
90
	 */
91
	public function __construct(
92
		$handle,
93
		$source,
94
		$dependencies = [],
95
		$version = self::VERSION,
96
		$in_footer = self::ENQUEUE_HEADER,
97
		$external = false
98
	) {
99
		$this->handle       = $handle;
100
		$this->source       = ! $external ? $this->normalize_source( $source, static::DEFAULT_EXTENSION ) : $source;
101
		$this->dependencies = (array) $dependencies;
102
		$this->version      = $version;
103
		$this->in_footer    = $in_footer;
104
	}
105
106
	/**
107
	 * Add a localization to the script.
108
	 *
109
	 * @since %VERSION%
110
	 *
111
	 * @param string $object_name Name of the object to create in JS space.
112
	 * @param array  $data_array  Array of data to attach to the object.
113
	 *
114
	 * @return static
115
	 */
116
	public function add_localization( $object_name, $data_array ) {
117
		$this->localizations[ $object_name ] = $data_array;
118
119
		return $this;
120
	}
121
122
	/**
123
	 * Get the enqueue closure to use.
124
	 *
125
	 * @since %VERSION%
126
	 *
127
	 * @return Closure
128
	 */
129 View Code Duplication
	protected function get_register_closure() {
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...
130
		return function () {
131
			if ( wp_script_is( $this->handle, 'registered' ) ) {
132
				return;
133
			}
134
135
			wp_register_script(
136
				$this->handle,
137
				$this->source,
138
				$this->dependencies,
139
				$this->version,
140
				$this->in_footer
141
			);
142
		};
143
	}
144
145
	/**
146
	 * Get the enqueue closure to use.
147
	 *
148
	 * @since %VERSION%
149
	 *
150
	 * @return Closure
151
	 */
152
	protected function get_enqueue_closure() {
153
		return function () {
154
			if ( ! wp_script_is( $this->handle, 'registered' ) ) {
155
				throw FailedToRegister::asset_not_registered( $this->handle );
156
			}
157
158
			foreach ( $this->localizations as $object_name => $data_array ) {
159
				wp_localize_script( $this->handle, $object_name, $data_array );
160
			}
161
162
			wp_enqueue_script( $this->handle );
163
		};
164
	}
165
166
	/**
167
	 * Get the dequeue closure to use.
168
	 *
169
	 * @since %VERSION%
170
	 *
171
	 * @return Closure
172
	 */
173
	protected function get_dequeue_closure() {
174
		return function () {
175
			wp_dequeue_script( $this->handle );
176
		};
177
	}
178
}
179