-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassets.php
More file actions
112 lines (96 loc) · 3.5 KB
/
Copy pathassets.php
File metadata and controls
112 lines (96 loc) · 3.5 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* Contains functions for working with assets (primarily JavaScript).
*
* phpcs:disable phpcs:ignore Squiz.PHP.CommentedOutCode.Found
*
* @package create-wordpress-plugin
*/
namespace Alley\WP\Create_WordPress_Plugin;
/**
* Validate file paths to prevent a PHP error if a file doesn't exist.
*
* @param string $path The file path to validate.
* @return bool True if the path is valid and the file exists.
*/
function validate_path( string $path ): bool {
return ( 0 === validate_file( $path ) || 2 === validate_file( $path ) ) && file_exists( $path );
}
/**
* Get the entry points directory path or public URL.
*
* @param string $dir_entry_name The directory name where the entry point was defined.
* @param boolean $dir Optional. Whether to return the directory path or the plugin URL path. Defaults to false (returns URL).
*/
function get_entry_dir_path( string $dir_entry_name, bool $dir = false ): string {
// The relative path from the plugin root.
$asset_build_dir = "/build/{$dir_entry_name}/";
// Set the absolute file path from the root directory.
$asset_dir_path = CREATE_WORDPRESS_PLUGIN_DIR . $asset_build_dir;
if ( validate_path( $asset_dir_path ) ) {
// Negotiate the base path.
return $dir
? $asset_dir_path
: plugins_url( $asset_build_dir, __DIR__ );
}
return '';
}
/**
* Get the assets dependencies and version.
*
* @param string $dir_entry_name The entry point directory name.
*
* @return array{dependencies?: string[], version?: string}
*/
function get_entry_asset_map( string $dir_entry_name ): array {
$base_path = get_entry_dir_path( $dir_entry_name, true );
if ( $base_path !== '' && $base_path !== '0' ) {
$asset_file_path = trailingslashit( $base_path ) . 'index.asset.php';
if ( validate_path( $asset_file_path ) ) {
$asset_map = include $asset_file_path; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.IncludingFile, WordPressVIPMinimum.Files.IncludingFile.UsingVariable
return is_array( $asset_map ) ? $asset_map : []; // @phpstan-ignore-line returns array
}
}
return [];
}
/**
* Get the dependency array for a given asset.
*
* @param string $dir_entry_name The entry point directory name.
*
* @return array<string> The asset's dependency array.
*/
function get_asset_dependency_array( string $dir_entry_name ): array {
$asset_arr = get_entry_asset_map( $dir_entry_name );
return $asset_arr['dependencies'] ?? [];
}
/**
* Get the version hash for a given asset.
*
* @param string $dir_entry_name The entry point directory name.
*
* @return string The asset's version hash.
*/
function get_asset_version( string $dir_entry_name ): string {
$asset_arr = get_entry_asset_map( $dir_entry_name );
return $asset_arr['version'] ?? '1.0';
}
/**
* Get the public url for the assets entry file.
*
* @param string $dir_entry_name The entry point directory name.
* @param string $filename The asset file name including the file type extension to get the public path for.
* @return string The public URL to the asset, empty string otherwise.
*/
function get_entry_asset_url( string $dir_entry_name, ?string $filename = 'index.js' ): string {
if ( in_array( $filename, [ null, '', '0' ], true ) ) {
return '';
}
if ( validate_path( trailingslashit( get_entry_dir_path( $dir_entry_name, true ) ) . $filename ) ) {
$entry_base_url = get_entry_dir_path( $dir_entry_name );
if ( $entry_base_url !== '' && $entry_base_url !== '0' ) {
return trailingslashit( $entry_base_url ) . $filename;
}
}
return '';
}