forked from WordPress/wordpress-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activate-plugin.ts
53 lines (51 loc) · 1.5 KB
/
activate-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
import { StepHandler } from '.';
export interface ActivatePluginStep {
step: 'activatePlugin';
/* Path to the plugin directory as absolute path (/wordpress/wp-content/plugins/plugin-name); or the plugin entry file relative to the plugins directory (plugin-name/plugin-name.php). */
pluginPath: string;
/* Optional plugin name */
pluginName?: string;
}
/**
* Activates a WordPress plugin in the Playground.
*
* @param playground The playground client.
*/
export const activatePlugin: StepHandler<ActivatePluginStep> = async (
playground,
{ pluginPath, pluginName },
progress
) => {
progress?.tracker.setCaption(`Activating ${pluginName || pluginPath}`);
const requiredFiles = [
`${await playground.documentRoot}/wp-load.php`,
`${await playground.documentRoot}/wp-admin/includes/plugin.php`,
];
const requiredFilesExist = requiredFiles.every((file) =>
playground.fileExists(file)
);
if (!requiredFilesExist) {
throw new Error(
`Required WordPress files do not exist: ${requiredFiles.join(', ')}`
);
}
await playground.run({
code: `<?php
${requiredFiles.map((file) => `require_once( '${file}' );`).join('\n')}
$plugin_path = '${pluginPath}';
if (!is_dir($plugin_path)) {
activate_plugin($plugin_path);
return;
}
// Find plugin entry file
foreach ( ( glob( $plugin_path . '/*.php' ) ?: array() ) as $file ) {
$info = get_plugin_data( $file, false, false );
if ( ! empty( $info['Name'] ) ) {
activate_plugin( $file );
return;
}
}
throw new Exception('Could not find plugin entry file.');
`,
});
};