Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Blocks/Assets
File: Api.php
<?php
[0] Fix | Delete
namespace Automattic\WooCommerce\Blocks\Assets;
[1] Fix | Delete
[2] Fix | Delete
use Automattic\WooCommerce\Blocks\Domain\Package;
[3] Fix | Delete
use Exception;
[4] Fix | Delete
use Automattic\Jetpack\Constants;
[5] Fix | Delete
/**
[6] Fix | Delete
* The Api class provides an interface to various asset registration helpers.
[7] Fix | Delete
*
[8] Fix | Delete
* Contains asset api methods
[9] Fix | Delete
*
[10] Fix | Delete
* @since 2.5.0
[11] Fix | Delete
*/
[12] Fix | Delete
class Api {
[13] Fix | Delete
[14] Fix | Delete
/**
[15] Fix | Delete
* Stores the prefixed WC version. Used because the WC Blocks version has not been updated since the monorepo merge.
[16] Fix | Delete
*
[17] Fix | Delete
* @var string
[18] Fix | Delete
*/
[19] Fix | Delete
public $wc_version;
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* Stores inline scripts already enqueued.
[23] Fix | Delete
*
[24] Fix | Delete
* @var array
[25] Fix | Delete
*/
[26] Fix | Delete
private $inline_scripts = [];
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* Determines if caching is enabled for script data.
[30] Fix | Delete
*
[31] Fix | Delete
* @var boolean
[32] Fix | Delete
*/
[33] Fix | Delete
private $disable_cache = false;
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* Stores loaded script data for the current request
[37] Fix | Delete
*
[38] Fix | Delete
* @var array|null
[39] Fix | Delete
*/
[40] Fix | Delete
private $script_data = null;
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* Tracks whether script_data was modified during the current request.
[44] Fix | Delete
*
[45] Fix | Delete
* @var boolean
[46] Fix | Delete
*/
[47] Fix | Delete
private $script_data_modified = false;
[48] Fix | Delete
[49] Fix | Delete
/**
[50] Fix | Delete
* Stores the hash for the script data, made up of the site url, plugin version and package path.
[51] Fix | Delete
*
[52] Fix | Delete
* @var string
[53] Fix | Delete
*/
[54] Fix | Delete
private $script_data_hash;
[55] Fix | Delete
[56] Fix | Delete
/**
[57] Fix | Delete
* Stores the transient key used to cache the script data. This will change if the site is accessed via HTTPS or HTTP.
[58] Fix | Delete
*
[59] Fix | Delete
* @var string
[60] Fix | Delete
*/
[61] Fix | Delete
private $script_data_transient_key = 'woocommerce_blocks_asset_api_script_data';
[62] Fix | Delete
[63] Fix | Delete
/**
[64] Fix | Delete
* Reference to the Package instance
[65] Fix | Delete
*
[66] Fix | Delete
* @var Package
[67] Fix | Delete
*/
[68] Fix | Delete
private $package;
[69] Fix | Delete
[70] Fix | Delete
/**
[71] Fix | Delete
* Constructor for class
[72] Fix | Delete
*
[73] Fix | Delete
* @param Package $package An instance of Package.
[74] Fix | Delete
*/
[75] Fix | Delete
public function __construct( Package $package ) {
[76] Fix | Delete
// Use wc- prefix here to prevent collisions when WC Core version catches up to a version previously used by the WC Blocks feature plugin.
[77] Fix | Delete
$this->wc_version = 'wc-' . Constants::get_constant( 'WC_VERSION' );
[78] Fix | Delete
$this->package = $package;
[79] Fix | Delete
$this->disable_cache = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) || wp_get_environment_type() !== 'production';
[80] Fix | Delete
[81] Fix | Delete
// If the site is accessed via HTTPS, change the transient key. This is to prevent the script URLs being cached
[82] Fix | Delete
// with the first scheme they are accessed on after cache expiry.
[83] Fix | Delete
if ( is_ssl() ) {
[84] Fix | Delete
$this->script_data_transient_key .= '_ssl';
[85] Fix | Delete
}
[86] Fix | Delete
if ( ! $this->disable_cache ) {
[87] Fix | Delete
$this->script_data_hash = $this->get_script_data_hash();
[88] Fix | Delete
}
[89] Fix | Delete
add_action( 'shutdown', array( $this, 'update_script_data_cache' ), 20 );
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
/**
[93] Fix | Delete
* Get the file modified time as a cache buster if we're in dev mode.
[94] Fix | Delete
*
[95] Fix | Delete
* @param string $file Local path to the file (relative to the plugin
[96] Fix | Delete
* directory).
[97] Fix | Delete
* @return string The cache buster value to use for the given file.
[98] Fix | Delete
*/
[99] Fix | Delete
protected function get_file_version( $file ) {
[100] Fix | Delete
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $this->package->get_path() . $file ) ) {
[101] Fix | Delete
return filemtime( $this->package->get_path( trim( $file, '/' ) ) );
[102] Fix | Delete
}
[103] Fix | Delete
return $this->wc_version;
[104] Fix | Delete
}
[105] Fix | Delete
[106] Fix | Delete
/**
[107] Fix | Delete
* Retrieve the url to an asset for this plugin.
[108] Fix | Delete
*
[109] Fix | Delete
* @param string $relative_path An optional relative path appended to the
[110] Fix | Delete
* returned url.
[111] Fix | Delete
*
[112] Fix | Delete
* @return string
[113] Fix | Delete
*/
[114] Fix | Delete
protected function get_asset_url( $relative_path = '' ) {
[115] Fix | Delete
return $this->package->get_url( $relative_path );
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
/**
[119] Fix | Delete
* Get the path to a block's metadata
[120] Fix | Delete
*
[121] Fix | Delete
* @param string $block_name The block to get metadata for.
[122] Fix | Delete
* @param string $path Optional. The path to the metadata file inside the 'assets/client/blocks' folder.
[123] Fix | Delete
*
[124] Fix | Delete
* @return string|boolean False if metadata file is not found for the block.
[125] Fix | Delete
*/
[126] Fix | Delete
public function get_block_metadata_path( $block_name, $path = '' ) {
[127] Fix | Delete
$path_to_metadata_from_plugin_root = $this->package->get_path( 'assets/client/blocks/' . $path . $block_name . '/block.json' );
[128] Fix | Delete
if ( ! file_exists( $path_to_metadata_from_plugin_root ) ) {
[129] Fix | Delete
return false;
[130] Fix | Delete
}
[131] Fix | Delete
return $path_to_metadata_from_plugin_root;
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
/**
[135] Fix | Delete
* Generates a hash containing the site url, plugin version and package path.
[136] Fix | Delete
*
[137] Fix | Delete
* Moving the plugin, changing the version, or changing the site url will result in a new hash and the cache will be invalidated.
[138] Fix | Delete
*
[139] Fix | Delete
* @return string The generated hash.
[140] Fix | Delete
*/
[141] Fix | Delete
private function get_script_data_hash() {
[142] Fix | Delete
return md5( get_option( 'siteurl', '' ) . $this->wc_version . $this->package->get_path() );
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
/**
[146] Fix | Delete
* Initialize and load cached script data from the transient cache.
[147] Fix | Delete
*
[148] Fix | Delete
* @return array
[149] Fix | Delete
*/
[150] Fix | Delete
private function get_cached_script_data() {
[151] Fix | Delete
if ( $this->disable_cache ) {
[152] Fix | Delete
return [];
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
$transient_value = json_decode( (string) get_transient( $this->script_data_transient_key ), true );
[156] Fix | Delete
[157] Fix | Delete
if (
[158] Fix | Delete
json_last_error() !== JSON_ERROR_NONE ||
[159] Fix | Delete
empty( $transient_value ) ||
[160] Fix | Delete
empty( $transient_value['script_data'] ) ||
[161] Fix | Delete
empty( $transient_value['version'] ) ||
[162] Fix | Delete
$transient_value['version'] !== $this->wc_version ||
[163] Fix | Delete
empty( $transient_value['hash'] ) ||
[164] Fix | Delete
$transient_value['hash'] !== $this->script_data_hash
[165] Fix | Delete
) {
[166] Fix | Delete
return [];
[167] Fix | Delete
}
[168] Fix | Delete
[169] Fix | Delete
return (array) ( $transient_value['script_data'] ?? [] );
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
/**
[173] Fix | Delete
* Store all cached script data in the transient cache.
[174] Fix | Delete
*/
[175] Fix | Delete
public function update_script_data_cache() {
[176] Fix | Delete
if ( is_null( $this->script_data ) || $this->disable_cache ) {
[177] Fix | Delete
return;
[178] Fix | Delete
}
[179] Fix | Delete
if ( ! $this->script_data_modified ) {
[180] Fix | Delete
return;
[181] Fix | Delete
}
[182] Fix | Delete
set_transient(
[183] Fix | Delete
$this->script_data_transient_key,
[184] Fix | Delete
wp_json_encode(
[185] Fix | Delete
array(
[186] Fix | Delete
'script_data' => $this->script_data,
[187] Fix | Delete
'version' => $this->wc_version,
[188] Fix | Delete
'hash' => $this->script_data_hash,
[189] Fix | Delete
)
[190] Fix | Delete
),
[191] Fix | Delete
DAY_IN_SECONDS * 30
[192] Fix | Delete
);
[193] Fix | Delete
}
[194] Fix | Delete
[195] Fix | Delete
/**
[196] Fix | Delete
* Use package path to find an asset data file and return the data.
[197] Fix | Delete
*
[198] Fix | Delete
* @param string $filename The filename of the asset.
[199] Fix | Delete
* @return array The asset data.
[200] Fix | Delete
*/
[201] Fix | Delete
public function get_asset_data( $filename ) {
[202] Fix | Delete
$asset_path = $this->package->get_path( $filename );
[203] Fix | Delete
$asset = file_exists( $asset_path ) ? require $asset_path : [];
[204] Fix | Delete
return $asset;
[205] Fix | Delete
}
[206] Fix | Delete
[207] Fix | Delete
/**
[208] Fix | Delete
* Get src, version and dependencies given a script relative src.
[209] Fix | Delete
*
[210] Fix | Delete
* @param string $relative_src Relative src to the script.
[211] Fix | Delete
* @param array $dependencies Optional. An array of registered script handles this script depends on. Default empty array.
[212] Fix | Delete
*
[213] Fix | Delete
* @return array src, version and dependencies of the script.
[214] Fix | Delete
*/
[215] Fix | Delete
public function get_script_data( $relative_src, $dependencies = [] ) {
[216] Fix | Delete
if ( ! $relative_src ) {
[217] Fix | Delete
return array(
[218] Fix | Delete
'src' => '',
[219] Fix | Delete
'version' => '1',
[220] Fix | Delete
'dependencies' => $dependencies,
[221] Fix | Delete
);
[222] Fix | Delete
}
[223] Fix | Delete
[224] Fix | Delete
if ( is_null( $this->script_data ) ) {
[225] Fix | Delete
$this->script_data = $this->get_cached_script_data();
[226] Fix | Delete
}
[227] Fix | Delete
[228] Fix | Delete
if ( empty( $this->script_data[ $relative_src ] ) ) {
[229] Fix | Delete
$asset_path = $this->package->get_path( str_replace( '.js', '.asset.php', $relative_src ) );
[230] Fix | Delete
// The following require is safe because we are checking if the file exists and it is not a user input.
[231] Fix | Delete
// nosemgrep audit.php.lang.security.file.inclusion-arg.
[232] Fix | Delete
$asset = file_exists( $asset_path ) ? require $asset_path : [];
[233] Fix | Delete
[234] Fix | Delete
$this->script_data[ $relative_src ] = array(
[235] Fix | Delete
'src' => $this->get_asset_url( $relative_src ),
[236] Fix | Delete
'version' => ! empty( $asset['version'] ) ? $asset['version'] : $this->get_file_version( $relative_src ),
[237] Fix | Delete
'dependencies' => ! empty( $asset['dependencies'] ) ? $asset['dependencies'] : [],
[238] Fix | Delete
);
[239] Fix | Delete
$this->script_data_modified = true;
[240] Fix | Delete
}
[241] Fix | Delete
[242] Fix | Delete
// Return asset details as well as the requested dependencies array.
[243] Fix | Delete
return [
[244] Fix | Delete
'src' => $this->script_data[ $relative_src ]['src'],
[245] Fix | Delete
'version' => $this->script_data[ $relative_src ]['version'],
[246] Fix | Delete
'dependencies' => array_merge( $this->script_data[ $relative_src ]['dependencies'], $dependencies ),
[247] Fix | Delete
];
[248] Fix | Delete
}
[249] Fix | Delete
[250] Fix | Delete
/**
[251] Fix | Delete
* Registers a script according to `wp_register_script`, adding the correct prefix, and additionally loading translations.
[252] Fix | Delete
*
[253] Fix | Delete
* When creating script assets, the following rules should be followed:
[254] Fix | Delete
* 1. All asset handles should have a `wc-` prefix.
[255] Fix | Delete
* 2. If the asset handle is for a Block (in editor context) use the `-block` suffix.
[256] Fix | Delete
* 3. If the asset handle is for a Block (in frontend context) use the `-block-frontend` suffix.
[257] Fix | Delete
* 4. If the asset is for any other script being consumed or enqueued by the blocks plugin, use the `wc-blocks-` prefix.
[258] Fix | Delete
*
[259] Fix | Delete
* @since 2.5.0
[260] Fix | Delete
* @throws Exception If the registered script has a dependency on itself.
[261] Fix | Delete
*
[262] Fix | Delete
* @param string $handle Unique name of the script.
[263] Fix | Delete
* @param string $relative_src Relative url for the script to the path from plugin root.
[264] Fix | Delete
* @param array $dependencies Optional. An array of registered script handles this script depends on. Default empty array.
[265] Fix | Delete
* @param bool $has_i18n Optional. Whether to add a script translation call to this file. Default: true.
[266] Fix | Delete
*/
[267] Fix | Delete
public function register_script( $handle, $relative_src, $dependencies = [], $has_i18n = true ) {
[268] Fix | Delete
$script_data = $this->get_script_data( $relative_src, $dependencies );
[269] Fix | Delete
[270] Fix | Delete
if ( in_array( $handle, $script_data['dependencies'], true ) ) {
[271] Fix | Delete
if ( wp_get_environment_type() === 'development' ) {
[272] Fix | Delete
$dependencies = array_diff( $script_data['dependencies'], [ $handle ] );
[273] Fix | Delete
add_action(
[274] Fix | Delete
'admin_notices',
[275] Fix | Delete
function () use ( $handle ) {
[276] Fix | Delete
echo '<div class="error"><p>';
[277] Fix | Delete
/* translators: %s file handle name. */
[278] Fix | Delete
printf( esc_html__( 'Script with handle %s had a dependency on itself which has been removed. This is an indicator that your JS code has a circular dependency that can cause bugs.', 'woocommerce' ), esc_html( $handle ) );
[279] Fix | Delete
echo '</p></div>';
[280] Fix | Delete
}
[281] Fix | Delete
);
[282] Fix | Delete
} else {
[283] Fix | Delete
throw new Exception( sprintf( 'Script with handle %s had a dependency on itself. This is an indicator that your JS code has a circular dependency that can cause bugs.', $handle ) );
[284] Fix | Delete
}
[285] Fix | Delete
}
[286] Fix | Delete
[287] Fix | Delete
/**
[288] Fix | Delete
* Filters the list of script dependencies.
[289] Fix | Delete
*
[290] Fix | Delete
* @since 3.0.0
[291] Fix | Delete
*
[292] Fix | Delete
* @param array $dependencies The list of script dependencies.
[293] Fix | Delete
* @param string $handle The script's handle.
[294] Fix | Delete
* @return array
[295] Fix | Delete
*/
[296] Fix | Delete
$script_dependencies = apply_filters( 'woocommerce_blocks_register_script_dependencies', $script_data['dependencies'], $handle );
[297] Fix | Delete
[298] Fix | Delete
wp_register_script( $handle, $script_data['src'], $script_dependencies, $script_data['version'], true );
[299] Fix | Delete
[300] Fix | Delete
if ( $has_i18n && function_exists( 'wp_set_script_translations' ) ) {
[301] Fix | Delete
wp_set_script_translations( $handle, 'woocommerce', $this->package->get_path( 'languages' ) );
[302] Fix | Delete
wp_set_script_translations( $handle, 'woocommerce', $this->package->get_path( 'i18n/languages' ) );
[303] Fix | Delete
}
[304] Fix | Delete
}
[305] Fix | Delete
[306] Fix | Delete
/**
[307] Fix | Delete
* Registers a style according to `wp_register_style`.
[308] Fix | Delete
*
[309] Fix | Delete
* @since 2.5.0
[310] Fix | Delete
* @since 2.6.0 Change src to be relative source.
[311] Fix | Delete
*
[312] Fix | Delete
* @param string $handle Name of the stylesheet. Should be unique.
[313] Fix | Delete
* @param string $relative_src Relative source of the stylesheet to the plugin path.
[314] Fix | Delete
* @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
[315] Fix | Delete
* @param string $media Optional. The media for which this stylesheet has been defined. Default 'all'. Accepts media types like
[316] Fix | Delete
* 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'.
[317] Fix | Delete
* @param boolean $rtl Optional. Whether or not to register RTL styles.
[318] Fix | Delete
*/
[319] Fix | Delete
public function register_style( $handle, $relative_src, $deps = [], $media = 'all', $rtl = false ) {
[320] Fix | Delete
$filename = str_replace( plugins_url( '/', dirname( __DIR__ ) ), '', $relative_src );
[321] Fix | Delete
$src = $this->get_asset_url( $relative_src );
[322] Fix | Delete
$ver = $this->get_file_version( $filename );
[323] Fix | Delete
wp_register_style( $handle, $src, $deps, $ver, $media );
[324] Fix | Delete
[325] Fix | Delete
if ( $rtl ) {
[326] Fix | Delete
wp_style_add_data( $handle, 'rtl', 'replace' );
[327] Fix | Delete
}
[328] Fix | Delete
}
[329] Fix | Delete
[330] Fix | Delete
/**
[331] Fix | Delete
* Returns the appropriate asset path for current builds.
[332] Fix | Delete
*
[333] Fix | Delete
* @param string $filename Filename for asset path (without extension).
[334] Fix | Delete
* @param string $type File type (.css or .js).
[335] Fix | Delete
* @return string The generated path.
[336] Fix | Delete
*/
[337] Fix | Delete
public function get_block_asset_build_path( $filename, $type = 'js' ) {
[338] Fix | Delete
return "assets/client/blocks/$filename.$type";
[339] Fix | Delete
}
[340] Fix | Delete
[341] Fix | Delete
/**
[342] Fix | Delete
* Adds an inline script, once.
[343] Fix | Delete
*
[344] Fix | Delete
* @param string $handle Script handle.
[345] Fix | Delete
* @param string $script Script contents.
[346] Fix | Delete
*/
[347] Fix | Delete
public function add_inline_script( $handle, $script ) {
[348] Fix | Delete
if ( ! empty( $this->inline_scripts[ $handle ] ) && in_array( $script, $this->inline_scripts[ $handle ], true ) ) {
[349] Fix | Delete
return;
[350] Fix | Delete
}
[351] Fix | Delete
[352] Fix | Delete
wp_add_inline_script( $handle, $script );
[353] Fix | Delete
[354] Fix | Delete
if ( isset( $this->inline_scripts[ $handle ] ) ) {
[355] Fix | Delete
$this->inline_scripts[ $handle ][] = $script;
[356] Fix | Delete
} else {
[357] Fix | Delete
$this->inline_scripts[ $handle ] = array( $script );
[358] Fix | Delete
}
[359] Fix | Delete
}
[360] Fix | Delete
}
[361] Fix | Delete
[362] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function