Skip to content

Commit 30d1c77

Browse files
committed
Simplify legacy-wp/ patches and trim verbose comments
Reviewed every patch in legacy-wp/legacy-fixes.ts (and supporting files) for need, gating tightness, and comment clarity. Net: 4828 → 3672 lines. Verified WP 1.0–4.9 + PHP 5.2 still boot end-to-end. Notable changes: - Drop ensureLegacyAdminAuth — duplicated the auto-login flow already provided by legacy-mu-plugins.ts. - Drop the wp_check_mysql_version no-op — mysql-shims.ts already returns '8.0.0' from mysql_get_server_info. - Drop the dead overrides array in patchWpAdminRelativePaths — the generic regex pass already covers every entry. - Drop duplicate mysql_/mysqli_/str_* stubs from generateDbPhpContent — the 0-sqlite.php preload runs first and already defines them. - Share legacyAuthCookieBlock(usernameExpr) between patchAdminAuthRedirect and patchAdminAjaxAuth; replace their brittle wp-settings.php text scans with readOnDiskWpVersion gates. - Add explicit version gates to patchLegacyWpCategoriesZeroPk, patchWpSchemaPhp, patchWp47ThemeSearchForms, and patchAdminNetworkCalls instead of needle-only / file-existence heuristics. - Replace the fragile one-level-nesting regex in patchCheckAdminReferer with a reusable balanced-brace helper (replacePhpFunctionBody), reused by the "not installed" die() and the do_action('init') rewrite extracted out of patchWpSettingsPhp. - Fix patchWpSettingsPhp bug where settingsChanged was set unconditionally inside the error_reporting block, rewriting wp-settings on every boot. - Split the PHP 5.2 vs 5.3+ error-handler boilerplate in legacy-mu-plugins.ts into named helpers instead of inline ternaries. - Trim multi-paragraph "## The bug / ## The fix" docblocks to a single why-sentence each, dropping embedded WP source samples and indentation diagrams.
1 parent 537d860 commit 30d1c77

File tree

5 files changed

+779
-1935
lines changed

5 files changed

+779
-1935
lines changed

packages/playground/wordpress/src/legacy-wp/legacy-boot.ts

Lines changed: 27 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ import {
3131
} from './legacy-fixes';
3232

3333
/**
34-
* Network I/O functions that must be disabled on legacy PHP builds
35-
* (< 7) to avoid "null function or function signature mismatch"
36-
* WASM crashes when WordPress calls fsockopen or cURL during cron,
37-
* update checks, dashboard RSS widgets, etc.
34+
* Network I/O functions disabled on legacy PHP (< 7) to keep
35+
* fsockopen/cURL calls (cron, update checks, dashboard RSS) from
36+
* tripping "null function or function signature mismatch" WASM
37+
* crashes — let them fail safely instead.
3838
*/
3939
const LEGACY_PHP_DISABLED_NETWORK_FUNCTIONS = [
4040
'fsockopen',
@@ -45,21 +45,9 @@ const LEGACY_PHP_DISABLED_NETWORK_FUNCTIONS = [
4545
] as const;
4646

4747
/**
48-
* Disables network I/O on legacy PHP (< 7) and merges the legacy
49-
* disable_functions list with any caller-supplied list.
50-
*
51-
* No-op on modern PHP — called unconditionally from bootRequestHandler.
52-
*
53-
* Old WordPress (2.5–3.6) calls fsockopen/cURL during cron, update
54-
* checks, and dashboard RSS widgets. The underlying socket/cURL
55-
* operations trigger "null function or function signature mismatch"
56-
* WASM errors; disabling them makes the calls fail safely (return
57-
* false) instead of crashing.
58-
*
59-
* setPhpIniEntries overwrites keys, so we merge with whatever the
60-
* caller already passed in `phpIniEntries` — otherwise a
61-
* networking-disabled list from the web worker would be silently
62-
* replaced by this legacy-only list.
48+
* Merges the legacy network disable list into php.ini for legacy PHP
49+
* (no-op on modern). Merge instead of overwrite so a caller-supplied
50+
* disable_functions list is preserved.
6351
*/
6452
export function applyLegacyPhpIniOverrides(
6553
php: PHP,
@@ -220,17 +208,12 @@ async function installLegacyWordPress(
220208
* catches the throw and proceeds to post-install fixups regardless.
221209
*/
222210
async function runLegacyInstaller(php: PHP): Promise<void> {
223-
// WP 1.0–3.0 on legacy PHP: skip the install.php HTTP request
224-
// entirely. These old installers trigger various unreachable
225-
// WASM traps (mail(), mysql_get_server_info(), etc.) that the
226-
// PHP 5.2 binary can't handle. The post-install PDO fallback
227-
// creates all tables, users, options, and content without
228-
// running any crashable PHP.
229-
//
230-
// WP 1.0–1.2: the post-install PDO fallback creates the very
231-
// simple schema entirely.
232-
// WP 1.5–3.0: needs dbDelta() for proper table schemas but
233-
// skips the rest of the installer.
211+
// WP 1.0–3.0 installers trigger WASM traps (mail(),
212+
// mysql_get_server_info(), etc.) on the PHP 5.2 binary, so skip
213+
// the install.php HTTP request entirely.
214+
// WP 1.0–1.2: post-install PDO fallback builds the full schema.
215+
// WP 1.5–3.0: needs dbDelta() for the schema; the rest is left
216+
// to the PDO fallback.
234217
const wpVersion = readOnDiskWpVersion(php, php.documentRoot);
235218
if (wpVersion !== null) {
236219
const parsed = parseFloat(wpVersion);
@@ -243,17 +226,12 @@ async function runLegacyInstaller(php: PHP): Promise<void> {
243226
}
244227
}
245228

246-
// Disable networking + mail functions during installation. This
247-
// must include all the functions already disabled via
248-
// applyLegacyPhpIniOverrides() — setPhpIniEntries replaces the
249-
// entire value, so listing only 'fsockopen' would re-enable
250-
// curl_init/curl_exec and cause WASM crashes when the installer
251-
// makes outbound HTTP requests.
252-
//
253-
// error_reporting is suppressed at the ini level too: old WP
254-
// class declarations (e.g. Walker_Page) trigger E_STRICT during
255-
// compile, which PHP may report using the ini value rather than
256-
// the runtime error_reporting() call.
229+
// withPHPIniValues replaces values wholesale, so re-list every
230+
// function from LEGACY_PHP_DISABLED_NETWORK_FUNCTIONS plus mail()
231+
// (the installer otherwise calls it and crashes). error_reporting
232+
// is suppressed at the ini level because old WP class declarations
233+
// trigger E_STRICT at compile time, which PHP reports against the
234+
// ini value rather than the runtime error_reporting() call.
257235
const iniOverrides: Record<string, string> = {
258236
disable_functions: [
259237
...LEGACY_PHP_DISABLED_NETWORK_FUNCTIONS,
@@ -284,10 +262,9 @@ async function runLegacyInstaller(php: PHP): Promise<void> {
284262
})
285263
);
286264

287-
// Skip isWordPressInstalled() entirely — it can trigger a WASM
288-
// trap (not a PHP exception) on old WordPress (< 3.0), which
289-
// corrupts the runtime beyond recovery. Use the installer
290-
// response text as a heuristic instead.
265+
// isWordPressInstalled() can WASM-trap on old WP (< 3.0) and
266+
// corrupt the runtime, so detect success from the installer
267+
// response text instead.
291268
const installSucceeded =
292269
response.text?.includes('Success') ||
293270
response.text?.includes('successful') ||
@@ -357,8 +334,8 @@ async function setLegacyPermalinkStructureViaPdo(php: PHP): Promise<void> {
357334

358335
/**
359336
* Runs dbDelta() and populate_options/populate_roles without the
360-
* full wp_install() function. Used for WP 2.3–3.0 where the
361-
* installer crashes but we still need the table schemas.
337+
* full wp_install(). Used for WP 2.1–3.0 where install.php crashes
338+
* but we still need the table schemas.
362339
*/
363340
async function runDbDeltaOnly(php: PHP): Promise<void> {
364341
try {
@@ -370,26 +347,17 @@ async function runDbDeltaOnly(php: PHP): Promise<void> {
370347
ob_start();
371348
require getenv('DOCUMENT_ROOT') . '/wp-load.php';
372349
ob_clean();
373-
// Load upgrade functions for dbDelta
374350
if (file_exists(ABSPATH . 'wp-admin/includes/upgrade.php')) {
375351
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
376352
} elseif (file_exists(ABSPATH . 'wp-admin/upgrade-functions.php')) {
377353
require_once ABSPATH . 'wp-admin/upgrade-functions.php';
378354
}
379-
// Create tables via dbDelta — the critical step that
380-
// creates the proper schema for the WP version.
381355
if (function_exists('make_db_current_silent')) {
382356
make_db_current_silent();
383357
}
384-
// populate_options/populate_roles on WP 2.3+ only.
385-
// WP 2.1-2.2 crash in these functions (WASM traps
386-
// from mail/network calls that bypass PHP try/catch).
387-
// The PDO fallback seeds essential options/roles.
388-
global $wp_version;
389-
// populate_options sets db_version and other essential
390-
// options. populate_roles creates the roles/capabilities.
391-
// On PHP 5.2 WP 2.1-2.2 these crash with WASM traps.
392-
// Run them for any WP version that defines them.
358+
// Seed essential options/roles when the loader exposes
359+
// them. The PDO fallback in runPostInstallLegacyFixups
360+
// backfills anything missing if either call dies.
393361
if (function_exists('populate_options')) populate_options();
394362
if (function_exists('populate_roles')) populate_roles();
395363
echo 'OK';

0 commit comments

Comments
 (0)