Skip to content
Open
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
29 changes: 29 additions & 0 deletions block/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "team51/colophon",
"version": "0.1.0",
"title": "Colophon",
"category": "theme",
"icon": "editor-textcolor",
"description": "A block for displaying a colophon.",
"supports": {
"html": false,
"align": true,
"typography": {
"fontSize": true,
"lineHeight": true,
"__experimentalFontFamily": true,
"__experimentalFontStyle": true,
"__experimentalFontWeight": true,
"__experimentalLetterSpacing": true,
"__experimentalTextTransform": true,
"__experimentalDefaultControls": {
"fontSize": true
}
}
},
"textdomain": "team51-blocks",
"editorScript": "file:./index.js",
"render": "file:./render.php"
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know block context can be used to set a block where it can only be used in a post context or the like --

https://developer.wordpress.org/block-editor/reference-guides/block-api/block-context/

Is there an option we can set so that the block is only usable in a FSE / page template context, so we're not cluttering up the block options when someone's writing a post?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocks meant for use in FSE themes are called Theme Blocks, and I think this would be the way to do it:

https://github.com/WordPress/gutenberg/blob/e706d67c7482e752a4adaae3e0b0449234e19902/packages/block-library/src/navigation/block.json#L6

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, even if using theme as the category for the theme block. It is still visible in the post editor. The only option I can think of now is to remove the block programatically on the post editor. Should we go that route?

39 changes: 39 additions & 0 deletions block/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Import the ServerSideRender component from the @wordpress/server-side-render package.
* This component is used to render the block on the server side.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-server-side-render/
*/
import ServerSideRender from "@wordpress/server-side-render";

/**
* 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";

/**
* Import the block.json to get the block meta.
*/
import blockMeta from "./block.json";

/**
* 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(props) {
return (
<div {...useBlockProps()}>
<ServerSideRender
block={blockMeta.name}
attributes={props.attributes}
/>
</div>
);
}
29 changes: 29 additions & 0 deletions block/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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";

/**
* 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,

/**
* @see ./save.js
*/
save: () => null,
});
3 changes: 3 additions & 0 deletions block/render.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div <?php echo wp_kses_data( get_block_wrapper_attributes() ); ?>>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this wp_kses_data() call is necessary here -- as it is designed to be sanitizing not escaping, (so focused on content not context), and doesn't seem to be used by the Gutenberg team when doing similar tasks:

https://github.com/WordPress/gutenberg/blob/e706d67c7482e752a4adaae3e0b0449234e19902/docs/getting-started/tutorial.md?plain=1#L685-L687

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<?php do_action( 'team51_credits' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound ?>
</div>
29 changes: 29 additions & 0 deletions build/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "team51/colophon",
"version": "0.1.0",
"title": "Colophon",
"category": "theme",
"icon": "editor-textcolor",
"description": "A block for displaying a colophon.",
"supports": {
"html": false,
"align": true,
"typography": {
"fontSize": true,
"lineHeight": true,
"__experimentalFontFamily": true,
"__experimentalFontStyle": true,
"__experimentalFontWeight": true,
"__experimentalLetterSpacing": true,
"__experimentalTextTransform": true,
"__experimentalDefaultControls": {
"fontSize": true
}
}
},
"textdomain": "team51-blocks",
"editorScript": "file:./index.js",
"render": "file:./render.php"
}
1 change: 1 addition & 0 deletions build/index.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-server-side-render'), 'version' => '3eead5c1aa4c3d8aaba2');
1 change: 1 addition & 0 deletions build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions build/render.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div <?php echo wp_kses_data( get_block_wrapper_attributes() ); ?>>
<?php do_action( 'team51_credits' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound ?>
</div>
13 changes: 13 additions & 0 deletions colophon.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,16 @@ function () {
}
);
endif;

if ( ! function_exists( 'team51_colophon_register_block' ) ) {

/**
* Registers the Colophon block.
*
* @return void
*/
function team51_colophon_register_block() {
register_block_type_from_metadata( __DIR__ . DIRECTORY_SEPARATOR . 'build' );
}
add_action( 'init', 'team51_colophon_register_block' );
}
Loading