-
Notifications
You must be signed in to change notification settings - Fork 260
/
DetectPluginAssets.php
executable file
·101 lines (82 loc) · 2.79 KB
/
DetectPluginAssets.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
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
<?php
namespace WP2Static;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
class DetectPluginAssets {
/**
* Detect Plugin asset URLs
*
* @return string[] list of URLs
*/
public static function detect() : array {
$files = [];
$plugins_path = SiteInfo::getPath( 'plugins' );
$plugins_url = SiteInfo::getUrl( 'plugins' );
if ( is_dir( $plugins_path ) ) {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$plugins_path,
RecursiveDirectoryIterator::SKIP_DOTS
)
);
/**
* @var string[] $active_plugins
*/
$active_plugins = get_option( 'active_plugins' );
/**
* @var string[] $active_sitewide_plugins
*/
$active_sitewide_plugins = get_option( 'active_sitewide_plugins' );
if ( is_multisite() ) {
$active_plugins = array_unique(
array_merge(
$active_plugins,
array_keys( $active_sitewide_plugins )
)
);
}
$active_plugin_dirs = array_map(
function ( $active_plugin ) {
return explode( '/', $active_plugin )[0];
},
$active_plugins
);
foreach ( $iterator as $filename => $file_object ) {
/**
* @var string $filename
*/
$path_crawlable =
FilesHelper::filePathLooksCrawlable( $filename );
if ( ! $path_crawlable ) {
continue;
}
$matches_active_plugin_dir =
( str_replace( $active_plugin_dirs, '', $filename ) !== $filename );
if ( ! $matches_active_plugin_dir ) {
continue;
}
// Standardise all paths to use / (Windows support)
$filename = str_replace( '\\', '/', $filename );
$detected_filename =
str_replace(
$plugins_path,
$plugins_url,
$filename
);
$detected_filename =
str_replace(
get_home_url(),
'',
$detected_filename
);
if ( is_string( $detected_filename ) ) {
array_push(
$files,
$detected_filename
);
}
}
}
return $files;
}
}