Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kellymears/block-sandbox

Better WordPress block testing for theme & plugin developers.
https://github.com/kellymears/block-sandbox

gutenberg wordpress wordpress-plugin

Last synced: 3 months ago
JSON representation

Better WordPress block testing for theme & plugin developers.

Awesome Lists containing this project

README

        

# Block Editor Sandbox

[![Latest Stable Version](https://poser.pugx.org/tiny-pixel/block-sandbox/v/stable)](https://packagist.org/packages/tiny-pixel/block-sandbox) [![Total Downloads](https://poser.pugx.org/tiny-pixel/block-sandbox/downloads)](https://packagist.org/packages/tiny-pixel/block-sandbox) [![License](https://poser.pugx.org/tiny-pixel/block-sandbox/license)](https://packagist.org/packages/tiny-pixel/block-sandbox)

This plugin registers a `Sandbox` posttype preset with a template including many common/core blocks in diverse arrangements. It requires zero configuration to start utilizing.

It was inspired by `coblocks/block-unit-test` (now `godaddy/block-unit-test` 😬).

**In comparison, `block-sandbox` aims to be:**

βœ… Hackable, with an easy-to-read and fun-to-modify API

βœ… Easy to keep updated as the block spec continues to change

βœ… Not the intellectual property of Godaddy. Nothing against Rich Tabor (get it!) but _no, thanks_.

## Simple usage

```console
$ composer require tiny-pixel/block-sandbox
$ wp plugin activate block-sandbox
```

## Programmatic usage & configuration

At a minimum you need to create the CPT, apply the template and register it with WordPress:

```php
add_action('init', function () {
(new Sandbox())
->create()
->applyTemplate()
->register();
});
```

Additionally, there are several methods available for you to utilize in modifying the behavior of the plugin.

### Append extra blocks

If you would like to add additional blocks to the template you can do so with the `appendBlocks` method.

```php
$extra = [['core/cover', [
'align' => 'wide',
'url' => 'example.png',
'title' => 'Additional block'
]]];

add_action('init', function () use ($extra) {
(new Sandbox())
->createSandbox()
->appendBlocks($extra)
->applyTemplate()
->register();
});
```

### Overwrite template

If you would like to wholesale replace the included template with your own you can do so using `setTemplate`. Just pass in an array of the blocks.

```php
$template = [
['core/cover', [
'align' => 'wide',
'url' => 'example.png',
'title' => 'Additional block']],

['core-embed/vimeo', [
'caption' => 'Γ©coute: El Guincho',
'align' => 'wide',
'url' => 'https://vimeo.com/70237487']],
];

add_action('init', function () use ($template) {
(new Sandbox())
->createSandbox()
->setTemplate($template)
->applyTemplate()
->register();
});
```

### Modify labels

If you would like to change `sandbox` to something else you can do that using `setLabel`. It takes an array with a new `id`, `singular`, and `plural` form of your desired label.

```php
$label = [
'id' => 'test',
'singular' => 'Test',
'plural' => 'Tests',
];

add_action('init', function () use ($label) {
(new Sandbox())
->create()
->setLabel($label)
->applyTemplate()
->register();
});
```

### All of the above

```php
add_action('init', function () use ($template, $extra, $posttype) {
(new Sandbox())
->create()
->setLabel($posttype)
->setTemplate($template)
->appendBlocks($extra)
->applyTemplate()
->register();
});
```

## License

[MIT License](https://github.com/kellymears/block-testing/blob/master/LICENSE.md). Happy hacking!