-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add documentation to example-blocks - php pattern globbing to eliminate need for adding individual blocks - add globbing to scripts to eliminate need for adding individual blocks - add plugin lint scripts
- Loading branch information
1 parent
a0a13e3
commit d7856bb
Showing
28 changed files
with
17,908 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,5 +47,7 @@ coverage/ | |
|
||
plugins/rollbar/ | ||
|
||
src/plugins/**/build/ | ||
|
||
# DB imports/exports | ||
sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Custom Blocks | ||
|
||
We have a plugin for custom blocks called `example-blocks`, which lives in `src/plugins`. For the blocks to be available in WordPress, these steps must be taken: | ||
|
||
1. Run `npm run plugins:install` to install the plugin's npm dependencies | ||
1. Run `npm start` for local development (this run's the plugin's `npm start` command) | ||
1. Activate the "Example Blocks" plugin from the WordPress menu | ||
|
||
For production builds, running `npm run build:prod` will also work, outputting production bundles for the blocks. | ||
|
||
## Creating a New Custom Block | ||
|
||
Follow these steps to create a new custom block and wire it up with the normal development/build processes: | ||
|
||
1. Create a new folder at `src/plugins/example-blocks/<block-name>` | ||
1. Either copy files from another block or manually create these files: | ||
- `block.json`: configuration/metadata for the block | ||
- `src/index.js`: entry point for the JS bundle | ||
- `src/edit.js`: the component used while editing | ||
- `src/save.js`: the component rendered on the site | ||
- `src/editor.scss`: custom styles for the editor view | ||
- `src/style.scss`: custom styles for the block when rendered on the site | ||
1. Configure the custom block by updating `block.json`, namely the `name`, `title`, `icon`, and `description` fields | ||
1. Implement the edit function, which will usually be form controls corresponding to attributes that you define in `index.js` | ||
1. Implement the save function, which will consume the attributes defined in `index.js` and render the block's desired markup | ||
|
||
## Useful Resources | ||
|
||
- [Create a Block Tutorial](https://developer.wordpress.org/block-editor/getting-started/create-block/) | ||
- [Component Reference](https://developer.wordpress.org/block-editor/reference-guides/components/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"printWidth": 110, | ||
"endOfLine": "lf", | ||
"trailingComma": "es5", | ||
"tabWidth": 2, | ||
"useTabs": true, | ||
"semi": true, | ||
"singleQuote": true, | ||
"quoteProps": "as-needed", | ||
"jsxSingleQuote": false, | ||
"bracketSpacing": true, | ||
"bracketSameLine": false, | ||
"arrowParens": "always" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 3, | ||
"name": "example-blocks/hello-world", | ||
"version": "0.1.0", | ||
"title": "Hello World", | ||
"category": "example-custom-blocks", | ||
"icon": "nametag", | ||
"description": "Prints Hello World!", | ||
"supports": { | ||
"html": false | ||
}, | ||
"textdomain": "example-blocks", | ||
"editorScript": "file:./build/index.js", | ||
"editorStyle": "file:./build/index.css", | ||
"style": "file:./build/style-index.css" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Retrieves the translation of text. | ||
* | ||
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/ | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* React hook that is used to mark the block wrapper element. | ||
* It provides all the necessary props like the class name. | ||
* | ||
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops | ||
*/ | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files. | ||
* Those files can contain any CSS code that gets applied to the editor. | ||
* | ||
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css | ||
*/ | ||
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 | ||
* | ||
* @return {WPElement} Element to render. | ||
*/ | ||
export default function Edit() { | ||
return <p {...useBlockProps()}>{__('Hello World!', 'example-blocks')}</p>; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/plugins/example-blocks/blocks/hello-world/src/editor.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* The following styles get applied inside the editor only. | ||
* | ||
* Replace them with your own styles or remove the file completely. | ||
*/ | ||
|
||
.wp-block-example-blocks-hello-world { | ||
|
||
/* insert custom styles here */ | ||
} |
39 changes: 39 additions & 0 deletions
39
src/plugins/example-blocks/blocks/hello-world/src/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* 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 save from './save'; | ||
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, | ||
|
||
/** | ||
* @see ./save.js | ||
*/ | ||
save, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* React hook that is used to mark the block wrapper element. | ||
* It provides all the necessary props like the class name. | ||
* | ||
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops | ||
*/ | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* The save function defines the way in which the different attributes should | ||
* be combined into the final markup, which is then serialized by the block | ||
* editor into `post_content`. | ||
* | ||
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#save | ||
* | ||
* @return {WPElement} Element to render. | ||
*/ | ||
export default function save() { | ||
return <p {...useBlockProps.save()}>{'Hello World!'}</p>; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/plugins/example-blocks/blocks/hello-world/src/style.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* 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-example-blocks-hello-world { | ||
background-color: #21759b; | ||
color: #fff; | ||
padding: 2px; | ||
} |
17 changes: 17 additions & 0 deletions
17
src/plugins/example-blocks/blocks/reverse-string/block.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 3, | ||
"name": "example-blocks/reverse-string", | ||
"version": "0.1.0", | ||
"title": "Reverse String", | ||
"category": "example-custom-blocks", | ||
"icon": "smiley", | ||
"description": "Prints a user-entered string in reverse order.", | ||
"supports": { | ||
"html": false | ||
}, | ||
"textdomain": "example-blocks", | ||
"editorScript": "file:./build/index.js", | ||
"editorStyle": "file:./build/index.css", | ||
"style": "file:./build/style-index.css" | ||
} |
39 changes: 39 additions & 0 deletions
39
src/plugins/example-blocks/blocks/reverse-string/src/edit.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { TextControl } from '@wordpress/components'; | ||
|
||
/** | ||
* React hook that is used to mark the block wrapper element. | ||
* It provides all the necessary props like the class name. | ||
* | ||
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops | ||
*/ | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files. | ||
* Those files can contain any CSS code that gets applied to the editor. | ||
* | ||
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css | ||
*/ | ||
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. | ||
* | ||
* @param {Object} props | ||
* @param {Object} props.attributes | ||
* @param {Function} props.setAttributes | ||
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit | ||
* | ||
* @return {WPElement} Element to render. | ||
*/ | ||
export default function Edit({ attributes, setAttributes }) { | ||
const { textToReverse } = attributes; | ||
const updateTextToReverse = (newText) => setAttributes({ textToReverse: newText }); | ||
|
||
return ( | ||
<div {...useBlockProps()}> | ||
<TextControl label="Text to Reverse" value={textToReverse} onChange={updateTextToReverse} /> | ||
</div> | ||
); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/plugins/example-blocks/blocks/reverse-string/src/editor.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* The following styles get applied inside the editor only. | ||
* | ||
* Replace them with your own styles or remove the file completely. | ||
*/ | ||
|
||
.wp-block-example-blocks-reverse-string { | ||
padding: 1em; | ||
box-sizing: border-box; | ||
border: none; | ||
border-radius: 2px; | ||
box-shadow: inset 0 0 0 1px #1e1e1e; | ||
} |
Oops, something went wrong.