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

Website Query API: Add import-site and import-content options #610

Merged
merged 12 commits into from
Dec 11, 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
10 changes: 9 additions & 1 deletion packages/docs/site/docs/08-query-api/01-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,19 @@ You can go ahead and try it out. The Playground will automatically install the t
| `url` | `/wp-admin/` | Load the specified initial page displaying WordPress |
| `mode` | `seamless`, `browser`, or `browser-full-screen` | Displays WordPress on a full-page or wraps it in a browser UI |
| `lazy` | | Defer loading the Playground assets until someone clicks on the "Run" button |
| `login` | `1` | Logs the user in as an admin |
| `login` | `1` | Logs the user in as an admin. Set to `0` to not log in. |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thank you @eliot-akira! I know it's not directly related to this PR, but it's such a small and useful change – let's keep it.

| `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. |

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

```html
<iframe src="https://playground.wordpress.net/?plugin=gutenberg&url=/wp-admin/post-new.php&mode=seamless"> </iframe>
```

:::info CORS policy

To import files from a URL, such as a site zip package, they must be served with `Access-Control-Allow-Origin` header set. For reference, see: [Cross-Origin Resource Sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#the_http_response_headers).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lovely note, thank you @eliot-akira! ❤️


:::
19 changes: 19 additions & 0 deletions packages/playground/website/src/lib/make-blueprint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ interface MakeBlueprintOptions {
features?: Blueprint['features'];
theme?: string;
plugins?: string[];
importSite?: string;
importContent?: string;
}

export function makeBlueprint(options: MakeBlueprintOptions): Blueprint {
const plugins = options.plugins || [];
return {
Expand All @@ -21,11 +24,27 @@ export function makeBlueprint(options: MakeBlueprintOptions): Blueprint {
phpExtensionBundles: options.phpExtensionBundles as any,
features: options.features,
steps: [
options.importSite &&
/^(http(s?)):\/\//i.test(options.importSite) && {
step: 'importWordPressFiles',
wordPressFilesZip: {
resource: 'url',
url: options.importSite,
},
},
options.login && {
step: 'login',
username: 'admin',
password: 'password',
},
options.importContent &&
/^(http(s?)):\/\//i.test(options.importContent) && {
step: 'importFile',
file: {
resource: 'url',
url: options.importContent,
},
},
options.theme && {
step: 'installTheme',
themeZipFile: {
Expand Down
2 changes: 2 additions & 0 deletions packages/playground/website/src/lib/resolve-blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export async function resolveBlueprint() {
plugins: query.getAll('plugin'),
landingPage: query.get('url') || undefined,
phpExtensionBundles: query.getAll('php-extension-bundle') || [],
importSite: query.get('import-site') || undefined,
importContent: query.get('import-content') || undefined,
});
}

Expand Down
Loading