Skip to content
2 changes: 1 addition & 1 deletion includes/blocks/broadcasts/block.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 1,
"apiVersion": 2,
"name": "convertkit/broadcasts",
"title": "Kit Broadcasts",
"category": "kit",
Expand Down
2 changes: 1 addition & 1 deletion includes/blocks/form-builder-field-custom/block.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 1,
"apiVersion": 2,
"name": "convertkit/form-builder-field-custom",
"title": "Kit Form Builder: Custom Field",
"category": "kit",
Expand Down
2 changes: 1 addition & 1 deletion includes/blocks/form-builder-field-email/block.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 1,
"apiVersion": 2,
"name": "convertkit/form-builder-field-email",
"title": "Kit Form Builder: Email Field",
"category": "kit",
Expand Down
2 changes: 1 addition & 1 deletion includes/blocks/form-builder-field-name/block.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 1,
"apiVersion": 2,
"name": "convertkit/form-builder-field-name",
"title": "Kit Form Builder: Name Field",
"category": "kit",
Expand Down
2 changes: 1 addition & 1 deletion includes/blocks/form-builder/block.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 1,
"apiVersion": 2,
"name": "convertkit/form-builder",
"title": "Kit Form Builder",
"category": "kit",
Expand Down
2 changes: 1 addition & 1 deletion includes/blocks/form/block.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 1,
"apiVersion": 2,
"name": "convertkit/form",
"title": "Kit Form",
"category": "kit",
Expand Down
2 changes: 1 addition & 1 deletion includes/blocks/formtrigger/block.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 1,
"apiVersion": 2,
"name": "convertkit/formtrigger",
"title": "Kit Form Trigger",
"category": "kit",
Expand Down
2 changes: 1 addition & 1 deletion includes/blocks/product/block.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 1,
"apiVersion": 2,
"name": "convertkit/product",
"title": "Kit Product",
"category": "kit",
Expand Down
96 changes: 62 additions & 34 deletions resources/backend/js/gutenberg.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ function convertKitGutenbergRegisterBlock(block) {
// Define some constants for the various items we'll use.
const el = element.createElement;
const { registerBlockType } = blocks;
const { InspectorControls, InnerBlocks } = editor;
const { Fragment, useState } = element;
const { InspectorControls, InnerBlocks, useBlockProps } = editor;
const { useState } = element;
const {
Button,
Icon,
Expand Down Expand Up @@ -364,14 +364,19 @@ function convertKitGutenbergRegisterBlock(block) {
* @param {Object} props Block properties.
* @return {Object} Block settings sidebar elements.
*/
const editBlock = function (props) {
const EditBlock = function (props) {
const blockProps = useBlockProps();

console.log(props);
console.log(blockProps);

// If requesting an example of how this block looks (which is requested
// when the user adds a new block and hovers over this block's icon),
// show the preview image.
if (props.attributes.is_gutenberg_example === true) {
return (
Fragment,
{},
return el(
'div',
blockProps,
el('img', {
src: block.gutenberg_example_image,
})
Expand All @@ -381,7 +386,7 @@ function convertKitGutenbergRegisterBlock(block) {
// If no access token has been defined in the Plugin, or no resources exist in Kit
// for this block, show a message in the block to tell the user what to do.
if (!block.has_access_token || !block.has_resources) {
return DisplayNoticeWithLink(props);
return DisplayNoticeWithLink(props, blockProps);
}

// Build Inspector Control Panels, which will appear in the Sidebar when editing the Block.
Expand All @@ -401,7 +406,11 @@ function convertKitGutenbergRegisterBlock(block) {
block,
props
);
return editBlockWithPanelsAndPreview(panels, preview);
return editBlockWithPanelsAndPreview(
panels,
preview,
blockProps
);
}

// If no settings have been defined for this block, render the block with a notice
Expand All @@ -416,7 +425,11 @@ function convertKitGutenbergRegisterBlock(block) {
block.name,
block.gutenberg_help_description
);
return editBlockWithPanelsAndPreview(panels, preview);
return editBlockWithPanelsAndPreview(
panels,
preview,
blockProps
);
}

// If no render_callback is defined, render the block.
Expand All @@ -443,7 +456,11 @@ function convertKitGutenbergRegisterBlock(block) {
template,
})
);
return editBlockWithPanelsAndPreview(panels, preview);
return editBlockWithPanelsAndPreview(
panels,
preview,
blockProps
);
}

// Use the block's PHP's render() function by calling the ServerSideRender component.
Expand All @@ -455,7 +472,7 @@ function convertKitGutenbergRegisterBlock(block) {
// apply styles with i.e. convertkit-block.name.
className: 'convertkit-ssr-' + block.name,
});
return editBlockWithPanelsAndPreview(panels, preview);
return editBlockWithPanelsAndPreview(panels, preview, blockProps);
};

/**
Expand All @@ -464,19 +481,20 @@ function convertKitGutenbergRegisterBlock(block) {
*
* @since 3.0.0
*
* @param {Object} panels Block panels.
* @param {Object} preview Block preview.
* @return {Object} Block settings sidebar elements.
* @param {Object} panels Block panels.
* @param {Object} preview Block preview.
* @param {Object} blockProps Block properties.
* @return {Object} Block settings sidebar elements.
*/
const editBlockWithPanelsAndPreview = function (panels, preview) {
return el(
// Sidebar Panel with Fields.
Fragment,
{},
const editBlockWithPanelsAndPreview = function (
panels,
preview,
blockProps
) {
return el('div', blockProps, [
el(InspectorControls, {}, panels),
// Block Preview.
preview
);
preview,
]);
};

/**
Expand All @@ -488,7 +506,10 @@ function convertKitGutenbergRegisterBlock(block) {
*/
const saveBlock = function () {
if (typeof block.gutenberg_template !== 'undefined') {
return el('div', {}, el(InnerBlocks.Content));
// Use useBlockProps.save() to preserve styling classes and attributes
// from block supports (colors, typography, spacing, etc.)
const blockProps = useBlockProps.save();
return el('div', blockProps, el(InnerBlocks.Content));
}

// Deliberate; preview in the editor is determined by the return statement in `edit` above.
Expand All @@ -504,10 +525,11 @@ function convertKitGutenbergRegisterBlock(block) {
*
* @since 2.2.5
*
* @param {Object} props Block properties.
* @return {Object} Notice.
* @param {Object} props Block properties.
* @param {Object} blockProps Block properties.
* @return {Object} Notice.
*/
const DisplayNoticeWithLink = function (props) {
const DisplayNoticeWithLink = function (props, blockProps) {
// useState to toggle the refresh button's disabled state.
const [buttonDisabled, setButtonDisabled] = useState(false);

Expand Down Expand Up @@ -541,13 +563,19 @@ function convertKitGutenbergRegisterBlock(block) {
// Return the element.
return el(
'div',
{
// convertkit-no-content class allows resources/backend/css/gutenberg.css
// to apply styling/branding to the block.
className:
'convertkit-' + block.name + ' convertkit-no-content',
},
elements
blockProps,
el(
'div',
{
// convertkit-no-content class allows resources/backend/css/gutenberg.css
// to apply styling/branding to the block.
className:
'convertkit-' +
block.name +
' convertkit-no-content',
},
elements
)
);
};

Expand Down Expand Up @@ -851,7 +879,7 @@ function convertKitGutenbergRegisterBlock(block) {
},

// Editor.
edit: editBlock,
edit: EditBlock,

// Output.
save: saveBlock,
Expand Down
12 changes: 7 additions & 5 deletions tests/Support/Helper/WPGutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ class WPGutenberg extends \Codeception\Module
{
/**
* Helper method to switch to the Gutenberg editor Iframe.
* Use this method if all blocks use the Block API v3,
* as this means Gutenberg will be served in an Iframe.
* At present, we use v2 to provide backwards compatibility
* down to WordPress 5.6:
* https://developer.wordpress.org/block-editor/reference-guides/block-api/block-api-versions/
* Use this method if all blocks use the block editor apiVersion 3,
* as this means the block editor will be served in an Iframe.
*
* Any blocks using apiVersion 2 or lower will result in the block editor
* *not* being served in an Iframe
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-api-versions/
*
* @since 2.7.7
*
Expand Down
Loading