Skip to content

Commit

Permalink
Throw an error when activating a theme or plugin that doesn't exist (#…
Browse files Browse the repository at this point in the history
…1391)

Fixes #1347

## What is this PR doing?
 
Throw an error when activating a theme or plugin that doesn't exist.

## What problem is it solving?

Users currently don't know when the activate plugin and theme steps
fail.

## How is the problem addressed?

By checking if the file exists before trying to activate it.

## Testing Instructions

- Checkout this branch
- [Run this blueprint
t](http://localhost:5400/website-server/#{%20%22landingPage%22:%20%22/wp-admin/site-editor.php%22,%20%22preferredVersions%22:%20{%20%22php%22:%20%227.4%22,%20%22wp%22:%20%226.5%22%20},%20%22phpExtensionBundles%22:%20[%20%22kitchen-sink%22%20],%20%22features%22:%20{%20%22networking%22:%20true%20},%20%22steps%22:%20[%20{%20%22step%22:%20%22login%22,%20%22username%22:%20%22admin%22,%20%22password%22:%20%22password%22%20},%20{%20%22step%22:%20%22activatePlugin%22,%20%22pluginName%22:%20%22Gutenberg%22,%20%22pluginPath%22:%20%22/wordpress/wp-content/plugins/gutenberg%22%20}%20]%20})o
activate an unexisting plugin
- You should see the error modal with an error about the failed plugin
install step
- [Run this blueprint
t](http://localhost:5400/website-server/#{%20%22steps%22:%20[%20{%20%22step%22:%20%22activateTheme%22,%20%22themeFolderName%22:%20%22storefront%22%20}%20]%20})o
activate an unexisting theme
- You should see the error modal with an error about the failed theme
install step

---------

Co-authored-by: Adam Zieliński <adam@adamziel.com>
  • Loading branch information
bgrgicak and adamziel committed May 16, 2024
1 parent 1237e8b commit 1e184d8
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 30 deletions.
7 changes: 6 additions & 1 deletion packages/php-wasm/logger/src/lib/handlers/log-to-console.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { LogHandler } from '../log-handlers';
import { Log } from '../logger';
import { Log, prepareLogMessage } from '../logger';

/**
* Log message to the console.
*/
export const logToConsole: LogHandler = (log: Log, ...args: any[]): void => {
if (typeof log.message === 'string') {
log.message = prepareLogMessage(log.message);
} else if (log.message.message && typeof log.message.message === 'string') {
log.message.message = prepareLogMessage(log.message.message);
}
/* eslint-disable no-console */
switch (log.severity) {
case 'Debug':
Expand Down
5 changes: 5 additions & 0 deletions packages/php-wasm/logger/src/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ const getDefaultHandlers = () => {
*/
export const logger: Logger = new Logger(getDefaultHandlers());

export const prepareLogMessage = (message: string) => {
return message.replace(/\t/g, '');
};

export const formatLogEntry = (
message: string,
severity: LogSeverity,
Expand All @@ -191,6 +195,7 @@ export const formatLogEntry = (
timeZoneName: 'short',
}).format(date);
const now = formattedDate + ' ' + formattedTime;
message = prepareLogMessage(message);
return `[${now}] ${prefix} ${severity}: ${message}`;
};

Expand Down
47 changes: 27 additions & 20 deletions packages/playground/blueprints/src/lib/steps/activate-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,37 @@ export const activatePlugin: StepHandler<ActivatePluginStep> = async (
const docroot = await playground.documentRoot;
await playground.run({
code: `<?php
define( 'WP_ADMIN', true );
require_once( ${phpVar(docroot)}. "/wp-load.php" );
require_once( ${phpVar(docroot)}. "/wp-admin/includes/plugin.php" );
define( 'WP_ADMIN', true );
require_once( ${phpVar(docroot)}. "/wp-load.php" );
require_once( ${phpVar(docroot)}. "/wp-admin/includes/plugin.php" );
// Set current user to admin
wp_set_current_user( get_users(array('role' => 'Administrator') )[0]->ID );
// Set current user to admin
wp_set_current_user( get_users(array('role' => 'Administrator') )[0]->ID );
$plugin_path = ${phpVar(pluginPath)};
$plugin_path = ${phpVar(pluginPath)};
$response = false;
if (!is_dir($plugin_path)) {
$response = activate_plugin($plugin_path);
}
if (!is_dir($plugin_path)) {
activate_plugin($plugin_path);
die();
}
// Activate plugin by name if activation by path wasn't successful
if ( null !== $response ) {
foreach ( ( glob( $plugin_path . '/*.php' ) ?: array() ) as $file ) {
$info = get_plugin_data( $file, false, false );
if ( ! empty( $info['Name'] ) ) {
$response = activate_plugin( $file );
break;
}
}
}
foreach ( ( glob( $plugin_path . '/*.php' ) ?: array() ) as $file ) {
$info = get_plugin_data( $file, false, false );
if ( ! empty( $info['Name'] ) ) {
activate_plugin( $file );
die();
}
}
if ( null === $response ) {
die('Plugin activated successfully');
} else if ( is_wp_error( $response ) ) {
throw new Exception( $response->get_error_message() );
}
// If we got here, the plugin was not found.
exit(1);
`,
throw new Exception( 'Unable to activate plugin' );
`,
});
};
23 changes: 17 additions & 6 deletions packages/playground/blueprints/src/lib/steps/activate-theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,26 @@ export const activateTheme: StepHandler<ActivateThemeStep> = async (
) => {
progress?.tracker.setCaption(`Activating ${themeFolderName}`);
const docroot = await playground.documentRoot;

const themeFolderPath = `${docroot}/wp-content/themes/${themeFolderName}`;
if (!(await playground.fileExists(themeFolderPath))) {
throw new Error(`
Couldn't activate theme ${themeFolderName}.
Theme not found at the provided theme path: ${themeFolderPath}.
Check the theme path to ensure it's correct.
If the theme is not installed, you can install it using the installTheme step.
More info can be found in the Blueprint documentation: https://wordpress.github.io/wordpress-playground/blueprints-api/steps/#ActivateThemeStep
`);
}
await playground.run({
code: `<?php
define( 'WP_ADMIN', true );
require_once( ${phpVar(docroot)}. "/wp-load.php" );
define( 'WP_ADMIN', true );
require_once( ${phpVar(docroot)}. "/wp-load.php" );
// Set current user to admin
wp_set_current_user( get_users(array('role' => 'Administrator') )[0]->ID );
// Set current user to admin
wp_set_current_user( get_users(array('role' => 'Administrator') )[0]->ID );
switch_theme( ${phpVar(themeFolderName)} );
`,
switch_theme( ${phpVar(themeFolderName)} );
`,
});
};
14 changes: 12 additions & 2 deletions packages/playground/website/cypress/e2e/blueprints.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,18 @@ describe('Blueprints', () => {
it('enableMultisite step should re-activate the plugins', () => {
const blueprint: Blueprint = {
landingPage: '/wp-admin/plugins.php',
plugins: ['hello-dolly'],
steps: [{ step: 'enableMultisite' }],
steps: [
{ step: 'login' },
{
step: 'installPlugin',
pluginZipFile: {
resource: 'wordpress.org/plugins',
slug: 'hello-dolly',
},
options: { activate: true },
},
{ step: 'enableMultisite' },
],
};
cy.visit('/#' + JSON.stringify(blueprint));
cy.wordPressDocument()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function LogModal(props: { description?: JSX.Element; title?: string }) {
useEffect(getLogs, [activeModal]);

function getLogs() {
setLogs(logger.getLogs().map((log) => log.replace(/\t/g, '')));
setLogs(logger.getLogs());
}

function onClose() {
Expand Down

0 comments on commit 1e184d8

Please sign in to comment.