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.2.0 10.0.1 5.2.0-beta.1 10.0.2 5.2.0-rc.1 10.0.3 5.2.0-rc.2 10.0.4 5.2.1 10.0.5 5.2.2 10.0.6 5.2.3 10.1.0 5.2.4 10.1.0-rc.1 5.2.5 10.1.0-rc.2 5.3.0 10.1.0-rc.3 5.3.0-beta.1 10.1.0-rc.4 5.3.0-rc.1 10.1.1 5.3.0-rc.2 10.1.2 5.3.1 10.1.3 5.3.2 10.1.4 5.3.3 10.2.0 5.4.0 10.2.0-beta.1 5.4.0-beta.1 10.2.0-beta.2 5.4.0-rc.1 10.2.0-rc.1 5.4.1 10.2.1 5.4.2 10.2.2 5.4.3 10.2.3 5.4.4 10.2.4 5.4.5 10.3.0 5.5.0 10.3.0-beta.1 5.5.0-beta.1 10.3.0-beta.2 5.5.0-rc.1 10.3.0-rc.1 5.5.0-rc.2 10.3.0-rc.2 5.5.1 10.3.1 5.5.2 10.3.2 5.5.3 10.3.3 5.5.4 10.3.4 5.5.5 10.3.5 5.6.0 10.3.6 5.6.0-beta.1 10.3.7 5.6.0-rc.1 10.3.8 5.6.0-rc.2 10.4.0 5.6.1 10.4.0-beta.1 5.6.2 10.4.0-beta.2 5.6.3 10.4.0-rc.1 5.7.0 10.4.1 5.7.0-beta.1 10.4.2 5.7.0-rc.1 10.4.3 5.7.1 10.4.4 5.7.2 10.5.0 5.7.3 10.5.0-beta.1 5.8.0 10.5.0-beta.2 5.8.0-beta.1 10.5.0-rc.1 5.8.0-beta.2 10.5.0-rc.2 5.8.0-rc.1 10.5.0-rc.3 5.8.1 10.5.1 5.8.2 10.5.2 5.9.0 10.5.3 5.9.0-beta.1 10.6.0 5.9.0-rc.1 10.6.0-beta.1 5.9.0-rc.2 10.6.0-beta.2 5.9.1 10.6.0-rc.1 5.9.2 10.6.1 6.0.0 10.6.2 6.0.0-beta.1 10.7.0 6.0.0-rc.1 10.7.0-beta.1 6.0.1 10.7.0-beta.2 6.0.2 10.7.0-rc.1 6.1.0 3.0.0 6.1.0-beta.1 3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.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 6 months ago EmailImprovements 1 year ago EmailPreview 1 month ago Emails 2 weeks ago ImportExport 1 year ago Logging 2 weeks ago Marketing 2 years ago Notes 2 weeks ago Onboarding 2 weeks ago Orders 1 month ago ProductForm 2 years ago ProductReviews 2 months ago RemoteFreeExtensions 2 months ago Reports 2 weeks ago Schedulers 2 weeks ago Settings 2 weeks ago Suggestions 2 weeks ago WCPayPromotion 1 year ago ActivityPanels.php 4 years ago Analytics.php 2 weeks ago CategoryLookup.php 4 years ago Coupons.php 2 weeks ago CouponsMovedTrait.php 7 months ago CustomerEffortScoreTracks.php 9 months ago Events.php 2 weeks ago FeaturePlugin.php 2 weeks ago Homescreen.php 2 weeks ago Loader.php 1 month ago Marketing.php 1 year ago Marketplace.php 7 months ago MobileAppBanner.php 4 years ago OrderMilestoneEasterEgg.php 2 months ago RemoteInboxNotifications.php 2 weeks ago Settings.php 2 weeks ago ShippingLabelBanner.php 1 year ago ShippingLabelBannerDisplayRules.php 2 years ago SiteHealth.php 2 weeks ago Survey.php 4 years ago SystemStatusReport.php 2 weeks ago TaxSettingsRecommendations.php 1 month ago Translations.php 1 year ago WCAdminAssets.php 1 week ago WCAdminSharedSettings.php 1 year ago WCAdminUser.php 10 months ago WcPayWelcomePage.php 1 year ago
WCAdminAssets.php
549 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, but only if the minified file exists.
93 // Core builds do not ship minified JS files, so this also guards against the
94 // 'minified-js' feature being force-enabled (e.g. via WooCommerce Beta Tester).
95 if ( $ext === 'js' ) {
96 $script_debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
97 $suffix = self::should_use_minified_js_file( $script_debug ) && is_readable( WC_ADMIN_ABSPATH . self::get_path( $ext ) . $file . '.min.' . $ext ) ? '.min' : '';
98 }
99
100 return plugins_url( self::get_path( $ext ) . $file . $suffix . '.' . $ext, WC_ADMIN_PLUGIN_FILE );
101 }
102
103 /**
104 * Gets the file modified time as a cache buster if we're in dev mode,
105 * or the asset version (file content hash) if exists, or the WooCommerce version.
106 *
107 * @param string $ext File extension.
108 * @param string|null $asset_version Optional. The version from the asset file.
109 * @return string The cache buster value to use for the given file.
110 */
111 public static function get_file_version( $ext, $asset_version = null ) {
112 if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
113 return filemtime( WC_ADMIN_ABSPATH . self::get_path( $ext ) );
114 }
115
116 if ( ! empty( $asset_version ) ) {
117 return $asset_version;
118 }
119
120 return WC_VERSION;
121 }
122
123 /**
124 * Gets a script asset registry filename. The asset registry lists dependencies for the given script.
125 *
126 * @param string $script_path_name Path to where the script asset registry is contained.
127 * @param string $file File name (without extension).
128 * @return string complete asset filename.
129 *
130 * @throws \Exception Throws an exception when a readable asset registry file cannot be found.
131 */
132 public static function get_script_asset_filename( $script_path_name, $file ) {
133 $minification_supported = Features::exists( 'minified-js' );
134 $script_min_filename = $file . '.min.asset.php';
135 $script_nonmin_filename = $file . '.asset.php';
136 $script_asset_path = WC_ADMIN_ABSPATH . WC_ADMIN_DIST_JS_FOLDER . $script_path_name . '/';
137
138 // Check minification is supported first, to avoid multiple is_readable checks when minification is
139 // not supported.
140 if ( $minification_supported && is_readable( $script_asset_path . $script_min_filename ) ) {
141 return $script_min_filename;
142 } elseif ( is_readable( $script_asset_path . $script_nonmin_filename ) ) {
143 return $script_nonmin_filename;
144 } else {
145 // could not find an asset file, throw an error.
146 throw new \Exception( 'Could not find asset registry for ' . $script_path_name );
147 }
148 }
149
150 /**
151 * Render a preload link tag for a dependency, optionally
152 * checked against a provided allowlist.
153 *
154 * See: https://macarthur.me/posts/preloading-javascript-in-wordpress
155 *
156 * @param WP_Dependency $dependency The WP_Dependency being preloaded.
157 * @param string $type Dependency type - 'script' or 'style'.
158 * @param array $allowlist Optional. List of allowed dependency handles.
159 */
160 private function maybe_output_preload_link_tag( $dependency, $type, $allowlist = array() ) {
161 if (
162 (
163 ! empty( $allowlist ) &&
164 ! in_array( $dependency->handle, $allowlist, true )
165 ) ||
166 ( ! empty( $this->preloaded_dependencies[ $type ] ) &&
167 in_array( $dependency->handle, $this->preloaded_dependencies[ $type ], true ) )
168 ) {
169 return;
170 }
171
172 $this->preloaded_dependencies[ $type ][] = $dependency->handle;
173
174 $source = $dependency->ver ? add_query_arg( 'ver', $dependency->ver, $dependency->src ) : $dependency->src;
175
176 echo '<link rel="preload" href="', esc_url( $source ), '" as="', esc_attr( $type ), '" />', "\n";
177 }
178
179 /**
180 * Output a preload link tag for dependencies (and their sub dependencies)
181 * with an optional allowlist.
182 *
183 * See: https://macarthur.me/posts/preloading-javascript-in-wordpress
184 *
185 * @param string $type Dependency type - 'script' or 'style'.
186 * @param array $allowlist Optional. List of allowed dependency handles.
187 */
188 private function output_header_preload_tags_for_type( $type, $allowlist = array() ) {
189 if ( $type === 'script' ) {
190 $dependencies_of_type = wp_scripts();
191 } elseif ( $type === 'style' ) {
192 $dependencies_of_type = wp_styles();
193 } else {
194 return;
195 }
196
197 foreach ( $dependencies_of_type->queue as $dependency_handle ) {
198 $dependency = $dependencies_of_type->query( $dependency_handle, 'registered' );
199
200 if ( $dependency === false ) {
201 continue;
202 }
203
204 // Preload the subdependencies first.
205 foreach ( $dependency->deps as $sub_dependency_handle ) {
206 $sub_dependency = $dependencies_of_type->query( $sub_dependency_handle, 'registered' );
207
208 if ( $sub_dependency ) {
209 $this->maybe_output_preload_link_tag( $sub_dependency, $type, $allowlist );
210 }
211 }
212
213 $this->maybe_output_preload_link_tag( $dependency, $type, $allowlist );
214 }
215 }
216
217 /**
218 * Output preload link tags for all enqueued stylesheets and scripts.
219 *
220 * See: https://macarthur.me/posts/preloading-javascript-in-wordpress
221 */
222 private function output_header_preload_tags() {
223 $wc_admin_scripts = array(
224 WC_ADMIN_APP,
225 'wc-components',
226 );
227
228 $wc_admin_styles = array(
229 WC_ADMIN_APP,
230 'wc-components',
231 'wc-material-icons',
232 );
233
234 // Preload styles.
235 $this->output_header_preload_tags_for_type( 'style', $wc_admin_styles );
236
237 // Preload scripts.
238 $this->output_header_preload_tags_for_type( 'script', $wc_admin_scripts );
239 }
240
241 /**
242 * Loads the required scripts on the correct pages.
243 */
244 public function enqueue_assets() {
245 if ( ! PageController::is_admin_or_embed_page() ) {
246 return;
247 }
248
249 wp_enqueue_script( WC_ADMIN_APP );
250 wp_enqueue_style( WC_ADMIN_APP );
251
252 wp_enqueue_style( 'wc-material-icons' );
253 wp_enqueue_style( 'wc-onboarding' );
254
255 if ( PageController::is_settings_page() ) {
256 $this->register_script( 'wp-admin-scripts', 'settings-embed', true, $this->get_settings_ui_script_dependencies() );
257 $this->register_style( 'settings-embed', 'style', array( 'wp-components' ) );
258 }
259
260 // Preload our assets.
261 $this->output_header_preload_tags();
262 }
263
264 /**
265 * Modify script dependencies based on various conditions to only load the necessary scripts.
266 *
267 * @param array $dependencies Array of script dependencies.
268 * @param string $script Script name.
269 * @return array Modified dependencies.
270 */
271 private function modify_script_dependencies( $dependencies, $script ) {
272 $dependencies = array_map(
273 static function ( $dependency ) {
274 return 'wp-route' === $dependency ? 'wp-router' : $dependency;
275 },
276 $dependencies
277 );
278
279 if ( WC_ADMIN_APP === $script ) {
280 // Remove wp-editor dependency if we're not on a customize store page since we don't use wp-editor in other pages.
281 $is_customize_store_page = (
282 PageController::is_admin_page() &&
283 isset( $_GET['path'] ) && // phpcs:ignore WordPress.Security.NonceVerification.Recommended
284 str_starts_with( wc_clean( wp_unslash( $_GET['path'] ) ), '/customize-store' ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
285 );
286 if ( ! $is_customize_store_page ) {
287 $dependencies = array_diff( $dependencies, array( 'wp-editor' ) );
288 }
289 }
290
291 return $dependencies;
292 }
293
294 /**
295 * Registers all the necessary scripts and styles to show the admin experience.
296 */
297 public function register_scripts() {
298 if ( ! function_exists( 'wp_set_script_translations' ) ) {
299 return;
300 }
301
302 // Register the JS scripts.
303 $scripts = array(
304 'wc-admin-layout',
305 'wc-explat',
306 'wc-experimental',
307 'wc-customer-effort-score',
308 // NOTE: This should be removed when Gutenberg is updated and the notices package is removed from WooCommerce Admin.
309 'wc-notices',
310 'wc-number',
311 'wc-tracks',
312 'wc-date',
313 'wc-components',
314 WC_ADMIN_APP,
315 'wc-csv',
316 'wc-store-data',
317 'wc-currency',
318 'wc-navigation',
319 'wc-settings-ui',
320 'wc-remote-logging',
321 'wc-sanitize',
322 );
323
324 $scripts_map = array(
325 WC_ADMIN_APP => PageController::is_embed_page() ? 'embed' : 'app',
326 'wc-csv' => 'csv-export',
327 'wc-store-data' => 'data',
328 );
329
330 $translated_scripts = array(
331 'wc-currency',
332 'wc-date',
333 'wc-components',
334 'wc-customer-effort-score',
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-customer-effort-score',
389 ),
390 array(
391 'handle' => 'wc-experimental',
392 ),
393 array(
394 'handle' => WC_ADMIN_APP,
395 'dependencies' => array( 'wc-components', 'wc-admin-layout', 'wc-customer-effort-score', 'wp-components', 'wc-experimental' ),
396 ),
397 array(
398 'handle' => 'wc-onboarding',
399 ),
400 );
401
402 $css_file_version = self::get_file_version( 'css' );
403 foreach ( $styles as $style ) {
404 $handle = $style['handle'];
405 $style_path_name = isset( $scripts_map[ $handle ] ) ? $scripts_map[ $handle ] : str_replace( 'wc-', '', $handle );
406
407 try {
408 $style_assets_filename = self::get_script_asset_filename( $style_path_name, 'style' );
409 $style_assets = require WC_ADMIN_ABSPATH . WC_ADMIN_DIST_JS_FOLDER . $style_path_name . '/' . $style_assets_filename;
410 $version = $style_assets['version'];
411 } catch ( \Throwable $e ) {
412 // Use the default version if the asset file could not be loaded.
413 $version = $css_file_version;
414 }
415
416 $dependencies = isset( $style['dependencies'] ) ? $style['dependencies'] : array();
417 wp_register_style(
418 $handle,
419 self::get_url( $style_path_name . '/style', 'css' ),
420 $dependencies,
421 self::get_file_version( 'css', $version ),
422 );
423 wp_style_add_data( $handle, 'rtl', 'replace' );
424 }
425 }
426
427 /**
428 * Get extension script handles that must load before the settings embed app mounts.
429 *
430 * @return array
431 */
432 private function get_settings_ui_script_dependencies(): array {
433 try {
434 if ( ! class_exists( SettingsUIRequestContext::class ) ) {
435 return array();
436 }
437
438 $context = SettingsUIRequestContext::get_current();
439 } catch ( \Throwable $e ) {
440 return array();
441 }
442
443 if ( ! $context ) {
444 return array();
445 }
446
447 $dependencies = array_merge(
448 array( 'wc-settings-ui' ),
449 $context->get_script_handles()
450 );
451
452 return array_values( array_unique( $dependencies ) );
453 }
454
455 /**
456 * Injects wp-shared-settings as a dependency if it's present.
457 */
458 public function inject_wc_settings_dependencies() {
459 $wp_scripts = wp_scripts();
460 if ( wp_script_is( 'wc-settings', 'registered' ) ) {
461 $handles_for_injection = array(
462 'wc-admin-layout',
463 'wc-csv',
464 'wc-currency',
465 'wc-customer-effort-score',
466 'wc-navigation',
467 // NOTE: This should be removed when Gutenberg is updated and
468 // the notices package is removed from WooCommerce Admin.
469 'wc-notices',
470 'wc-number',
471 'wc-date',
472 'wc-components',
473 'wc-tracks',
474 );
475 foreach ( $handles_for_injection as $handle ) {
476 $script = $wp_scripts->query( $handle, 'registered' );
477 if ( $script instanceof _WP_Dependency ) {
478 $script->deps[] = 'wc-settings';
479 $wp_scripts->add_data( $handle, 'group', 1 );
480 }
481 }
482 foreach ( $wp_scripts->registered as $handle => $script ) {
483 // scripts that are loaded in the footer has extra->group = 1.
484 if ( array_intersect( $handles_for_injection, $script->deps ) && ! isset( $script->extra['group'] ) ) {
485 // Append the script to footer.
486 $wp_scripts->add_data( $handle, 'group', 1 );
487 // Show a warning.
488 $error_handle = 'wc-settings-dep-in-header';
489 $used_deps = implode( ', ', array_intersect( $handles_for_injection, $script->deps ) );
490 $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";
491 // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter,WordPress.WP.EnqueuedResourceParameters.MissingVersion
492 wp_register_script( $error_handle, '' );
493 wp_enqueue_script( $error_handle );
494 wp_add_inline_script(
495 $error_handle,
496 sprintf( 'console.warn( "%s" );', $error_message )
497 );
498
499 }
500 }
501 }
502 }
503
504 /**
505 * Loads a script
506 *
507 * @param string $script_path_name The script path name.
508 * @param string $script_name Filename of the script to load.
509 * @param bool $need_translation Whether the script need translations.
510 * @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.
511 */
512 public static function register_script( $script_path_name, $script_name, $need_translation = false, $dependencies = array() ) {
513 $script_assets_filename = self::get_script_asset_filename( $script_path_name, $script_name );
514 $script_assets = require WC_ADMIN_ABSPATH . WC_ADMIN_DIST_JS_FOLDER . $script_path_name . '/' . $script_assets_filename;
515
516 wp_enqueue_script(
517 'wc-admin-' . $script_name,
518 self::get_url( $script_path_name . '/' . $script_name, 'js' ),
519 array_merge( array( WC_ADMIN_APP ), $script_assets ['dependencies'], $dependencies ),
520 self::get_file_version( 'js', $script_assets['version'] ),
521 true
522 );
523 if ( $need_translation ) {
524 wp_set_script_translations( 'wc-admin-' . $script_name, 'woocommerce' );
525 }
526 }
527
528 /**
529 * Loads a style
530 *
531 * @param string $style_path_name The style path name.
532 * @param string $style_name Filename of the style to load.
533 * @param array $dependencies Array of any extra dependencies.
534 */
535 public static function register_style( $style_path_name, $style_name, $dependencies = array() ) {
536 $style_assets_filename = self::get_script_asset_filename( $style_path_name, $style_name );
537 $style_assets = require WC_ADMIN_ABSPATH . WC_ADMIN_DIST_CSS_FOLDER . $style_path_name . '/' . $style_assets_filename;
538
539 $handle = 'wc-admin-' . $style_name;
540 wp_enqueue_style(
541 $handle,
542 self::get_url( $style_path_name . '/' . $style_name, 'css' ),
543 $dependencies,
544 self::get_file_version( 'css', $style_assets['version'] ),
545 );
546 wp_style_add_data( $handle, 'rtl', 'replace' );
547 }
548 }
549