-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrait-wp-codebox-abilities-browser-blueprint.php
More file actions
365 lines (319 loc) · 13.1 KB
/
trait-wp-codebox-abilities-browser-blueprint.php
File metadata and controls
365 lines (319 loc) · 13.1 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
/**
* WP_Codebox_Abilities_Browser_Blueprint implementation.
*
* @package WPCodebox
*/
defined( 'ABSPATH' ) || exit;
trait WP_Codebox_Abilities_Browser_Blueprint {
private static function browser_site_blueprint_artifact( array $input ): array|WP_Error {
$artifact = is_array( $input['site_blueprint_artifact'] ?? null ) ? $input['site_blueprint_artifact'] : array();
if ( empty( $artifact ) ) {
return array();
}
$blueprint = $artifact['blueprint'] ?? null;
if ( ! is_array( $blueprint ) ) {
return new WP_Error( 'wp_codebox_site_blueprint_artifact_invalid', 'site_blueprint_artifact.blueprint must be a Playground blueprint object.', array( 'status' => 400 ) );
}
return array(
'schema' => (string) ( $artifact['schema'] ?? 'wp-codebox/site-blueprint-artifact/v1' ),
'id' => (string) ( $artifact['id'] ?? '' ),
'blueprint' => $blueprint,
'provenance' => is_array( $artifact['provenance'] ?? null ) ? $artifact['provenance'] : array(),
);
}
/** @param array<string,mixed> $blueprint Blueprint override. @param array<string,mixed> $site_blueprint_artifact Normalized site blueprint artifact. @return array<string,mixed> */
private static function browser_blueprint_with_site_artifact( array $blueprint, array $site_blueprint_artifact ): array {
$site_blueprint = is_array( $site_blueprint_artifact['blueprint'] ?? null ) ? $site_blueprint_artifact['blueprint'] : array();
if ( empty( $site_blueprint ) ) {
return $blueprint;
}
$site_steps = is_array( $site_blueprint['steps'] ?? null ) ? $site_blueprint['steps'] : array();
$base_steps = is_array( $blueprint['steps'] ?? null ) ? $blueprint['steps'] : array();
$merged = array_merge( $site_blueprint, $blueprint );
$merged['steps'] = array_values( array_merge( $site_steps, $base_steps ) );
if ( isset( $site_blueprint['features'] ) && isset( $blueprint['features'] ) && is_array( $site_blueprint['features'] ) && is_array( $blueprint['features'] ) ) {
$merged['features'] = array_merge( $site_blueprint['features'], $blueprint['features'] );
}
return $merged;
}
/** @param array<string,mixed> $blueprint Blueprint override. @param array<string,mixed> $runtime Runtime dependency specs. @return array<string,mixed> */
private static function browser_blueprint_with_runtime( array $blueprint, array $runtime, array $playground = array() ): array {
$steps = is_array( $blueprint['steps'] ?? null ) ? $blueprint['steps'] : array();
if ( ! self::browser_blueprint_has_login_step( $steps ) ) {
array_unshift(
$steps,
array(
'step' => 'login',
'username' => 'admin',
'password' => 'password',
)
);
}
foreach ( $runtime['plugins'] as $plugin ) {
if ( ! empty( $plugin['local_package'] ) && self::browser_plugin_uses_loopback_url( (string) ( $plugin['url'] ?? '' ) ) ) {
$steps[] = array(
'step' => 'runPHP',
'code' => self::browser_plugin_install_php( $plugin ),
);
continue;
}
$plugin_data = array(
'resource' => (string) ( $plugin['resource'] ?? 'url' ),
'url' => $plugin['url'],
);
if ( 'git:directory' === $plugin_data['resource'] ) {
$plugin_data['ref'] = (string) ( $plugin['ref'] ?? 'main' );
$plugin_data['refType'] = (string) ( $plugin['refType'] ?? 'branch' );
if ( '' !== (string) ( $plugin['path'] ?? '' ) ) {
$plugin_data['path'] = (string) $plugin['path'];
}
}
$options = array(
'activate' => (bool) $plugin['activate'],
);
if ( '' !== (string) ( $plugin['targetFolderName'] ?? '' ) ) {
$options['targetFolderName'] = (string) $plugin['targetFolderName'];
}
$steps[] = array(
'step' => 'installPlugin',
'pluginData' => $plugin_data,
'options' => $options,
);
}
foreach ( $runtime['mu_plugins'] as $mu_plugin ) {
$steps[] = array(
'step' => 'runPHP',
'code' => self::browser_mu_plugin_install_php( $mu_plugin ),
);
}
foreach ( $runtime['themes'] as $theme ) {
if ( ! empty( $theme['url'] ) ) {
$steps[] = array(
'step' => 'installTheme',
'themeData' => array(
'resource' => 'url',
'url' => $theme['url'],
),
'options' => array(
'activate' => (bool) $theme['activate'],
),
);
}
if ( ! empty( $theme['files'] ) ) {
$steps[] = array(
'step' => 'runPHP',
'code' => self::browser_theme_files_install_php( $theme ),
);
}
}
foreach ( $runtime['bootstrap'] as $operation ) {
$steps[] = array(
'step' => 'runPHP',
'code' => self::browser_bootstrap_operation_php( $operation ),
);
}
$blueprint['steps'] = $steps;
if ( ! isset( $blueprint['preferredVersions'] ) ) {
$blueprint['preferredVersions'] = array(
'wp' => (string) ( $playground['wp'] ?? 'latest' ),
'php' => (string) ( $playground['php'] ?? 'latest' ),
);
}
if ( ! isset( $blueprint['features'] ) ) {
$blueprint['features'] = array( 'networking' => true );
}
return $blueprint;
}
/** @param array<string,mixed> $plugin Plugin spec. */
private static function browser_plugin_install_php( array $plugin ): string {
$target_folder = sanitize_key( (string) ( $plugin['targetFolderName'] ?? $plugin['slug'] ?? '' ) );
if ( '' === $target_folder ) {
$target_folder = 'wp-codebox-runtime-plugin';
}
$package_url = (string) ( $plugin['url'] ?? '' );
return '<?php
$package_url = ' . var_export( $package_url, true ) . ';
$expected_sha256 = ' . var_export( (string) ( $plugin['sha256'] ?? '' ), true ) . ';
$target_folder = ' . var_export( $target_folder, true ) . ';
$activate = ' . ( ! empty( $plugin['activate'] ) ? 'true' : 'false' ) . ';
$archive = str_starts_with( $package_url, "data:application/zip;base64," )
? base64_decode( substr( $package_url, strlen( "data:application/zip;base64," ) ), true )
: file_get_contents( $package_url );
if ( ! is_string( $archive ) || "" === $archive ) {
throw new RuntimeException( "Could not read browser plugin package." );
}
if ( "" !== $expected_sha256 && ! hash_equals( $expected_sha256, hash( "sha256", $archive ) ) ) {
throw new RuntimeException( "Browser plugin package hash mismatch." );
}
$tmp_zip = tempnam( sys_get_temp_dir(), "wp-codebox-plugin-" );
if ( false === $tmp_zip || false === file_put_contents( $tmp_zip, $archive ) ) {
throw new RuntimeException( "Could not stage browser plugin package." );
}
$zip = new ZipArchive();
if ( true !== $zip->open( $tmp_zip ) ) {
@unlink( $tmp_zip );
throw new RuntimeException( "Could not open browser plugin package." );
}
$plugins_directory = "/wordpress/wp-content/plugins";
if ( ! is_dir( $plugins_directory ) ) {
mkdir( $plugins_directory, 0777, true );
}
$zip->extractTo( $plugins_directory );
$zip->close();
@unlink( $tmp_zip );
if ( $activate ) {
require_once "/wordpress/wp-load.php";
require_once ABSPATH . "wp-admin/includes/plugin.php";
$plugins = get_plugins( "/" . $target_folder );
$plugin_file = "";
foreach ( array_keys( $plugins ) as $file ) {
$plugin_file = $target_folder . "/" . $file;
break;
}
if ( "" === $plugin_file ) {
throw new RuntimeException( "Browser plugin package entry file is missing." );
}
$result = activate_plugin( $plugin_file );
if ( is_wp_error( $result ) ) {
throw new RuntimeException( $result->get_error_message() );
}
}
';
}
/** @param array<string,mixed> $mu_plugin Mu-plugin spec. */
private static function browser_mu_plugin_install_php( array $mu_plugin ): string {
if ( ! empty( $mu_plugin['local_package'] ) ) {
return self::browser_packaged_mu_plugin_install_php( $mu_plugin );
}
return '<?php
$path = ' . var_export( $mu_plugin['path'], true ) . ';
$directory = dirname( $path );
if ( ! is_dir( $directory ) ) {
mkdir( $directory, 0777, true );
}
file_put_contents( $path, ' . var_export( $mu_plugin['content'], true ) . ' );
';
}
/** @param array<string,mixed> $mu_plugin Packaged mu-plugin spec. */
private static function browser_packaged_mu_plugin_install_php( array $mu_plugin ): string {
$package_url = (string) ( $mu_plugin['url'] ?? '' );
return '<?php
$package_url = ' . var_export( $package_url, true ) . ';
$expected_sha256 = ' . var_export( (string) ( $mu_plugin['sha256'] ?? '' ), true ) . ';
$target_directory = "/wordpress/wp-content/mu-plugins/" . ' . var_export( (string) $mu_plugin['targetFolderName'], true ) . ';
$loader_path = ' . var_export( (string) $mu_plugin['path'], true ) . ';
$entry = ' . var_export( (string) $mu_plugin['entry'], true ) . ';
$archive = str_starts_with( $package_url, "data:application/zip;base64," )
? base64_decode( substr( $package_url, strlen( "data:application/zip;base64," ) ), true )
: file_get_contents( $package_url );
if ( ! is_string( $archive ) || "" === $archive ) {
throw new RuntimeException( "Could not read browser mu-plugin package." );
}
if ( "" !== $expected_sha256 && ! hash_equals( $expected_sha256, hash( "sha256", $archive ) ) ) {
throw new RuntimeException( "Browser mu-plugin package hash mismatch." );
}
$tmp_zip = tempnam( sys_get_temp_dir(), "wp-codebox-mu-plugin-" );
if ( false === $tmp_zip || false === file_put_contents( $tmp_zip, $archive ) ) {
throw new RuntimeException( "Could not stage browser mu-plugin package." );
}
$zip = new ZipArchive();
if ( true !== $zip->open( $tmp_zip ) ) {
@unlink( $tmp_zip );
throw new RuntimeException( "Could not open browser mu-plugin package." );
}
if ( ! is_dir( $target_directory ) ) {
mkdir( $target_directory, 0777, true );
}
$zip->extractTo( dirname( $target_directory ) );
$zip->close();
@unlink( $tmp_zip );
$entry_path = $target_directory . "/" . $entry;
if ( ! is_readable( $entry_path ) ) {
throw new RuntimeException( "Browser mu-plugin package entry file is missing." );
}
$loader_directory = dirname( $loader_path );
if ( ! is_dir( $loader_directory ) ) {
mkdir( $loader_directory, 0777, true );
}
file_put_contents( $loader_path, "<?php\nrequire_once " . var_export( $entry_path, true ) . ";\n" );
';
}
/** @param array<string,mixed> $theme Theme spec. */
private static function browser_theme_files_install_php( array $theme ): string {
$files = array();
foreach ( $theme['files'] as $file ) {
$files[ $file['playground_path'] ] = $file['content'];
}
return '<?php
$files = ' . var_export( $files, true ) . ';
foreach ( $files as $path => $content ) {
$directory = dirname( $path );
if ( ! is_dir( $directory ) ) {
mkdir( $directory, 0777, true );
}
file_put_contents( $path, $content );
}
' . ( (bool) $theme['activate'] ? self::browser_theme_activation_php( (string) $theme['slug'] ) : '' ) . '
';
}
private static function browser_theme_activation_php( string $slug ): string {
return self::browser_wordpress_bootstrap_php() . '
if ( ! function_exists( \'switch_theme\' ) ) {
require_once ABSPATH . WPINC . \'/theme.php\';
}
switch_theme( ' . var_export( $slug, true ) . ' );';
}
private static function browser_wordpress_bootstrap_php(): string {
return 'if ( ! defined( \'ABSPATH\' ) ) {
require_once \'/wordpress/wp-load.php\';
}';
}
/** @param array<string,mixed> $operation Bootstrap operation spec. */
private static function browser_bootstrap_operation_php( array $operation ): string {
$args = is_array( $operation['args'] ?? null ) ? $operation['args'] : array();
switch ( $operation['operation'] ) {
case 'set_option':
return '<?php
' . self::browser_wordpress_bootstrap_php() . '
update_option( ' . var_export( (string) ( $args['name'] ?? $args['option'] ?? '' ), true ) . ', ' . var_export( $args['value'] ?? '', true ) . ' );
';
case 'activate_plugin':
return '<?php
' . self::browser_wordpress_bootstrap_php() . '
activate_plugin( ' . var_export( (string) ( $args['plugin'] ?? '' ), true ) . ' );
';
case 'activate_theme':
return '<?php
' . self::browser_theme_activation_php( (string) ( $args['slug'] ?? $args['theme'] ?? '' ) ) . '
';
case 'flush_rewrite_rules':
return '<?php
' . self::browser_wordpress_bootstrap_php() . '
flush_rewrite_rules();
';
}
return '<?php';
}
/** @param array<string,mixed> $runtime Runtime dependency specs. @return array<string,mixed> */
private static function browser_runtime_readiness_metadata( array $runtime ): array {
return array(
'schema' => 'wp-codebox/browser-runtime-readiness/v1',
'compiled' => true,
'summary' => $runtime['summary'] ?? array(),
'plugins' => array_map( static fn( array $plugin ): array => array( 'slug' => $plugin['slug'] ?? '', 'activate' => (bool) ( $plugin['activate'] ?? true ), 'readiness' => 'compiled' ), $runtime['plugins'] ?? array() ),
'mu_plugins' => array_map( static fn( array $mu_plugin ): array => array( 'slug' => $mu_plugin['slug'] ?? '', 'file' => $mu_plugin['file'] ?? '', 'readiness' => $mu_plugin['readiness'] ?? 'compiled' ), $runtime['mu_plugins'] ?? array() ),
'themes' => array_map( static fn( array $theme ): array => array( 'slug' => $theme['slug'] ?? '', 'activate' => (bool) ( $theme['activate'] ?? true ), 'readiness' => $theme['readiness'] ?? 'compiled' ), $runtime['themes'] ?? array() ),
'bootstrap' => array_map( static fn( array $operation ): array => array( 'operation' => $operation['operation'] ?? '', 'readiness' => $operation['readiness'] ?? 'compiled' ), $runtime['bootstrap'] ?? array() ),
);
}
/** @param array<int,mixed> $steps Blueprint steps. */
private static function browser_blueprint_has_login_step( array $steps ): bool {
foreach ( $steps as $step ) {
if ( is_array( $step ) && 'login' === (string) ( $step['step'] ?? '' ) ) {
return true;
}
}
return false;
}
}