diff --git a/assets/before-after/frontblocks-before-after-option.jsx b/assets/before-after/frontblocks-before-after-option.jsx index d4f5e489..1f391cad 100644 --- a/assets/before-after/frontblocks-before-after-option.jsx +++ b/assets/before-after/frontblocks-before-after-option.jsx @@ -288,6 +288,7 @@ function BeforeAfterEdit( props ) { // Register the block. registerBlockType( 'frontblocks/before-after', { + apiVersion: 3, title: __( 'FrontBlocks - Before After', 'frontblocks' ), description: __( 'Compare two images with a draggable before/after slider.', 'frontblocks' ), category: 'media', diff --git a/assets/before-after/frontblocks-before-after.js b/assets/before-after/frontblocks-before-after.js index 949991c2..fd36aa32 100644 --- a/assets/before-after/frontblocks-before-after.js +++ b/assets/before-after/frontblocks-before-after.js @@ -295,6 +295,7 @@ function BeforeAfterEdit(props) { // Register the block. registerBlockType('frontblocks/before-after', { + apiVersion: 3, title: __('FrontBlocks - Before After', 'frontblocks'), description: __('Compare two images with a draggable before/after slider.', 'frontblocks'), category: 'media', diff --git a/assets/google-signin/frontblocks-google-signin-block.js b/assets/google-signin/frontblocks-google-signin-block.js index 74937f08..bd590794 100644 --- a/assets/google-signin/frontblocks-google-signin-block.js +++ b/assets/google-signin/frontblocks-google-signin-block.js @@ -12,6 +12,7 @@ var __ = i18n.__; var registerBlockType = blocks.registerBlockType; var InspectorControls = blockEditor.InspectorControls; + var useBlockProps = blockEditor.useBlockProps; var PanelBody = components.PanelBody; var SelectControl = components.SelectControl; var TextControl = components.TextControl; @@ -27,10 +28,13 @@ ); registerBlockType( 'frontblocks/google-login', { + apiVersion: 3, icon: googleIcon, edit: function ( props ) { var attributes = props.attributes; var setAttributes = props.setAttributes; + // API version 3: the root element of the edit output must carry the block props. + var blockProps = useBlockProps(); return el( Fragment, @@ -62,18 +66,22 @@ } ) ) ), - ServerSideRender - ? el( ServerSideRender, { - block: 'frontblocks/google-login', - attributes: attributes, - } ) - : el( - 'p', - {}, - 'signup' === attributes.mode - ? __( 'Sign up with Google', 'frontblocks' ) - : __( 'Sign in with Google', 'frontblocks' ) - ) + el( + 'div', + blockProps, + ServerSideRender + ? el( ServerSideRender, { + block: 'frontblocks/google-login', + attributes: attributes, + } ) + : el( + 'p', + {}, + 'register' === attributes.mode + ? __( 'Sign up with Google', 'frontblocks' ) + : __( 'Sign in with Google', 'frontblocks' ) + ) + ) ); }, save: function () { diff --git a/assets/insert-post/frontblocks-insert-post-option.js b/assets/insert-post/frontblocks-insert-post-option.js index 5b8919ef..f605a6cb 100644 --- a/assets/insert-post/frontblocks-insert-post-option.js +++ b/assets/insert-post/frontblocks-insert-post-option.js @@ -271,6 +271,7 @@ function InsertPostBlock(props) { // Register the custom block var registerBlockType = wp.blocks.registerBlockType; registerBlockType('frontblocks/insert-post', { + apiVersion: 3, title: __('FrontBlocks - Insert Post', 'frontblocks'), description: __('Display content from another post or page', 'frontblocks'), category: 'generateblocks', diff --git a/assets/insert-post/frontblocks-insert-post-option.jsx b/assets/insert-post/frontblocks-insert-post-option.jsx index 3a7dcdda..5bf08111 100644 --- a/assets/insert-post/frontblocks-insert-post-option.jsx +++ b/assets/insert-post/frontblocks-insert-post-option.jsx @@ -270,6 +270,7 @@ function InsertPostBlock(props) { const { registerBlockType } = wp.blocks; registerBlockType('frontblocks/insert-post', { + apiVersion: 3, title: __('FrontBlocks - Insert Post', 'frontblocks'), description: __('Display content from another post or page', 'frontblocks'), category: 'generateblocks', diff --git a/assets/product-categories/frontblocks-product-categories-option.jsx b/assets/product-categories/frontblocks-product-categories-option.jsx index c183a4b8..56e49a2b 100644 --- a/assets/product-categories/frontblocks-product-categories-option.jsx +++ b/assets/product-categories/frontblocks-product-categories-option.jsx @@ -611,6 +611,7 @@ function ProductCategoriesEdit(props) { } registerBlockType('frontblocks/product-categories', { + apiVersion: 3, title: __('FrontBlocks - Product Categories', 'frontblocks'), description: __('Display a list of WooCommerce Product Categories.', 'frontblocks'), category: 'woocommerce', diff --git a/assets/product-categories/frontblocks-product-categories.js b/assets/product-categories/frontblocks-product-categories.js index 4e374f82..ee55407e 100644 --- a/assets/product-categories/frontblocks-product-categories.js +++ b/assets/product-categories/frontblocks-product-categories.js @@ -757,6 +757,7 @@ function ProductCategoriesEdit(props) { })))); } registerBlockType('frontblocks/product-categories', { + apiVersion: 3, title: __('FrontBlocks - Product Categories', 'frontblocks'), description: __('Display a list of WooCommerce Product Categories.', 'frontblocks'), category: 'woocommerce', diff --git a/assets/reading-time/frontblocks-reading-time-option.jsx b/assets/reading-time/frontblocks-reading-time-option.jsx index e294b574..0a764014 100644 --- a/assets/reading-time/frontblocks-reading-time-option.jsx +++ b/assets/reading-time/frontblocks-reading-time-option.jsx @@ -201,6 +201,7 @@ function ReadingTimeEdit(props) { // Register the block. registerBlockType('frontblocks/reading-time', { + apiVersion: 3, title: __('FrontBlocks - Reading Time', 'frontblocks'), description: __('Display estimated reading time for the current post or page.', 'frontblocks'), category: 'common', diff --git a/assets/reading-time/frontblocks-reading-time.js b/assets/reading-time/frontblocks-reading-time.js index 834e6a5e..9a5af6a1 100644 --- a/assets/reading-time/frontblocks-reading-time.js +++ b/assets/reading-time/frontblocks-reading-time.js @@ -252,6 +252,7 @@ function ReadingTimeEdit(props) { // Register the block. registerBlockType('frontblocks/reading-time', { + apiVersion: 3, title: __('FrontBlocks - Reading Time', 'frontblocks'), description: __('Display estimated reading time for the current post or page.', 'frontblocks'), category: 'common', diff --git a/assets/stacked-images/frontblocks-stacked-images-option.jsx b/assets/stacked-images/frontblocks-stacked-images-option.jsx index 02b90f50..e1d6ae2b 100644 --- a/assets/stacked-images/frontblocks-stacked-images-option.jsx +++ b/assets/stacked-images/frontblocks-stacked-images-option.jsx @@ -161,6 +161,7 @@ function StackedImagesEdit(props) { // Register the block. registerBlockType('frontblocks/stacked-images', { + apiVersion: 3, title: __('FrontBlocks - Stacked Images', 'frontblocks'), description: __('Display images with stacked animation effect from different directions.', 'frontblocks'), category: 'media', diff --git a/assets/stacked-images/frontblocks-stacked-images.js b/assets/stacked-images/frontblocks-stacked-images.js index 8a606a74..341b603d 100644 --- a/assets/stacked-images/frontblocks-stacked-images.js +++ b/assets/stacked-images/frontblocks-stacked-images.js @@ -188,6 +188,7 @@ function StackedImagesEdit(props) { // Register the block. registerBlockType('frontblocks/stacked-images', { + apiVersion: 3, title: __('FrontBlocks - Stacked Images', 'frontblocks'), description: __('Display images with stacked animation effect from different directions.', 'frontblocks'), category: 'media', diff --git a/assets/table-of-contents/frontblocks-toc-option.js b/assets/table-of-contents/frontblocks-toc-option.js index 9829b879..ea25683b 100644 --- a/assets/table-of-contents/frontblocks-toc-option.js +++ b/assets/table-of-contents/frontblocks-toc-option.js @@ -1,6 +1,11 @@ "use strict"; -function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } +function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } +function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } +function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } +function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } +function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } +function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } @@ -162,7 +167,7 @@ function TableOfContentsEdit(props) { }, label: __('Accent color', 'frontblocks') }] - })), /*#__PURE__*/React.createElement("nav", _extends({}, blockProps, { + })), /*#__PURE__*/React.createElement("nav", _objectSpread(_objectSpread({}, blockProps), {}, { style: accentColor ? { '--frbl-toc-accent': accentColor } : undefined @@ -184,6 +189,7 @@ function TableOfContentsEdit(props) { })))); } registerBlockType('frontblocks/table-of-contents', { + apiVersion: 3, title: __('FrontBlocks - Table of Content', 'frontblocks'), description: __('An accessible table of contents generated from the headings in this post.', 'frontblocks'), category: 'widgets', diff --git a/assets/table-of-contents/frontblocks-toc-option.jsx b/assets/table-of-contents/frontblocks-toc-option.jsx index 883ee5e3..d678a7f7 100644 --- a/assets/table-of-contents/frontblocks-toc-option.jsx +++ b/assets/table-of-contents/frontblocks-toc-option.jsx @@ -151,6 +151,7 @@ function TableOfContentsEdit( props ) { } registerBlockType( 'frontblocks/table-of-contents', { + apiVersion: 3, title: __( 'FrontBlocks - Table of Content', 'frontblocks' ), description: __( 'An accessible table of contents generated from the headings in this post.', 'frontblocks' ), category: 'widgets', diff --git a/assets/text-animation/frontblocks-text-animation-option.jsx b/assets/text-animation/frontblocks-text-animation-option.jsx index dbe48a0f..209eb2ba 100644 --- a/assets/text-animation/frontblocks-text-animation-option.jsx +++ b/assets/text-animation/frontblocks-text-animation-option.jsx @@ -415,8 +415,8 @@ const ANIMATION_PREVIEWS = { if ( ! el ) return; el.innerHTML = ''; - const textSpan = document.createElement( 'span' ); - const cursor = document.createElement( 'span' ); + const textSpan = el.ownerDocument.createElement( 'span' ); + const cursor = el.ownerDocument.createElement( 'span' ); cursor.style.cssText = 'display:inline-block;width:0.6em;height:1.1em;background:currentColor;margin-left:4px;vertical-align:middle;animation:frblBlinkCursor 1s infinite;'; el.appendChild( textSpan ); @@ -469,11 +469,11 @@ const ANIMATION_PREVIEWS = { el.style.overflow = 'hidden'; el.innerHTML = ''; - const textSpan = document.createElement( 'span' ); + const textSpan = el.ownerDocument.createElement( 'span' ); textSpan.textContent = text; textSpan.style.cssText = 'display:inline-block;opacity:0;'; - const block = document.createElement( 'span' ); + const block = el.ownerDocument.createElement( 'span' ); block.style.cssText = 'position:absolute;top:0;left:0;width:100%;height:100%;background:currentColor;transform-origin:left;transform:scaleX(0);transition:transform 0.5s cubic-bezier(0.86,0,0.07,1);'; el.appendChild( textSpan ); @@ -530,7 +530,7 @@ const ANIMATION_PREVIEWS = { const indices = chars.map( ( _, i ) => i ).sort( () => Math.random() - 0.5 ); const spanList = chars.map( ( char ) => { - const span = document.createElement( 'span' ); + const span = el.ownerDocument.createElement( 'span' ); span.className = 'frbl-char'; span.textContent = char; span.style.cssText = 'display:inline-block;white-space:pre;opacity:0;'; @@ -831,7 +831,7 @@ const ANIMATION_PREVIEWS = { const chars = text.split( '' ); const spanList = chars.map( ( char ) => { - const span = document.createElement( 'span' ); + const span = el.ownerDocument.createElement( 'span' ); span.className = 'frbl-char'; el.appendChild( span ); return { el: span, char, isSpace: char === ' ' }; @@ -1245,12 +1245,17 @@ function TextAnimationEdit( props ) { { showPreview ? ( - + // API version 3: the edit output must always carry the block props on its + // root element. The preview Tag has its own typography, so the wrapper only + // keeps the block identity (ref, data attributes, classes) and the margins. +
+ +
) : ( { @@ -116,7 +128,7 @@ registerBlockType( 'frontblocks/user-text', { // Resolve CSS vars to real values for display const resolved = {}; Object.keys( merged ).forEach( ( k ) => { - resolved[ k ] = resolveCssVar( merged[ k ] ); + resolved[ k ] = resolveCssVar( merged[ k ], blockRef.current ); } ); setThemeTypo( resolved ); } ) @@ -151,7 +163,7 @@ registerBlockType( 'frontblocks/user-text', { textAlign: textAlign || undefined, }; - const blockProps = useBlockProps(); + const blockProps = useBlockProps( { ref: blockRef } ); const Tag = htmlTag || 'p'; // Helper: placeholder showing theme default when field is empty diff --git a/assets/user-text/frontblocks-user-text.js b/assets/user-text/frontblocks-user-text.js index 1a00c4fe..6e56daa7 100644 --- a/assets/user-text/frontblocks-user-text.js +++ b/assets/user-text/frontblocks-user-text.js @@ -1,7 +1,6 @@ "use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } -function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } @@ -17,7 +16,8 @@ var registerBlockType = wp.blocks.registerBlockType; var _wp$element = wp.element, Fragment = _wp$element.Fragment, useState = _wp$element.useState, - useEffect = _wp$element.useEffect; + useEffect = _wp$element.useEffect, + useRef = _wp$element.useRef; var useSelect = wp.data.useSelect; var _wp$blockEditor = wp.blockEditor, InspectorControls = _wp$blockEditor.InspectorControls, @@ -108,16 +108,24 @@ var TEXT_ALIGN_OPTIONS = [{ /** * Resolve a CSS var like "var(--wp--preset--font-size--large)" to a real value - * using computed styles on the document root. + * using computed styles on the root of the document that holds the block. + * + * The editor canvas is an iframe (block API version 3) and the theme presets + * are defined inside it, so the lookup must go through the block's own + * ownerDocument rather than the global document of the wp-admin page. */ -function resolveCssVar(value) { +function resolveCssVar(value, element) { if (!value || !value.startsWith('var(')) return value; var match = value.match(/var\(\s*(--[^,)]+)/); if (!match) return value; - var resolved = getComputedStyle(document.documentElement).getPropertyValue(match[1]).trim(); + var doc = element ? element.ownerDocument : null; + var view = doc ? doc.defaultView : null; + if (!doc || !view) return value; + var resolved = view.getComputedStyle(doc.documentElement).getPropertyValue(match[1]).trim(); return resolved || value; } registerBlockType('frontblocks/user-text', { + apiVersion: 3, title: __('User Text', 'frontblocks'), description: __('Text pattern with logged-in user data placeholders.', 'frontblocks'), category: 'text', @@ -176,6 +184,9 @@ registerBlockType('frontblocks/user-text', { textAlign = attributes.textAlign, loggedOutText = attributes.loggedOutText; + // Ref on the block root, used to reach the (iframed) canvas document. + var blockRef = useRef(null); + // ── Current user (with email, needs context=edit) ────────────────── var _useState = useState(null), _useState2 = _slicedToArray(_useState, 2), @@ -211,7 +222,7 @@ registerBlockType('frontblocks/user-text', { // Resolve CSS vars to real values for display var resolved = {}; Object.keys(merged).forEach(function (k) { - resolved[k] = resolveCssVar(merged[k]); + resolved[k] = resolveCssVar(merged[k], blockRef.current); }); setThemeTypo(resolved); }).catch(function () {}); @@ -246,7 +257,9 @@ registerBlockType('frontblocks/user-text', { fontWeight: fontWeight || undefined, textAlign: textAlign || undefined }; - var blockProps = useBlockProps(); + var blockProps = useBlockProps({ + ref: blockRef + }); var Tag = htmlTag || 'p'; // Helper: placeholder showing theme default when field is empty @@ -355,7 +368,7 @@ registerBlockType('frontblocks/user-text', { }, label: __('Text Color on Hover', 'frontblocks') }] - })), /*#__PURE__*/React.createElement(Tag, _extends({}, blockProps, { + })), /*#__PURE__*/React.createElement(Tag, _objectSpread(_objectSpread({}, blockProps), {}, { style: _objectSpread(_objectSpread({}, inlineStyle), {}, { margin: 0 }) diff --git a/includes/Frontend/BeforeAfter.php b/includes/Frontend/BeforeAfter.php index edb10404..8c24776f 100644 --- a/includes/Frontend/BeforeAfter.php +++ b/includes/Frontend/BeforeAfter.php @@ -27,6 +27,7 @@ class BeforeAfter { public function __construct() { add_action( 'init', array( $this, 'register_before_after_block' ), 20 ); add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); + add_action( 'enqueue_block_assets', array( $this, 'enqueue_editor_canvas_style' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) ); } @@ -58,18 +59,33 @@ public function enqueue_frontend_scripts() { } /** - * Enqueue block editor assets. + * Enqueue the block's stylesheet inside the editor canvas. + * + * The block uses API version 3, so the canvas is an iframe. Styles enqueued + * on enqueue_block_editor_assets only reach the wp-admin document, while + * enqueue_block_assets (in the admin) is mirrored into the iframe. * * @return void */ - public function enqueue_block_editor_assets() { + public function enqueue_editor_canvas_style() { + if ( ! is_admin() ) { + return; + } + wp_enqueue_style( 'frontblocks-before-after-style', FRBL_PLUGIN_URL . 'assets/before-after/frontblocks-before-after.css', array(), FRBL_VERSION ); + } + /** + * Enqueue block editor assets. + * + * @return void + */ + public function enqueue_block_editor_assets() { wp_enqueue_script( 'frontblocks-a11y-utils' ); wp_enqueue_script( @@ -92,6 +108,7 @@ public function register_before_after_block() { } $args = array( + 'api_version' => 3, 'editor_script' => 'frontblocks-before-after-option', 'render_callback' => array( $this, 'render_before_after_block' ), 'attributes' => array( diff --git a/includes/Frontend/InsertPost.php b/includes/Frontend/InsertPost.php index 675bceb5..f006e88f 100644 --- a/includes/Frontend/InsertPost.php +++ b/includes/Frontend/InsertPost.php @@ -145,6 +145,7 @@ public function register_insert_post_block() { register_block_type( 'frontblocks/insert-post', array( + 'api_version' => 3, 'editor_script' => 'frontblocks-insert-post-option', 'render_callback' => array( $this, 'render_insert_post_block' ), 'attributes' => array( diff --git a/includes/Frontend/ProductCategories.php b/includes/Frontend/ProductCategories.php index a85a7c17..1370da9b 100644 --- a/includes/Frontend/ProductCategories.php +++ b/includes/Frontend/ProductCategories.php @@ -28,6 +28,7 @@ public function __construct() { add_action( 'init', array( $this, 'register_product_categories_block' ), 20 ); add_action( 'init', array( $this, 'enable_rest_api_for_product_cat' ), 20 ); add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); + add_action( 'enqueue_block_assets', array( $this, 'enqueue_editor_canvas_style' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_styles' ) ); add_action( 'rest_api_init', array( $this, 'register_rest_fields' ) ); } @@ -124,19 +125,33 @@ public function enqueue_frontend_styles() { } /** - * Enqueue block editor assets. + * Enqueue the block's stylesheet inside the editor canvas. + * + * The block uses API version 3, so the canvas is an iframe. Styles enqueued + * on enqueue_block_editor_assets only reach the wp-admin document, while + * enqueue_block_assets (in the admin) is mirrored into the iframe. * * @return void */ - public function enqueue_block_editor_assets() { - // Enqueue styles for editor. + public function enqueue_editor_canvas_style() { + if ( ! is_admin() ) { + return; + } + wp_enqueue_style( 'frontblocks-product-categories-grid-style', FRBL_PLUGIN_URL . 'assets/product-categories/frontblocks-product-categories.css', array(), FRBL_VERSION ); + } + /** + * Enqueue block editor assets. + * + * @return void + */ + public function enqueue_block_editor_assets() { wp_enqueue_script( 'frontblocks-product-categories-option', FRBL_PLUGIN_URL . 'assets/product-categories/frontblocks-product-categories.js', @@ -161,16 +176,13 @@ public function enqueue_block_editor_assets() { } /** - * Register the Product Categories block. + * Arguments the Product Categories block is registered with. * - * @return void + * @return array */ - public function register_product_categories_block() { - if ( ! class_exists( 'WooCommerce' ) ) { - return; - } - - $args = array( + public function get_block_args() { + return array( + 'api_version' => 3, 'editor_script' => 'frontblocks-product-categories-option', 'render_callback' => array( $this, 'render_product_categories_block' ), 'attributes' => array( @@ -306,6 +318,19 @@ public function register_product_categories_block() { ), ), ); + } + + /** + * Register the Product Categories block. + * + * @return void + */ + public function register_product_categories_block() { + if ( ! class_exists( 'WooCommerce' ) ) { + return; + } + + $args = $this->get_block_args(); if ( ! WP_Block_Type_Registry::get_instance()->is_registered( 'frontblocks/product-categories' ) ) { register_block_type( diff --git a/includes/Frontend/ReadingTime.php b/includes/Frontend/ReadingTime.php index 3288bac9..a3e3bebc 100644 --- a/includes/Frontend/ReadingTime.php +++ b/includes/Frontend/ReadingTime.php @@ -139,6 +139,7 @@ public function enqueue_editor_script() { */ public function register_reading_time_block() { $args = array( + 'api_version' => 3, 'editor_script' => 'frontblocks-reading-time-option', 'render_callback' => array( $this, 'render_reading_time_block' ), 'attributes' => array( diff --git a/includes/Frontend/StackedImages.php b/includes/Frontend/StackedImages.php index b283a360..00543379 100644 --- a/includes/Frontend/StackedImages.php +++ b/includes/Frontend/StackedImages.php @@ -27,6 +27,7 @@ class StackedImages { public function __construct() { add_action( 'init', array( $this, 'register_stacked_images_block' ), 20 ); add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); + add_action( 'enqueue_block_assets', array( $this, 'enqueue_editor_canvas_style' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) ); } @@ -58,19 +59,33 @@ public function enqueue_frontend_scripts() { } /** - * Enqueue block editor assets. + * Enqueue the block's stylesheet inside the editor canvas. + * + * The block uses API version 3, so the canvas is an iframe. Styles enqueued + * on enqueue_block_editor_assets only reach the wp-admin document, while + * enqueue_block_assets (in the admin) is mirrored into the iframe. * * @return void */ - public function enqueue_block_editor_assets() { - // Enqueue styles for editor. + public function enqueue_editor_canvas_style() { + if ( ! is_admin() ) { + return; + } + wp_enqueue_style( 'frontblocks-stacked-images-style', FRBL_PLUGIN_URL . 'assets/stacked-images/frontblocks-stacked-images.css', array(), FRBL_VERSION ); + } + /** + * Enqueue block editor assets. + * + * @return void + */ + public function enqueue_block_editor_assets() { wp_enqueue_script( 'frontblocks-stacked-images-option', FRBL_PLUGIN_URL . 'assets/stacked-images/frontblocks-stacked-images.js', @@ -91,6 +106,7 @@ public function register_stacked_images_block() { } $args = array( + 'api_version' => 3, 'editor_script' => 'frontblocks-stacked-images-option', 'render_callback' => array( $this, 'render_stacked_images_block' ), 'attributes' => array( diff --git a/includes/Frontend/TableOfContents.php b/includes/Frontend/TableOfContents.php index 68649982..9224f643 100644 --- a/includes/Frontend/TableOfContents.php +++ b/includes/Frontend/TableOfContents.php @@ -127,6 +127,7 @@ public function register_block() { } $args = array( + 'api_version' => 3, 'editor_script' => 'frontblocks-toc-option', 'render_callback' => array( $this, 'render_placeholder' ), 'attributes' => array( diff --git a/includes/Frontend/TextAnimation.php b/includes/Frontend/TextAnimation.php index ecf93388..aa667509 100644 --- a/includes/Frontend/TextAnimation.php +++ b/includes/Frontend/TextAnimation.php @@ -337,6 +337,7 @@ public function register_block() { register_block_type( 'frontblocks/text-animation', array( + 'api_version' => 3, 'editor_script' => 'frontblocks-text-animation-editor', 'editor_style' => 'frontblocks-text-animation', 'style' => 'frontblocks-text-animation', diff --git a/includes/Frontend/UserText.php b/includes/Frontend/UserText.php index 6ce1368d..e4874ac6 100644 --- a/includes/Frontend/UserText.php +++ b/includes/Frontend/UserText.php @@ -59,6 +59,7 @@ public function register_block() { register_block_type( 'frontblocks/user-text', array( + 'api_version' => 3, 'editor_script' => 'frontblocks-user-text-editor', 'editor_style' => 'frontblocks-user-text-editor', 'render_callback' => array( $this, 'render_block' ), diff --git a/includes/GoogleSignIn/GoogleSignIn.php b/includes/GoogleSignIn/GoogleSignIn.php index 697def18..4b7bd87b 100644 --- a/includes/GoogleSignIn/GoogleSignIn.php +++ b/includes/GoogleSignIn/GoogleSignIn.php @@ -606,6 +606,7 @@ public function register_block() { register_block_type( 'frontblocks/google-login', array( + 'api_version' => 3, 'editor_script' => 'frontblocks-google-login-block-editor', 'render_callback' => array( $this, 'render_block' ), 'attributes' => $this->get_block_attributes(), diff --git a/tests/Unit/BeforeAfterTest.php b/tests/Unit/BeforeAfterTest.php index 45e6476a..8077cffd 100644 --- a/tests/Unit/BeforeAfterTest.php +++ b/tests/Unit/BeforeAfterTest.php @@ -191,4 +191,32 @@ public function test_register_before_after_block_registers_the_block_type_and_is $this->assertTrue( \WP_Block_Type_Registry::get_instance()->is_registered( 'frontblocks/before-after' ) ); } + + /** + * The block opts into block API version 3 (iframed editor canvas). + */ + public function test_block_is_registered_with_api_version_3() { + $this->before_after->register_before_after_block(); + + $block_type = \WP_Block_Type_Registry::get_instance()->get_registered( 'frontblocks/before-after' ); + + $this->assertSame( 3, $block_type->api_version ); + } + + /** + * The editor stylesheet is hooked on enqueue_block_assets, the only editor + * hook whose styles are mirrored into the iframed canvas, and is enqueued + * in the admin only. + */ + public function test_editor_canvas_style_is_enqueued_on_enqueue_block_assets_in_admin_only() { + $this->assertNotFalse( has_action( 'enqueue_block_assets', array( $this->before_after, 'enqueue_editor_canvas_style' ) ) ); + + $this->before_after->enqueue_editor_canvas_style(); + $this->assertFalse( wp_style_is( 'frontblocks-before-after-style', 'enqueued' ) ); + + set_current_screen( 'post' ); + $this->before_after->enqueue_editor_canvas_style(); + $this->assertTrue( wp_style_is( 'frontblocks-before-after-style', 'enqueued' ) ); + set_current_screen( 'front' ); + } } diff --git a/tests/Unit/GoogleSignInTest.php b/tests/Unit/GoogleSignInTest.php new file mode 100644 index 00000000..72e52093 --- /dev/null +++ b/tests/Unit/GoogleSignInTest.php @@ -0,0 +1,47 @@ +get_registered( 'frontblocks/google-login' ); + + $this->assertNotNull( $block_type ); + $this->assertSame( 3, $block_type->api_version ); + $this->assertContains( 'frontblocks-google-login-block-editor', $block_type->editor_script_handles ); + } + + /** + * The editor script is registered by register_block() and its edit + * function must use useBlockProps (required by API version 3), so the + * compiled file has to reference it and declare the version itself. + */ + public function test_editor_script_declares_api_version_3_and_uses_block_props() { + $source = file_get_contents( FRBL_PLUGIN_PATH . 'assets/google-signin/frontblocks-google-signin-block.js' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents + + $this->assertMatchesRegularExpression( '/apiVersion:\s*3/', $source ); + $this->assertStringContainsString( 'useBlockProps', $source ); + } + + /** + * The pre-5.5 fallback registration (no block.json support) must declare + * the same API version as block.json. + */ + public function test_block_json_and_fallback_registration_agree_on_api_version() { + $metadata = json_decode( file_get_contents( FRBL_PLUGIN_PATH . 'includes/GoogleSignIn/block/block.json' ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents + $this->assertSame( 3, $metadata['apiVersion'] ); + + $source = file_get_contents( FRBL_PLUGIN_PATH . 'includes/GoogleSignIn/GoogleSignIn.php' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents + $this->assertMatchesRegularExpression( "/'api_version'\s*=>\s*3,/", $source ); + } +} diff --git a/tests/Unit/InsertPostTest.php b/tests/Unit/InsertPostTest.php index 3bf3fa87..5145946c 100644 --- a/tests/Unit/InsertPostTest.php +++ b/tests/Unit/InsertPostTest.php @@ -29,6 +29,13 @@ public function test_constructor_registers_the_expected_hooks() { $this->assertSame( 5, has_action( 'init', array( $this->insert_post, 'register_custom_attributes' ) ) ); } + public function test_block_is_registered_with_api_version_3() { + $block_type = \WP_Block_Type_Registry::get_instance()->get_registered( 'frontblocks/insert-post' ); + + $this->assertNotNull( $block_type ); + $this->assertSame( 3, $block_type->api_version ); + } + public function test_render_returns_empty_placeholder_when_no_post_selected() { $result = $this->insert_post->render_insert_post_block( array( 'selectedPostId' => 0 ) ); diff --git a/tests/Unit/ProductCategoriesTest.php b/tests/Unit/ProductCategoriesTest.php index 2cc5953c..8900a0e1 100644 --- a/tests/Unit/ProductCategoriesTest.php +++ b/tests/Unit/ProductCategoriesTest.php @@ -83,6 +83,42 @@ public function test_register_product_categories_block_is_noop_without_woocommer ); } + /** + * Registration itself is gated on WooCommerce, which this environment does + * not load, so the arguments are registered here under a throwaway name to + * check what WordPress reports for them. + */ + public function test_block_arguments_declare_api_version_3() { + $args = $this->product_categories->get_block_args(); + + $this->assertSame( 3, $args['api_version'] ); + + $registry = WP_Block_Type_Registry::get_instance(); + $block = register_block_type( 'frontblocks/product-categories-apiv3-test', $args ); + $this->assertSame( 3, $block->api_version ); + $registry->unregister( 'frontblocks/product-categories-apiv3-test' ); + } + + /** + * The editor stylesheet is hooked on enqueue_block_assets, the only editor + * hook whose styles are mirrored into the iframed canvas, and is enqueued + * in the admin only. + */ + public function test_editor_canvas_style_is_enqueued_on_enqueue_block_assets_in_admin_only() { + $this->assertNotFalse( has_action( 'enqueue_block_assets', array( $this->product_categories, 'enqueue_editor_canvas_style' ) ) ); + + wp_dequeue_style( 'frontblocks-product-categories-grid-style' ); + $this->product_categories->enqueue_editor_canvas_style(); + $this->assertFalse( wp_style_is( 'frontblocks-product-categories-grid-style', 'enqueued' ) ); + + set_current_screen( 'post' ); + $this->product_categories->enqueue_editor_canvas_style(); + $this->assertTrue( wp_style_is( 'frontblocks-product-categories-grid-style', 'enqueued' ) ); + + wp_dequeue_style( 'frontblocks-product-categories-grid-style' ); + set_current_screen( 'front' ); + } + public function test_render_product_categories_block_returns_empty_without_woocommerce() { $result = $this->product_categories->render_product_categories_block( array( 'count' => 5 ) ); diff --git a/tests/Unit/ReadingTimeTest.php b/tests/Unit/ReadingTimeTest.php index e33162e2..65b8b065 100644 --- a/tests/Unit/ReadingTimeTest.php +++ b/tests/Unit/ReadingTimeTest.php @@ -165,6 +165,12 @@ public function test_register_reading_time_block_registers_the_expected_default_ $this->assertSame( 'left', $block_type->attributes['alignment']['default'] ); } + public function test_block_is_registered_with_api_version_3() { + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'frontblocks/reading-time' ); + + $this->assertSame( 3, $block_type->api_version ); + } + public function test_enqueue_style_only_loads_on_the_frontend_when_the_block_is_present() { $post_id = self::factory()->post->create( array( 'post_content' => '' ) diff --git a/tests/Unit/StackedImagesTest.php b/tests/Unit/StackedImagesTest.php index 6a326d53..25ea6155 100644 --- a/tests/Unit/StackedImagesTest.php +++ b/tests/Unit/StackedImagesTest.php @@ -31,6 +31,30 @@ public function test_register_stacked_images_block_registers_expected_default_at $this->assertSame( 500, $block_type->attributes['containerHeight']['default'] ); } + public function test_block_is_registered_with_api_version_3() { + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'frontblocks/stacked-images' ); + + $this->assertSame( 3, $block_type->api_version ); + } + + /** + * The editor stylesheet must be enqueued on enqueue_block_assets (admin + * only) so it reaches the iframed canvas of an API version 3 block. + */ + public function test_editor_canvas_style_is_enqueued_on_enqueue_block_assets_in_admin_only() { + $this->assertNotFalse( has_action( 'enqueue_block_assets', array( $this->stacked_images, 'enqueue_editor_canvas_style' ) ) ); + + wp_dequeue_style( 'frontblocks-stacked-images-style' ); + $this->stacked_images->enqueue_editor_canvas_style(); + $this->assertFalse( wp_style_is( 'frontblocks-stacked-images-style', 'enqueued' ) ); + + set_current_screen( 'post' ); + $this->stacked_images->enqueue_editor_canvas_style(); + $this->assertTrue( wp_style_is( 'frontblocks-stacked-images-style', 'enqueued' ) ); + wp_dequeue_style( 'frontblocks-stacked-images-style' ); + set_current_screen( 'front' ); + } + public function test_render_returns_a_placeholder_when_no_images_are_set() { $output = $this->stacked_images->render_stacked_images_block( array( 'images' => array() ) ); diff --git a/tests/Unit/TableOfContentsTest.php b/tests/Unit/TableOfContentsTest.php index fd4025ec..dcb1b934 100644 --- a/tests/Unit/TableOfContentsTest.php +++ b/tests/Unit/TableOfContentsTest.php @@ -295,4 +295,20 @@ public function test_generated_id_avoids_colliding_with_a_non_heading_element_id $this->assertStringContainsString( 'Overview', $result ); $this->assertStringContainsString( '

is_registered( 'frontblocks/table-of-contents' ) ) { + unregister_block_type( 'frontblocks/table-of-contents' ); + } + + $this->toc->register_block(); + + $block_type = $registry->get_registered( 'frontblocks/table-of-contents' ); + $this->assertNotNull( $block_type ); + $this->assertSame( 3, $block_type->api_version ); + } } diff --git a/tests/Unit/TextAnimationTest.php b/tests/Unit/TextAnimationTest.php index 14847cae..6a2e4ad9 100644 --- a/tests/Unit/TextAnimationTest.php +++ b/tests/Unit/TextAnimationTest.php @@ -164,4 +164,22 @@ public function test_enqueue_animation_scripts_does_nothing_extra_for_unknown_an $this->assertTrue( wp_style_is( 'frontblocks-text-animation', 'enqueued' ) ); $this->assertTrue( wp_script_is( 'frontblocks-text-animation-frontend', 'enqueued' ) ); } + + /** + * The block opts into block API version 3 and keeps its stylesheet as the + * editor_style handle, which is what loads it inside the iframed canvas. + */ + public function test_block_is_registered_with_api_version_3_and_editor_style() { + $registry = \WP_Block_Type_Registry::get_instance(); + if ( $registry->is_registered( 'frontblocks/text-animation' ) ) { + unregister_block_type( 'frontblocks/text-animation' ); + } + + $this->text_animation->register_block(); + + $block_type = $registry->get_registered( 'frontblocks/text-animation' ); + $this->assertNotNull( $block_type ); + $this->assertSame( 3, $block_type->api_version ); + $this->assertContains( 'frontblocks-text-animation', $block_type->editor_style_handles ); + } } diff --git a/tests/Unit/UserTextTest.php b/tests/Unit/UserTextTest.php index fdf50898..41ddd175 100644 --- a/tests/Unit/UserTextTest.php +++ b/tests/Unit/UserTextTest.php @@ -43,6 +43,27 @@ public function test_register_block_registers_block_type_with_expected_default_a $this->assertSame( array( $this->user_text, 'render_block' ), $block_type->render_callback ); } + public function test_block_is_registered_with_api_version_3() { + $this->user_text->register_block(); + + $block_type = \WP_Block_Type_Registry::get_instance()->get_registered( 'frontblocks/user-text' ); + + $this->assertSame( 3, $block_type->api_version ); + } + + /** + * The editor stylesheet is attached as the block's editor_style so it is + * loaded inside the iframed canvas of an API version 3 block. + */ + public function test_editor_style_is_attached_to_the_block_for_the_iframed_canvas() { + $this->user_text->register_block(); + + $block_type = \WP_Block_Type_Registry::get_instance()->get_registered( 'frontblocks/user-text' ); + + $this->assertContains( 'frontblocks-user-text-editor', $block_type->editor_style_handles ); + $this->assertTrue( wp_style_is( 'frontblocks-user-text-editor', 'registered' ) ); + } + public function test_render_block_returns_empty_string_when_no_filter_is_hooked() { $output = $this->user_text->render_block( array( 'textPattern' => 'Hello {first_name}' ) );