-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathScriptWithBuiltDependenciesAsset.php
70 lines (64 loc) · 2.48 KB
/
ScriptWithBuiltDependenciesAsset.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Assets;
use Automattic\WooCommerce\GoogleListingsAndAds\Value\BuiltScriptDependencyArray as DependencyArray;
use Throwable;
defined( 'ABSPATH' ) || exit;
/**
* Construct a ScriptAsset loading the dependencies from a generated file.
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Assets
*/
class ScriptWithBuiltDependenciesAsset extends ScriptAsset {
/**
* ScriptWithBuiltDependenciesAsset constructor.
*
* @param string $handle The script handle.
* @param string $uri The URI for the script.
* @param string $build_dependency_path Path to the generated dependency file.
* @param DependencyArray $fallback_dependency_data Fallback dependencies (if the generated file is not readable).
* @param callable|null $enqueue_condition_callback (Optional) The asset is always enqueued if this callback
* returns true or isn't set.
* @param bool $in_footer Whether to enqueue the script before </body> instead of in
* the <head> (default: true).
*/
public function __construct(
string $handle,
string $uri,
string $build_dependency_path,
DependencyArray $fallback_dependency_data,
callable $enqueue_condition_callback = null,
bool $in_footer = true
) {
$dependency_data = $this->get_dependency_data( $build_dependency_path, $fallback_dependency_data );
parent::__construct(
$handle,
$uri,
$dependency_data->get_dependencies(),
$dependency_data->get_version(),
$enqueue_condition_callback,
$in_footer
);
}
/**
* Get usable dependency data from an asset path or from the fallback.
*
* @param string $build_dependency_path
* @param DependencyArray $fallback
*
* @return DependencyArray
*/
protected function get_dependency_data( string $build_dependency_path, DependencyArray $fallback ): DependencyArray {
try {
if ( ! is_readable( $build_dependency_path ) ) {
return $fallback;
}
// Reason of exclusion: These files are being loaded manually in the function call with no user input
// nosemgrep: audit.php.lang.security.file.inclusion-arg
return new DependencyArray( include $build_dependency_path );
} catch ( Throwable $e ) {
do_action( 'woocommerce_gla_exception', $e, __METHOD__ );
return $fallback;
}
}
}