| 1 |
<?php |
| 2 |
/** |
| 3 |
* Enqueue Trait |
| 4 |
* |
| 5 |
* Reusable trait for enqueueing scripts, styles, translations, |
| 6 |
* and localizing data with consistent handle prefixing. |
| 7 |
* |
| 8 |
* @package zip-ai |
| 9 |
* @since 0.0.1 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace ZipAI\Classes\Traits; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Trait Enqueue. |
| 20 |
* |
| 21 |
* @since 0.0.1 |
| 22 |
*/ |
| 23 |
trait Enqueue { |
| 24 |
|
| 25 |
/** |
| 26 |
* Handle prefix for all enqueued assets. |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
public $enqueue_prefix = 'zip-ai'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Build path (absolute filesystem path). |
| 34 |
* |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
public $build_path = ZIP_AI_DIR . 'assets/'; |
| 38 |
|
| 39 |
/** |
| 40 |
* Build URL. |
| 41 |
* |
| 42 |
* @var string |
| 43 |
*/ |
| 44 |
public $build_url = ZIP_AI_URL . 'assets/'; |
| 45 |
|
| 46 |
/** |
| 47 |
* Language directory for translations. |
| 48 |
* |
| 49 |
* @var string |
| 50 |
*/ |
| 51 |
public $language_dir = ZIP_AI_DIR . 'languages'; |
| 52 |
|
| 53 |
/** |
| 54 |
* Register frontend enqueue hook. |
| 55 |
* |
| 56 |
* @since 0.0.1 |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function enqueue_scripts() { |
| 60 |
add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ) ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Register admin enqueue hook. |
| 65 |
* |
| 66 |
* @since 0.0.1 |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function enqueue_scripts_admin() { |
| 70 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Register, enqueue, localize, and set translations for a script. |
| 75 |
* |
| 76 |
* @param string $hook Handle suffix (prefixed automatically). |
| 77 |
* @param string $path Full URL to the script file. |
| 78 |
* @param array $dependency Script dependencies. |
| 79 |
* @param array $localization_array Localization config: [ 'hook' => '', 'object_name' => '', 'data' => [] ]. |
| 80 |
* @param string $version Asset version. Defaults to plugin version. |
| 81 |
* |
| 82 |
* @since 0.0.1 |
| 83 |
* @return void |
| 84 |
*/ |
| 85 |
public function script_operations( $hook, $path, $dependency, $localization_array = array(), $version = ZIP_AI_VERSION ) { |
| 86 |
$this->register_script( $hook, $path, $dependency, $version ); |
| 87 |
$this->enqueue_script( $hook ); |
| 88 |
|
| 89 |
if ( ! empty( $localization_array['hook'] ) && ! empty( $localization_array['object_name'] ) && ! empty( $localization_array['data'] ) ) { |
| 90 |
$this->localize_script( $localization_array['hook'], $localization_array['object_name'], $localization_array['data'] ); |
| 91 |
} |
| 92 |
|
| 93 |
if ( function_exists( 'wp_set_script_translations' ) ) { |
| 94 |
wp_set_script_translations( |
| 95 |
$this->enqueue_prefix . '-' . $hook, |
| 96 |
$this->enqueue_prefix, |
| 97 |
$this->language_dir |
| 98 |
); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Register and enqueue a style. |
| 104 |
* |
| 105 |
* @param string $hook Handle suffix (prefixed automatically). |
| 106 |
* @param string $path Full URL to the CSS file. |
| 107 |
* @param array $dependency Style dependencies. |
| 108 |
* @param string $version Asset version. Defaults to plugin version. |
| 109 |
* |
| 110 |
* @since 0.0.1 |
| 111 |
* @return void |
| 112 |
*/ |
| 113 |
public function style_operations( $hook, $path, $dependency = array(), $version = ZIP_AI_VERSION ) { |
| 114 |
$this->register_style( $hook, $path, $dependency, $version ); |
| 115 |
$this->enqueue_style( $hook ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Enqueue a build asset (JS + CSS) using its .asset.php manifest. |
| 120 |
* |
| 121 |
* Reads the asset manifest generated by @wordpress/scripts to resolve |
| 122 |
* dependencies and version hashes automatically. |
| 123 |
* |
| 124 |
* @param string $handle Directory name under build path (also used as handle suffix). |
| 125 |
* @param array $localization_data Localization config: [ 'hook' => '', 'object_name' => '', 'data' => [] ]. |
| 126 |
* @param array $script_dep Additional script dependencies beyond manifest. |
| 127 |
* |
| 128 |
* @since 0.0.1 |
| 129 |
* @return void |
| 130 |
*/ |
| 131 |
public function build_assets_operations( $handle, $localization_data = array(), $script_dep = array() ) { |
| 132 |
if ( empty( $handle ) || ! is_string( $handle ) ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
$script_asset_path = $this->build_path . $handle . '/index.asset.php'; |
| 137 |
$script_info = file_exists( $script_asset_path ) |
| 138 |
? include $script_asset_path |
| 139 |
: array( |
| 140 |
'dependencies' => array(), |
| 141 |
'version' => ZIP_AI_VERSION . wp_rand(), |
| 142 |
); |
| 143 |
|
| 144 |
$script_dep = array_merge( $script_info['dependencies'], $script_dep ); |
| 145 |
|
| 146 |
$this->enqueue_files_with_deps( $handle, $handle, $script_dep, $localization_data ); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Register a script with prefixed handle. |
| 151 |
* |
| 152 |
* @param string $hook Handle suffix. |
| 153 |
* @param string $path Full URL to the script. |
| 154 |
* @param array $dependency Script dependencies. |
| 155 |
* @param string $version Asset version. |
| 156 |
* |
| 157 |
* @since 0.0.1 |
| 158 |
* @return void |
| 159 |
*/ |
| 160 |
public function register_script( $hook, $path, $dependency, $version = ZIP_AI_VERSION ) { |
| 161 |
wp_register_script( |
| 162 |
$this->enqueue_prefix . '-' . $hook, |
| 163 |
$path, |
| 164 |
$dependency, |
| 165 |
$version, |
| 166 |
true |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Enqueue a previously registered script. |
| 172 |
* |
| 173 |
* @param string $hook Handle suffix. |
| 174 |
* |
| 175 |
* @since 0.0.1 |
| 176 |
* @return void |
| 177 |
*/ |
| 178 |
public function enqueue_script( $hook ) { |
| 179 |
wp_enqueue_script( $this->enqueue_prefix . '-' . $hook ); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Localize a script with prefixed handle and object name. |
| 184 |
* |
| 185 |
* @param string $hook Handle suffix. |
| 186 |
* @param string $object_name JS global object name. |
| 187 |
* @param array $data Key-value data to expose. |
| 188 |
* |
| 189 |
* @since 0.0.1 |
| 190 |
* @return void |
| 191 |
*/ |
| 192 |
public function localize_script( $hook, $object_name, $data ) { |
| 193 |
wp_localize_script( |
| 194 |
$this->enqueue_prefix . '-' . $hook, |
| 195 |
$object_name, |
| 196 |
$data |
| 197 |
); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Register a style with prefixed handle. |
| 202 |
* |
| 203 |
* @param string $hook Handle suffix. |
| 204 |
* @param string $path Full URL to the CSS file. |
| 205 |
* @param array $dependency Style dependencies. |
| 206 |
* @param string $version Asset version. |
| 207 |
* |
| 208 |
* @since 0.0.1 |
| 209 |
* @return void |
| 210 |
*/ |
| 211 |
public function register_style( $hook, $path, $dependency = array(), $version = ZIP_AI_VERSION ) { |
| 212 |
wp_register_style( |
| 213 |
$this->enqueue_prefix . '-' . $hook, |
| 214 |
$path, |
| 215 |
$dependency, |
| 216 |
$version |
| 217 |
); |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Enqueue a previously registered style. |
| 222 |
* |
| 223 |
* @param string $hook Handle suffix. |
| 224 |
* |
| 225 |
* @since 0.0.1 |
| 226 |
* @return void |
| 227 |
*/ |
| 228 |
public function enqueue_style( $hook ) { |
| 229 |
wp_enqueue_style( $this->enqueue_prefix . '-' . $hook ); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Create nonces for an array of event names. |
| 234 |
* |
| 235 |
* @param array $events Event name strings. |
| 236 |
* |
| 237 |
* @since 0.0.1 |
| 238 |
* @return array Associative array of event_name_nonce => nonce_value. |
| 239 |
*/ |
| 240 |
public function create_nonces( $events ) { |
| 241 |
$nonces = array(); |
| 242 |
foreach ( $events as $event ) { |
| 243 |
$nonces[ $event . '_nonce' ] = wp_create_nonce( $this->enqueue_prefix . '_' . $event ); |
| 244 |
} |
| 245 |
return $nonces; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Helper: enqueue JS (+ optional CSS) from a build directory using its asset manifest. |
| 250 |
* |
| 251 |
* @param string $name File/handle name. |
| 252 |
* @param string $dir Directory name under build path. |
| 253 |
* @param array $deps Additional dependencies. |
| 254 |
* @param array $localization_data Localization config. |
| 255 |
* |
| 256 |
* @since 0.0.1 |
| 257 |
* @return void |
| 258 |
*/ |
| 259 |
private function enqueue_files_with_deps( $name, $dir, $deps = array(), $localization_data = array() ) { |
| 260 |
if ( empty( $name ) || empty( $dir ) ) { |
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
$asset_path = $this->build_path . $dir . '/index.asset.php'; |
| 265 |
|
| 266 |
if ( file_exists( $asset_path ) ) { |
| 267 |
$info = include $asset_path; |
| 268 |
$all_deps = array_merge( $deps, $info['dependencies'] ); |
| 269 |
$version = $info['version']; |
| 270 |
|
| 271 |
$this->script_operations( |
| 272 |
$name, |
| 273 |
$this->build_url . $dir . '/index.js', |
| 274 |
$all_deps, |
| 275 |
$localization_data, |
| 276 |
$version |
| 277 |
); |
| 278 |
|
| 279 |
// Enqueue companion CSS if present. |
| 280 |
$style_path = $this->build_path . $dir . '/style.css'; |
| 281 |
if ( file_exists( $style_path ) ) { |
| 282 |
$this->style_operations( |
| 283 |
$name, |
| 284 |
$this->build_url . $dir . '/style.css', |
| 285 |
array(), |
| 286 |
$version |
| 287 |
); |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|