| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Framework; |
| 4 |
|
| 5 |
use function wp_set_script_translations; |
| 6 |
|
| 7 |
/** |
| 8 |
* This class use to register script. |
| 9 |
* This class internally uses asset information to set script dependencies and version generated by @wordpress/dependency-extraction-webpack-plugin package. |
| 10 |
* It also handles script translation registration. |
| 11 |
* |
| 12 |
* @since 2.19.6 |
| 13 |
*/ |
| 14 |
class EnqueueScript |
| 15 |
{ |
| 16 |
/** |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
private $scriptId; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
private $relativeScriptPath; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
private $absoluteScriptPath; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
private $scriptDependencies = []; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
private $version = ''; |
| 40 |
|
| 41 |
/** |
| 42 |
* @var bool |
| 43 |
*/ |
| 44 |
private $loadScriptInFooter = false; |
| 45 |
|
| 46 |
/** |
| 47 |
* @var bool |
| 48 |
*/ |
| 49 |
private $registerTranslations = false; |
| 50 |
|
| 51 |
/** |
| 52 |
* @var string |
| 53 |
*/ |
| 54 |
private $localizeScriptParamName; |
| 55 |
|
| 56 |
/** |
| 57 |
* @var mixed |
| 58 |
*/ |
| 59 |
private $localizeScriptParamData; |
| 60 |
|
| 61 |
/** |
| 62 |
* @var string |
| 63 |
*/ |
| 64 |
private $pluginDirPath; |
| 65 |
|
| 66 |
/** |
| 67 |
* @var string |
| 68 |
*/ |
| 69 |
private $pluginDirUrl; |
| 70 |
|
| 71 |
/** |
| 72 |
* @var string |
| 73 |
*/ |
| 74 |
private $textDomain; |
| 75 |
|
| 76 |
/** |
| 77 |
* @since 2.19.6 |
| 78 |
* |
| 79 |
* @param string $scriptId |
| 80 |
* @param string $scriptPath |
| 81 |
* @param string $pluginDirPath |
| 82 |
* @param string $pluginDirUrl |
| 83 |
* @param string $textDomain |
| 84 |
*/ |
| 85 |
public function __construct($scriptId, $scriptPath, $pluginDirPath, $pluginDirUrl, $textDomain) |
| 86 |
{ |
| 87 |
$this->pluginDirPath = trailingslashit($pluginDirPath); |
| 88 |
$this->pluginDirUrl = trailingslashit($pluginDirUrl); |
| 89 |
$this->textDomain = $textDomain; |
| 90 |
$this->scriptId = $scriptId; |
| 91 |
$this->relativeScriptPath = $scriptPath; |
| 92 |
$this->absoluteScriptPath = $this->pluginDirPath . $this->relativeScriptPath; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @since 2.19.6 |
| 97 |
* |
| 98 |
* @param string $version |
| 99 |
* |
| 100 |
* @return $this |
| 101 |
*/ |
| 102 |
public function version($version) |
| 103 |
{ |
| 104 |
$this->version = $version; |
| 105 |
return $this; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* @since 2.19.6 |
| 110 |
* @return $this |
| 111 |
*/ |
| 112 |
public function loadInFooter() |
| 113 |
{ |
| 114 |
$this->loadScriptInFooter = true; |
| 115 |
return $this; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @since 2.19.6 |
| 120 |
* |
| 121 |
* @param array $scriptDependencies |
| 122 |
* |
| 123 |
* @return $this |
| 124 |
*/ |
| 125 |
public function dependencies(array $scriptDependencies) |
| 126 |
{ |
| 127 |
$this->scriptDependencies = $scriptDependencies; |
| 128 |
return $this; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* @since 2.19.6 |
| 133 |
* @return $this |
| 134 |
*/ |
| 135 |
public function register() |
| 136 |
{ |
| 137 |
$scriptUrl = $this->pluginDirUrl . $this->relativeScriptPath; |
| 138 |
$scriptAsset = $this->getAssetFileData(); |
| 139 |
|
| 140 |
wp_register_script( |
| 141 |
$this->scriptId, |
| 142 |
$scriptUrl, |
| 143 |
$scriptAsset['dependencies'], |
| 144 |
$scriptAsset['version'], |
| 145 |
$this->loadScriptInFooter |
| 146 |
); |
| 147 |
|
| 148 |
if ($this->registerTranslations) { |
| 149 |
wp_set_script_translations( |
| 150 |
$this->scriptId, |
| 151 |
$this->textDomain, |
| 152 |
$this->pluginDirPath . 'languages' |
| 153 |
); |
| 154 |
} |
| 155 |
|
| 156 |
if ($this->localizeScriptParamData) { |
| 157 |
wp_localize_script( |
| 158 |
$this->scriptId, |
| 159 |
$this->localizeScriptParamName, |
| 160 |
$this->localizeScriptParamData |
| 161 |
); |
| 162 |
} |
| 163 |
|
| 164 |
return $this; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* This function should be called after enqueue or register function. |
| 169 |
* |
| 170 |
* @since 2.19.6 |
| 171 |
* @return $this |
| 172 |
*/ |
| 173 |
public function registerTranslations() |
| 174 |
{ |
| 175 |
$this->registerTranslations = true; |
| 176 |
|
| 177 |
return $this; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* This function should be called after enqueue or register function. |
| 182 |
* |
| 183 |
* @param string $jsVariableName |
| 184 |
* @param mixed $data |
| 185 |
* |
| 186 |
* @return $this |
| 187 |
*/ |
| 188 |
public function registerLocalizeData($jsVariableName, $data) |
| 189 |
{ |
| 190 |
$this->localizeScriptParamName = $jsVariableName; |
| 191 |
$this->localizeScriptParamData = $data; |
| 192 |
|
| 193 |
return $this; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* @since 2.19.6 |
| 198 |
* @return $this |
| 199 |
*/ |
| 200 |
public function enqueue() |
| 201 |
{ |
| 202 |
if (!wp_script_is($this->scriptId, 'registered')) { |
| 203 |
$this->register(); |
| 204 |
} |
| 205 |
wp_enqueue_script($this->scriptId); |
| 206 |
|
| 207 |
return $this; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* @since 2.19.6 |
| 212 |
* @return string |
| 213 |
*/ |
| 214 |
public function getScriptId() |
| 215 |
{ |
| 216 |
return $this->scriptId; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* @since 2.19.6 |
| 221 |
* |
| 222 |
* @return array |
| 223 |
*/ |
| 224 |
public function getAssetFileData() |
| 225 |
{ |
| 226 |
$scriptAssetPath = trailingslashit(dirname($this->absoluteScriptPath)) |
| 227 |
. basename($this->absoluteScriptPath, '.js') |
| 228 |
. '.asset.php'; |
| 229 |
$scriptAsset = file_exists($scriptAssetPath) |
| 230 |
? require($scriptAssetPath) |
| 231 |
: ['dependencies' => [], 'version' => $this->version ?: filemtime($this->absoluteScriptPath)]; |
| 232 |
|
| 233 |
if ($this->scriptDependencies) { |
| 234 |
$scriptAsset['dependencies'] = array_merge($this->scriptDependencies, $scriptAsset['dependencies']); |
| 235 |
} |
| 236 |
|
| 237 |
return $scriptAsset; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
|