Skip to content

Commit 413e4cf

Browse files
committed
Adjust for compatibility with WordPress Playground
See WordPress/wordpress-playground#1051
1 parent 9ba10d6 commit 413e4cf

File tree

5 files changed

+38
-19
lines changed

5 files changed

+38
-19
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ The Blueprints library is distributed as a .phar library. To build the .phar fil
3434
vendor/bin/box compile
3535
```
3636

37+
Note that in box.json, the `"check-requirements"` option is set to `false`. Somehow, keeping it as `true` results in a
38+
.phar file
39+
that breaks HTTP requests in Playground. @TODO: Investigate why this is the case.
40+
3741
To try the built .phar file, run:
3842

3943
```shell

box.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"compactors": [
55
"KevinGH\\Box\\Compactor\\Php"
66
],
7+
"check-requirements": false,
78
"compression": "GZ",
89
"annotations": false,
910
"directories": [

src/WordPress/Blueprints/Compile/BlueprintCompiler.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ protected function expandShorthandsIntoSteps( Blueprint $blueprint ) {
4444
( new UrlResource() )
4545
->setUrl( $blueprint->WordPressVersion )
4646
);
47-
$additional_steps[] = ( new InstallSqliteIntegrationStep() )
48-
->setSqlitePluginZip(
49-
( new UrlResource() )
50-
->setUrl( 'https://downloads.wordpress.org/plugin/sqlite-database-integration.zip' )
51-
);
52-
$additional_steps[] = ( new WriteFileStep() )
53-
->setPath( 'wp-cli.phar' )
54-
->setData( ( new UrlResource() )->setUrl( 'https://playground.wordpress.net/wp-cli.phar' ) );
55-
$additional_steps[] = ( new RunWordPressInstallerStep() )
56-
->setOptions( new WordPressInstallationOptions() );
47+
// $additional_steps[] = ( new InstallSqliteIntegrationStep() )
48+
// ->setSqlitePluginZip(
49+
// ( new UrlResource() )
50+
// ->setUrl( 'https://downloads.wordpress.org/plugin/sqlite-database-integration.zip' )
51+
// );
52+
// $additional_steps[] = ( new WriteFileStep() )
53+
// ->setPath( 'wp-cli.phar' )
54+
// ->setData( ( new UrlResource() )->setUrl( 'https://playground.wordpress.net/wp-cli.phar' ) );
55+
// $additional_steps[] = ( new RunWordPressInstallerStep() )
56+
// ->setOptions( new WordPressInstallationOptions() );
5757
}
5858
if ( $blueprint->constants ) {
5959
$step = new DefineWpConfigConstsStep();

src/WordPress/Blueprints/Runner/Step/SetSiteOptionsStepRunner.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ function run( SetSiteOptionsStep $input, Tracker $tracker ) {
1717
// with a separate wp-cli command.
1818
return $this->getRuntime()->evalPhpInSubProcess( <<<'CODE'
1919
<?php
20-
require 'wp-load.php';
20+
ini_set('display_errors', '1');
21+
error_reporting(E_ALL);
22+
23+
require getenv('DOCROOT'). '/wp-load.php';
2124
$site_options = getenv("OPTIONS") ? json_decode(getenv("OPTIONS"), true) : [];
25+
var_dump($site_options);
2226
foreach($site_options as $name => $value) {
2327
update_option($name, $value);
2428
}
29+
echo "Done!";
2530
CODE,
2631
[
2732
'OPTIONS' => json_encode( $input->options ),

src/WordPress/DataSource/UrlSource.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function stream( $resourceIdentifier ) {
3737
if ( $this->cache->has( $url ) ) {
3838
// Return a stream resource.
3939
// @TODO: Stream directly from the cache
40-
$cached = $this->cache->get( $url );
40+
$cached = $this->cache->get( $url );
4141
$data_size = strlen( $cached );
4242
$this->events->dispatch( new ProgressEvent(
4343
$url,
@@ -52,26 +52,35 @@ public function stream( $resourceIdentifier ) {
5252
}
5353

5454
$response = $this->client->request( 'GET', $url, [
55-
'on_progress' => function ( int $dlNow, int $dlSize, array $info ) use ( $url ): void {
55+
'on_progress' => function ( int $dlNow, int $dlSize, array $info ) use ( $url ): void {
5656
$this->events->dispatch( new ProgressEvent(
5757
$url,
5858
$dlNow,
5959
$dlSize
6060
) );
6161
},
62+
// @TODO: Only use these unsecure options in in-browser Playground.
63+
// We use a fake SSL server in there to MITM the HTTPS requests
64+
// and funnel them through fetch() – and fetch() handles HTTPS
65+
// security for us.
66+
'verify_host' => false,
67+
'verify_peer' => false,
68+
'crypto_method' => \STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT,
6269
] );
63-
$stream = StreamWrapper::createResource( $response, $this->client );
70+
$stream = StreamWrapper::createResource( $response, $this->client );
6471
if ( ! $stream ) {
6572
throw new \Exception( 'Failed to download file' );
6673
}
6774
$onChunk = function ( $chunk ) use ( $url, $response, $stream ) {
6875
// Handle response caching
6976
// @TODO: don't buffer, just keep appending to the cache.
70-
static $bufferedChunks = [];
71-
$bufferedChunks[] = $chunk;
72-
if ( feof( $stream ) ) {
73-
$this->cache->set( $url, implode( '', $bufferedChunks ) );
74-
}
77+
// Buffering the response causes an out of memory error in the in-browser
78+
// version of Playground
79+
// static $bufferedChunks = [];
80+
// $bufferedChunks[] = $chunk;
81+
// if ( feof( $stream ) ) {
82+
// $this->cache->set( $url, implode( '', $bufferedChunks ) );
83+
// }
7584
};
7685
$onClose = function () use ( $response ) {
7786
$response->cancel();

0 commit comments

Comments
 (0)