PluginProbe
ZIP AI – AI Website Builder & AI Agent (Beta) / 0.0.5
ZIP AI – AI Website Builder & AI Agent (Beta) v0.0.5
0.0.10 0.0.9 trunk 0.0.4 0.0.5 0.0.6 0.0.7 0.0.8
zip-ai / classes / traits / enqueue.php

enqueue.php in ZIP AI – AI Website Builder & AI Agent (Beta) 0.0.5, at classes/traits/enqueue.php

213 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 1.0.0
10 */
11
12 namespace ZipAI\MCP\Classes\Traits;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 /**
19 * Trait Enqueue.
20 *
21 * @since 1.0.0
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 = ZIPAI_MCP_DIR . 'assets/';
38
39 /**
40 * Build URL.
41 *
42 * @var string
43 */
44 public $build_url = ZIPAI_MCP_URL . 'assets/';
45
46 /**
47 * Language directory for translations.
48 *
49 * @var string
50 */
51 public $language_dir = ZIPAI_MCP_DIR . 'languages';
52
53 /**
54 * Register frontend enqueue hook.
55 *
56 * @since 1.0.0
57 * @return void
58 */
59 public function enqueue_scripts() {
60 add_action( 'wp_enqueue_scripts', [ $this, 'wp_enqueue_scripts' ] );
61 }
62
63 /**
64 * Register admin enqueue hook.
65 *
66 * @since 1.0.0
67 * @return void
68 */
69 public function enqueue_scripts_admin() {
70 add_action( 'admin_enqueue_scripts', [ $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 1.0.0
83 * @return void
84 */
85 public function script_operations( $hook, $path, $dependency, $localization_array = [], $version = ZIPAI_MCP_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 1.0.0
111 * @return void
112 */
113 public function style_operations( $hook, $path, $dependency = [], $version = ZIPAI_MCP_VERSION ) {
114 $this->register_style( $hook, $path, $dependency, $version );
115 $this->enqueue_style( $hook );
116 }
117
118 /**
119 * Register a script with prefixed handle.
120 *
121 * @param string $hook Handle suffix.
122 * @param string $path Full URL to the script.
123 * @param array $dependency Script dependencies.
124 * @param string $version Asset version.
125 *
126 * @since 1.0.0
127 * @return void
128 */
129 public function register_script( $hook, $path, $dependency, $version = ZIPAI_MCP_VERSION ) {
130 // Cache-bust hand-enqueued source scripts on every edit: when the caller
131 // uses the default plugin version, derive a filemtime version from the
132 // on-disk file. In dev this busts the moment a handler changes; in
133 // production it is the build time (stable per deploy). Falls back to the
134 // constant for false/remote/missing sources.
135 if ( ZIPAI_MCP_VERSION === $version && is_string( $path ) && '' !== $path ) {
136 $local = str_replace( ZIPAI_MCP_URL, ZIPAI_MCP_DIR, $path );
137 if ( $local !== $path && file_exists( $local ) ) {
138 $version = (string) filemtime( $local );
139 }
140 }
141 wp_register_script(
142 $this->enqueue_prefix . '-' . $hook,
143 $path,
144 $dependency,
145 $version,
146 true
147 );
148 }
149
150 /**
151 * Enqueue a previously registered script.
152 *
153 * @param string $hook Handle suffix.
154 *
155 * @since 1.0.0
156 * @return void
157 */
158 public function enqueue_script( $hook ) {
159 wp_enqueue_script( $this->enqueue_prefix . '-' . $hook );
160 }
161
162 /**
163 * Localize a script with prefixed handle and object name.
164 *
165 * @param string $hook Handle suffix.
166 * @param string $object_name JS global object name.
167 * @param array $data Key-value data to expose.
168 *
169 * @since 1.0.0
170 * @return void
171 */
172 public function localize_script( $hook, $object_name, $data ) {
173 wp_localize_script(
174 $this->enqueue_prefix . '-' . $hook,
175 $object_name,
176 $data
177 );
178 }
179
180 /**
181 * Register a style with prefixed handle.
182 *
183 * @param string $hook Handle suffix.
184 * @param string $path Full URL to the CSS file.
185 * @param array $dependency Style dependencies.
186 * @param string $version Asset version.
187 *
188 * @since 1.0.0
189 * @return void
190 */
191 public function register_style( $hook, $path, $dependency = [], $version = ZIPAI_MCP_VERSION ) {
192 wp_register_style(
193 $this->enqueue_prefix . '-' . $hook,
194 $path,
195 $dependency,
196 $version
197 );
198 }
199
200 /**
201 * Enqueue a previously registered style.
202 *
203 * @param string $hook Handle suffix.
204 *
205 * @since 1.0.0
206 * @return void
207 */
208 public function enqueue_style( $hook ) {
209 wp_enqueue_style( $this->enqueue_prefix . '-' . $hook );
210 }
211
212 }
213