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

Throw an error when activating a theme or plugin that doesn't exist #1391

Merged
merged 12 commits into from
May 16, 2024
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, '');
};

Comment on lines +170 to +173
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Unrelated to the PR. It removes tabs from log messages.

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: 26 additions & 21 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,35 @@ 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);
} else {
// A plugin name was provided instead of a path
foreach ( ( glob( $plugin_path . '/*.php' ) ?: array() ) as $file ) {
$info = get_plugin_data( $file, false, false );
if ( ! empty( $info['Name'] ) ) {
$response = activate_plugin( $file );
break;
}
}
}

if (!is_dir($plugin_path)) {
activate_plugin($plugin_path);
die();
}

foreach ( ( glob( $plugin_path . '/*.php' ) ?: array() ) as $file ) {
$info = get_plugin_data( $file, false, false );
if ( ! empty( $info['Name'] ) ) {
activate_plugin( $file );
die();
}
}
if ( false === $response ) {
throw new Exception( 'Plugin not found' );
} else if ( is_wp_error( $response ) ) {
throw new Exception( $response->get_error_message() );
}

// If we got here, the plugin was not found.
exit(1);
`,
die('Plugin activated successfully');
`,
});
};
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' },
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This step started failing because it was missing the login step.
Without login, the user get's stuck on the login screens instead of being redirected to wp-admin.

{
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
Loading