Skip to content

Commit

Permalink
Remote.html: Preload the SQLite database plugin, but only execute it …
Browse files Browse the repository at this point in the history
…if there's no custom db.php inside wp-content (#1424)

This PR always preloads the SQLite plugin in remote.html, even if the
provided WordPress already ships one. The preloaded plugin will only be
used if no `wp-content/db.php` drop-in plugin is found in the
filesystem. This change sets the stage for:

* #1426, which ships a minified WordPress build without the SQLite
plugin baked-in.
* #1425, which runs the WordPress installation wizard if the site isn't
installed yet

Related to [Boot
Protocol](#1398)

## Problem solved

We want the official WordPress release `.zip` to just work in
Playground. Currently it doesn't because remote.html expects a
pre-processed build with the SQLite plugin already baked-in. This PR, in
conjunction with #1425 and #1426, remove that requirement.

## Testing Instructions

Confirm the CI checks pass.
  • Loading branch information
adamziel authored May 20, 2024
1 parent 100bfe5 commit aa1e061
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
20 changes: 19 additions & 1 deletion packages/playground/remote/src/lib/worker-thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import {
getWordPressModuleDetails,
LatestSupportedWordPressVersion,
SupportedWordPressVersions,
sqliteDatabaseIntegration,
} from '@wp-playground/wordpress-builds';
import { wordPressRewriteRules } from '@wp-playground/wordpress';
import {
preloadSqliteIntegration,
wordPressRewriteRules,
} from '@wp-playground/wordpress';
import { PHPRequestHandler } from '@php-wasm/universal';
import {
SyncProgressCallback,
Expand Down Expand Up @@ -57,6 +61,14 @@ if (
wordPressAvailableInOPFS = await playgroundAvailableInOpfs(virtualOpfsDir!);
}

// The SQLite integration must always be downloaded, even when using OPFS or Native FS,
// because it can't be assumed to exist in WordPress document root. Instead, it's installed
// in the /internal directory to avoid polluting the mounted directory structure.
downloadMonitor.expectAssets({
[sqliteDatabaseIntegration.url]: sqliteDatabaseIntegration.size,
});
const sqliteIntegrationRequest = monitoredFetch(sqliteDatabaseIntegration.url);

// Start downloading WordPress if needed
let wordPressRequest = null;
if (!wordPressAvailableInOPFS) {
Expand Down Expand Up @@ -210,6 +222,12 @@ try {
});
}

const sqliteIntegrationZip = await (await sqliteIntegrationRequest).blob();
await preloadSqliteIntegration(
primaryPhp,
new File([sqliteIntegrationZip], 'sqlite.zip')
);

// Always setup the current site URL.
await defineSiteUrl(primaryPhp, {
siteUrl: scopedSiteUrl,
Expand Down
25 changes: 23 additions & 2 deletions packages/playground/wordpress/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,21 @@ export async function preloadSqliteIntegration(
"'{SQLITE_PLUGIN}'",
phpVar(joinPaths(SQLITE_PLUGIN_FOLDER, 'load.php'))
);
const dbPhpPath = joinPaths(await php.documentRoot, 'wp-content/db.php');
const stopIfDbPhpExists = `<?php
// Do not preload this if WordPress comes with a custom db.php file.
if(file_exists(${phpVar(dbPhpPath)})) {
return;
}
?>`;
const SQLITE_MUPLUGIN_PATH =
'/internal/shared/mu-plugins/sqlite-database-integration.php';
await php.writeFile(SQLITE_MUPLUGIN_PATH, dbPhp);
await php.writeFile(SQLITE_MUPLUGIN_PATH, stopIfDbPhpExists + dbPhp);
await php.writeFile(
`/internal/shared/preload/0-sqlite.php`,
`<?php
stopIfDbPhpExists +
`<?php
/**
* Loads the SQLite integration plugin before WordPress is loaded
* and without creating a drop-in "db.php" file.
Expand Down Expand Up @@ -256,6 +265,18 @@ class Playground_SQLite_Integration_Loader {
}
}
$wpdb = $GLOBALS['wpdb'] = new Playground_SQLite_Integration_Loader();
/**
* WordPress is capable of using a preloaded global $wpdb. However, if
* it cannot find the drop-in db.php plugin it still checks whether
* the mysqli_connect() function exists even though it's not used.
*
* What WordPress demands, Playground shall provide.
*/
if(!function_exists('mysqli_connect')) {
function mysqli_connect() {}
}
`
);
/**
Expand Down

0 comments on commit aa1e061

Please sign in to comment.