Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for PHP->JS->PHP data transfer by returning value from post_message_to_js() #732

Merged
merged 1 commit into from
Nov 6, 2023

Conversation

adamziel
Copy link
Collaborator

@adamziel adamziel commented Nov 6, 2023

Description

A part of #724

post_message_to_js passes a message to the JavaScript module where it can be handled inside onMessage(). This commit adds support for returning values and promises inside that handler to feed them back to PHP. For example:

$response = post_message_to_js(json_encode($request_data));
	await playground.onMessage(async (message: string) => {
		const requestData = JSON.parse(message);
		const response = await fetch(requestData);
		return JSON.stringify(toSimpleObject(response));
	});

A handler like above could be used to add support for network requests, which is in fact what #724 does

@adamziel adamziel force-pushed the add/return-value-from-post-message branch from 3244671 to c59004d Compare November 6, 2023 21:14
@adamziel adamziel marked this pull request as ready for review November 6, 2023 21:15
@adamziel adamziel changed the title Support for returning data from post_message_to_js() Support for PHP->JS->PHP data transfer by returning value from post_message_to_js() Nov 6, 2023
@adamziel
Copy link
Collaborator Author

adamziel commented Nov 6, 2023

I'll go ahead and merge this feature alone without merging support for network requests.

@adamziel adamziel merged commit dbad2aa into trunk Nov 6, 2023
4 checks passed
@adamziel adamziel deleted the add/return-value-from-post-message branch November 6, 2023 21:21
adamziel added a commit that referenced this pull request Nov 8, 2023
## Description

This pull request introduces network support to the WordPress Playground
via the JavaScript fetch() API. By adding this support, the playground
will have the ability to execute HTTP requests, paving the way for
enhanced interactions with various APIs and services.

The implementation involves creating a custom transport that delegates
HTTP requests to the fetch() API. This custom transport handles network
requests, thus enabling communication outside the local environment.

Local testing will be a bit challenging until I deploy the updated
plugin-proxy.php to playground.wordpress.net

<img width="1241" alt="CleanShot 2023-10-26 at 00 29 07@2x"
src="https://github.com/WordPress/wordpress-playground/assets/205419/72ae0da5-daa1-4934-9c1f-774524a0ff5c">

## How does it work?

A new Requests transport for WordPress asks JavaScript to run fetch()
via `post_message_to_js()`. JavaScript runs the request and returns the
response ľ see
#732 for more
details of that mechanism

## Testing Instructions:

Testing this feature locally might be challenging until the updated
plugin-proxy.php is deployed to the playground environment. Here are
some basic steps to test the changes:

1. Open local Playground
2. Set up an mu-plugin that calls `wp_safe_remote_get()` to domain not
on this list: ['api.wordpress.org', 'w.org', 's.w.org']
3. Ensure that the requests are handled correctly and the expected
responses are received.
4. Test various types of requests (GET, POST, etc.) and verify the
correct response.
5. Check the functionality with different APIs (e.g., REST API, external
APIs).

cc @akirk @dmsnell @ellatrix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant