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

Send WordPress API requests directly. #933

Merged
merged 5 commits into from
Jan 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,47 @@ export async function setupFetchNetworkTransport(playground: UniversalPHP) {
return '';
}

// PHP encodes empty arrays as JSON arrays, not objects.
// We can't easily reason about the request body, but we know
// headers should be an object so let's convert it here.
if (Array.isArray(data.headers)) {
data.headers = Object.fromEntries(data.headers);
}

return handleRequest(data);
});
}

export async function handleRequest(data: RequestData, fetchFn = fetch) {
const hostname = new URL(data.url).hostname;
const fetchUrl = ['api.wordpress.org', 'w.org', 's.w.org'].includes(
hostname
)
const fetchUrl = ['w.org', 's.w.org'].includes(hostname)
? `/plugin-proxy.php?url=${encodeURIComponent(data.url)}`
: data.url;

let response;
try {
const fetchMethod = data.method || 'GET';
const fetchHeaders = data.headers || {};
if (fetchMethod == 'POST') {
fetchHeaders['Content-Type'] = 'application/x-www-form-urlencoded';

/**
* Removes a few custom request headers.
*
* This is required because the fetch API will send a CORS preflight
* request if the request is cross-origin and has custom headers.
*
* However, the api.wordpress.org/core/version-check/1.7/ endpoint
* doesn't support CORS preflight requests. These two headers
* aren't critical to the request, so we can just remove them.
*/
delete fetchHeaders['wp_install'];
delete fetchHeaders['wp_blog'];
}

response = await fetchFn(fetchUrl, {
method: data.method || 'GET',
headers: data.headers,
method: fetchMethod,
headers: fetchHeaders,
body: data.data,
credentials: 'omit',
});
Expand Down
Loading