From cc6ca45abd1dd4579212cb716d797bd66c3093f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Thu, 22 Feb 2024 16:31:13 +0100 Subject: [PATCH 1/2] Fix: Networking broken when extra PHP extensions are enabled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The recent [removal of custom WordPress patches](https://github.com/WordPress/wordpress-playground/pull/1004) broke the networking feature when the extra PHP extensions are enabled. It removed the default networking tranports via the http_api_transports filter, but it did not account for WordPress trying to use the Fetch and Fsockopen transports anyway whenever their `::test()` method returns true – which it does when the OpenSSL extension is available. This PR uses the reflections API to force-replace the Requests::$transports array with one containing exclusively the Fetch transport. Open Playground, enable PHP extensions and networking, go to WP Admin > Plugins > Add, confirm the list of plugins is visible CC @bgrgicak @annezazu --- .../lib/playground-mu-plugin/0-playground.php | 19 ++++++++++++++++--- .../website/cypress/e2e/query-api.cy.ts | 9 +++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/packages/playground/remote/src/lib/playground-mu-plugin/0-playground.php b/packages/playground/remote/src/lib/playground-mu-plugin/0-playground.php index 8a8619dec3..14cd09a47f 100644 --- a/packages/playground/remote/src/lib/playground-mu-plugin/0-playground.php +++ b/packages/playground/remote/src/lib/playground-mu-plugin/0-playground.php @@ -141,6 +141,18 @@ function networking_disabled() { $__requests_class = class_exists( '\WpOrg\Requests\Requests' ) ? '\WpOrg\Requests\Requests' : 'Requests'; if (defined('USE_FETCH_FOR_REQUESTS') && USE_FETCH_FOR_REQUESTS) { require(__DIR__ . '/playground-includes/wp_http_fetch.php'); + // Force-replace the default WordPress requests transports with the Fetch transport. + // + // WordPress doesn't provide a way to change the default transports, + // that is Curl and FSockopen. Even with all the `http_api_tranports` + // filter used below, WordPress tests if they are supported and will + // use them if their `::test()` method returns true – which it does + // when PHP.wasm runs with the openssl extension loaded. + $reflection = new ReflectionClass($__requests_class); + $property = $reflection->getProperty('transports'); + $property->setAccessible(true); + $property->setValue(['Fetch' => 'Wp_Http_Fetch']); + $__requests_class::add_transport('Wp_Http_Fetch'); /** @@ -164,9 +176,10 @@ function networking_disabled() { return []; }); - add_filter('http_request_host_is_external', function ($arg) { - return true; - }); + // add_filter('http_request_host_is_external', function ($arg) { + // return true; + // }); + add_filter('http_request_host_is_external', '__return_true'); } else { require(__DIR__ . '/playground-includes/wp_http_dummy.php'); $__requests_class::add_transport('Wp_Http_Dummy'); diff --git a/packages/playground/website/cypress/e2e/query-api.cy.ts b/packages/playground/website/cypress/e2e/query-api.cy.ts index e5bb7ddfb5..d96c87fe0d 100644 --- a/packages/playground/website/cypress/e2e/query-api.cy.ts +++ b/packages/playground/website/cypress/e2e/query-api.cy.ts @@ -69,6 +69,15 @@ describe('Query API', () => { .should('have.length.above', 4); }); + it('should enable networking when requested AND the kitchen sink extension bundle is enabled', () => { + cy.visit( + '/?php-extension-bundle=kitchen-sink&networking=yes&url=/wp-admin/plugin-install.php' + ); + cy.wordPressDocument() + .find('.plugin-card') + .should('have.length.above', 4); + }); + /** * @see https://github.com/WordPress/wordpress-playground/pull/819 * @TODO: Turn this into a unit test once WordPress modules are available From 51d05898cdd56ec758d3d93842d493eba1d31435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Thu, 22 Feb 2024 16:36:07 +0100 Subject: [PATCH 2/2] Reference the PR --- .../remote/src/lib/playground-mu-plugin/0-playground.php | 2 ++ packages/playground/website/cypress/e2e/query-api.cy.ts | 3 +++ 2 files changed, 5 insertions(+) diff --git a/packages/playground/remote/src/lib/playground-mu-plugin/0-playground.php b/packages/playground/remote/src/lib/playground-mu-plugin/0-playground.php index 14cd09a47f..9300ac7b3b 100644 --- a/packages/playground/remote/src/lib/playground-mu-plugin/0-playground.php +++ b/packages/playground/remote/src/lib/playground-mu-plugin/0-playground.php @@ -148,6 +148,8 @@ function networking_disabled() { // filter used below, WordPress tests if they are supported and will // use them if their `::test()` method returns true – which it does // when PHP.wasm runs with the openssl extension loaded. + // + // @see https://github.com/WordPress/wordpress-playground/pull/1045 $reflection = new ReflectionClass($__requests_class); $property = $reflection->getProperty('transports'); $property->setAccessible(true); diff --git a/packages/playground/website/cypress/e2e/query-api.cy.ts b/packages/playground/website/cypress/e2e/query-api.cy.ts index d96c87fe0d..550b606d4c 100644 --- a/packages/playground/website/cypress/e2e/query-api.cy.ts +++ b/packages/playground/website/cypress/e2e/query-api.cy.ts @@ -69,6 +69,9 @@ describe('Query API', () => { .should('have.length.above', 4); }); + /** + * @see https://github.com/WordPress/wordpress-playground/pull/1045 + */ it('should enable networking when requested AND the kitchen sink extension bundle is enabled', () => { cy.visit( '/?php-extension-bundle=kitchen-sink&networking=yes&url=/wp-admin/plugin-install.php'