Automattic\WooCommerce\Blocks
DependencyDetection::is_woocommerce_script
Check if a script URL belongs to WooCommerce core.
Checks if the script is loaded from the WooCommerce core plugin directory, not from third-party extensions that may use similar handle naming.
Method of the class: DependencyDetection{}
No Hooks.
Returns
true|false.
Usage
// private - for code of main (parent) class only $result = $this->is_woocommerce_script( $url ): bool;
- $url(string) (required)
- Script URL.
DependencyDetection::is_woocommerce_script() DependencyDetection::is woocommerce script code WC 10.7.0
private function is_woocommerce_script( string $url ): bool {
// Get the WooCommerce plugin URL (accounts for custom plugin directories).
$wc_plugin_url = \plugins_url( '/', WC_PLUGIN_FILE );
// Check if the URL starts with the WooCommerce plugin URL and is in a known subdirectory.
if ( strpos( $url, $wc_plugin_url ) !== 0 ) {
return false;
}
// Get the path after the WooCommerce plugin URL.
$relative_path = substr( $url, strlen( $wc_plugin_url ) );
// Check if it's in one of the known WooCommerce asset directories.
return (bool) preg_match( '#^(client|assets|build|vendor)/#', $relative_path );
}