Skip to content
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
25 changes: 25 additions & 0 deletions packages/playground/remote/src/lib/wordpress-popup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { getURLScope } from '@php-wasm/scopes';
import { wordPressSiteUrl } from './config';

/**
* Opens a WordPress tool in a popup with the same frame layout as the main app:
* an app-origin top-level page containing a WordPress-origin iframe.
*
* Browsers can select service-worker registrations using both the WordPress
* origin and the top-level site. Opening WordPress directly can then miss the
* worker serving it inside the main app. This app-origin wrapper preserves
* that top-level site, so #wp can use the existing worker and PHP instance.
*
* The hash supplies the destination. Accept only scoped WordPress URLs so this
* wrapper cannot be used to load trusted app documents into the iframe.
*/
function bootWordPressPopup() {
const target = new URL(location.hash.substring(1), wordPressSiteUrl);
if (target.origin !== wordPressSiteUrl || !getURLScope(target)) {
throw new Error('The popup URL must name a scoped WordPress page.');
}
const frame = document.querySelector('#wp') as HTMLIFrameElement;
frame.src = target.href;
}

bootWordPressPopup();
1 change: 1 addition & 0 deletions packages/playground/remote/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ export default defineConfig(({ mode }) => {
rollupOptions: {
input: {
wordpress: path('/remote.html'),
'wordpress-popup': path('/wordpress-popup.html'),
},
output: {
assetFileNames: (chunkInfo) => {
Expand Down
28 changes: 28 additions & 0 deletions packages/playground/remote/wordpress-popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!doctype html>
<html>
<head>
<title>WordPress Playground</title>
<style>
html,
body,
iframe {
width: 100%;
height: 100%;
margin: 0;
border: 0;
}
iframe {
display: block;
}
</style>
</head>
<body>
<iframe
id="wp"
name="wordpress-popup"
title="The WordPress site"
sandbox="allow-same-origin allow-scripts allow-popups allow-forms allow-downloads allow-modals"
></iframe>
<script type="module" src="./src/lib/wordpress-popup.ts"></script>
</body>
</html>
5 changes: 5 additions & 0 deletions packages/playground/tools/src/phpmyadmin/config.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
$cfg['ShowCreateDb'] = false;
$cfg['ShowChgPassword'] = false;

// Split-origin popups keep phpMyAdmin in a WordPress iframe. Its frame-busting
// script must not try to replace the app-origin wrapper; the iframe's sandbox
// and the browser's origin checks still prevent access to app documents/storage.
$cfg['AllowThirdPartyFraming'] = defined('PLAYGROUND_PARENT_ORIGIN');

// Cookie authentication secret.
$cfg['blowfish_secret'] = 'r/g+J#&)L2&p!z5gUS)d(vEU#KAynq#g';

Expand Down
45 changes: 30 additions & 15 deletions packages/playground/website/playwright/e2e/website-ui.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test, expect } from '../playground-fixtures.ts';
import type { Blueprint } from '@wp-playground/blueprints';
import type { Page } from '@playwright/test';
import type { Frame, Page } from '@playwright/test';
import { getDirectoryNameForSlug } from '../../src/lib/state/opfs/opfs-site-path';

// We can't import the SupportedPHPVersions versions directly from the remote package
Expand Down Expand Up @@ -1224,8 +1224,8 @@ echo $wp_env['db']['path'];
await adminerButton.click();

// Verify Adminer opened in new tab
const newPage = await pagePromise;
await newPage.waitForLoadState();
const popup = await pagePromise;
const newPage = await getWordPressPopupDocument(popup);
expect(newPage.url()).toContain('/adminer/');
await expect(newPage.locator('body')).toContainText('Adminer');
await expect(newPage.locator('body')).toContainText('wp_posts');
Expand Down Expand Up @@ -1271,15 +1271,18 @@ echo $wp_env['db']['path'];
).toContainText('Updated post content.');

// Go to SQL tab and execute "SHOW TABLES"
await newPage.getByRole('link', { name: 'SQL command' }).click();
await newPage
.locator('#menu')
.getByRole('link', { name: 'SQL command' })
.click();
await newPage.waitForLoadState();
const sqlTextarea = newPage.locator('textarea[name="query"]');
await sqlTextarea.fill('SHOW TABLES', { force: true });
await newPage.getByRole('button', { name: 'Execute' }).click();
await newPage.waitForLoadState();
await expect(newPage.locator('body')).toContainText('wp_posts');

await newPage.close();
await popup.close();
});

test('should load and open phpMyAdmin', async ({ website }) => {
Expand All @@ -1296,8 +1299,8 @@ echo $wp_env['db']['path'];
await phpMyAdminButton.click();

// Verify phpMyAdmin opened in new tab
const newPage = await pagePromise;
await newPage.waitForLoadState();
const popup = await pagePromise;
const newPage = await getWordPressPopupDocument(popup);
expect(newPage.url()).toContain('/phpmyadmin');
await expect(newPage.locator('body')).toContainText('phpMyAdmin');
await expect(newPage.locator('body')).toContainText('wp_posts');
Expand Down Expand Up @@ -1361,12 +1364,12 @@ echo $wp_env['db']['path'];
.click();
await newPage.waitForLoadState();
await newPage.locator('.CodeMirror.cm-s-default').click();
await newPage.keyboard.type('SHOW TABLES');
await popup.keyboard.type('SHOW TABLES');
await newPage.getByRole('button', { name: 'Go' }).click();
await newPage.waitForLoadState();
await expect(newPage.locator('body')).toContainText('wp_posts');

await newPage.close();
await popup.close();
});

/*
Expand All @@ -1379,27 +1382,39 @@ echo $wp_env['db']['path'];
await website.page
.getByRole('button', { name: 'Open Adminer' })
.click();
const adminerPage = await adminerPagePromise;
await adminerPage.waitForLoadState();
const adminerPopup = await adminerPagePromise;
const adminerPage = await getWordPressPopupDocument(adminerPopup);
await expect(adminerPage.locator('body')).toContainText('Adminer');
await expect(adminerPage.locator('body')).toContainText('wp_posts');

const phpMyAdminPagePromise = website.page.waitForEvent('popup');
await website.page
.getByRole('button', { name: 'Open phpMyAdmin' })
.click();
const phpMyAdminPage = await phpMyAdminPagePromise;
await phpMyAdminPage.waitForLoadState();
const phpMyAdminPopup = await phpMyAdminPagePromise;
const phpMyAdminPage = await getWordPressPopupDocument(phpMyAdminPopup);
await expect(phpMyAdminPage.locator('body')).toContainText(
'phpMyAdmin'
);
await expect(phpMyAdminPage.locator('body')).toContainText('wp_posts');

await adminerPage.close();
await phpMyAdminPage.close();
await adminerPopup.close();
await phpMyAdminPopup.close();
});
});

/** Follow the isolated WordPress iframe when the database tool needs an app-origin popup. */
async function getWordPressPopupDocument(popup: Page): Promise<Page | Frame> {
await popup.waitForLoadState();
const frame = popup.frame({ name: 'wordpress-popup' });
if (!frame) {
return popup;
}
// WebKit can finish loading the wrapper before its iframe leaves about:blank.
await frame.waitForURL((url) => url.pathname.startsWith('/scope:'));
return frame;
}

// Test browser-saved Playgrounds by default and explicit temporary opt-outs.
// The `@storage` tag routes this group to the one-worker CI storage lane.
test.describe('Default Playground storage', { tag: '@storage' }, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { logger } from '@php-wasm/logger';
import { external } from '@wordpress/icons';
import { useEffect, useState } from 'react';
import css from './style.module.css';
import { getWordPressPopupUrl } from '../../../lib/wordpress-popup';
import {
type PlaygroundClient,
type StepDefinition,
Expand Down Expand Up @@ -53,6 +54,7 @@ async function installAdminer(playground: PlaygroundClient) {
await runBlueprintV1Steps(blueprint, playground as UniversalPHP);
}

/** Install and open Adminer without moving WordPress into another storage partition. */
export function AdminerButton({
playground,
}: {
Expand Down Expand Up @@ -91,6 +93,7 @@ export function AdminerButton({
};
}, [playground]);

/** Open the installed tool through the matching WordPress browsing context. */
const handleOpenAdminer = async () => {
if (!playground) {
return;
Expand All @@ -117,7 +120,7 @@ export function AdminerButton({
const playgroundUrl = await playground.absoluteUrl;
if (playgroundUrl) {
window.open(
`${playgroundUrl}/adminer/`,
getWordPressPopupUrl(`${playgroundUrl}/adminer/`),
'_blank',
'noopener,noreferrer'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { logger } from '@php-wasm/logger';
import { Button, Icon, Flex, FlexItem } from '@wordpress/components';
import { external } from '@wordpress/icons';
import css from './style.module.css';
import { getWordPressPopupUrl } from '../../../lib/wordpress-popup';
import {
type PlaygroundClient,
type UniversalPHP,
Expand All @@ -27,6 +28,7 @@ async function installPhpMyAdmin(playground: PlaygroundClient) {
await runBlueprintV1Steps(blueprint, playground as UniversalPHP);
}

/** Install and open phpMyAdmin without moving WordPress into another storage partition. */
export function PhpMyAdminButton({
playground,
}: {
Expand Down Expand Up @@ -65,6 +67,7 @@ export function PhpMyAdminButton({
};
}, [playground]);

/** Open the installed tool through the matching WordPress browsing context. */
const handleOpenPhpMyAdmin = async () => {
if (!playground) {
return;
Expand All @@ -91,7 +94,9 @@ export function PhpMyAdminButton({
const playgroundUrl = await playground.absoluteUrl;
if (playgroundUrl) {
window.open(
`${playgroundUrl}${PHPMYADMIN_URL_PATH}${PHPMYADMIN_ENTRY_PATH}`,
getWordPressPopupUrl(
`${playgroundUrl}${PHPMYADMIN_URL_PATH}${PHPMYADMIN_ENTRY_PATH}`
),
'_blank',
'noopener,noreferrer'
);
Expand Down
30 changes: 30 additions & 0 deletions packages/playground/website/src/lib/wordpress-popup.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getWordPressPopupUrl } from './wordpress-popup';

vi.mock('virtual:website-config', () => ({ buildVersion: 'test' }));

describe('WordPress popup URLs', () => {
beforeEach(() => {
vi.stubGlobal('window', {
location: { origin: 'http://playground.localhost:9500' },
});
});
afterEach(() => vi.unstubAllGlobals());

it('keeps same-origin popup URLs unchanged', () => {
const url =
'http://playground.localhost:9500/scope:site/adminer/?select=wp_posts';
expect(getWordPressPopupUrl(url)).toBe(url);
});

it('preserves the scoped path, query and fragment inside the app-origin popup', () => {
const popup = new URL(
getWordPressPopupUrl(
'http://wp.playground.localhost:9500/scope:site/adminer/?select=wp_posts#result'
)
);
expect(popup.origin).toBe('http://playground.localhost:9500');
expect(popup.pathname).toBe('/wordpress-popup.html');
expect(popup.search).toBe('');
expect(popup.hash).toBe('#/scope:site/adminer/?select=wp_posts#result');
});
});
14 changes: 14 additions & 0 deletions packages/playground/website/src/lib/wordpress-popup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getRemoteUrl } from './config';

/** Keep a split-origin popup in the same service-worker partition as the running site. */
export function getWordPressPopupUrl(url: string): string {
const target = new URL(url);
const remote = getRemoteUrl();
if (target.origin === remote.origin) {
return target.href;
}
remote.pathname = '/wordpress-popup.html';
remote.search = '';
remote.hash = target.pathname + target.search + target.hash;
return remote.href;
}