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