Skip to content

Commit

Permalink
Blueprints: Rename importFile to importWxr, switch to humanmade/WordP…
Browse files Browse the repository at this point in the history
…ress importer (#1192)

The importFile step works by interacting with HTML form like a user
would. Problem is, the WXR importer requires some interactions with the
forms which we fake by adjusting the POST data before submitting the
form. This approach causes a number of issues listed below.

More context:
#1183.

## Implementation

Refactors the importFile Blueprint step to:

* Be named importWxz. The old name continues to work, though.
* Use the humanmade/WordPress-Importer for importing content – it is
  more reliable than the official wordpress-importer and has a
  convenient PHP API.
* Drop WXZ support – it's a maintenance burden and doesn't add clear
  value.
* Remove dependency on DOMParser – this step should now work in `wp-now`

## Testing instructions

1. Go to
http://localhost:5400/website-server/?import-wxr=https://raw.githubusercontent.com/helgatheviking/Simple-User-Listing/3e38ea3eb3a057424fbae4305cba2fd9f73d896b/demo-content/demo-content.xml&php=8.0&wp=6.5&storage=none&php-extension-bundle=kitchen-sink
2. Confirm the "Simple User Listing Block" page was created
3. Confirm in wp-admin a bunch of user accounts were created
4. Confirm in wp-admin the WordPress Importer plugin was installed

Closes #1183
Closes #379
Closes #1158
Closes #1161

cc @dmsnell
  • Loading branch information
adamziel authored Apr 4, 2024
1 parent 8d12b9b commit fad3ccf
Show file tree
Hide file tree
Showing 805 changed files with 156,368 additions and 158,473 deletions.
6 changes: 3 additions & 3 deletions packages/docs/site/docs/02-start-using/01-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ You can specify major versions like `wp=6.2` or `php=8.1` and expect the most re

:::

## Import a WXZ or a WXR file
## Import a WXR file

You can import a WordPress export file by uploading a WXZ, or WXR file in [/wp-admin/](https://playground.wordpress.net/wp-admin/).
You can import a WordPress export file by uploading a WXR file in [/wp-admin/](https://playground.wordpress.net/wp-admin/).

You can also use [JSON Blueprints](../09-blueprints-api/01-index.md). See [getting started with Blueprints](../09-blueprints-api/01-index.md) to learn more.

This is different from the import feature described above. The import feature exports the entire site, including the database. This import feature imports a WXR or WXZ file into an existing site.
This is different from the import feature described above. The import feature exports the entire site, including the database. This import feature imports a WXR file into an existing site.

## Build apps with WordPress Playground

Expand Down
4 changes: 2 additions & 2 deletions packages/docs/site/docs/03-build-an-app/01-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ You can still showcase it on Playground by using [JSON Blueprints](../09-bluepri
}
},
{
"step": "importFile",
"step": "importWxr",
"pluginZipFile": {
"resource": "url",
"url": "https://your-site.com/starter-content.wxz"
"url": "https://your-site.com/starter-content.wxr"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/site/docs/08-query-api/01-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ You can go ahead and try it out. The Playground will automatically install the t
| `multisite` | `no` | Enables the WordPress multisite mode. |
| `storage` | | Selects the storage for Playground: `none` gets erased on page refresh, `browser` is stored in the browser, and `device` is stored in the selected directory on a device. The last two protect the user from accidentally losing their work upon page refresh. |
| `import-site` | | Imports site files and database from a zip file specified by URL. |
| `import-content` | | Imports site content from a WXR or WXZ file specified by URL. It uses the WordPress Importer, so the default admin user must be logged in. |
| `import-wxr` | | Imports site content from a WXR file specified by URL. It uses the WordPress Importer, so the default admin user must be logged in. |

For example, the following code embeds a Playground with a preinstalled Gutenberg plugin, and opens the post editor:

Expand Down
4 changes: 2 additions & 2 deletions packages/docs/site/docs/09-blueprints-api/08-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ blueprint={{
}
},
{
"step": "importFile",
"step": "importWxr",
"file": {
"resource": "url",
"url": "https://your-site.com/starter-content.wxz"
"url": "https://your-site.com/starter-content.wxr"
}
},
{
Expand Down
27 changes: 27 additions & 0 deletions packages/playground/blueprints/public/blueprint-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,33 @@
},
"required": ["step"]
},
{
"type": "object",
"additionalProperties": false,
"properties": {
"progress": {
"type": "object",
"properties": {
"weight": {
"type": "number"
},
"caption": {
"type": "string"
}
},
"additionalProperties": false
},
"step": {
"type": "string",
"const": "importWxr"
},
"file": {
"$ref": "#/definitions/FileReference",
"description": "The file to import"
}
},
"required": ["file", "step"]
},
{
"type": "object",
"additionalProperties": false,
Expand Down
54 changes: 50 additions & 4 deletions packages/playground/blueprints/src/lib/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const { wpCLI, ...otherStepHandlers } = allStepHandlers;
const keyedStepHandlers = {
...otherStepHandlers,
'wp-cli': wpCLI,
importFile: otherStepHandlers.importWxr,
};

import Ajv from 'ajv';
Expand Down Expand Up @@ -86,6 +87,16 @@ export function compileBlueprint(
...blueprint,
steps: (blueprint.steps || []).filter(isStepDefinition),
};
// Convert legacy importFile steps to importWxr
for (const step of blueprint.steps!) {
if (typeof step === 'object' && (step as any).step === 'importFile') {
(step as any).step = 'importWxr';
console.warn(
`The "importFile" step is deprecated. Use "importWxr" instead.`
);
}
}

// Experimental declarative syntax {{{
if (blueprint.constants) {
blueprint.steps!.unshift({
Expand Down Expand Up @@ -133,6 +144,9 @@ export function compileBlueprint(
: blueprint.login),
});
}
if (!blueprint.phpExtensionBundles) {
blueprint.phpExtensionBundles = [];
}

if (!blueprint.phpExtensionBundles) {
blueprint.phpExtensionBundles = [];
Expand All @@ -145,7 +159,7 @@ export function compileBlueprint(

/**
* Download WP-CLI. {{{
* Hardcoding this in the compilt() function is a temporary solution
* Hardcoding this in the compile() function is a temporary solution
* to provide the wpCLI step with the wp-cli.phar file it needs. Eventually,
* each Blueprint step may be able to specify any pre-requisite resources.
* Also, wp-cli should only be downloaded if it's not already present.
Expand All @@ -154,10 +168,13 @@ export function compileBlueprint(
(step) => typeof step === 'object' && step?.step === 'wp-cli'
);
if (wpCliStepIndex !== undefined && wpCliStepIndex > -1) {
if (!blueprint.phpExtensionBundles.includes('kitchen-sink')) {
blueprint.phpExtensionBundles.push('kitchen-sink');
if (blueprint.phpExtensionBundles.includes('light')) {
blueprint.phpExtensionBundles =
blueprint.phpExtensionBundles.filter(
(bundle) => bundle !== 'light'
);
console.warn(
`The WP-CLI step used in your Blueprint requires the iconv and mbstring PHP extensions. ` +
`The wpCli step used in your Blueprint requires the iconv and mbstring PHP extensions. ` +
`However, you did not specify the kitchen-sink extension bundle. Playground will override your ` +
`choice and load the kitchen-sink PHP extensions bundle to prevent the WP-CLI step from failing. `
);
Expand All @@ -183,6 +200,35 @@ export function compileBlueprint(
}
// }}}

/**
* Download the WordPress-importer plugin. {{{
* Hardcoding this in the compile() function is a temporary solution
*/
const importWxrStepIndex = blueprint.steps?.findIndex(
(step) => typeof step === 'object' && step?.step === 'importWxr'
);
if (importWxrStepIndex !== undefined && importWxrStepIndex > -1) {
if (blueprint.phpExtensionBundles.includes('light')) {
blueprint.phpExtensionBundles =
blueprint.phpExtensionBundles.filter(
(bundle) => bundle !== 'light'
);
console.warn(
`The importWxr step used in your Blueprint requires the iconv and mbstring PHP extensions. ` +
`However, you did not specify the kitchen-sink extension bundle. Playground will override your ` +
`choice and load the kitchen-sink PHP extensions bundle to prevent the WP-CLI step from failing. `
);
}
blueprint.steps?.splice(importWxrStepIndex, 0, {
step: 'installPlugin',
pluginZipFile: {
resource: 'url',
url: 'https://playground.wordpress.net/wordpress-importer.zip',
caption: 'Downloading the WordPress Importer plugin',
},
});
}

const { valid, errors } = validateBlueprint(blueprint);
if (!valid) {
const e = new Error(
Expand Down
15 changes: 0 additions & 15 deletions packages/playground/blueprints/src/lib/steps/export-wxz.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/playground/blueprints/src/lib/steps/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ export { mkdir } from './mkdir';
export { rmdir } from './rmdir';
export { writeFile } from './write-file';
export { defineSiteUrl } from './define-site-url';
export { importFile } from './import-file';
export { importWxr as importWxr } from './import-wxr';
export { importWordPressFiles } from './import-wordpress-files';
export { exportWXR } from './export-wxr';
export { exportWXZ } from './export-wxz';
export { unzip } from './unzip';
export { installPlugin } from './install-plugin';
export { installTheme } from './install-theme';
Expand Down
93 changes: 0 additions & 93 deletions packages/playground/blueprints/src/lib/steps/import-file.ts

This file was deleted.

55 changes: 55 additions & 0 deletions packages/playground/blueprints/src/lib/steps/import-wxr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { StepHandler } from '.';
import { writeFile } from './write-file';
import { phpVar } from '@php-wasm/util';

/**
* @inheritDoc importWxr
* @example
*
* <code>
* {
* "step": "importWxr",
* "file": {
* "resource": "url",
* "url": "https://your-site.com/starter-content.wxr"
* }
* }
* </code>
*/
export interface ImportWxrStep<ResourceType> {
step: 'importWxr';
/** The file to import */
file: ResourceType;
}

/**
* Imports a WXR file into WordPress.
*
* @param playground Playground client.
* @param file The file to import.
*/
export const importWxr: StepHandler<ImportWxrStep<File>> = async (
playground,
{ file },
progress?
) => {
progress?.tracker?.setCaption('Importing content');
await writeFile(playground, {
path: '/tmp/import.wxr',
data: file,
});
const docroot = await playground.documentRoot;
await playground.run({
code: `<?php
require ${phpVar(docroot)} . '/wp-load.php';
$admin_id = get_users(array('role' => 'Administrator') )[0];
$importer = new WXR_Importer( array(
'fetch_attachments' => true,
'default_author' => $admin_id
) );
$logger = new WP_Importer_Logger_CLI();
$importer->set_logger( $logger );
$result = $importer->import( '/tmp/import.wxr' );
`,
});
};
6 changes: 3 additions & 3 deletions packages/playground/blueprints/src/lib/steps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { DefineWpConfigConstsStep } from './define-wp-config-consts';
import { ActivateThemeStep } from './activate-theme';
import { UnzipStep } from './unzip';
import { ImportWordPressFilesStep } from './import-wordpress-files';
import { ImportFileStep } from './import-file';
import { ImportWxrStep } from './import-wxr';
import { EnableMultisiteStep } from './enable-multisite';
import { WPCLIStep } from './wp-cli';

Expand All @@ -51,7 +51,7 @@ export type GenericStep<Resource> =
| DefineWpConfigConstsStep
| DefineSiteUrlStep
| EnableMultisiteStep
| ImportFileStep<Resource>
| ImportWxrStep<Resource>
| ImportWordPressFilesStep<Resource>
| InstallPluginStep<Resource>
| InstallThemeStep<Resource>
Expand Down Expand Up @@ -79,7 +79,7 @@ export type {
DefineWpConfigConstsStep,
DefineSiteUrlStep,
EnableMultisiteStep,
ImportFileStep,
ImportWxrStep,
ImportWordPressFilesStep,
InstallPluginStep,
InstallPluginOptions,
Expand Down
Loading

0 comments on commit fad3ccf

Please sign in to comment.