Skip to content

Commit

Permalink
Apply browser-targeted WordPress patches via Blueprints, not in remot…
Browse files Browse the repository at this point in the history
…e.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
  • Loading branch information
adamziel committed Oct 16, 2023
1 parent 0290100 commit 78ef1b2
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 384 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ import { StepHandler } from '..';
import { updateFile } from '../common';
import { defineWpConfigConsts } from '../define-wp-config-consts';

/** @ts-ignore */
import transportFetch from './wp-content/mu-plugins/includes/requests_transport_fetch.php?raw';
/** @ts-ignore */
import transportDummy from './wp-content/mu-plugins/includes/requests_transport_dummy.php?raw';
/** @ts-ignore */
import addRequests from './wp-content/mu-plugins/add_requests_transport.php?raw';
/** @ts-ignore */
import showAdminCredentialsOnWpLogin from './wp-content/mu-plugins/1-show-admin-credentials-on-wp-login.php?raw';
/** @ts-ignore */
import niceErrorMessagesForPluginsAndThemesDirectories from './wp-content/mu-plugins/2-nice-error-messages-for-plugins-and-themes-directories.php?raw';
/** @ts-ignore */
import linksTargetingTopFrameShouldTargetPlaygroundIframe from './wp-content/mu-plugins/3-links-targeting-top-frame-should-target-playground-iframe.php?raw';

/**
* @private
*/
Expand All @@ -15,6 +28,7 @@ export interface ApplyWordPressPatchesStep {
disableSiteHealth?: boolean;
disableWpNewBlogNotification?: boolean;
makeEditorFrameControlled?: boolean;
prepareForRunningInsideWebBrowser?: boolean;
}

export const applyWordPressPatches: StepHandler<
Expand Down Expand Up @@ -47,6 +61,9 @@ export const applyWordPressPatches: StepHandler<
`${patch.wordpressPath}/wp-includes/js/dist/block-editor.min.js`,
]);
}
if (options.prepareForRunningInsideWebBrowser === true) {
await patch.prepareForRunningInsideWebBrowser();
}
};

class WordPressPatcher {
Expand Down Expand Up @@ -120,6 +137,63 @@ class WordPressPatcher {
`${contents} function wp_new_blog_notification(...$args){} `
);
}

async prepareForRunningInsideWebBrowser() {
await updateFile(
this.php,
`${this.wordpressPath}/wp-config.php`,
(contents) => `${contents} define('USE_FETCH_FOR_REQUESTS', false);`
);

// Force the fsockopen and cUrl transports to report they don't work:
const transports = [
`${this.wordpressPath}/wp-includes/Requests/Transport/fsockopen.php`,
`${this.wordpressPath}/wp-includes/Requests/Transport/cURL.php`,
];
for (const transport of transports) {
// One of the transports might not exist in the latest WordPress version.
if (!(await this.php.fileExists(transport))) {
continue;
}
await updateFile(this.php, transport, (contents) =>
contents.replace(
'public static function test',
'public static function test( $capabilities = array() ) { return false; } public static function test2'
)
);
}

// Add fetch and dummy transports for HTTP requests
await this.php.mkdirTree(
`${this.wordpressPath}/wp-content/mu-plugins/includes`
);
await this.php.writeFile(
`${this.wordpressPath}/wp-content/mu-plugins/includes/requests_transport_fetch.php`,
transportFetch
);
await this.php.writeFile(
`${this.wordpressPath}/wp-content/mu-plugins/includes/requests_transport_dummy.php`,
transportDummy
);
await this.php.writeFile(
`${this.wordpressPath}/wp-content/mu-plugins/add_requests_transport.php`,
addRequests
);

// Various tweaks
await this.php.writeFile(
`${this.wordpressPath}/wp-content/mu-plugins/1-show-admin-credentials-on-wp-login.php`,
showAdminCredentialsOnWpLogin
);
await this.php.writeFile(
`${this.wordpressPath}/wp-content/mu-plugins/2-nice-error-messages-for-plugins-and-themes-directories.php`,
niceErrorMessagesForPluginsAndThemesDirectories
);
await this.php.writeFile(
`${this.wordpressPath}/wp-content/mu-plugins/3-links-targeting-top-frame-should-target-playground-iframe.php`,
linksTargetingTopFrameShouldTargetPlaygroundIframe
);
}
}

function randomString(length: number) {
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/compile-wordpress/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ RUN cd wordpress && \
-o -name '*.ttf' -o -name '*.txt' -o -name '*.woff' \
-o -name '*.woff2' -o -name '*.jpeg' -o -name '*.jpg' \
\) \
# Preserve the wp-admin SVG files that are read by PHP
# Preserve the wp-admin SVG files that are read by PHP
-not -path '*/wp-admin/images/*.svg' \
-delete

Expand Down
98 changes: 0 additions & 98 deletions packages/playground/remote/src/lib/web-wordpress-patches/index.ts

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 78ef1b2

Please sign in to comment.