Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wp-now: Remove magic WordPress loading for php command #441

Merged
merged 4 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/wp-now/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ wp-now php my-file.php
- `--port=<port>`: the port number on which the server will listen. This is optional and if not provided, it will pick an open port number automatically. The default port number is set to `8881`(example of usage: `--port=3000`);
- `--wp=<version>`: the version of WordPress to use. This is optional and if not provided, it will use a default version. The default version is set to the [latest WordPress version](https://wordpress.org/download/releases/)(example usage: `--wp=5.8`)

Of these, `wp-now php` currently supports the `--path=<path>`, `--php=<version>`, and `--wp=<version>` arguments.
Of these, `wp-now php` currently supports the `--path=<path>` and `--php=<version>` arguments.

## Technical Details

Expand Down
22 changes: 2 additions & 20 deletions packages/wp-now/src/execute-php-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import startWPNow from './wp-now';
import { WPNowOptions } from './config';
import { disableOutput } from './output';

const VFS_TMP_PATH = '/vfs-wp-now-tmp';
const VFS_PHP_FILE = path.join(VFS_TMP_PATH, 'parent.php');

/**
*
* Execute a PHP file given its path. For non index mode it loads WordPress context.
Expand All @@ -22,7 +19,7 @@ export async function executePHPFile(
options: WPNowOptions = {}
) {
disableOutput();
const { phpInstances, options: wpNowOptions } = await startWPNow({
const { phpInstances } = await startWPNow({
...options,
numberOfPhpInstances: 2,
});
Expand All @@ -34,24 +31,9 @@ export async function executePHPFile(
throw new Error(`Could not open input file: ${absoluteFilePath}`);
}

let fileToExecute = absoluteFilePath;
if (wpNowOptions.mode !== 'index') {
// Load WordPress context for non index mode.
php.mkdirTree(VFS_TMP_PATH);
php.writeFile(
VFS_PHP_FILE,
`<?php
$_SERVER['HTTP_HOST'] = '${wpNowOptions.absoluteUrl}';
require_once '${path.join(wpNowOptions.documentRoot, 'wp-load.php')}';
require_once '${filePath}';
`
);
fileToExecute = VFS_PHP_FILE;
}

try {
php.useHostFilesystem();
await php.cli(['php', fileToExecute]);
await php.cli(['php', absoluteFilePath]);
} catch (resultOrError) {
const success =
resultOrError.name === 'ExitStatus' && resultOrError.status === 0;
Expand Down
9 changes: 4 additions & 5 deletions packages/wp-now/src/run-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ function commonParameters(yargs) {
.option('php', {
describe: 'PHP version to use.',
type: 'string',
})
.option('wp', {
describe: "WordPress version to use: e.g. '--wp=6.2'",
type: 'string',
});
}

Expand All @@ -59,6 +55,10 @@ export async function runCli() {
'Start the server',
(yargs) => {
commonParameters(yargs);
yargs.option('wp', {
describe: "WordPress version to use: e.g. '--wp=6.2'",
type: 'string',
});
yargs.option('port', {
describe: 'Server port',
type: 'number',
Expand Down Expand Up @@ -101,7 +101,6 @@ export async function runCli() {
const options = await getWpNowConfig({
path: argv.path as string,
php: argv.php as SupportedPHPVersion,
wp: argv.wp as string,
});
await executePHPFile(argv.file as string, options);
process.exit(0);
Expand Down