PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.18
Booktics – Appointment Booking Calendar for Service Businesses v1.0.18
1.0.29 1.0.28 1.0.27 1.0.26 1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.2 1.0.3 All 31 releases
booktics / core / assets / base-assets.php

base-assets.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.18, at core/assets/base-assets.php

288 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Booktics\Assets;
3
4 if ( ! defined( 'ABSPATH' ) ) exit;
5
6 use Booktics\Contracts\Hookable_Service_Contract;
7
8 /**
9 * Main assets management class
10 */
11 abstract class Base_Assets implements Hookable_Service_Contract {
12 /**
13 * Get all scripts
14 *
15 * @return array list of script to register
16 */
17 abstract public function get_scripts(): array;
18
19 /**
20 * Get all styles
21 *
22 * @return array list of styles to regiser
23 */
24 abstract public function get_styles(): array;
25
26 /**
27 * Register scripts and styles
28 *
29 * @return void
30 */
31 public function register_styles_scripts() {
32 // Register i18n loader early so other scripts can depend on it.
33 $this->register_global_scripts();
34 $this->register_scripts();
35 $this->register_styles();
36 }
37
38 /**
39 * Register scripts
40 *
41 * @param array $scripts
42 *
43 * @return void
44 */
45 public function register_scripts() {
46 $scripts = $this->get_scripts();
47
48 foreach ( $scripts as $handle => $script ) {
49 $deps = isset( $script['deps'] ) ? $script['deps'] : [];
50 $in_footer = isset( $script['in_footer'] ) ? $script['in_footer'] : true;
51 $version = isset( $script['version'] ) ? $script['version'] : $this->get_version( $script['src'] );
52
53 $deps = $this->get_dependencies( $script['src'], $deps );
54
55 // Mirror wp-cafe: if script uses wp-i18n, ensure our i18n loader runs first.
56 if ( in_array( 'wp-i18n', $deps, true ) ) {
57 $deps[] = 'booktics-i18n-loader';
58 }
59
60 wp_register_script( $handle, $script['src'], $deps, $version, $in_footer );
61
62 // Enable JS translations for bundles that use wp.i18n.
63 if ( function_exists( 'wp_set_script_translations' ) ) {
64 // Don't force a path: translations are commonly in WP_LANG_DIR/plugins.
65 wp_set_script_translations( $handle, 'booktics' );
66 }
67
68 // Ensure preloaded locale data is actually registered in wp.i18n for
69 // pages that never trigger per-chunk JSON downloads.
70 if ( 'booktics-packages' === $handle ) {
71 wp_add_inline_script(
72 'booktics-packages',
73 '(function(){try{if(window.bookticsPreloadedLocaleData && window.wp && window.wp.i18n && window.wp.i18n.setLocaleData){window.wp.i18n.setLocaleData(window.bookticsPreloadedLocaleData,"booktics");}}catch(e){}})();',
74 'before'
75 );
76 }
77 }
78 }
79
80 /**
81 * Register global scripts.
82 *
83 * @return void
84 */
85 private function register_global_scripts() {
86 $scripts = [
87 'booktics-i18n-loader' => [
88 'src' => booktics()->assets_url . '/build/js/i18n-loader.js',
89 'deps' => [],
90 'in_footer' => true,
91 ],
92 ];
93
94 foreach ( $scripts as $handle => $script ) {
95 $deps = isset( $script['deps'] ) ? $script['deps'] : [];
96 $in_footer = isset( $script['in_footer'] ) ? $script['in_footer'] : true;
97 $version = isset( $script['version'] ) ? $script['version'] : $this->get_version( $script['src'] );
98
99 $deps = $this->get_dependencies( $script['src'], $deps );
100
101 wp_register_script( $handle, $script['src'], $deps, $version, $in_footer );
102 }
103 }
104
105 /**
106 * Enqueue i18n loader and set its state.
107 *
108 * @return void
109 */
110 protected function enqueue_i18n_loader() {
111 $data = [
112 'baseUrl' => false,
113 'locale' => determine_locale(),
114 'domainMap' => [],
115 'domainPaths' => [],
116 ];
117
118 $lang_dir = WP_LANG_DIR;
119 $content_dir = WP_CONTENT_DIR;
120 $abspath = ABSPATH;
121
122 if ( strpos( $lang_dir, $content_dir ) === 0 ) {
123 $data['baseUrl'] = content_url(
124 substr( trailingslashit( $lang_dir ), strlen( trailingslashit( $content_dir ) ) )
125 );
126 } elseif ( strpos( $lang_dir, $abspath ) === 0 ) {
127 $data['baseUrl'] = site_url(
128 substr( trailingslashit( $lang_dir ), strlen( untrailingslashit( $abspath ) ) )
129 );
130 }
131
132 wp_enqueue_script( 'booktics-i18n-loader' );
133
134 $data['domainMap'] = (object) $data['domainMap'];
135 $data['domainPaths'] = (object) $data['domainPaths'];
136
137 // If a consolidated .l10n.php file exists, preload all messages to avoid
138 // relying on per-chunk JSON requests (which may be missing).
139 $preload = null;
140 $l10n_file = trailingslashit( WP_LANG_DIR ) . 'plugins/booktics-' . $data['locale'] . '.l10n.php';
141 $l10n_file_alt = trailingslashit( WP_LANG_DIR ) . 'plugins/booktics-' . str_replace( '-', '_', $data['locale'] ) . '.l10n.php';
142 $l10n_file_legacy = trailingslashit( WP_LANG_DIR ) . 'plugins/booktics-' . str_replace( '_', '-', $data['locale'] ) . '.l10n.php';
143 $l10n_candidates = array_unique( array_filter( [ $l10n_file, $l10n_file_alt, $l10n_file_legacy ] ) );
144
145 foreach ( $l10n_candidates as $candidate ) {
146 if ( file_exists( $candidate ) ) {
147 $preload = include $candidate;
148 break;
149 }
150 }
151
152 $inline = '(function(){var k="bookticsI18nLoader"; if (typeof window[k] === "undefined") { window[k] = {}; } window[k].state = ' . wp_json_encode( $data, JSON_UNESCAPED_SLASHES ) . ';';
153
154 if ( is_array( $preload ) && ! empty( $preload['messages'] ) && is_array( $preload['messages'] ) ) {
155 $messages = [];
156 foreach ( $preload['messages'] as $msgid => $msgstr ) {
157 if ( is_string( $msgid ) && is_string( $msgstr ) ) {
158 $messages[ $msgid ] = [ $msgstr ];
159 }
160 }
161
162 $locale_data = [
163 '' => [
164 'domain' => 'booktics',
165 'lang' => $data['locale'],
166 'plural-forms' => isset( $preload['plural-forms'] ) ? $preload['plural-forms'] : 'nplurals=2; plural=n != 1;',
167 ],
168 ] + $messages;
169
170 $encoded_locale_data = wp_json_encode( $locale_data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
171 $inline .= ' window[k].preloadedLocaleData = ' . $encoded_locale_data . '; window.bookticsPreloadedLocaleData = ' . $encoded_locale_data . ';';
172 }
173
174 $inline .= '})();';
175
176 wp_add_inline_script(
177 'booktics-i18n-loader',
178 $inline,
179 'before'
180 );
181 }
182
183 /**
184 * Register styles
185 *
186 * @param array $styles
187 *
188 * @return void
189 */
190 public function register_styles() {
191 $styles = $this->get_styles();
192
193 foreach ( $styles as $handle => $style ) {
194 $deps = isset( $style['deps'] ) ? $style['deps'] : false;
195 $version = booktics()->version;
196
197 wp_register_style( $handle, $style['src'], $deps, $version );
198 }
199 }
200
201 /**
202 * Get script and style file dependencies
203 *
204 * @param string $file_name
205 * @param array $deps
206 *
207 * @return array
208 */
209 private function get_dependencies( $file_name, $deps = [] ) {
210 $assets = $this->get_file_assets( $file_name );
211
212 $assets_deps = ! empty( $assets['dependencies'] ) ? $assets['dependencies'] : [];
213
214 $merged_deps = array_merge( $assets_deps, $deps );
215
216 return $merged_deps;
217 }
218
219 /**
220 * Get file assets
221 *
222 * @param string $file_name
223 *
224 * @return array
225 */
226 private function get_file_assets( $file_url ) {
227 $file = $this->get_file_path( $file_url );
228 $assets = [];
229
230 if ( file_exists( $file ) ) {
231 $assets = include $file;
232 }
233
234 return $assets;
235 }
236
237 /**
238 * Get script file version
239 *
240 * @param string $file_name
241 *
242 * @return string
243 */
244 private function get_version( $file_name ) {
245 $assets = $this->get_file_assets( $file_name );
246 $assets_vers = ! empty( $assets['version'] ) ? $assets['version'] : booktics()->version;
247 return $assets_vers;
248 }
249
250 /**
251 * Get file path from url
252 *
253 * @param string $url
254 *
255 * @return string
256 */
257 private function get_file_path( $url ) {
258 // Check if the URL is valid
259 if ( ! filter_var( $url, FILTER_VALIDATE_URL ) ) {
260 return false;
261 }
262
263 // Parse the URL
264 $url_parts = wp_parse_url( $url );
265
266 // Check if the URL has a path component
267 if ( ! isset( $url_parts['path'] ) ) {
268 return false; // URL does not contain a path
269 }
270
271 $clean_path = str_replace( '.js', '.asset.php', $url_parts['path'] );
272
273 // Get the file path from the URL path.
274 if ( isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
275 $file_path = sanitize_text_field( wp_unslash( $_SERVER['DOCUMENT_ROOT'] ) ) . $clean_path;
276 } else {
277 $file_path = ''; // Or another appropriate fallback.
278 }
279
280 // Check if the file exists
281 if ( ! file_exists( $file_path ) ) {
282 return false; // File does not exist
283 }
284
285 return $file_path;
286 }
287 }
288