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
  • Loading branch information
adamziel committed May 17, 2024
1 parent 51d1d88 commit 16293ba
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 @@ -202,12 +202,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 @@ -260,6 +269,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 16293ba

Please sign in to comment.