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

Gutenberg block: WordPress Playground Demo #126

Merged
merged 7 commits into from
Dec 23, 2023

Conversation

dawidurbanski
Copy link
Contributor

@dawidurbanski dawidurbanski commented Nov 19, 2023

What?

This pull request aims to demonstrate a possible usage of WordPress Playground inside a Gutenberg block.

CleanShot 2023-11-19 at 16 20 53

Why?

We should make adding WordPress Playground to any WordPress site as easy as possible.

With this highly customizable block, users can embed WordPress Playground within their posts or pages content.

CleanShot 2023-11-19 at 16 27 16

Possibility of adding files to the Playground instance from the code editor make this the easiest way of integrating Playground with your WordPress website.

CleanShot 2023-11-21 at 10 14 24

The code editor can be editable for users on the front-end so it may serve a role of interactive tutorial/guide.

CleanShot 2023-11-21 at 10 17 31

All features

  • Embed WordPress Playground
  • Log in automatically
  • Specify landing page
  • Create new post or page
  • Redirect to newly created post or page
  • Specify blueprint to apply on init (WIP)
  • Enable/disable code editor
  • Make code editor read-only
  • Allow multiple files in code editor
  • Specify mode:
    • Plugin: add all files to a plugin and enable it in Playground
    • Editor script: add script inline via wp_add_inline_script

Caveats

  • This only works on sites with https (even locally) - Playground limitation
  • It's not going to work inside Playground itself - Unable to connect to playground remote, I didn't check with wp-now, it could work there but probably also going to fail because of lack of https
  • The block has to use "apiVersion": 2 to prevent wrapping in iframe - it won't work inside another iframe for now - again Playground limitation
  • No build steps available (we are thinking about adding support for node in browser in the future)

Notes

  • Blueprints support not yet implemented (but the control is added in Edit component),
  • This is just POC. PlaygroundDemo.tsx definitely requires some work (to split it into multiple components),
  • No internationalization in place,
  • We may change the refreshing behaviour in InspectorControls input fields from onChange to onBlur to prevent so many Playground inits,
  • Files management is a bit primitive for now, no folders or subfolders allowed yet,
  • Currently supported editor languages: JavaScript, JSON, PHP,
  • We may add option for dark theme in the code editor.
  • I developed this using the standard @wordpress/scripts package. I was unable to add this to the project custom build and nx shared package.json so this PR is as is. Any contributions to make it work with this repo are welcomed,

Testing Instructions

Two options

From zip

  1. git clone git@github.com:dawidurbanski/playground-tools.git
  2. cd playground-tools/packages/playground-demo-block
  3. npm install && npm run plugin-zip
  4. Upload generated zip file to your WordPress and enable it
  5. Go to post or page edit screen
  6. Insert Playground Demo block or copy and paste in the editor the example block from "Complete block example" below

From source

  1. Go to wp-content/plugins of your WordPress instalation
  2. Run git clone git@github.com:dawidurbanski/playground-tools.git
  3. Run npm install && npm run build
  4. Open your WordPress and enable the plugin
  5. Go to post or page edit screen
  6. Insert Playground Demo block or copy and paste in the editor the example block from "Complete block example" below

Complete block example in plugin mode

Expand to grab block code
<!-- wp:group {"align":"wide","layout":{"type":"default"}} -->
<div class="wp-block-group alignwide"><!-- wp:wordpress-playground/playground-demo {"codeEditor":true,"files":[{"name":"plugin.php","file":"\u003c?php\n/**\n * Plugin name: Demo Block\n */\n\nfunction playground_demo_block_init() {\n\tregister_block_type( __DIR__ );\n}\nadd_action( 'init', 'playground_demo_block_init' );\n"},{"name":"block.json","file":"{\n    \u0022apiVersion\u0022: 3,\n    \u0022title\u0022: \u0022Example: Basic\u0022,\n    \u0022name\u0022: \u0022example/example-01-basic\u0022,\n    \u0022icon\u0022: \u0022universal-access-alt\u0022,\n    \u0022editorScript\u0022: \u0022file:./block.js\u0022\n}"},{"name":"block.js","file":"wp.blocks.registerBlockType(\n    \u0022example/example-01-basic\u0022,\n    {\n        title: \u0022Example: Basic\u0022,\n        edit: function () {\n            return wp.element.createElement(\n                \u0022p\u0022,\n                {},\n                \u0022Hi!\u0022\n            );\n        }\n    }\n);"},{"name":"block.asset.php","file":"\u003c?php return array(\n  'dependencies' =\u003e array('wp-blocks'),\n  'version' =\u003e '0.1'\n);"}],"landingPageUrl":"/wp-admin/plugins.php","createNewPost":true,"createNewPostContent":"\u003c!\u002d\u002d wp:example/example-01-basic /\u002d\u002d\u003e","redirectToPost":true,"redirectToPostType":"admin"} /--></div>
<!-- /wp:group -->

@adamziel
Copy link
Collaborator

adamziel commented Nov 21, 2023

Something tells me @jonathanbossenger, @westnz, and @juanmaguitar will love this. Thank you so much for making the web better @dawidurbanski!

@adamziel adamziel added the Enhancement New feature or request label Nov 21, 2023
@jonathanbossenger
Copy link
Contributor

This is indeed exciting. I will add it to my list of things to test out!

@bph bph mentioned this pull request Nov 21, 2023
@westnz
Copy link

westnz commented Nov 21, 2023

This is amazing! 🥳

@adamziel
Copy link
Collaborator

adamziel commented Dec 23, 2023

Thank you @dawidurbanski! I got it to work with nx and this repo's structure. I'm going to merge it as–is to enable iterating on this block.

@adamziel adamziel merged commit 29dba5d into WordPress:trunk Dec 23, 2023
2 checks passed
johnhooks pushed a commit to johnhooks/playground-tools that referenced this pull request Oct 11, 2024
## Description

Adds support for previewing Gutenberg Pull Requests via the a `gutenberg-pr` query parameter:

https://playground.wordpress.net/wordpress.html?gutenberg-pr=47739&url=/wp-admin/plugins.php?test=test

My test PR: WordPress/gutenberg#47739

<img width="1149" alt="CleanShot 2023-02-03 at 19 33 17@2x" src="https://user-images.githubusercontent.com/205419/216681315-2af555c1-6249-4758-afe0-aa54b66c68a3.png">

## Implementation

The plugin bundle is downloaded from GitHub CI and installed by applying the following Blueprint:

```js
function applyGutenbergPRSteps(prNumber: number): StepDefinition[] {
	return [
		{
			step: 'mkdir',
			path: '/wordpress/pr',
		},
		{
			step: 'writeFile',
			path: '/wordpress/pr/pr.zip',
			data: {
				resource: 'url',
				url: `/plugin-proxy?org=WordPress&repo=gutenberg&workflow=Build%20Gutenberg%20Plugin%20Zip&artifact=gutenberg-plugin&pr=${prNumber}`,
				caption: `Downloading Gutenberg PR ${prNumber}`,
			},
			progress: {
				weight: 2,
				caption: `Applying Gutenberg PR ${prNumber}`,
			},
		},
		{
			step: 'unzip',
			zipPath: '/wordpress/pr/pr.zip',
			extractToPath: '/wordpress/pr',
		},
		{
			step: 'installPlugin',
			pluginZipFile: {
				resource: 'vfs',
				path: '/wordpress/pr/gutenberg.zip',
			},
		},
	];
}
```

Gutenberg PR preview from playground.wordpress.net can be embedded in other apps via an iframe.

To implement your own PR previewer that pulls data from another repo, you'll need to expose an API endpoint to download the ZIP bundle and then plug it in the `writeFile` step above. Once you have that, here's how you'd apply a custom blueprint:

```ts
import { startPlaygroundWeb } from "https://unpkg.com/@wp-playground/client@0.1.32/index.js";
startPlaygroundWeb({
	iframe,
	remoteUrl: `https://playground.wordpress.net/remote.html`,
	blueprint: {
		steps: applyPR( 47339 )
	}
})

function applyPR( prNumber ) {
	return [
		{
			step: 'mkdir',
			path: '/wordpress/pr',
		},
		// ...
	];
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

4 participants