Skip to content

Commit 78ef1b2

Browse files
committed
Apply browser-targeted WordPress patches via Blueprints, not in remote.html.
Enables patching arbitrary WordPress bundles for compatibility with web browsers. Without this PR, remote.html applies these patch right after the WordPress module is loaded into the /wordpress VFS directory. However, in #700 we want to enable using an arbitrary WordPress build from any PR in wordpress-develop. We must, thus, expose a way of applying the necessary patches. Testing instructions 1. Start Playground, log out 2. Go to /wp-login.php 3. Confirm there's a message explaining what login credentials to use
1 parent 0290100 commit 78ef1b2

File tree

10 files changed

+75
-384
lines changed

10 files changed

+75
-384
lines changed

packages/playground/blueprints/src/lib/steps/apply-wordpress-patches/index.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ import { StepHandler } from '..';
33
import { updateFile } from '../common';
44
import { defineWpConfigConsts } from '../define-wp-config-consts';
55

6+
/** @ts-ignore */
7+
import transportFetch from './wp-content/mu-plugins/includes/requests_transport_fetch.php?raw';
8+
/** @ts-ignore */
9+
import transportDummy from './wp-content/mu-plugins/includes/requests_transport_dummy.php?raw';
10+
/** @ts-ignore */
11+
import addRequests from './wp-content/mu-plugins/add_requests_transport.php?raw';
12+
/** @ts-ignore */
13+
import showAdminCredentialsOnWpLogin from './wp-content/mu-plugins/1-show-admin-credentials-on-wp-login.php?raw';
14+
/** @ts-ignore */
15+
import niceErrorMessagesForPluginsAndThemesDirectories from './wp-content/mu-plugins/2-nice-error-messages-for-plugins-and-themes-directories.php?raw';
16+
/** @ts-ignore */
17+
import linksTargetingTopFrameShouldTargetPlaygroundIframe from './wp-content/mu-plugins/3-links-targeting-top-frame-should-target-playground-iframe.php?raw';
18+
619
/**
720
* @private
821
*/
@@ -15,6 +28,7 @@ export interface ApplyWordPressPatchesStep {
1528
disableSiteHealth?: boolean;
1629
disableWpNewBlogNotification?: boolean;
1730
makeEditorFrameControlled?: boolean;
31+
prepareForRunningInsideWebBrowser?: boolean;
1832
}
1933

2034
export const applyWordPressPatches: StepHandler<
@@ -47,6 +61,9 @@ export const applyWordPressPatches: StepHandler<
4761
`${patch.wordpressPath}/wp-includes/js/dist/block-editor.min.js`,
4862
]);
4963
}
64+
if (options.prepareForRunningInsideWebBrowser === true) {
65+
await patch.prepareForRunningInsideWebBrowser();
66+
}
5067
};
5168

5269
class WordPressPatcher {
@@ -120,6 +137,63 @@ class WordPressPatcher {
120137
`${contents} function wp_new_blog_notification(...$args){} `
121138
);
122139
}
140+
141+
async prepareForRunningInsideWebBrowser() {
142+
await updateFile(
143+
this.php,
144+
`${this.wordpressPath}/wp-config.php`,
145+
(contents) => `${contents} define('USE_FETCH_FOR_REQUESTS', false);`
146+
);
147+
148+
// Force the fsockopen and cUrl transports to report they don't work:
149+
const transports = [
150+
`${this.wordpressPath}/wp-includes/Requests/Transport/fsockopen.php`,
151+
`${this.wordpressPath}/wp-includes/Requests/Transport/cURL.php`,
152+
];
153+
for (const transport of transports) {
154+
// One of the transports might not exist in the latest WordPress version.
155+
if (!(await this.php.fileExists(transport))) {
156+
continue;
157+
}
158+
await updateFile(this.php, transport, (contents) =>
159+
contents.replace(
160+
'public static function test',
161+
'public static function test( $capabilities = array() ) { return false; } public static function test2'
162+
)
163+
);
164+
}
165+
166+
// Add fetch and dummy transports for HTTP requests
167+
await this.php.mkdirTree(
168+
`${this.wordpressPath}/wp-content/mu-plugins/includes`
169+
);
170+
await this.php.writeFile(
171+
`${this.wordpressPath}/wp-content/mu-plugins/includes/requests_transport_fetch.php`,
172+
transportFetch
173+
);
174+
await this.php.writeFile(
175+
`${this.wordpressPath}/wp-content/mu-plugins/includes/requests_transport_dummy.php`,
176+
transportDummy
177+
);
178+
await this.php.writeFile(
179+
`${this.wordpressPath}/wp-content/mu-plugins/add_requests_transport.php`,
180+
addRequests
181+
);
182+
183+
// Various tweaks
184+
await this.php.writeFile(
185+
`${this.wordpressPath}/wp-content/mu-plugins/1-show-admin-credentials-on-wp-login.php`,
186+
showAdminCredentialsOnWpLogin
187+
);
188+
await this.php.writeFile(
189+
`${this.wordpressPath}/wp-content/mu-plugins/2-nice-error-messages-for-plugins-and-themes-directories.php`,
190+
niceErrorMessagesForPluginsAndThemesDirectories
191+
);
192+
await this.php.writeFile(
193+
`${this.wordpressPath}/wp-content/mu-plugins/3-links-targeting-top-frame-should-target-playground-iframe.php`,
194+
linksTargetingTopFrameShouldTargetPlaygroundIframe
195+
);
196+
}
123197
}
124198

125199
function randomString(length: number) {

packages/playground/compile-wordpress/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ RUN cd wordpress && \
6262
-o -name '*.ttf' -o -name '*.txt' -o -name '*.woff' \
6363
-o -name '*.woff2' -o -name '*.jpeg' -o -name '*.jpg' \
6464
\) \
65-
# Preserve the wp-admin SVG files that are read by PHP
65+
# Preserve the wp-admin SVG files that are read by PHP
6666
-not -path '*/wp-admin/images/*.svg' \
6767
-delete
6868

packages/playground/remote/src/lib/web-wordpress-patches/index.ts

Lines changed: 0 additions & 98 deletions
This file was deleted.

packages/playground/remote/src/lib/web-wordpress-patches/wp-content/mu-plugins/1-show-admin-credentials-on-wp-login.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/playground/remote/src/lib/web-wordpress-patches/wp-content/mu-plugins/2-nice-error-messages-for-plugins-and-themes-directories.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

packages/playground/remote/src/lib/web-wordpress-patches/wp-content/mu-plugins/3-links-targeting-top-frame-should-target-playground-iframe.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

packages/playground/remote/src/lib/web-wordpress-patches/wp-content/mu-plugins/add_requests_transport.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

packages/playground/remote/src/lib/web-wordpress-patches/wp-content/mu-plugins/includes/requests_transport_dummy.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)