Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Blocks
File: AssetsController.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\Jetpack\Constants;
[5] Fix | Delete
use Automattic\WooCommerce\Blocks\Assets\Api as AssetApi;
[6] Fix | Delete
use Automattic\WooCommerce\Admin\Features\Features;
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* AssetsController class.
[10] Fix | Delete
*
[11] Fix | Delete
* @since 5.0.0
[12] Fix | Delete
* @internal
[13] Fix | Delete
*/
[14] Fix | Delete
final class AssetsController {
[15] Fix | Delete
[16] Fix | Delete
/**
[17] Fix | Delete
* Asset API interface for various asset registration.
[18] Fix | Delete
*
[19] Fix | Delete
* @var AssetApi
[20] Fix | Delete
*/
[21] Fix | Delete
private $api;
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Constructor.
[25] Fix | Delete
*
[26] Fix | Delete
* @param AssetApi $asset_api Asset API interface for various asset registration.
[27] Fix | Delete
*/
[28] Fix | Delete
public function __construct( AssetApi $asset_api ) {
[29] Fix | Delete
$this->api = $asset_api;
[30] Fix | Delete
$this->init();
[31] Fix | Delete
}
[32] Fix | Delete
[33] Fix | Delete
/**
[34] Fix | Delete
* Initialize class features.
[35] Fix | Delete
*/
[36] Fix | Delete
protected function init() { // phpcs:ignore WooCommerce.Functions.InternalInjectionMethod.MissingPublic
[37] Fix | Delete
add_action( 'init', array( $this, 'register_assets' ) );
[38] Fix | Delete
add_action( 'init', array( $this, 'register_script_modules' ) );
[39] Fix | Delete
add_action( 'enqueue_block_editor_assets', array( $this, 'register_and_enqueue_site_editor_assets' ) );
[40] Fix | Delete
add_filter( 'wp_resource_hints', array( $this, 'add_resource_hints' ), 10, 2 );
[41] Fix | Delete
add_action( 'body_class', array( $this, 'add_theme_body_class' ), 1 );
[42] Fix | Delete
add_action( 'admin_body_class', array( $this, 'add_theme_body_class' ), 1 );
[43] Fix | Delete
add_action( 'admin_enqueue_scripts', array( $this, 'update_block_style_dependencies' ), 20 );
[44] Fix | Delete
add_action( 'wp_enqueue_scripts', array( $this, 'update_block_settings_dependencies' ), 100 );
[45] Fix | Delete
add_action( 'admin_enqueue_scripts', array( $this, 'update_block_settings_dependencies' ), 100 );
[46] Fix | Delete
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_wc_entities' ), 100 );
[47] Fix | Delete
add_filter( 'js_do_concat', array( $this, 'skip_boost_minification_for_cart_checkout' ), 10, 2 );
[48] Fix | Delete
[49] Fix | Delete
if ( Features::is_enabled( 'experimental-iapi-runtime' ) ) {
[50] Fix | Delete
// Run after the WordPress iAPI runtime has been registered by setting a lower priority.
[51] Fix | Delete
add_filter( 'wp_default_scripts', array( $this, 'reregister_core_iapi_runtime' ), 20 );
[52] Fix | Delete
}
[53] Fix | Delete
}
[54] Fix | Delete
[55] Fix | Delete
/**
[56] Fix | Delete
* Re-registers the iAPI runtime registered by WordPress Core/Gutenberg, allowing WooCommerce to register its own version of the iAPI runtime.
[57] Fix | Delete
*/
[58] Fix | Delete
public function reregister_core_iapi_runtime() {
[59] Fix | Delete
$interactivity_api_asset_data = $this->api->get_asset_data(
[60] Fix | Delete
$this->api->get_block_asset_build_path( 'interactivity-api-assets', 'php' )
[61] Fix | Delete
);
[62] Fix | Delete
[63] Fix | Delete
foreach ( $interactivity_api_asset_data as $handle => $data ) {
[64] Fix | Delete
$handle_without_js = str_replace( '.js', '', $handle );
[65] Fix | Delete
if ( '@wordpress/interactivity' === $handle_without_js || '@wordpress/interactivity-router' === $handle_without_js ) {
[66] Fix | Delete
wp_deregister_script_module( $handle_without_js );
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
wp_register_script_module( $handle_without_js, plugins_url( $this->api->get_block_asset_build_path( $handle_without_js ), dirname( __DIR__ ) ), $data['dependencies'], $data['version'] );
[70] Fix | Delete
}
[71] Fix | Delete
}
[72] Fix | Delete
[73] Fix | Delete
/**
[74] Fix | Delete
* Register script modules.
[75] Fix | Delete
*/
[76] Fix | Delete
public function register_script_modules() {
[77] Fix | Delete
// Right now we only have one script modules build for supported interactivity API powered block front-ends.
[78] Fix | Delete
// We generate a combined asset file for that via DependencyExtractionWebpackPlugin to make registration more
[79] Fix | Delete
// efficient.
[80] Fix | Delete
$asset_data = $this->api->get_asset_data(
[81] Fix | Delete
$this->api->get_block_asset_build_path( 'interactivity-blocks-frontend-assets', 'php' )
[82] Fix | Delete
);
[83] Fix | Delete
[84] Fix | Delete
foreach ( $asset_data as $handle => $data ) {
[85] Fix | Delete
$handle_without_js = str_replace( '.js', '', $handle );
[86] Fix | Delete
wp_register_script_module( $handle_without_js, plugins_url( $this->api->get_block_asset_build_path( $handle_without_js ), dirname( __DIR__ ) ), $data['dependencies'], $data['version'] );
[87] Fix | Delete
}
[88] Fix | Delete
}
[89] Fix | Delete
[90] Fix | Delete
/**
[91] Fix | Delete
* Register block scripts & styles.
[92] Fix | Delete
*/
[93] Fix | Delete
public function register_assets() {
[94] Fix | Delete
$this->register_style( 'wc-blocks-packages-style', plugins_url( $this->api->get_block_asset_build_path( 'packages-style', 'css' ), dirname( __DIR__ ) ), array(), 'all', true );
[95] Fix | Delete
$this->register_style( 'wc-blocks-style', plugins_url( $this->api->get_block_asset_build_path( 'wc-blocks', 'css' ), dirname( __DIR__ ) ), array(), 'all', true );
[96] Fix | Delete
$this->register_style( 'wc-blocks-editor-style', plugins_url( $this->api->get_block_asset_build_path( 'wc-blocks-editor-style', 'css' ), dirname( __DIR__ ) ), array( 'wp-edit-blocks' ), 'all', true );
[97] Fix | Delete
[98] Fix | Delete
$this->api->register_script( 'wc-types', $this->api->get_block_asset_build_path( 'wc-types' ), array(), false );
[99] Fix | Delete
$this->api->register_script( 'wc-entities', 'assets/client/blocks/wc-entities.js', array(), false );
[100] Fix | Delete
$this->api->register_script( 'wc-blocks-middleware', 'assets/client/blocks/wc-blocks-middleware.js', array(), false );
[101] Fix | Delete
$this->api->register_script( 'wc-blocks-data-store', 'assets/client/blocks/wc-blocks-data.js', array( 'wc-blocks-middleware' ) );
[102] Fix | Delete
$this->api->register_script( 'wc-blocks-vendors', $this->api->get_block_asset_build_path( 'wc-blocks-vendors' ), array(), false );
[103] Fix | Delete
$this->api->register_script( 'wc-blocks-registry', 'assets/client/blocks/wc-blocks-registry.js', array(), false );
[104] Fix | Delete
$this->api->register_script( 'wc-blocks', $this->api->get_block_asset_build_path( 'wc-blocks' ), array( 'wc-blocks-vendors' ), false );
[105] Fix | Delete
$this->api->register_script( 'wc-blocks-shared-context', 'assets/client/blocks/wc-blocks-shared-context.js' );
[106] Fix | Delete
$this->api->register_script( 'wc-blocks-shared-hocs', 'assets/client/blocks/wc-blocks-shared-hocs.js', array(), false );
[107] Fix | Delete
[108] Fix | Delete
// The price package is shared externally so has no blocks prefix.
[109] Fix | Delete
$this->api->register_script( 'wc-price-format', 'assets/client/blocks/price-format.js', array(), false );
[110] Fix | Delete
[111] Fix | Delete
// Vendor scripts for blocks frontends (not including cart and checkout).
[112] Fix | Delete
$this->api->register_script( 'wc-blocks-frontend-vendors', $this->api->get_block_asset_build_path( 'wc-blocks-frontend-vendors-frontend' ), array(), true );
[113] Fix | Delete
[114] Fix | Delete
// Cart and checkout frontend scripts.
[115] Fix | Delete
$this->api->register_script( 'wc-cart-checkout-vendors', $this->api->get_block_asset_build_path( 'wc-cart-checkout-vendors-frontend' ), array(), true );
[116] Fix | Delete
$this->api->register_script( 'wc-cart-checkout-base', $this->api->get_block_asset_build_path( 'wc-cart-checkout-base-frontend' ), array(), true );
[117] Fix | Delete
$this->api->register_script( 'wc-blocks-checkout', 'assets/client/blocks/blocks-checkout.js' );
[118] Fix | Delete
$this->api->register_script( 'wc-blocks-checkout-events', 'assets/client/blocks/blocks-checkout-events.js' );
[119] Fix | Delete
$this->api->register_script( 'wc-blocks-components', 'assets/client/blocks/blocks-components.js' );
[120] Fix | Delete
$this->api->register_script( 'wc-schema-parser', 'assets/client/blocks/wc-schema-parser.js', array(), false );
[121] Fix | Delete
[122] Fix | Delete
// Sanitize.
[123] Fix | Delete
$this->api->register_script(
[124] Fix | Delete
'wc-sanitize',
[125] Fix | Delete
'assets/client/admin/sanitize/index.js',
[126] Fix | Delete
array()
[127] Fix | Delete
);
[128] Fix | Delete
[129] Fix | Delete
// Customer Effort Score.
[130] Fix | Delete
$this->api->register_script(
[131] Fix | Delete
'wc-customer-effort-score',
[132] Fix | Delete
'assets/client/admin/customer-effort-score/index.js',
[133] Fix | Delete
array( 'wp-data', 'wp-data-controls', 'wc-store-data' )
[134] Fix | Delete
);
[135] Fix | Delete
$this->api->register_style(
[136] Fix | Delete
'wc-customer-effort-score',
[137] Fix | Delete
'assets/client/admin/customer-effort-score/style.css',
[138] Fix | Delete
);
[139] Fix | Delete
[140] Fix | Delete
wp_add_inline_script(
[141] Fix | Delete
'wc-blocks-middleware',
[142] Fix | Delete
"
[143] Fix | Delete
var wcBlocksMiddlewareConfig = {
[144] Fix | Delete
storeApiNonce: '" . esc_js( wp_create_nonce( 'wc_store_api' ) ) . "',
[145] Fix | Delete
wcStoreApiNonceTimestamp: '" . esc_js( time() ) . "'
[146] Fix | Delete
};
[147] Fix | Delete
",
[148] Fix | Delete
'before'
[149] Fix | Delete
);
[150] Fix | Delete
}
[151] Fix | Delete
[152] Fix | Delete
/**
[153] Fix | Delete
* Register and enqueue assets for exclusive usage within the Site Editor.
[154] Fix | Delete
*/
[155] Fix | Delete
public function register_and_enqueue_site_editor_assets() {
[156] Fix | Delete
// Customer Effort Score.
[157] Fix | Delete
wp_enqueue_script( 'wc-customer-effort-score' );
[158] Fix | Delete
wp_enqueue_style( 'wc-customer-effort-score' );
[159] Fix | Delete
}
[160] Fix | Delete
[161] Fix | Delete
/**
[162] Fix | Delete
* Defines resource hints to help speed up the loading of some critical blocks.
[163] Fix | Delete
*
[164] Fix | Delete
* These will not impact page loading times negatively because they are loaded once the current page is idle.
[165] Fix | Delete
*
[166] Fix | Delete
* @param array $urls URLs to print for resource hints. Each URL is an array of resource attributes, or a URL string.
[167] Fix | Delete
* @param string $relation_type The relation type the URLs are printed. Possible values: preconnect, dns-prefetch, prefetch, prerender.
[168] Fix | Delete
* @return array URLs to print for resource hints.
[169] Fix | Delete
*/
[170] Fix | Delete
public function add_resource_hints( $urls, $relation_type ) {
[171] Fix | Delete
if ( ! in_array( $relation_type, array( 'prefetch', 'prerender' ), true ) || is_admin() ) {
[172] Fix | Delete
return $urls;
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
// We only need to prefetch when the cart has contents.
[176] Fix | Delete
$cart = wc()->cart;
[177] Fix | Delete
[178] Fix | Delete
if ( ! $cart instanceof \WC_Cart || 0 === $cart->get_cart_contents_count() ) {
[179] Fix | Delete
return $urls;
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
if ( 'prefetch' === $relation_type ) {
[183] Fix | Delete
$urls = array_merge(
[184] Fix | Delete
$urls,
[185] Fix | Delete
$this->get_prefetch_resource_hints()
[186] Fix | Delete
);
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
if ( 'prerender' === $relation_type ) {
[190] Fix | Delete
$urls = array_merge(
[191] Fix | Delete
$urls,
[192] Fix | Delete
$this->get_prerender_resource_hints()
[193] Fix | Delete
);
[194] Fix | Delete
}
[195] Fix | Delete
[196] Fix | Delete
return $urls;
[197] Fix | Delete
}
[198] Fix | Delete
[199] Fix | Delete
/**
[200] Fix | Delete
* Get resource hints during prefetch requests.
[201] Fix | Delete
*
[202] Fix | Delete
* @return array Array of URLs.
[203] Fix | Delete
*/
[204] Fix | Delete
private function get_prefetch_resource_hints() {
[205] Fix | Delete
$urls = array();
[206] Fix | Delete
[207] Fix | Delete
// Core page IDs.
[208] Fix | Delete
$cart_page_id = wc_get_page_id( 'cart' );
[209] Fix | Delete
$checkout_page_id = wc_get_page_id( 'checkout' );
[210] Fix | Delete
[211] Fix | Delete
// Checks a specific page (by ID) to see if it contains the named block.
[212] Fix | Delete
$has_block_cart = $cart_page_id && has_block( 'woocommerce/cart', $cart_page_id );
[213] Fix | Delete
$has_block_checkout = $checkout_page_id && has_block( 'woocommerce/checkout', $checkout_page_id );
[214] Fix | Delete
[215] Fix | Delete
// Checks the current page to see if it contains the named block.
[216] Fix | Delete
$is_block_cart = has_block( 'woocommerce/cart' );
[217] Fix | Delete
$is_block_checkout = has_block( 'woocommerce/checkout' );
[218] Fix | Delete
[219] Fix | Delete
if ( $has_block_cart && ! $is_block_cart ) {
[220] Fix | Delete
$urls = array_merge( $urls, $this->get_block_asset_resource_hints( 'cart-frontend' ) );
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
if ( $has_block_checkout && ! $is_block_checkout ) {
[224] Fix | Delete
$urls = array_merge( $urls, $this->get_block_asset_resource_hints( 'checkout-frontend' ) );
[225] Fix | Delete
}
[226] Fix | Delete
[227] Fix | Delete
return $urls;
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
/**
[231] Fix | Delete
* Get resource hints during prerender requests.
[232] Fix | Delete
*
[233] Fix | Delete
* @return array Array of URLs.
[234] Fix | Delete
*/
[235] Fix | Delete
private function get_prerender_resource_hints() {
[236] Fix | Delete
$urls = array();
[237] Fix | Delete
$is_block_cart = has_block( 'woocommerce/cart' );
[238] Fix | Delete
[239] Fix | Delete
if ( ! $is_block_cart ) {
[240] Fix | Delete
return $urls;
[241] Fix | Delete
}
[242] Fix | Delete
[243] Fix | Delete
$checkout_page_id = wc_get_page_id( 'checkout' );
[244] Fix | Delete
$checkout_page_url = $checkout_page_id ? get_permalink( $checkout_page_id ) : '';
[245] Fix | Delete
[246] Fix | Delete
if ( $checkout_page_url ) {
[247] Fix | Delete
$urls[] = $checkout_page_url;
[248] Fix | Delete
}
[249] Fix | Delete
[250] Fix | Delete
return $urls;
[251] Fix | Delete
}
[252] Fix | Delete
[253] Fix | Delete
/**
[254] Fix | Delete
* Get the block asset resource hints in the cache or null if not found.
[255] Fix | Delete
*
[256] Fix | Delete
* @return array|null Array of resource hints.
[257] Fix | Delete
*/
[258] Fix | Delete
private function get_block_asset_resource_hints_cache() {
[259] Fix | Delete
if ( wp_is_development_mode( 'plugin' ) ) {
[260] Fix | Delete
return null;
[261] Fix | Delete
}
[262] Fix | Delete
[263] Fix | Delete
$cache = get_transient( 'woocommerce_block_asset_resource_hints' );
[264] Fix | Delete
[265] Fix | Delete
$current_version = array(
[266] Fix | Delete
'woocommerce' => Constants::get_constant( 'WC_VERSION' ),
[267] Fix | Delete
'wordpress' => get_bloginfo( 'version' ),
[268] Fix | Delete
'site_url' => site_url(),
[269] Fix | Delete
);
[270] Fix | Delete
[271] Fix | Delete
if ( isset( $cache['version'] ) && $cache['version'] === $current_version ) {
[272] Fix | Delete
return $cache['files'];
[273] Fix | Delete
}
[274] Fix | Delete
[275] Fix | Delete
return null;
[276] Fix | Delete
}
[277] Fix | Delete
[278] Fix | Delete
/**
[279] Fix | Delete
* Set the block asset resource hints in the cache.
[280] Fix | Delete
*
[281] Fix | Delete
* @param string $filename File name.
[282] Fix | Delete
* @param array $data Array of resource hints.
[283] Fix | Delete
*/
[284] Fix | Delete
private function set_block_asset_resource_hints_cache( $filename, $data ) {
[285] Fix | Delete
$cache = $this->get_block_asset_resource_hints_cache();
[286] Fix | Delete
$updated = array(
[287] Fix | Delete
'files' => $cache ?? array(),
[288] Fix | Delete
'version' => array(
[289] Fix | Delete
'woocommerce' => Constants::get_constant( 'WC_VERSION' ),
[290] Fix | Delete
'wordpress' => get_bloginfo( 'version' ),
[291] Fix | Delete
'site_url' => site_url(),
[292] Fix | Delete
),
[293] Fix | Delete
);
[294] Fix | Delete
[295] Fix | Delete
$updated['files'][ $filename ] = $data;
[296] Fix | Delete
set_transient( 'woocommerce_block_asset_resource_hints', $updated, WEEK_IN_SECONDS );
[297] Fix | Delete
}
[298] Fix | Delete
[299] Fix | Delete
/**
[300] Fix | Delete
* Get resource hint for a block by name.
[301] Fix | Delete
*
[302] Fix | Delete
* @param string $filename Block filename.
[303] Fix | Delete
* @return array
[304] Fix | Delete
*/
[305] Fix | Delete
private function get_block_asset_resource_hints( $filename = '' ) {
[306] Fix | Delete
if ( ! $filename ) {
[307] Fix | Delete
return array();
[308] Fix | Delete
}
[309] Fix | Delete
[310] Fix | Delete
$cached = $this->get_block_asset_resource_hints_cache();
[311] Fix | Delete
[312] Fix | Delete
if ( isset( $cached[ $filename ] ) ) {
[313] Fix | Delete
return $cached[ $filename ];
[314] Fix | Delete
}
[315] Fix | Delete
[316] Fix | Delete
$script_data = $this->api->get_script_data(
[317] Fix | Delete
$this->api->get_block_asset_build_path( $filename )
[318] Fix | Delete
);
[319] Fix | Delete
$resources = array_merge(
[320] Fix | Delete
array( esc_url( add_query_arg( 'ver', $script_data['version'], $script_data['src'] ) ) ),
[321] Fix | Delete
$this->get_script_dependency_src_array( $script_data['dependencies'] )
[322] Fix | Delete
);
[323] Fix | Delete
[324] Fix | Delete
$data = array_map(
[325] Fix | Delete
function ( $src ) {
[326] Fix | Delete
return array(
[327] Fix | Delete
'href' => $src,
[328] Fix | Delete
'as' => 'script',
[329] Fix | Delete
);
[330] Fix | Delete
},
[331] Fix | Delete
array_unique( array_filter( $resources ) )
[332] Fix | Delete
);
[333] Fix | Delete
[334] Fix | Delete
$this->set_block_asset_resource_hints_cache( $filename, $data );
[335] Fix | Delete
[336] Fix | Delete
return $data;
[337] Fix | Delete
}
[338] Fix | Delete
[339] Fix | Delete
/**
[340] Fix | Delete
* Get the src of all script dependencies (handles).
[341] Fix | Delete
*
[342] Fix | Delete
* @param array $dependencies Array of dependency handles.
[343] Fix | Delete
* @return string[] Array of src strings.
[344] Fix | Delete
*/
[345] Fix | Delete
private function get_script_dependency_src_array( array $dependencies ) {
[346] Fix | Delete
$wp_scripts = wp_scripts();
[347] Fix | Delete
[348] Fix | Delete
$found_dependencies = array();
[349] Fix | Delete
$this->gather_script_dependency_handles( $dependencies, $wp_scripts, $found_dependencies );
[350] Fix | Delete
[351] Fix | Delete
$src = array();
[352] Fix | Delete
foreach ( $found_dependencies as $handle => $unused ) {
[353] Fix | Delete
$src[] = esc_url( add_query_arg( 'ver', $wp_scripts->registered[ $handle ]->ver, $this->get_absolute_url( $wp_scripts->registered[ $handle ]->src ) ) );
[354] Fix | Delete
}
[355] Fix | Delete
return $src;
[356] Fix | Delete
}
[357] Fix | Delete
[358] Fix | Delete
/**
[359] Fix | Delete
* Recursively gather all unique script dependency handles from a starting list.
[360] Fix | Delete
*
[361] Fix | Delete
* Traverses the dependency graph for each input handle, collecting any found handles
[362] Fix | Delete
* and their nested dependencies in the provided array. Used internally to build a
[363] Fix | Delete
* complete, deduplicated set of handles for further processing (e.g., mapping to src URLs).
[364] Fix | Delete
*
[365] Fix | Delete
* @param array $dependencies Array of initial script handles to process.
[366] Fix | Delete
* @param \WP_Scripts $wp_scripts WP_Scripts instance containing all registered scripts.
[367] Fix | Delete
* @param array $found_dependencies Reference to array in which discovered handles are stored.
[368] Fix | Delete
*
[369] Fix | Delete
* @return void
[370] Fix | Delete
*/
[371] Fix | Delete
private function gather_script_dependency_handles( array $dependencies, \WP_Scripts $wp_scripts, &$found_dependencies = array() ) {
[372] Fix | Delete
foreach ( $dependencies as $handle ) {
[373] Fix | Delete
if ( isset( $wp_scripts->registered[ $handle ] ) && ! isset( $found_dependencies[ $handle ] ) ) {
[374] Fix | Delete
$found_dependencies[ $handle ] = true;
[375] Fix | Delete
if ( ! empty( $wp_scripts->registered[ $handle ]->deps ) ) {
[376] Fix | Delete
$this->gather_script_dependency_handles( $wp_scripts->registered[ $handle ]->deps, $wp_scripts, $found_dependencies );
[377] Fix | Delete
}
[378] Fix | Delete
}
[379] Fix | Delete
}
[380] Fix | Delete
}
[381] Fix | Delete
[382] Fix | Delete
/**
[383] Fix | Delete
* Returns an absolute url to relative links for WordPress core scripts.
[384] Fix | Delete
*
[385] Fix | Delete
* @param string $src Original src that can be relative.
[386] Fix | Delete
* @return string Correct full path string.
[387] Fix | Delete
*/
[388] Fix | Delete
private function get_absolute_url( $src ) {
[389] Fix | Delete
$wp_scripts = wp_scripts();
[390] Fix | Delete
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && 0 === strpos( $src, $wp_scripts->content_url ) ) ) {
[391] Fix | Delete
$src = $wp_scripts->base_url . $src;
[392] Fix | Delete
}
[393] Fix | Delete
return $src;
[394] Fix | Delete
}
[395] Fix | Delete
[396] Fix | Delete
/**
[397] Fix | Delete
* Skip Jetpack Boost minification on older versions of Jetpack Boost where it causes issues.
[398] Fix | Delete
*
[399] Fix | Delete
* @param mixed $do_concat Whether to concatenate the script or not.
[400] Fix | Delete
* @param mixed $handle The script handle.
[401] Fix | Delete
* @return mixed
[402] Fix | Delete
*/
[403] Fix | Delete
public function skip_boost_minification_for_cart_checkout( $do_concat, $handle ) {
[404] Fix | Delete
$boost_is_outdated = defined( 'JETPACK_BOOST_VERSION' ) && version_compare( JETPACK_BOOST_VERSION, '3.4.2', '<' );
[405] Fix | Delete
$scripts_to_ignore = array(
[406] Fix | Delete
'wc-cart-checkout-vendors',
[407] Fix | Delete
'wc-cart-checkout-base',
[408] Fix | Delete
);
[409] Fix | Delete
[410] Fix | Delete
return $boost_is_outdated && in_array( $handle, $scripts_to_ignore, true ) ? false : $do_concat;
[411] Fix | Delete
}
[412] Fix | Delete
[413] Fix | Delete
/**
[414] Fix | Delete
* Add body classes to the frontend and within admin.
[415] Fix | Delete
*
[416] Fix | Delete
* @param string|array $classes Array or string of CSS classnames.
[417] Fix | Delete
* @return string|array Modified classnames.
[418] Fix | Delete
*/
[419] Fix | Delete
public function add_theme_body_class( $classes ) {
[420] Fix | Delete
$class = 'theme-' . get_template();
[421] Fix | Delete
[422] Fix | Delete
if ( is_array( $classes ) ) {
[423] Fix | Delete
$classes[] = $class;
[424] Fix | Delete
} else {
[425] Fix | Delete
$classes .= ' ' . $class . ' ';
[426] Fix | Delete
}
[427] Fix | Delete
[428] Fix | Delete
return $classes;
[429] Fix | Delete
}
[430] Fix | Delete
[431] Fix | Delete
/**
[432] Fix | Delete
* Get the file modified time as a cache buster if we're in dev mode.
[433] Fix | Delete
*
[434] Fix | Delete
* @param string $file Local path to the file.
[435] Fix | Delete
* @return string The cache buster value to use for the given file.
[436] Fix | Delete
*/
[437] Fix | Delete
protected function get_file_version( $file ) {
[438] Fix | Delete
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( \Automattic\WooCommerce\Blocks\Package::get_path() . $file ) ) {
[439] Fix | Delete
return filemtime( \Automattic\WooCommerce\Blocks\Package::get_path() . $file );
[440] Fix | Delete
}
[441] Fix | Delete
return $this->api->wc_version;
[442] Fix | Delete
}
[443] Fix | Delete
[444] Fix | Delete
/**
[445] Fix | Delete
* Registers a style according to `wp_register_style`.
[446] Fix | Delete
*
[447] Fix | Delete
* @param string $handle Name of the stylesheet. Should be unique.
[448] Fix | Delete
* @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
[449] Fix | Delete
* @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
[450] Fix | Delete
* @param string $media Optional. The media for which this stylesheet has been defined. Default 'all'. Accepts media types like
[451] Fix | Delete
* 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'.
[452] Fix | Delete
* @param boolean $rtl Optional. Whether or not to register RTL styles.
[453] Fix | Delete
*/
[454] Fix | Delete
protected function register_style( $handle, $src, $deps = array(), $media = 'all', $rtl = false ) {
[455] Fix | Delete
$filename = str_replace( plugins_url( '/', dirname( __DIR__ ) ), '', $src );
[456] Fix | Delete
$ver = self::get_file_version( $filename );
[457] Fix | Delete
[458] Fix | Delete
wp_register_style( $handle, $src, $deps, $ver, $media );
[459] Fix | Delete
[460] Fix | Delete
if ( $rtl ) {
[461] Fix | Delete
wp_style_add_data( $handle, 'rtl', 'replace' );
[462] Fix | Delete
}
[463] Fix | Delete
}
[464] Fix | Delete
[465] Fix | Delete
/**
[466] Fix | Delete
* Update block style dependencies after they have been registered.
[467] Fix | Delete
*/
[468] Fix | Delete
public function update_block_style_dependencies() {
[469] Fix | Delete
$wp_styles = wp_styles();
[470] Fix | Delete
$style = $wp_styles->query( 'wc-blocks-style', 'registered' );
[471] Fix | Delete
[472] Fix | Delete
if ( ! $style ) {
[473] Fix | Delete
return;
[474] Fix | Delete
}
[475] Fix | Delete
[476] Fix | Delete
// In WC < 5.5, `woocommerce-general` is not registered in block editor
[477] Fix | Delete
// screens, so we don't add it as a dependency if it's not registered.
[478] Fix | Delete
// In WC >= 5.5, `woocommerce-general` is registered on `admin_enqueue_scripts`,
[479] Fix | Delete
// so we need to check if it's registered here instead of on `init`.
[480] Fix | Delete
if (
[481] Fix | Delete
wp_style_is( 'woocommerce-general', 'registered' ) &&
[482] Fix | Delete
! in_array( 'woocommerce-general', $style->deps, true )
[483] Fix | Delete
) {
[484] Fix | Delete
$style->deps[] = 'woocommerce-general';
[485] Fix | Delete
}
[486] Fix | Delete
}
[487] Fix | Delete
[488] Fix | Delete
/**
[489] Fix | Delete
* Fix scripts with wc-settings dependency.
[490] Fix | Delete
*
[491] Fix | Delete
* The wc-settings script only works correctly when enqueued in the footer. This is to give blocks etc time to
[492] Fix | Delete
* register their settings data before it's printed.
[493] Fix | Delete
*
[494] Fix | Delete
* This code will look at registered scripts, and if they have a wc-settings dependency, force them to print in the
[495] Fix | Delete
* footer instead of the header.
[496] Fix | Delete
*
[497] Fix | Delete
* This only supports packages known to require wc-settings!
[498] Fix | Delete
*
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function