|
| 1 | +import { startPlaygroundWeb } from '@wp-playground/client'; |
| 2 | +import { getRemoteUrl } from '../src/lib/config'; |
| 3 | +import { joinPaths } from '@php-wasm/util'; |
| 4 | +export {}; |
| 5 | + |
| 6 | +const iframe = document.querySelector('iframe')!; |
| 7 | +const playground = await startPlaygroundWeb({ |
| 8 | + iframe, |
| 9 | + remoteUrl: getRemoteUrl().toString(), |
| 10 | + // Blueprint v1, implemented in TypeScript: |
| 11 | + blueprint: { |
| 12 | + preferredVersions: { |
| 13 | + wp: 'latest', |
| 14 | + // Required for the PHP library to run: |
| 15 | + php: '8.2', |
| 16 | + }, |
| 17 | + features: { |
| 18 | + networking: true, |
| 19 | + }, |
| 20 | + // landingPage: '/wp-content/index.php', |
| 21 | + landingPage: '/', |
| 22 | + // Required for the PHP library to run: |
| 23 | + phpExtensionBundles: ['kitchen-sink'], |
| 24 | + }, |
| 25 | +}); |
| 26 | + |
| 27 | +const response = await fetch('./blueprints.phar'); |
| 28 | +const phar = new Uint8Array(await response.arrayBuffer()); |
| 29 | +await playground.writeFile( |
| 30 | + joinPaths(await playground.documentRoot, 'blueprints.phar'), |
| 31 | + phar |
| 32 | +); |
| 33 | +const outputDiv = document.getElementById('output')!; |
| 34 | + |
| 35 | +try { |
| 36 | + const wpCliRequest = fetch( |
| 37 | + 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar' |
| 38 | + ); |
| 39 | + const wpCliResponse = await wpCliRequest; |
| 40 | + const wpCli = await wpCliResponse.arrayBuffer(); |
| 41 | + await playground.writeFile('/wordpress/wp-cli.phar', new Uint8Array(wpCli)); |
| 42 | + |
| 43 | + // Blueprint v2, implemented in PHP. The PHP builder is not required. It only |
| 44 | + // produces a JSON document that is then used to run the Blueprint. |
| 45 | + const result = await playground.run({ |
| 46 | + code: `<?php |
| 47 | + use WordPress\\Blueprints\\Model\\DataClass\\Blueprint; |
| 48 | + use WordPress\\Blueprints\\Model\\BlueprintBuilder; |
| 49 | + use WordPress\\Blueprints\\Model\\DataClass\\UrlResource; |
| 50 | + use function WordPress\\Blueprints\\run_blueprint; |
| 51 | +
|
| 52 | + // Provide stdin, stdout, stderr streams outside of |
| 53 | + // the CLI SAPI. |
| 54 | + define('STDIN', fopen('php://stdin', 'rb')); |
| 55 | + define('STDOUT', fopen('php://stdout', 'wb')); |
| 56 | + define('STDERR', fopen('/tmp/stderr', 'wb')); |
| 57 | +
|
| 58 | + /* |
| 59 | + * When the .phar file is build with this box option: |
| 60 | + * > "check-requirements": false, |
| 61 | + * Then requiring it breaks http and https requests: |
| 62 | + * |
| 63 | + * > echo file_get_contents('http://localhost:5400/website-server/'); |
| 64 | + * > <b>Warning</b>: PHP Request Startup: Failed to open stream: Operation timed out in <b>php-wasm run script</b> on line <b>13</b><br /> |
| 65 | + * |
| 66 | + * The check is therefore disabled for now. |
| 67 | + */ |
| 68 | + require '/wordpress/blueprints.phar'; |
| 69 | + |
| 70 | + $blueprint = BlueprintBuilder::create() |
| 71 | + // This isn't a WordPress zip file since wordpress.org |
| 72 | + // doesn't expose the right CORS headers. It is a HTTPS-hosted |
| 73 | + // zip file nonetheless, and we can use it for testing. |
| 74 | + // Uncomment this as needed |
| 75 | + // ->setWordPressVersion( 'https://downloads.wordpress.org/plugin/hello-dolly.1.7.3.zip' ) |
| 76 | +
|
| 77 | + ->withFile( 'wordpress.txt', (new UrlResource())->setUrl('https://downloads.wordpress.org/plugin/hello-dolly.zip') ) |
| 78 | + ->withSiteOptions( [ |
| 79 | + 'blogname' => 'My Playground Blog', |
| 80 | + ] ) |
| 81 | + ->withWpConfigConstants( [ |
| 82 | + 'WP_DEBUG' => true, |
| 83 | + 'WP_DEBUG_LOG' => true, |
| 84 | + 'WP_DEBUG_DISPLAY' => true, |
| 85 | + 'WP_CACHE' => true, |
| 86 | + ] ) |
| 87 | + ->withPlugins( [ |
| 88 | + 'https://downloads.wordpress.org/plugin/hello-dolly.zip', |
| 89 | + // When the regular UrlDataSource is used, the second |
| 90 | + // downloaded zip file always errors with: |
| 91 | + // > Failed to open stream: Operation timed out |
| 92 | + 'https://downloads.wordpress.org/plugin/classic-editor.zip', |
| 93 | + 'https://downloads.wordpress.org/plugin/gutenberg.17.7.0.zip', |
| 94 | + ] ) |
| 95 | + ->withTheme( 'https://downloads.wordpress.org/theme/pendant.zip' ) |
| 96 | + ->withContent( 'https://raw.githubusercontent.com/WordPress/theme-test-data/master/themeunittestdata.wordpress.xml' ) |
| 97 | + ->andRunSQL( <<<'SQL' |
| 98 | + CREATE TABLE tmp_table ( id INT ); |
| 99 | + INSERT INTO tmp_table VALUES (1); |
| 100 | + INSERT INTO tmp_table VALUES (2); |
| 101 | + SQL |
| 102 | + ) |
| 103 | + ->withFile( 'wordpress.txt', 'Data' ) |
| 104 | + ->toBlueprint() |
| 105 | + ; |
| 106 | + |
| 107 | + echo "Running the following Blueprint:\n"; |
| 108 | + echo json_encode($blueprint, JSON_PRETTY_PRINT)."\n\n"; |
| 109 | + $results = run_blueprint( $blueprint, '/wordpress' ); |
| 110 | + echo "Blueprint execution finished!\n"; |
| 111 | + echo "Contents of /wordpress/wp-content/plugins:"; |
| 112 | + print_r(glob('/wordpress/wp-content/plugins/*')); |
| 113 | + `, |
| 114 | + throwOnError: true, |
| 115 | + }); |
| 116 | + |
| 117 | + outputDiv.textContent = result.text; |
| 118 | + console.log(result.text); |
| 119 | +} catch (e) { |
| 120 | + console.error(e); |
| 121 | + outputDiv.textContent = e + ''; |
| 122 | + throw e; |
| 123 | +} |
| 124 | + |
| 125 | +console.log(await playground.listFiles('/wordpress/wp-content/plugins')); |
0 commit comments