|
1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
|
2
|
|
|
/** |
|
3
|
|
|
* Plugin Name: Monorepo Helper |
|
4
|
|
|
* Description: A common place for monorepo things. |
|
5
|
|
|
* Version: 1.0 |
|
6
|
|
|
* Author: Automattic |
|
7
|
|
|
* Author URI: https://automattic.com/ |
|
8
|
|
|
* Text Domain: jetpack |
|
9
|
|
|
* |
|
10
|
|
|
* @package automattic/jetpack |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Jetpack\Docker\MuPlugin; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Monorepo Tools. |
|
17
|
|
|
*/ |
|
18
|
|
|
class Monorepo { |
|
19
|
|
|
/** |
|
20
|
|
|
* Path to monorepo. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $monorepo; |
|
25
|
|
|
/** |
|
26
|
|
|
* Path to monorepo plugins. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $plugins; |
|
31
|
|
|
/** |
|
32
|
|
|
* Path to monorepo packages. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $packages; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Class constructor. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct() { |
|
42
|
|
|
$this->monorepo = '/usr/local/src/jetpack-monorepo/'; |
|
43
|
|
|
$this->plugins = $this->monorepo . 'projects/plugins/'; |
|
44
|
|
|
$this->packages = $this->monorepo . 'projects/packages/'; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Property Getter |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $var Property to get. |
|
51
|
|
|
* |
|
52
|
|
|
* @throws Exception If the requested property does not exist. |
|
53
|
|
|
*/ |
|
54
|
|
|
public function get( $var ) { |
|
55
|
|
|
if ( is_string( $var ) && isset( $this->$var ) ) { |
|
56
|
|
|
return $this->$var; |
|
57
|
|
|
} |
|
58
|
|
|
throw new Exception( "Class property $var does not exist." ); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* The same as Core's get_plugins, without forcing a passed value to be within the wp-content/plugins folder. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $plugin_folder Folder to find plugins within. |
|
65
|
|
|
*/ |
|
66
|
|
|
private function get_plugins( $plugin_folder = '' ) { |
|
67
|
|
|
$cache_plugins = wp_cache_get( 'monorepo_plugins', 'monorepo_plugins' ); // Updated cache values to not conflict. |
|
68
|
|
|
if ( ! $cache_plugins ) { |
|
69
|
|
|
$cache_plugins = array(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if ( isset( $cache_plugins[ $plugin_folder ] ) ) { |
|
73
|
|
|
return $cache_plugins[ $plugin_folder ]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$wp_plugins = array(); |
|
77
|
|
|
$plugin_root = WP_PLUGIN_DIR; |
|
78
|
|
|
if ( ! empty( $plugin_folder ) ) { |
|
79
|
|
|
$plugin_root = $plugin_folder; // This is what we changed, but it's dangerous. Thus a private function. |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// Files in wp-content/plugins directory. |
|
83
|
|
|
$plugins_dir = @opendir( $plugin_root ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
84
|
|
|
$plugin_files = array(); |
|
85
|
|
|
|
|
86
|
|
|
if ( $plugins_dir ) { |
|
87
|
|
|
while ( ( $file = readdir( $plugins_dir ) ) !== false ) { // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition |
|
88
|
|
|
if ( '.' === substr( $file, 0, 1 ) ) { |
|
89
|
|
|
continue; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if ( is_dir( $plugin_root . '/' . $file ) ) { |
|
93
|
|
|
$plugins_subdir = @opendir( $plugin_root . '/' . $file ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
94
|
|
|
|
|
95
|
|
|
if ( $plugins_subdir ) { |
|
96
|
|
|
while ( ( $subfile = readdir( $plugins_subdir ) ) !== false ) { // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition |
|
97
|
|
|
if ( '.' === substr( $subfile, 0, 1 ) ) { |
|
98
|
|
|
continue; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if ( '.php' === substr( $subfile, -4 ) ) { |
|
102
|
|
|
$plugin_files[] = "$file/$subfile"; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
closedir( $plugins_subdir ); |
|
107
|
|
|
} |
|
108
|
|
|
} else { |
|
109
|
|
|
if ( '.php' === substr( $file, -4 ) ) { |
|
110
|
|
|
$plugin_files[] = $file; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
closedir( $plugins_dir ); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
if ( empty( $plugin_files ) ) { |
|
119
|
|
|
return $wp_plugins; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
foreach ( $plugin_files as $plugin_file ) { |
|
123
|
|
|
if ( ! is_readable( "$plugin_root/$plugin_file" ) ) { |
|
124
|
|
|
continue; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
// Do not apply markup/translate as it will be cached. |
|
128
|
|
|
$plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); |
|
129
|
|
|
|
|
130
|
|
|
if ( empty( $plugin_data['Name'] ) ) { |
|
131
|
|
|
continue; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$wp_plugins[ plugin_basename( $plugin_file ) ] = $plugin_data; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
uasort( $wp_plugins, '_sort_uname_callback' ); |
|
138
|
|
|
|
|
139
|
|
|
$cache_plugins[ $plugin_folder ] = $wp_plugins; |
|
140
|
|
|
wp_cache_set( 'monorepo_plugins', $cache_plugins, 'monorepo_plugins' ); // Updated cache values to not conflict. |
|
141
|
|
|
|
|
142
|
|
|
return $wp_plugins; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Returns an array of monorepo plugins. |
|
147
|
|
|
* |
|
148
|
|
|
* @return array Array of monorepo plugins. |
|
149
|
|
|
*/ |
|
150
|
|
|
public function plugins() { |
|
151
|
|
|
return array_keys( $this->get_plugins( $this->plugins ) ); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|