Skip to content

Commit

Permalink
Fix: Networking broken when extra PHP extensions are enabled
Browse files Browse the repository at this point in the history
The recent [removal of custom WordPress
patches](#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
  • Loading branch information
adamziel committed Feb 22, 2024
1 parent 1467766 commit cc6ca45
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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');

/**
Expand All @@ -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');
Expand Down
9 changes: 9 additions & 0 deletions packages/playground/website/cypress/e2e/query-api.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cc6ca45

Please sign in to comment.