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2.0-rc.1 3.0.9 6.2.0-rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Internal / Admin / WCAdminAssets.php
woocommerce / src / Internal / Admin Last commit date
Agentic 4 months ago EmailImprovements 1 year ago EmailPreview 4 days ago Emails 1 year ago ImportExport 1 year ago Logging 4 days ago Marketing 2 years ago Notes 1 month ago Onboarding 5 months ago Orders 4 days ago ProductForm 2 years ago ProductReviews 1 month ago RemoteFreeExtensions 1 month ago Schedulers 4 days ago Settings 4 days ago Suggestions 1 month ago WCPayPromotion 11 months ago ActivityPanels.php 3 years ago Analytics.php 1 month ago CategoryLookup.php 4 years ago Coupons.php 1 year ago CouponsMovedTrait.php 5 months ago CustomerEffortScoreTracks.php 7 months ago Events.php 2 months ago FeaturePlugin.php 4 months ago Homescreen.php 2 months ago Loader.php 4 days ago Marketing.php 1 year ago Marketplace.php 5 months ago MobileAppBanner.php 4 years ago OrderMilestoneEasterEgg.php 1 month ago RemoteInboxNotifications.php 3 years ago Settings.php 4 days ago ShippingLabelBanner.php 1 year ago ShippingLabelBannerDisplayRules.php 1 year ago SiteHealth.php 4 days ago Survey.php 4 years ago SystemStatusReport.php 1 year ago TaxSettingsRecommendations.php 4 days ago Translations.php 1 year ago WCAdminAssets.php 4 days ago WCAdminSharedSettings.php 1 year ago WCAdminUser.php 9 months ago WcPayWelcomePage.php 1 year ago
WCAdminAssets.php
553 lines
1 <?php
2 /**
3 * Register the scripts, and styles used within WooCommerce Admin.
4 */
5
6 namespace Automattic\WooCommerce\Internal\Admin;
7
8 use _WP_Dependency;
9 use Automattic\WooCommerce\Admin\Features\Features;
10 use Automattic\WooCommerce\Admin\PageController;
11 use Automattic\WooCommerce\Internal\Admin\Loader;
12 use Automattic\WooCommerce\Internal\Admin\Settings\SettingsUIRequestContext;
13
14 /**
15 * WCAdminAssets Class.
16 */
17 class WCAdminAssets {
18
19 /**
20 * Class instance.
21 *
22 * @var WCAdminAssets instance
23 */
24 protected static $instance = null;
25
26 /**
27 * An array of dependencies that have been preloaded (to avoid duplicates).
28 *
29 * @var array
30 */
31 protected $preloaded_dependencies;
32
33
34 /**
35 * Get class instance.
36 */
37 public static function get_instance() {
38 if ( ! self::$instance ) {
39 self::$instance = new self();
40 }
41 return self::$instance;
42 }
43
44 /**
45 * Constructor.
46 * Hooks added here should be removed in `wc_admin_initialize` via the feature plugin.
47 */
48 public function __construct() {
49 Features::get_instance();
50 add_action( 'admin_enqueue_scripts', array( $this, 'register_scripts' ) );
51
52 add_action( 'admin_enqueue_scripts', array( $this, 'inject_wc_settings_dependencies' ), 14 );
53 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ), 15 );
54 }
55
56 /**
57 * Gets the path for the asset depending on file type.
58 *
59 * @param string $ext File extension.
60 * @return string Folder path of asset.
61 */
62 public static function get_path( $ext ) {
63 return ( $ext === 'css' ) ? WC_ADMIN_DIST_CSS_FOLDER : WC_ADMIN_DIST_JS_FOLDER;
64 }
65
66 /**
67 * Determines if a minified JS file should be served.
68 *
69 * @param boolean $script_debug Only serve unminified files if script debug is on.
70 * @return boolean If js asset should use minified version.
71 */
72 public static function should_use_minified_js_file( $script_debug ) {
73 // minified files are only shipped in non-core versions of wc-admin, return false if minified files are not available.
74 if ( ! Features::exists( 'minified-js' ) ) {
75 return false;
76 }
77
78 // Otherwise we will serve un-minified files if SCRIPT_DEBUG is on, or if anything truthy is passed in-lieu of SCRIPT_DEBUG.
79 return ! $script_debug;
80 }
81
82 /**
83 * Gets the URL to an asset file.
84 *
85 * @param string $file File name (without extension).
86 * @param string $ext File extension.
87 * @return string URL to asset.
88 */
89 public static function get_url( $file, $ext ) {
90 $suffix = '';
91
92 // Potentially enqueue minified JavaScript.
93 if ( $ext === 'js' ) {
94 $script_debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
95 $suffix = self::should_use_minified_js_file( $script_debug ) ? '.min' : '';
96 }
97
98 return plugins_url( self::get_path( $ext ) . $file . $suffix . '.' . $ext, WC_ADMIN_PLUGIN_FILE );
99 }
100
101 /**
102 * Gets the file modified time as a cache buster if we're in dev mode,
103 * or the asset version (file content hash) if exists, or the WooCommerce version.
104 *
105 * @param string $ext File extension.
106 * @param string|null $asset_version Optional. The version from the asset file.
107 * @return string The cache buster value to use for the given file.
108 */
109 public static function get_file_version( $ext, $asset_version = null ) {
110 if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
111 return filemtime( WC_ADMIN_ABSPATH . self::get_path( $ext ) );
112 }
113
114 if ( ! empty( $asset_version ) ) {
115 return $asset_version;
116 }
117
118 return WC_VERSION;
119 }
120
121 /**
122 * Gets a script asset registry filename. The asset registry lists dependencies for the given script.
123 *
124 * @param string $script_path_name Path to where the script asset registry is contained.
125 * @param string $file File name (without extension).
126 * @return string complete asset filename.
127 *
128 * @throws \Exception Throws an exception when a readable asset registry file cannot be found.
129 */
130 public static function get_script_asset_filename( $script_path_name, $file ) {
131 $minification_supported = Features::exists( 'minified-js' );
132 $script_min_filename = $file . '.min.asset.php';
133 $script_nonmin_filename = $file . '.asset.php';
134 $script_asset_path = WC_ADMIN_ABSPATH . WC_ADMIN_DIST_JS_FOLDER . $script_path_name . '/';
135
136 // Check minification is supported first, to avoid multiple is_readable checks when minification is
137 // not supported.
138 if ( $minification_supported && is_readable( $script_asset_path . $script_min_filename ) ) {
139 return $script_min_filename;
140 } elseif ( is_readable( $script_asset_path . $script_nonmin_filename ) ) {
141 return $script_nonmin_filename;
142 } else {
143 // could not find an asset file, throw an error.
144 throw new \Exception( 'Could not find asset registry for ' . $script_path_name );
145 }
146 }
147
148 /**
149 * Render a preload link tag for a dependency, optionally
150 * checked against a provided allowlist.
151 *
152 * See: https://macarthur.me/posts/preloading-javascript-in-wordpress
153 *
154 * @param WP_Dependency $dependency The WP_Dependency being preloaded.
155 * @param string $type Dependency type - 'script' or 'style'.
156 * @param array $allowlist Optional. List of allowed dependency handles.
157 */
158 private function maybe_output_preload_link_tag( $dependency, $type, $allowlist = array() ) {
159 if (
160 (
161 ! empty( $allowlist ) &&
162 ! in_array( $dependency->handle, $allowlist, true )
163 ) ||
164 ( ! empty( $this->preloaded_dependencies[ $type ] ) &&
165 in_array( $dependency->handle, $this->preloaded_dependencies[ $type ], true ) )
166 ) {
167 return;
168 }
169
170 $this->preloaded_dependencies[ $type ][] = $dependency->handle;
171
172 $source = $dependency->ver ? add_query_arg( 'ver', $dependency->ver, $dependency->src ) : $dependency->src;
173
174 echo '<link rel="preload" href="', esc_url( $source ), '" as="', esc_attr( $type ), '" />', "\n";
175 }
176
177 /**
178 * Output a preload link tag for dependencies (and their sub dependencies)
179 * with an optional allowlist.
180 *
181 * See: https://macarthur.me/posts/preloading-javascript-in-wordpress
182 *
183 * @param string $type Dependency type - 'script' or 'style'.
184 * @param array $allowlist Optional. List of allowed dependency handles.
185 */
186 private function output_header_preload_tags_for_type( $type, $allowlist = array() ) {
187 if ( $type === 'script' ) {
188 $dependencies_of_type = wp_scripts();
189 } elseif ( $type === 'style' ) {
190 $dependencies_of_type = wp_styles();
191 } else {
192 return;
193 }
194
195 foreach ( $dependencies_of_type->queue as $dependency_handle ) {
196 $dependency = $dependencies_of_type->query( $dependency_handle, 'registered' );
197
198 if ( $dependency === false ) {
199 continue;
200 }
201
202 // Preload the subdependencies first.
203 foreach ( $dependency->deps as $sub_dependency_handle ) {
204 $sub_dependency = $dependencies_of_type->query( $sub_dependency_handle, 'registered' );
205
206 if ( $sub_dependency ) {
207 $this->maybe_output_preload_link_tag( $sub_dependency, $type, $allowlist );
208 }
209 }
210
211 $this->maybe_output_preload_link_tag( $dependency, $type, $allowlist );
212 }
213 }
214
215 /**
216 * Output preload link tags for all enqueued stylesheets and scripts.
217 *
218 * See: https://macarthur.me/posts/preloading-javascript-in-wordpress
219 */
220 private function output_header_preload_tags() {
221 $wc_admin_scripts = array(
222 WC_ADMIN_APP,
223 'wc-components',
224 );
225
226 $wc_admin_styles = array(
227 WC_ADMIN_APP,
228 'wc-components',
229 'wc-material-icons',
230 );
231
232 // Preload styles.
233 $this->output_header_preload_tags_for_type( 'style', $wc_admin_styles );
234
235 // Preload scripts.
236 $this->output_header_preload_tags_for_type( 'script', $wc_admin_scripts );
237 }
238
239 /**
240 * Loads the required scripts on the correct pages.
241 */
242 public function enqueue_assets() {
243 if ( ! PageController::is_admin_or_embed_page() ) {
244 return;
245 }
246
247 wp_enqueue_script( WC_ADMIN_APP );
248 wp_enqueue_style( WC_ADMIN_APP );
249
250 wp_enqueue_style( 'wc-material-icons' );
251 wp_enqueue_style( 'wc-onboarding' );
252
253 if ( PageController::is_settings_page() ) {
254 $this->register_script( 'wp-admin-scripts', 'settings-embed', true, $this->get_settings_ui_script_dependencies() );
255 $this->register_style( 'settings-embed', 'style', array( 'wp-components' ) );
256 }
257
258 // Preload our assets.
259 $this->output_header_preload_tags();
260 }
261
262 /**
263 * Modify script dependencies based on various conditions to only load the necessary scripts.
264 *
265 * @param array $dependencies Array of script dependencies.
266 * @param string $script Script name.
267 * @return array Modified dependencies.
268 */
269 private function modify_script_dependencies( $dependencies, $script ) {
270 $dependencies = array_map(
271 static function ( $dependency ) {
272 return 'wp-route' === $dependency ? 'wp-router' : $dependency;
273 },
274 $dependencies
275 );
276
277 if ( WC_ADMIN_APP === $script ) {
278 // Remove wp-editor dependency if we're not on a customize store page since we don't use wp-editor in other pages.
279 $is_customize_store_page = (
280 PageController::is_admin_page() &&
281 isset( $_GET['path'] ) && // phpcs:ignore WordPress.Security.NonceVerification.Recommended
282 str_starts_with( wc_clean( wp_unslash( $_GET['path'] ) ), '/customize-store' ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
283 );
284 if ( ! $is_customize_store_page ) {
285 $dependencies = array_diff( $dependencies, array( 'wp-editor' ) );
286 }
287 }
288
289 return $dependencies;
290 }
291
292 /**
293 * Registers all the necessary scripts and styles to show the admin experience.
294 */
295 public function register_scripts() {
296 if ( ! function_exists( 'wp_set_script_translations' ) ) {
297 return;
298 }
299
300 // Register the JS scripts.
301 $scripts = array(
302 'wc-admin-layout',
303 'wc-explat',
304 'wc-experimental',
305 'wc-customer-effort-score',
306 // NOTE: This should be removed when Gutenberg is updated and the notices package is removed from WooCommerce Admin.
307 'wc-notices',
308 'wc-number',
309 'wc-tracks',
310 'wc-date',
311 'wc-components',
312 WC_ADMIN_APP,
313 'wc-csv',
314 'wc-store-data',
315 'wc-currency',
316 'wc-navigation',
317 'wc-experimental-products-app',
318 'wc-settings-ui',
319 'wc-remote-logging',
320 'wc-sanitize',
321 );
322
323 $scripts_map = array(
324 WC_ADMIN_APP => PageController::is_embed_page() ? 'embed' : 'app',
325 'wc-csv' => 'csv-export',
326 'wc-store-data' => 'data',
327 );
328
329 $translated_scripts = array(
330 'wc-currency',
331 'wc-date',
332 'wc-components',
333 'wc-customer-effort-score',
334 'wc-experimental-products-app',
335 'wc-experimental',
336 'wc-navigation',
337 'wc-settings-ui',
338 WC_ADMIN_APP,
339 );
340
341 foreach ( $scripts as $script ) {
342 $script_path_name = isset( $scripts_map[ $script ] ) ? $scripts_map[ $script ] : str_replace( 'wc-', '', $script );
343
344 try {
345 $script_assets_filename = self::get_script_asset_filename( $script_path_name, 'index' );
346 $script_assets = require WC_ADMIN_ABSPATH . WC_ADMIN_DIST_JS_FOLDER . $script_path_name . '/' . $script_assets_filename;
347 $script_version = self::get_file_version( 'js', $script_assets['version'] );
348
349 $script_dependencies = $this->modify_script_dependencies( $script_assets['dependencies'], $script, $script_path_name );
350
351 wp_register_script(
352 $script,
353 self::get_url( $script_path_name . '/index', 'js' ),
354 $script_dependencies,
355 $script_version,
356 true
357 );
358
359 if ( in_array( $script, $translated_scripts, true ) ) {
360 wp_set_script_translations( $script, 'woocommerce' );
361 }
362
363 if ( WC_ADMIN_APP === $script ) {
364 wp_localize_script(
365 WC_ADMIN_APP,
366 'wcAdminAssets',
367 array(
368 'path' => plugins_url( self::get_path( 'js' ), WC_ADMIN_PLUGIN_FILE ),
369 'version' => $script_version,
370 )
371 );
372 }
373 } catch ( \Exception $e ) {
374 // Avoid crashing WordPress if an asset file could not be loaded.
375 wc_caught_exception( $e, __CLASS__ . '::' . __FUNCTION__, $script_path_name );
376 }
377 }
378
379 // Register the CSS styles.
380 $styles = array(
381 array(
382 'handle' => 'wc-admin-layout',
383 ),
384 array(
385 'handle' => 'wc-components',
386 ),
387 array(
388 'handle' => 'wc-experimental-products-app',
389 ),
390 array(
391 'handle' => 'wc-customer-effort-score',
392 ),
393 array(
394 'handle' => 'wc-experimental',
395 ),
396 array(
397 'handle' => WC_ADMIN_APP,
398 'dependencies' => array( 'wc-components', 'wc-admin-layout', 'wc-customer-effort-score', 'wp-components', 'wc-experimental' ),
399 ),
400 array(
401 'handle' => 'wc-onboarding',
402 ),
403 );
404
405 $css_file_version = self::get_file_version( 'css' );
406 foreach ( $styles as $style ) {
407 $handle = $style['handle'];
408 $style_path_name = isset( $scripts_map[ $handle ] ) ? $scripts_map[ $handle ] : str_replace( 'wc-', '', $handle );
409
410 try {
411 $style_assets_filename = self::get_script_asset_filename( $style_path_name, 'style' );
412 $style_assets = require WC_ADMIN_ABSPATH . WC_ADMIN_DIST_JS_FOLDER . $style_path_name . '/' . $style_assets_filename;
413 $version = $style_assets['version'];
414 } catch ( \Throwable $e ) {
415 // Use the default version if the asset file could not be loaded.
416 $version = $css_file_version;
417 }
418
419 $dependencies = isset( $style['dependencies'] ) ? $style['dependencies'] : array();
420 wp_register_style(
421 $handle,
422 self::get_url( $style_path_name . '/style', 'css' ),
423 $dependencies,
424 self::get_file_version( 'css', $version ),
425 );
426 wp_style_add_data( $handle, 'rtl', 'replace' );
427 }
428 }
429
430 /**
431 * Get extension script handles that must load before the settings embed app mounts.
432 *
433 * @return array
434 */
435 private function get_settings_ui_script_dependencies(): array {
436 try {
437 if ( ! class_exists( SettingsUIRequestContext::class ) ) {
438 return array();
439 }
440
441 $context = SettingsUIRequestContext::get_current();
442 } catch ( \Throwable $e ) {
443 return array();
444 }
445
446 if ( ! $context ) {
447 return array();
448 }
449
450 $dependencies = array_merge(
451 array( 'wc-settings-ui' ),
452 $context->get_script_handles()
453 );
454
455 return array_values( array_unique( $dependencies ) );
456 }
457
458 /**
459 * Injects wp-shared-settings as a dependency if it's present.
460 */
461 public function inject_wc_settings_dependencies() {
462 $wp_scripts = wp_scripts();
463 if ( wp_script_is( 'wc-settings', 'registered' ) ) {
464 $handles_for_injection = array(
465 'wc-admin-layout',
466 'wc-csv',
467 'wc-currency',
468 'wc-customer-effort-score',
469 'wc-experimental-products-app',
470 'wc-navigation',
471 // NOTE: This should be removed when Gutenberg is updated and
472 // the notices package is removed from WooCommerce Admin.
473 'wc-notices',
474 'wc-number',
475 'wc-date',
476 'wc-components',
477 'wc-tracks',
478 );
479 foreach ( $handles_for_injection as $handle ) {
480 $script = $wp_scripts->query( $handle, 'registered' );
481 if ( $script instanceof _WP_Dependency ) {
482 $script->deps[] = 'wc-settings';
483 $wp_scripts->add_data( $handle, 'group', 1 );
484 }
485 }
486 foreach ( $wp_scripts->registered as $handle => $script ) {
487 // scripts that are loaded in the footer has extra->group = 1.
488 if ( array_intersect( $handles_for_injection, $script->deps ) && ! isset( $script->extra['group'] ) ) {
489 // Append the script to footer.
490 $wp_scripts->add_data( $handle, 'group', 1 );
491 // Show a warning.
492 $error_handle = 'wc-settings-dep-in-header';
493 $used_deps = implode( ', ', array_intersect( $handles_for_injection, $script->deps ) );
494 $error_message = "Scripts that have a dependency on [$used_deps] must be loaded in the footer, {$handle} was registered to load in the header, but has been switched to load in the footer instead. See https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/5059";
495 // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter,WordPress.WP.EnqueuedResourceParameters.MissingVersion
496 wp_register_script( $error_handle, '' );
497 wp_enqueue_script( $error_handle );
498 wp_add_inline_script(
499 $error_handle,
500 sprintf( 'console.warn( "%s" );', $error_message )
501 );
502
503 }
504 }
505 }
506 }
507
508 /**
509 * Loads a script
510 *
511 * @param string $script_path_name The script path name.
512 * @param string $script_name Filename of the script to load.
513 * @param bool $need_translation Whether the script need translations.
514 * @param array $dependencies Array of any extra dependencies. Note wc-admin and any application JS dependencies are automatically added by Dependency Extraction Webpack Plugin. Use this parameter to designate any extra dependencies.
515 */
516 public static function register_script( $script_path_name, $script_name, $need_translation = false, $dependencies = array() ) {
517 $script_assets_filename = self::get_script_asset_filename( $script_path_name, $script_name );
518 $script_assets = require WC_ADMIN_ABSPATH . WC_ADMIN_DIST_JS_FOLDER . $script_path_name . '/' . $script_assets_filename;
519
520 wp_enqueue_script(
521 'wc-admin-' . $script_name,
522 self::get_url( $script_path_name . '/' . $script_name, 'js' ),
523 array_merge( array( WC_ADMIN_APP ), $script_assets ['dependencies'], $dependencies ),
524 self::get_file_version( 'js', $script_assets['version'] ),
525 true
526 );
527 if ( $need_translation ) {
528 wp_set_script_translations( 'wc-admin-' . $script_name, 'woocommerce' );
529 }
530 }
531
532 /**
533 * Loads a style
534 *
535 * @param string $style_path_name The style path name.
536 * @param string $style_name Filename of the style to load.
537 * @param array $dependencies Array of any extra dependencies.
538 */
539 public static function register_style( $style_path_name, $style_name, $dependencies = array() ) {
540 $style_assets_filename = self::get_script_asset_filename( $style_path_name, $style_name );
541 $style_assets = require WC_ADMIN_ABSPATH . WC_ADMIN_DIST_CSS_FOLDER . $style_path_name . '/' . $style_assets_filename;
542
543 $handle = 'wc-admin-' . $style_name;
544 wp_enqueue_style(
545 $handle,
546 self::get_url( $style_path_name . '/' . $style_name, 'css' ),
547 $dependencies,
548 self::get_file_version( 'css', $style_assets['version'] ),
549 );
550 wp_style_add_data( $handle, 'rtl', 'replace' );
551 }
552 }
553