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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 / Blocks / DependencyDetection.php
woocommerce / src / Blocks Last commit date
AI 1 year ago AIContent 1 year ago Assets 2 months ago BlockTypes 1 week ago Domain 4 weeks ago Images 1 year ago Integrations 2 years ago Patterns 5 months ago Payments 4 months ago Registry 2 years ago SharedStores 4 weeks ago Shipping 6 months ago Templates 4 weeks ago Utils 4 weeks ago Assets.php 2 years ago AssetsController.php 4 months ago BlockPatterns.php 6 months ago BlockTemplatesController.php 6 months ago BlockTemplatesRegistry.php 9 months ago BlockTypesController.php 3 months ago DependencyDetection.php 4 months ago InboxNotifications.php 2 years ago Installer.php 4 weeks ago Library.php 2 years ago Options.php 2 years ago Package.php 1 year ago QueryFilters.php 6 months ago TemplateOptions.php 1 year ago
DependencyDetection.php
358 lines
1 <?php
2 declare( strict_types = 1 );
3
4 namespace Automattic\WooCommerce\Blocks;
5
6 use Automattic\WooCommerce\Internal\Utilities\BlocksUtil;
7
8 /**
9 * DependencyDetection class.
10 *
11 * Provides runtime detection of extensions that use Blocks related WooCommerce globals
12 * (window.wc.*) without properly declaring their PHP script dependencies.
13 *
14 * This runs by default to warn developers about missing dependencies.
15 *
16 * @since 10.5.0
17 * @internal
18 */
19 final class DependencyDetection {
20
21 /**
22 * WooCommerce blocks that use the tracked globals.
23 *
24 * Detection script only runs on pages containing these blocks.
25 *
26 * @var array<string>
27 */
28 private const TRACKED_BLOCKS = array(
29 'woocommerce/checkout',
30 'woocommerce/cart',
31 'woocommerce/mini-cart',
32 );
33
34 /**
35 * Maps window.wc.* property names to their required script handles.
36 *
37 * This is the source of truth for both PHP and JS dependency detection.
38 * Based on wcDepMap and wcHandleMap in client/blocks/bin/webpack-helpers.js.
39 *
40 * @var array<string, string>
41 */
42 private const WC_GLOBAL_EXPORTS = array(
43 'wcBlocksRegistry' => 'wc-blocks-registry',
44 'wcSettings' => 'wc-settings',
45 'wcBlocksData' => 'wc-blocks-data-store',
46 'data' => 'wc-store-data',
47 'wcBlocksSharedContext' => 'wc-blocks-shared-context',
48 'wcBlocksSharedHocs' => 'wc-blocks-shared-hocs',
49 'priceFormat' => 'wc-price-format',
50 'blocksCheckout' => 'wc-blocks-checkout',
51 'blocksCheckoutEvents' => 'wc-blocks-checkout-events',
52 'blocksComponents' => 'wc-blocks-components',
53 'wcTypes' => 'wc-types',
54 'sanitize' => 'wc-sanitize',
55 );
56
57 /**
58 * Whether the proxy script was output.
59 *
60 * Used to ensure we only output the registry if the proxy was set up.
61 *
62 * @var bool
63 */
64 private bool $proxy_output = false;
65
66 /**
67 * Constructor.
68 */
69 public function __construct() {
70 $this->init();
71 }
72
73 /**
74 * Initialize hooks.
75 *
76 * @since 10.5.0
77 */
78 public function init(): void {
79 // Only run when debugging is enabled.
80 if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) {
81 return;
82 }
83
84 // Output an early inline script to set up the Proxy before any other scripts run.
85 add_action( 'wp_head', array( $this, 'output_early_proxy_setup' ), 1 );
86 add_action( 'admin_head', array( $this, 'output_early_proxy_setup' ), 1 );
87
88 // Output registry late when all scripts (including IntegrationInterface) are registered.
89 add_action( 'wp_print_footer_scripts', array( $this, 'output_script_registry' ), 1 );
90 add_action( 'admin_print_footer_scripts', array( $this, 'output_script_registry' ), 1 );
91 }
92
93 /**
94 * Output early inline script to set up the Proxy on window.wc.
95 *
96 * This must run before any WooCommerce scripts to intercept access.
97 * The script is loaded from a separate file for better IDE support and testing,
98 * but output inline to ensure correct timing (before any enqueued scripts).
99 *
100 * @since 10.5.0
101 */
102 public function output_early_proxy_setup(): void {
103 // Only run on pages that have the tracked blocks.
104 if ( ! $this->page_has_tracked_blocks() ) {
105 return;
106 }
107
108 // Load from the production assets directory (built by webpack and copied during release build).
109 $script_path = __DIR__ . '/../../assets/client/blocks/dependency-detection.js';
110
111 if ( ! file_exists( $script_path ) ) {
112 return;
113 }
114
115 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local file read for inline script output.
116 $script_content = file_get_contents( $script_path );
117
118 if ( ! $script_content ) {
119 return;
120 }
121
122 // Inject the global-to-handle mapping from PHP (source of truth).
123 $mapping_json = \wp_json_encode( self::WC_GLOBAL_EXPORTS );
124 if ( false === $mapping_json ) {
125 return;
126 }
127 $script_content = str_replace(
128 '__WC_GLOBAL_EXPORTS_PLACEHOLDER__',
129 $mapping_json,
130 $script_content
131 );
132
133 // Inject the WooCommerce plugin URL for script origin detection.
134 // This accounts for custom plugin directories (WP_PLUGIN_DIR, WP_CONTENT_DIR).
135 $wc_plugin_url = \plugins_url( '/', WC_PLUGIN_FILE );
136 $script_content = str_replace(
137 '__WC_PLUGIN_URL_PLACEHOLDER__',
138 '"' . esc_js( $wc_plugin_url ) . '"',
139 $script_content
140 );
141
142 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Script content is from a trusted local file, JSON is safely encoded.
143 echo '<script id="wc-dependency-detection">' . $script_content . '</script>' . "\n";
144
145 $this->proxy_output = true;
146 }
147
148 /**
149 * Output the script registry JSON for dependency checking.
150 *
151 * This runs late (wp_print_footer_scripts) to ensure all scripts,
152 * including those registered via IntegrationInterface, are captured.
153 *
154 * @since 10.5.0
155 */
156 public function output_script_registry(): void {
157 // Only output registry if the proxy was set up earlier.
158 // This avoids the duplicate page_has_tracked_blocks() check and ensures
159 // we don't output a registry without a proxy to consume it.
160 if ( ! $this->proxy_output ) {
161 return;
162 }
163
164 // Build the script registry mapping URLs to handles and dependencies.
165 $script_registry = $this->build_script_registry();
166 $registry_json = \wp_json_encode( $script_registry );
167
168 if ( false === $registry_json ) {
169 return;
170 }
171
172 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- JSON is safely encoded by wp_json_encode.
173 echo '<script id="wc-dependency-detection-registry">if(typeof window.wc.wcUpdateDependencyRegistry==="function"){window.wc.wcUpdateDependencyRegistry(' . $registry_json . ');}</script>' . "\n";
174 }
175
176 /**
177 * Build a registry of all enqueued scripts with their URLs and dependencies.
178 *
179 * @return array<string, array{handle: string, deps: array<string>}>
180 */
181 private function build_script_registry(): array {
182 $wp_scripts = wp_scripts();
183 $registry = array();
184
185 foreach ( $wp_scripts->registered as $handle => $script ) {
186 // Skip scripts without a source URL.
187 if ( empty( $script->src ) ) {
188 continue;
189 }
190
191 // Get the full URL.
192 $src = $script->src;
193 if ( ! is_string( $src ) ) {
194 // Skip malformed src.
195 continue;
196 }
197 if ( ! preg_match( '|^(https?:)?//|', $src ) ) {
198 // Relative URL - make it absolute.
199 $src = $wp_scripts->base_url . $src;
200 }
201
202 // Skip WooCommerce's own scripts - we don't need to check those.
203 if ( $this->is_woocommerce_script( $src ) ) {
204 continue;
205 }
206
207 // Skip WordPress core scripts - they won't use wc.* globals.
208 if ( $this->is_wordpress_core_script( $src ) ) {
209 continue;
210 }
211
212 // Normalize the URL for consistent matching.
213 $src = $this->normalize_url( $src );
214
215 $registry[ $src ] = array(
216 'handle' => $handle,
217 'deps' => $this->get_all_dependencies( $script->deps ),
218 );
219 }
220
221 return $registry;
222 }
223
224 /**
225 * Check if a script URL belongs to WooCommerce core.
226 *
227 * Checks if the script is loaded from the WooCommerce core plugin directory,
228 * not from third-party extensions that may use similar handle naming.
229 *
230 * @param string $url Script URL.
231 * @return bool
232 */
233 private function is_woocommerce_script( string $url ): bool {
234 // Get the WooCommerce plugin URL (accounts for custom plugin directories).
235 $wc_plugin_url = \plugins_url( '/', WC_PLUGIN_FILE );
236
237 // Check if the URL starts with the WooCommerce plugin URL and is in a known subdirectory.
238 if ( strpos( $url, $wc_plugin_url ) !== 0 ) {
239 return false;
240 }
241
242 // Get the path after the WooCommerce plugin URL.
243 $relative_path = substr( $url, strlen( $wc_plugin_url ) );
244
245 // Check if it's in one of the known WooCommerce asset directories.
246 return (bool) preg_match( '#^(client|assets|build|vendor)/#', $relative_path );
247 }
248
249 /**
250 * Check if a script URL belongs to WordPress core.
251 *
252 * WordPress core scripts (wp-includes, wp-admin) won't use wc.* globals,
253 * so we can skip them to reduce registry size.
254 *
255 * @param string $url Script URL.
256 * @return bool
257 */
258 private function is_wordpress_core_script( string $url ): bool {
259 return (bool) preg_match( '#/(wp-includes|wp-admin)/#', $url );
260 }
261
262 /**
263 * Recursively get all dependencies including nested ones.
264 *
265 * @param array<string> $deps Direct dependencies.
266 * @return array<string> All dependencies (flattened).
267 */
268 private function get_all_dependencies( array $deps ): array {
269 $wp_scripts = wp_scripts();
270 $all_deps = array();
271 $deps_to_process = $deps;
272
273 while ( ! empty( $deps_to_process ) ) {
274 $handle = array_shift( $deps_to_process );
275
276 if ( in_array( $handle, $all_deps, true ) ) {
277 continue;
278 }
279
280 $all_deps[] = $handle;
281
282 // Add nested dependencies to process.
283 if ( isset( $wp_scripts->registered[ $handle ] ) ) {
284 foreach ( $wp_scripts->registered[ $handle ]->deps as $nested_dep ) {
285 if ( ! in_array( $nested_dep, $all_deps, true ) ) {
286 $deps_to_process[] = $nested_dep;
287 }
288 }
289 }
290 }
291
292 // Filter to only include WooCommerce handles we care about.
293 $wc_handles = array_values( self::WC_GLOBAL_EXPORTS );
294 return array_values(
295 array_filter(
296 $all_deps,
297 function ( $dep ) use ( $wc_handles ) {
298 return in_array( $dep, $wc_handles, true );
299 }
300 )
301 );
302 }
303
304 /**
305 * Check if the current page contains any of the tracked blocks.
306 * Checks post content, widget areas, and template parts (header) for blocks.
307 *
308 * @return bool True if page has tracked blocks.
309 */
310 private function page_has_tracked_blocks(): bool {
311 // Check post content for blocks.
312 foreach ( self::TRACKED_BLOCKS as $block_name ) {
313 if ( \has_block( $block_name ) ) {
314 return true;
315 }
316 }
317
318 // Check widget areas for mini-cart (classic themes).
319 $mini_cart_in_widgets = BlocksUtil::get_blocks_from_widget_area( 'woocommerce/mini-cart' );
320 if ( ! empty( $mini_cart_in_widgets ) ) {
321 return true;
322 }
323
324 // Check header template part for mini-cart (block themes).
325 try {
326 $mini_cart_in_header = BlocksUtil::get_block_from_template_part( 'woocommerce/mini-cart', 'header' );
327 if ( ! empty( $mini_cart_in_header ) ) {
328 return true;
329 }
330 } catch ( \Throwable $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
331 // Template part may not exist in all themes, silently continue.
332 }
333
334 return false;
335 }
336
337 /**
338 * Normalize a URL by removing query strings and hash fragments.
339 *
340 * This helps match URLs in stack traces which don't include query strings.
341 *
342 * @param string $url URL to normalize.
343 * @return string Normalized URL without query string or hash.
344 */
345 private function normalize_url( string $url ): string {
346 $scheme = wp_parse_url( $url, PHP_URL_SCHEME );
347 $host = wp_parse_url( $url, PHP_URL_HOST );
348 $path = wp_parse_url( $url, PHP_URL_PATH );
349
350 if ( $scheme && $host && $path ) {
351 $port = wp_parse_url( $url, PHP_URL_PORT );
352 return $scheme . '://' . $host . ( $port ? ':' . $port : '' ) . $path;
353 }
354
355 return $url;
356 }
357 }
358