Skip to content

Commit

Permalink
WDSBT-20 - Add block template
Browse files Browse the repository at this point in the history
  • Loading branch information
khleomix committed Jun 17, 2024
1 parent 0a4ea16 commit 1f8626c
Show file tree
Hide file tree
Showing 13 changed files with 351 additions and 0 deletions.
26 changes: 26 additions & 0 deletions inc/block-template/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# WDS Block Template

This template is configured to generate a block that is ready for block registration using the [`@wordpress/create-block`](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-create-block/) tool.

## Usage

Run the following in the terminal of your choice:

`npx @wordpress/create-block --template ../../inc/block-template --no-plugin`


## Structure

Once the command has completed, the following structure will be created:

``` text
📁src
└── 📁blocks
└── 📁{example-block}
└── block.json
└── edit.js
└── editor.scss
└── index.js
└── render.php
└── style.scss
```
22 changes: 22 additions & 0 deletions inc/block-template/block/edit.js.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* WordPress Dependencies
*/
import { __ } from '@wordpress/i18n';
import { useBlockProps } from '@wordpress/block-editor';

/**
* Internal Dependencies
*/
import './editor.scss';

/**
* The edit function describes the structure of your block in the context of the
* editor. This represents what the editor will render when the block is used.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
*/
export default function Edit() {
return (
<p {...useBlockProps()}>{__('{{title}} – hello from the editor!', '{{textdomain}}')}</p>
);
}
9 changes: 9 additions & 0 deletions inc/block-template/block/editor.scss.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* The following styles get applied inside the editor only.
*
* Replace them with your own styles or remove the file completely.
*/

.wp-block-{{namespace}}-{{slug}} {
border: 1px dotted var(--wp--preset--color--grey-400);
}
33 changes: 33 additions & 0 deletions inc/block-template/block/index.js.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Registers a new block provided a unique name and an object defining its behavior.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
*/
import { registerBlockType } from '@wordpress/blocks';

/**
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
* All files containing `style` keyword are bundled together. The code used
* gets applied both to the front of your site and to the editor.
*
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
*/
import './style.scss';

/**
* Internal dependencies
*/
import Edit from './edit';
import metadata from './block.json';

/**
* Every block starts by registering a new block type definition.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
*/
registerBlockType(metadata.name, {
/**
* @see ./edit.js
*/
edit: Edit,
});
35 changes: 35 additions & 0 deletions inc/block-template/block/render.php.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* PHP file to use when rendering the block type on the server to show on the front end.
*
* The following variables are exposed to the file:
* $attributes (array): The block attributes.
* $content (string): The block default content.
* $block (WP_Block): The block instance.
*
* @see https://github.com/WordPress/gutenberg/blob/trunk/docs/reference-guides/block-api/block-metadata.md#render
*/

{{#isInteractiveVariant}}
// Define some global state
wp_interactivity_state(
'{{slug}}',
array()
);

// Define some context.
$context = array();
?>
<p <?php echo wp_kses_data( get_block_wrapper_attributes() ); ?>
data-wp-interactive="{{slug}}"
<?php echo wp_interactivity_data_wp_context( $context ); ?>
>
<?php esc_html_e( '{{title}} – hello from an interactive block!', '{{textdomain}}' ); ?>
</p>
{{/isInteractiveVariant}}
{{#isDynamicVariant}}
?>
<p <?php echo wp_kses_data( get_block_wrapper_attributes() ); ?>>
<?php esc_html_e( '{{title}} – hello from a dynamic block!', '{{textdomain}}' ); ?>
</p>
{{/isDynamicVariant}}
10 changes: 10 additions & 0 deletions inc/block-template/block/style.scss.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* The following styles get applied both on the front of your site
* and in the editor.
*
* Replace them with your own styles or remove the file completely.
*/

.wp-block-{{namespace}}-{{slug}} {
border: 1px dotted var(--wp--preset--color--grey-400);
}
12 changes: 12 additions & 0 deletions inc/block-template/block/view.js.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{#isInteractiveVariant}}
/**
* WordPress dependencies
*/
import { store } from '@wordpress/interactivity';

store( '{{slug}}', {
state: {},
actions: {},
callbacks: {},
} );
{{/isInteractiveVariant}}
43 changes: 43 additions & 0 deletions inc/block-template/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Dependencies
*/
const { join } = require('path');

module.exports = {
defaultValues: {
transformer: (view) => {
const {
variantVars: { isInteractiveVariant },
} = view;
return {
...view,
requiresAtLeast: isInteractiveVariant ? '6.5' : '6.1',
};
},
author: 'WebDevStudios',
category: 'wds-blocks',
dashicon: 'pets',
description: 'A custom block created by the create-block for the theme',
namespace: 'wdsbt',
render: 'file:./render.php',
version: '1.0.0',
customPackageJSON: {
prettier: '@wordpress/prettier-config',
},
},
variants: {
dynamic: {},
interactive: {
viewScriptModule: 'file:./view.js',
customScripts: {
build: 'wp-scripts build --experimental-modules',
start: 'wp-scripts start --experimental-modules',
},
supports: {
interactive: true,
},
},
},
pluginTemplatesPath: join(__dirname, 'plugin'),
blockTemplatesPath: join(__dirname, 'block'),
};
49 changes: 49 additions & 0 deletions inc/block-template/plugin/$slug.php.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Plugin Name: {{title}}
{{#pluginURI}}
* Plugin URI: {{{pluginURI}}}
{{/pluginURI}}
{{#description}}
* Description: {{description}}
{{/description}}
* Requires at least: {{requiresAtLeast}}
* Requires PHP: 7.0
* Version: {{version}}
{{#author}}
* Author: {{author}}
{{/author}}
{{#license}}
* License: {{license}}
{{/license}}
{{#licenseURI}}
* License URI: {{{licenseURI}}}
{{/licenseURI}}
* Text Domain: {{textdomain}}
{{#domainPath}}
* Domain Path: {{{domainPath}}}
{{/domainPath}}
{{#updateURI}}
* Update URI: {{{updateURI}}}
{{/updateURI}}
*
* @package {{slugPascalCase}}
*/

namespace {{slugPascalCase}};

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function {{slugSnakeCase}}_block_init() {
register_block_type_from_metadata( __DIR__ . '/build' );
}
add_action( 'init', __NAMESPACE__ . '{{slugSnakeCase}}_block_init' );
18 changes: 18 additions & 0 deletions inc/block-template/plugin/.editorconfig.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

# WordPress Coding Standards
# https://make.wordpress.org/core/handbook/coding-standards/

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab

[*.{yml,yaml}]
indent_style = space
indent_size = 2
3 changes: 3 additions & 0 deletions inc/block-template/plugin/.eslintrc.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": [ "plugin:@wordpress/eslint-plugin/recommended" ]
}
30 changes: 30 additions & 0 deletions inc/block-template/plugin/.gitignore.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Coverage directory used by tools like istanbul
coverage

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Output of `npm pack`
*.tgz

# Output of `wp-scripts plugin-zip`
*.zip

# dotenv environment variables file
.env
61 changes: 61 additions & 0 deletions inc/block-template/plugin/readme.txt.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
=== {{title}} ===
{{#author}}
Contributors: {{author}}
{{/author}}
Tags: block
Tested up to: 6.1
Stable tag: {{version}}
{{#license}}
License: {{license}}
{{/license}}
{{#licenseURI}}
License URI: {{{licenseURI}}}
{{/licenseURI}}

{{description}}

== Description ==

This is the long description. No limit, and you can use Markdown (as well as in the following sections).

For backwards compatibility, if this section is missing, the full length of the short description will be used, and
Markdown parsed.

== Installation ==

This section describes how to install the plugin and get it working.

e.g.

1. Upload the plugin files to the `/wp-content/plugins/{{slug}}` directory, or install the plugin through the WordPress plugins screen directly.
1. Activate the plugin through the 'Plugins' screen in WordPress


== Frequently Asked Questions ==

= A question that someone might have =

An answer to that question.

= What about foo bar? =

Answer to foo bar dilemma.

== Screenshots ==

1. This screen shot description corresponds to screenshot-1.(png|jpg|jpeg|gif). Note that the screenshot is taken from
the /src directory or the directory that contains the stable readme.txt (tags or trunk). Screenshots in the /src
directory take precedence. For example, `/src/screenshot-1.png` would win over `/tags/4.3/screenshot-1.png`
(or jpg, jpeg, gif).
2. This is the second screen shot

== Changelog ==

= {{version}} =
* Release

== Arbitrary section ==

You may provide arbitrary sections, in the same format as the ones above. This may be of use for extremely complicated
plugins where more information needs to be conveyed that doesn't fit into the categories of "description" or
"installation." Arbitrary sections will be shown below the built-in sections outlined above.

0 comments on commit 1f8626c

Please sign in to comment.