-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathinstall-plugin.ts
174 lines (166 loc) · 4.96 KB
/
install-plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { UniversalPHP } from '@php-wasm/universal';
import { StepHandler } from '.';
import { asDOM, zipNameToHumanName } from './common';
/**
* @inheritDoc installPlugin
* @hasRunnableExample
* @needsLogin
* @landingPage /wp-admin/plugins.php
* @example
*
* <code>
* {
* "step": "installPlugin",
* "pluginZipFile": {
* "resource": "wordpress.org/plugins",
* "slug": "gutenberg"
* },
* "options": {
* "activate": true
* }
* }
* </code>
*/
export interface InstallPluginStep<ResourceType> {
/**
* The step identifier.
*/
step: 'installPlugin';
/**
* The plugin zip file to install.
*/
pluginZipFile: ResourceType;
/**
* Optional installation options.
*/
options?: InstallPluginOptions;
}
export interface InstallPluginOptions {
/**
* Whether to activate the plugin after installing it.
*/
activate?: boolean;
}
/**
* Installs a WordPress plugin in the Playground.
* Technically, it uses the same plugin upload form as a WordPress user
* would, and then activates the plugin if needed.
*
* @param playground The playground client.
* @param pluginZipFile The plugin zip file.
* @param options Optional. Set `activate` to false if you don't want to activate the plugin.
*/
export const installPlugin: StepHandler<InstallPluginStep<File>> = async (
playground,
{ pluginZipFile, options = {} },
progress?
) => {
progress?.tracker.setCaption(
`Installing the ${zipNameToHumanName(pluginZipFile?.name)} plugin`
);
try {
const activate = 'activate' in options ? options.activate : true;
// Upload it to WordPress
const pluginForm = await playground.request({
url: '/wp-admin/plugin-install.php?tab=upload',
});
const pluginFormPage = asDOM(pluginForm);
const pluginFormData = new FormData(
pluginFormPage.querySelector('.wp-upload-form')! as HTMLFormElement
) as any;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { pluginzip, ...postData } = Object.fromEntries(
pluginFormData.entries()
);
const pluginInstalledResponse = await playground.request({
url: '/wp-admin/update.php?action=upload-plugin',
method: 'POST',
formData: postData,
files: { pluginzip: pluginZipFile },
});
// Activate if needed
if (activate) {
const pluginInstalledPage = asDOM(pluginInstalledResponse);
const activateButtonHref = pluginInstalledPage
.querySelector('#wpbody-content .button.button-primary')!
.attributes.getNamedItem('href')!.value;
const activatePluginUrl = new URL(
activateButtonHref,
await playground.pathToInternalUrl('/wp-admin/')
).toString();
await playground.request({
url: activatePluginUrl,
});
}
/**
* Pair the site editor's nested iframe to the Service Worker.
*
* Without the patch below, the site editor initiates network requests that
* aren't routed through the service worker. That's a known browser issue:
*
* * https://bugs.chromium.org/p/chromium/issues/detail?id=880768
* * https://bugzilla.mozilla.org/show_bug.cgi?id=1293277
* * https://github.com/w3c/ServiceWorker/issues/765
*
* The problem with iframes using srcDoc and src="about:blank" as they
* fail to inherit the root site's service worker.
*
* Gutenberg loads the site editor using <iframe srcDoc="<!doctype html">
* to force the standards mode and not the quirks mode:
*
* https://github.com/WordPress/gutenberg/pull/38855
*
* This commit patches the site editor to achieve the same result via
* <iframe src="/doctype.html"> and a doctype.html file containing just
* `<!doctype html>`. This allows the iframe to inherit the service worker
* and correctly load all the css, js, fonts, images, and other assets.
*
* Ideally this issue would be fixed directly in Gutenberg and the patch
* below would be removed.
*
* See https://github.com/WordPress/wordpress-playground/issues/42 for more details
*/
if (
(await playground.isDir(
'/wordpress/wp-content/plugins/gutenberg'
)) &&
!(await playground.fileExists('/wordpress/.gutenberg-patched'))
) {
await playground.writeFile('/wordpress/.gutenberg-patched', '1');
await updateFile(
playground,
`/wordpress/wp-content/plugins/gutenberg/build/block-editor/index.js`,
(contents) =>
contents.replace(
/srcDoc:("[^"]+"|[^,]+)/g,
'src:"/wp-includes/empty.html"'
)
);
await updateFile(
playground,
`/wordpress/wp-content/plugins/gutenberg/build/block-editor/index.min.js`,
(contents) =>
contents.replace(
/srcDoc:("[^"]+"|[^,]+)/g,
'src:"/wp-includes/empty.html"'
)
);
}
} catch (error) {
console.error(
`Proceeding without the ${pluginZipFile.name} theme. Could not install it in wp-admin. ` +
`The original error was: ${error}`
);
console.error(error);
}
};
async function updateFile(
playground: UniversalPHP,
path: string,
callback: (contents: string) => string
) {
return await playground.writeFile(
path,
callback(await playground.readFileAsText(path))
);
}