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

Rewires the importFile 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.

Closes #1183
Closes #379
Closes #1158
Closes #1161
  • Loading branch information
adamziel committed Apr 3, 2024
1 parent 31856c7 commit c7a63f8
Show file tree
Hide file tree
Showing 802 changed files with 156,310 additions and 158,435 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
2 changes: 1 addition & 1 deletion packages/docs/site/docs/03-build-an-app/01-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ 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"
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-content` | | 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
2 changes: 1 addition & 1 deletion packages/docs/site/docs/09-blueprints-api/08-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ blueprint={{
}
},
{
"step": "importFile",
"step": "importWxr",
"file": {
"resource": "url",
"url": "https://your-site.com/starter-content.wxz"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@
},
"step": {
"type": "string",
"const": "importFile"
"const": "importWxr"
},
"file": {
"$ref": "#/definitions/FileReference",
Expand Down
56 changes: 49 additions & 7 deletions packages/playground/blueprints/src/lib/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ export function compileBlueprint(
...blueprint,
steps: (blueprint.steps || []).filter(isStepDefinition),
};

// Backwards compatibility for the legacy "importFile" name of
// the importWxr step
for (const step of blueprint.steps!) {
if (typeof step === 'object' && (step as any).step === 'importFile') {
step!.step = 'importWxr';
}
}

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

/**
* 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 @@ -145,13 +157,13 @@ export function compileBlueprint(
(step) => typeof step === 'object' && step?.step === 'wp-cli'
);
if (wpCliStepIndex !== undefined && wpCliStepIndex > -1) {
if (!blueprint.phpExtensionBundles) {
blueprint.phpExtensionBundles = [];
}
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 @@ -177,6 +189,36 @@ 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: 'unzip',
extractToPath: '/tmp',
zipFile: {
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' | 'importFile';
/** 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 as importWxrStep,
ImportWordPressFilesStep,
InstallPluginStep,
InstallPluginOptions,
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/website/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ AddEncoding x-gzip .gz
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
</FilesMatch>
<FilesMatch "index\.js|blueprint-schema\.json|logger.php|wp-cli.phar">
<FilesMatch "index\.js|blueprint-schema\.json|logger.php|wp-cli.phar|wordpress-importer.zip">
Header set Access-Control-Allow-Origin "*"
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Expand Down
Binary file not shown.
10 changes: 5 additions & 5 deletions packages/playground/website/src/lib/make-blueprint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface MakeBlueprintOptions {
theme?: string;
plugins?: string[];
importSite?: string;
importContent?: string;
importWxr?: string;
}

export function makeBlueprint(options: MakeBlueprintOptions): Blueprint {
Expand Down Expand Up @@ -41,12 +41,12 @@ export function makeBlueprint(options: MakeBlueprintOptions): Blueprint {
username: 'admin',
password: 'password',
},
options.importContent &&
/^(http(s?)):\/\//i.test(options.importContent) && {
step: 'importFile',
options.importWxr &&
/^(http(s?)):\/\//i.test(options.importWxr) && {
step: 'importWxr',
file: {
resource: 'url',
url: options.importContent,
url: options.importWxr,
},
},
options.theme && {
Expand Down
5 changes: 4 additions & 1 deletion packages/playground/website/src/lib/resolve-blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ export async function resolveBlueprint() {
landingPage: query.get('url') || undefined,
phpExtensionBundles: query.getAll('php-extension-bundle') || [],
importSite: query.get('import-site') || undefined,
importContent: query.get('import-content') || undefined,
importWxr:
query.get('import-wxr') ||
query.get('import-content') ||
undefined,
});
}

Expand Down

0 comments on commit c7a63f8

Please sign in to comment.