From bd53de2a2873432161cdd54882264f0a53ff4200 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 20 Apr 2023 21:03:45 -0500 Subject: [PATCH 01/61] Add behaviors to the core theme.json --- lib/theme.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/theme.json b/lib/theme.json index 88befe6dff2ed0..09949e539dcf53 100644 --- a/lib/theme.json +++ b/lib/theme.json @@ -450,6 +450,11 @@ "style": true, "width": true } + }, + "core/image": { + "behaviors": { + "lightbox": true + } } } }, From bd43bf6b12129d7bf70f8b69b20b3905b522ac8d Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 20 Apr 2023 21:12:03 -0500 Subject: [PATCH 02/61] Add behaviors to json schemas for theme.json --- schemas/json/theme.json | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/schemas/json/theme.json b/schemas/json/theme.json index 5f9fd13c041575..3bac7cb795396a 100644 --- a/schemas/json/theme.json +++ b/schemas/json/theme.json @@ -675,7 +675,8 @@ "shadow": {}, "spacing": {}, "typography": {}, - "custom": {} + "custom": {}, + "behaviors": {} }, "additionalProperties": false } @@ -796,7 +797,28 @@ "$ref": "#/definitions/settingsPropertiesComplete" }, "core/image": { - "$ref": "#/definitions/settingsPropertiesComplete" + "type": "object", + "allOf": [ + { + "$ref": "#/definitions/settingsPropertiesComplete" + }, + { + "type": "object", + "properties": { + "behaviors": { + "description": "Allows users to set named behaviors for a block.\nGutenberg plugin required.", + "type": "object", + "properties": { + "lightbox": { + "description": "Show a lightbox for the image.\nGutenberg plugin required.", + "type": "boolean", + "default": true + } + } + } + } + } + ] }, "core/latest-comments": { "$ref": "#/definitions/settingsPropertiesComplete" From e1dd73759f24449587fd15af6acf4185d8797f16 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Wed, 3 May 2023 18:50:57 -0500 Subject: [PATCH 03/61] Add a behaviors panel --- packages/block-editor/src/hooks/behaviors.js | 70 ++++++++++++++++++++ packages/block-editor/src/hooks/index.js | 1 + 2 files changed, 71 insertions(+) create mode 100644 packages/block-editor/src/hooks/behaviors.js diff --git a/packages/block-editor/src/hooks/behaviors.js b/packages/block-editor/src/hooks/behaviors.js new file mode 100644 index 00000000000000..90dbee8ffc245a --- /dev/null +++ b/packages/block-editor/src/hooks/behaviors.js @@ -0,0 +1,70 @@ +/** + * WordPress dependencies + */ +import { addFilter } from '@wordpress/hooks'; +import { TextControl } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { createHigherOrderComponent } from '@wordpress/compose'; +import { useSettings } from '@wordpress/block-editor'; + +/** + * Internal dependencies + */ +import { InspectorControls } from '../components'; + +/** + * Override the default edit UI to include a new block inspector control for + * assigning the custom class name, if block supports custom class name. + * The control is displayed within the Advanced panel in the block inspector. + * + * @param {WPComponent} BlockEdit Original component. + * + * @return {WPComponent} Wrapped component. + */ +export const withInspectorControl = createHigherOrderComponent( + ( BlockEdit ) => { + return ( props ) => { + const { behaviors } = props.attributes; + const { behaviors: behaviorsSupport } = props.blockType.attributes; + + const settings = useSettings(); + + // eslint-disable-next-line no-console + console.log( 'behaviors', behaviors ); + // eslint-disable-next-line no-console + console.log( 'behaviorsSupport', behaviorsSupport ); + // eslint-disable-next-line no-console + console.log( settings ); + + return ( + <> + + + { + props.setAttributes( { + behaviors: + nextValue !== '' + ? nextValue + : undefined, + } ); + } } + help={ __( 'Add behaviors' ) } + /> + + + ); + }; + }, + 'withInspectorControl' +); + +addFilter( + 'editor.BlockEdit', + 'core/behaviors/with-inspector-control', + withInspectorControl +); diff --git a/packages/block-editor/src/hooks/index.js b/packages/block-editor/src/hooks/index.js index 2077c143952f59..a66aa0a73ed411 100644 --- a/packages/block-editor/src/hooks/index.js +++ b/packages/block-editor/src/hooks/index.js @@ -20,6 +20,7 @@ import './layout'; import './content-lock-ui'; import './metadata'; import './metadata-name'; +import './behaviors'; export { useCustomSides } from './dimensions'; export { useLayoutClasses, useLayoutStyles } from './layout'; From 8b84a22c7f340d954f28b72da7a966695d2f9cf8 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 4 May 2023 21:49:59 -0500 Subject: [PATCH 04/61] Remove the changes to theme.json schema --- schemas/json/theme.json | 26 ++------------------------ 1 file changed, 2 insertions(+), 24 deletions(-) diff --git a/schemas/json/theme.json b/schemas/json/theme.json index 3bac7cb795396a..5f9fd13c041575 100644 --- a/schemas/json/theme.json +++ b/schemas/json/theme.json @@ -675,8 +675,7 @@ "shadow": {}, "spacing": {}, "typography": {}, - "custom": {}, - "behaviors": {} + "custom": {} }, "additionalProperties": false } @@ -797,28 +796,7 @@ "$ref": "#/definitions/settingsPropertiesComplete" }, "core/image": { - "type": "object", - "allOf": [ - { - "$ref": "#/definitions/settingsPropertiesComplete" - }, - { - "type": "object", - "properties": { - "behaviors": { - "description": "Allows users to set named behaviors for a block.\nGutenberg plugin required.", - "type": "object", - "properties": { - "lightbox": { - "description": "Show a lightbox for the image.\nGutenberg plugin required.", - "type": "boolean", - "default": true - } - } - } - } - } - ] + "$ref": "#/definitions/settingsPropertiesComplete" }, "core/latest-comments": { "$ref": "#/definitions/settingsPropertiesComplete" From d5dbc1c9c0afcf6b36a06453055159be8906b766 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 4 May 2023 23:08:51 -0500 Subject: [PATCH 05/61] Add behaviors to the VALID_SETTINGS in class-wp-theme-json-gutenberg.php --- lib/class-wp-theme-json-gutenberg.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 9e7a7316ffdd29..91783a746ebf27 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -404,6 +404,9 @@ class WP_Theme_JSON_Gutenberg { 'textDecoration' => null, 'textTransform' => null, ), + 'behaviors' => array( + 'lightbox' => null, + ), ); /** From 037d6692c4b618812c55be591b5ff999eb1ca17c Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 4 May 2023 23:14:45 -0500 Subject: [PATCH 06/61] Add the first (still broken) version of the lightbox settings --- packages/block-editor/src/hooks/behaviors.js | 86 ++++++++++---------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/packages/block-editor/src/hooks/behaviors.js b/packages/block-editor/src/hooks/behaviors.js index 90dbee8ffc245a..deb8ad08267d15 100644 --- a/packages/block-editor/src/hooks/behaviors.js +++ b/packages/block-editor/src/hooks/behaviors.js @@ -5,66 +5,66 @@ import { addFilter } from '@wordpress/hooks'; import { TextControl } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { createHigherOrderComponent } from '@wordpress/compose'; -import { useSettings } from '@wordpress/block-editor'; +import { select } from '@wordpress/data'; /** * Internal dependencies */ import { InspectorControls } from '../components'; +import { store as blockEditorStore } from '../store'; /** - * Override the default edit UI to include a new block inspector control for - * assigning the custom class name, if block supports custom class name. - * The control is displayed within the Advanced panel in the block inspector. + * TODO: Add description. * * @param {WPComponent} BlockEdit Original component. * * @return {WPComponent} Wrapped component. */ -export const withInspectorControl = createHigherOrderComponent( - ( BlockEdit ) => { - return ( props ) => { - const { behaviors } = props.attributes; - const { behaviors: behaviorsSupport } = props.blockType.attributes; +export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => { + return ( props ) => { + const { behaviors: blockBehaviors } = props.attributes; + const settings = select( blockEditorStore ).getSettings(); + const themeBehaviors = + settings?.__experimentalFeatures?.blocks?.[ 'core/image' ] + .behaviors; - const settings = useSettings(); + if ( ! blockBehaviors && ! themeBehaviors ) { + return ; + } - // eslint-disable-next-line no-console - console.log( 'behaviors', behaviors ); - // eslint-disable-next-line no-console - console.log( 'behaviorsSupport', behaviorsSupport ); - // eslint-disable-next-line no-console - console.log( settings ); + if ( ! blockBehaviors && themeBehaviors ) { + props.attributes.behaviors = themeBehaviors; + } - return ( - <> - - - { - props.setAttributes( { - behaviors: - nextValue !== '' - ? nextValue - : undefined, - } ); - } } - help={ __( 'Add behaviors' ) } - /> - - - ); - }; - }, - 'withInspectorControl' -); + return ( + <> + + + { + props.setAttributes( { + behaviors: + nextValue !== '' ? nextValue : undefined, + } ); + } } + help={ __( 'Add behaviors' ) } + /> + + + ); + }; +}, 'withBehaviors' ); addFilter( 'editor.BlockEdit', 'core/behaviors/with-inspector-control', - withInspectorControl + withBehaviors ); From 8f62d34b29d2109e625d752e7fa1b061a02ee8a6 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Mon, 8 May 2023 20:21:43 -0500 Subject: [PATCH 07/61] WIP: Added a SelectControl for the behaviors --- packages/block-editor/src/hooks/behaviors.js | 39 ++++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/packages/block-editor/src/hooks/behaviors.js b/packages/block-editor/src/hooks/behaviors.js index deb8ad08267d15..7c0227c7163aa6 100644 --- a/packages/block-editor/src/hooks/behaviors.js +++ b/packages/block-editor/src/hooks/behaviors.js @@ -2,7 +2,7 @@ * WordPress dependencies */ import { addFilter } from '@wordpress/hooks'; -import { TextControl } from '@wordpress/components'; +import { SelectControl } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { createHigherOrderComponent } from '@wordpress/compose'; import { select } from '@wordpress/data'; @@ -22,6 +22,11 @@ import { store as blockEditorStore } from '../store'; */ export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => { return ( props ) => { + // Only add behaviors to the core/image block. + if ( props.name !== 'core/image' ) { + return ; + } + const { behaviors: blockBehaviors } = props.attributes; const settings = select( blockEditorStore ).getSettings(); const themeBehaviors = @@ -32,30 +37,42 @@ export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => { return ; } + // By default, use the block behaviors. + let behaviors = blockBehaviors; + + // If the theme has behaviors, but the block does not, use the theme behaviors. if ( ! blockBehaviors && themeBehaviors ) { - props.attributes.behaviors = themeBehaviors; + behaviors = themeBehaviors; } return ( <> - ( { + value: behavior, + label: behavior.toUpperCase(), + } ) ) + .concat( { + value: '', + label: __( 'None' ), + } ) } onChange={ ( nextValue ) => { props.setAttributes( { - behaviors: - nextValue !== '' ? nextValue : undefined, + behaviors: { + lightbox: nextValue === '' ? false : true, + }, } ); } } + hideCancelButton={ true } help={ __( 'Add behaviors' ) } + size="__unstable-large" /> From 62b6217860e41d5b112ec59662425289225e1529 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Tue, 9 May 2023 13:27:21 -0500 Subject: [PATCH 08/61] Format PHP --- lib/class-wp-theme-json-gutenberg.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 91783a746ebf27..614f0dd2c9c4e0 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -404,8 +404,8 @@ class WP_Theme_JSON_Gutenberg { 'textDecoration' => null, 'textTransform' => null, ), - 'behaviors' => array( - 'lightbox' => null, + 'behaviors' => array( + 'lightbox' => null, ), ); From edfd7d19523ac88e3db804a4735c29890ed187fa Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Tue, 9 May 2023 15:08:23 -0500 Subject: [PATCH 09/61] Format correctly again --- lib/class-wp-theme-json-gutenberg.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 614f0dd2c9c4e0..5eb65062d5a5c5 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -405,7 +405,7 @@ class WP_Theme_JSON_Gutenberg { 'textTransform' => null, ), 'behaviors' => array( - 'lightbox' => null, + 'lightbox' => null, ), ); From 8ee4065a867c94f85e3041654c556cb875a9ede8 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Tue, 9 May 2023 21:07:51 -0500 Subject: [PATCH 10/61] Use the props.name when getting the behaviors --- packages/block-editor/src/hooks/behaviors.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/block-editor/src/hooks/behaviors.js b/packages/block-editor/src/hooks/behaviors.js index 7c0227c7163aa6..1e58797cf697a3 100644 --- a/packages/block-editor/src/hooks/behaviors.js +++ b/packages/block-editor/src/hooks/behaviors.js @@ -30,8 +30,7 @@ export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => { const { behaviors: blockBehaviors } = props.attributes; const settings = select( blockEditorStore ).getSettings(); const themeBehaviors = - settings?.__experimentalFeatures?.blocks?.[ 'core/image' ] - .behaviors; + settings?.__experimentalFeatures?.blocks?.[ props.name ]?.behaviors; if ( ! blockBehaviors && ! themeBehaviors ) { return ; From 594224be51cf8cece4224505b2fd216efc0a0adb Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Wed, 10 May 2023 18:08:04 +0200 Subject: [PATCH 11/61] Add initial e2e tests --- .../specs/editor/various/behaviors.spec.js | 76 +++++++++++++++++++ .../gutenberg-test-themes/behaviors/index.php | 0 .../gutenberg-test-themes/behaviors/style.css | 15 ++++ .../behaviors/theme.json | 12 +++ 4 files changed, 103 insertions(+) create mode 100644 test/e2e/specs/editor/various/behaviors.spec.js create mode 100644 test/gutenberg-test-themes/behaviors/index.php create mode 100644 test/gutenberg-test-themes/behaviors/style.css create mode 100644 test/gutenberg-test-themes/behaviors/theme.json diff --git a/test/e2e/specs/editor/various/behaviors.spec.js b/test/e2e/specs/editor/various/behaviors.spec.js new file mode 100644 index 00000000000000..c9b01e50740121 --- /dev/null +++ b/test/e2e/specs/editor/various/behaviors.spec.js @@ -0,0 +1,76 @@ +/** + * External dependencies + */ +const path = require( 'path' ); + +/** + * WordPress dependencies + */ +const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); + +test.describe( 'Testing behaviors functionality', () => { + test.afterAll( async ( { requestUtils } ) => { + await requestUtils.activateTheme( 'twentytwentyone' ); + await requestUtils.deleteAllPosts(); + } ); + + test.afterEach( async ( { requestUtils } ) => { + await requestUtils.deleteAllMedia(); + } ); + + test( 'Lightbox option should be selected in an image block, as defined in theme.json', async ( { + admin, + editor, + requestUtils, + page, + } ) => { + await admin.createNewPost(); + const filename = '1024x768_e2e_test_image_size.jpeg'; + const filepath = path.join( './test/e2e/assets', filename ); + + await admin.createNewPost(); + const media = await requestUtils.uploadMedia( filepath ); + + await editor.insertBlock( { + name: 'core/image', + attributes: { + alt: filename, + id: media.id, + url: media.source_url, + }, + } ); + + await page.getByRole( 'button', { name: 'Advanced' } ).click(); + await expect( page.getByLabel( 'Behavior' ) ).toHaveValue( 'lightbox' ); + } ); + + test( 'None option should be selected in an image block, as defined in theme.json', async ( { + admin, + editor, + requestUtils, + page, + } ) => { + // Lightbox is the default behavior, so we need a theme that has it disabled. Change if we change the default. + await requestUtils.activateTheme( 'behaviors' ); + await admin.createNewPost(); + + const filename = '1024x768_e2e_test_image_size.jpeg'; + const filepath = path.join( './test/e2e/assets', filename ); + + await admin.createNewPost(); + const media = await requestUtils.uploadMedia( filepath ); + + await editor.insertBlock( { + name: 'core/image', + attributes: { + alt: filename, + id: media.id, + url: media.source_url, + }, + } ); + + await page.getByRole( 'button', { name: 'Advanced' } ).click(); + // Right now the selector has an empty value. + await expect( page.getByLabel( 'Behavior' ) ).toHaveValue( '' ); + } ); +} ); diff --git a/test/gutenberg-test-themes/behaviors/index.php b/test/gutenberg-test-themes/behaviors/index.php new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/test/gutenberg-test-themes/behaviors/style.css b/test/gutenberg-test-themes/behaviors/style.css new file mode 100644 index 00000000000000..972dbacbb5cabd --- /dev/null +++ b/test/gutenberg-test-themes/behaviors/style.css @@ -0,0 +1,15 @@ +/* +Theme Name: Behaviors +Theme URI: https://github.com/wordpress/theme-experiments/ +Author: the WordPress team +Description: Behaviors test theme. +Requires at least: 5.3 +Tested up to: 6.2 +Requires PHP: 5.6 +Version: 1.0 +License: GNU General Public License v2 or later +License URI: http://www.gnu.org/licenses/gpl-2.0.html +Text Domain: behaviors +Behaviors WordPress Theme, (C) 2023 WordPress.org +Behaviors is distributed under the terms of the GNU GPL. +*/ \ No newline at end of file diff --git a/test/gutenberg-test-themes/behaviors/theme.json b/test/gutenberg-test-themes/behaviors/theme.json new file mode 100644 index 00000000000000..a9f920f6dd0abc --- /dev/null +++ b/test/gutenberg-test-themes/behaviors/theme.json @@ -0,0 +1,12 @@ +{ + "version": 2, + "settings": { + "blocks": { + "core/image": { + "behaviors": { + "lightbox": false + } + } + } + } +} From fd6fe36aa5323daca2a938fb095cfe07baeb6a17 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Wed, 10 May 2023 17:03:13 -0500 Subject: [PATCH 12/61] Update the withBehaviors description --- packages/block-editor/src/hooks/behaviors.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/block-editor/src/hooks/behaviors.js b/packages/block-editor/src/hooks/behaviors.js index 1e58797cf697a3..2b6201873fc7be 100644 --- a/packages/block-editor/src/hooks/behaviors.js +++ b/packages/block-editor/src/hooks/behaviors.js @@ -14,7 +14,10 @@ import { InspectorControls } from '../components'; import { store as blockEditorStore } from '../store'; /** - * TODO: Add description. + * Override the default edit UI to include a new block inspector control for + * assigning behaviors to blocks if behaviors are enabled in the theme.json. + * + * Currently, only the `core/image` block is supported. * * @param {WPComponent} BlockEdit Original component. * From da4a5ad5a72e1fe942f1a40c71a16a8d928b940c Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Wed, 10 May 2023 22:31:34 -0500 Subject: [PATCH 13/61] Cleaned up behaviors.js --- packages/block-editor/src/hooks/behaviors.js | 20 ++++++++++++++------ packages/block-library/src/image/block.json | 6 ++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/packages/block-editor/src/hooks/behaviors.js b/packages/block-editor/src/hooks/behaviors.js index 2b6201873fc7be..0aae8fdbea48ba 100644 --- a/packages/block-editor/src/hooks/behaviors.js +++ b/packages/block-editor/src/hooks/behaviors.js @@ -30,15 +30,21 @@ export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => { return ; } - const { behaviors: blockBehaviors } = props.attributes; + // Check the value of `settings.blocks.core/image.behavior.lightbox` in the + // theme.json. If false, do not add the behaviors inspector control. const settings = select( blockEditorStore ).getSettings(); - const themeBehaviors = - settings?.__experimentalFeatures?.blocks?.[ props.name ]?.behaviors; - - if ( ! blockBehaviors && ! themeBehaviors ) { + if ( + ! settings?.__experimentalFeatures?.blocks?.[ props.name ] + ?.behaviors?.lightbox + ) { return ; } + const { behaviors: blockBehaviors } = props.attributes; + + // TODO: Here we should get the theme behaviors but it doesn't work ATM. + const themeBehaviors = settings?.behaviors; + // By default, use the block behaviors. let behaviors = blockBehaviors; @@ -55,7 +61,7 @@ export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => { __nextHasNoMarginBottom label={ __( 'Behaviors' ) } // At the moment we are only supporting one behavior (lightbox) - value={ behaviors?.lightbox || '' } + value={ behaviors?.lightbox ? 'LIGHTBOX' : '' } options={ Object.keys( behaviors ) .map( ( behavior ) => ( { value: behavior, @@ -66,6 +72,8 @@ export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => { label: __( 'None' ), } ) } onChange={ ( nextValue ) => { + // If the user selects something, it means that they want to + // change the default value (true) so we save it in the attributes. props.setAttributes( { behaviors: { lightbox: nextValue === '' ? false : true, diff --git a/packages/block-library/src/image/block.json b/packages/block-library/src/image/block.json index 92931455c1144c..f2e9be4e10e06d 100644 --- a/packages/block-library/src/image/block.json +++ b/packages/block-library/src/image/block.json @@ -80,6 +80,12 @@ "source": "attribute", "selector": "figure > a", "attribute": "target" + }, + "behaviors": { + "type": "object", + "default": { + "lightbox": true + } } }, "supports": { From 2958fd16e88411c1c0adfefc70fd763386fcfd51 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Fri, 12 May 2023 13:20:12 +0200 Subject: [PATCH 14/61] Update e2e tests to check visibility --- test/e2e/specs/editor/various/behaviors.spec.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/e2e/specs/editor/various/behaviors.spec.js b/test/e2e/specs/editor/various/behaviors.spec.js index c9b01e50740121..5866721a05d5dc 100644 --- a/test/e2e/specs/editor/various/behaviors.spec.js +++ b/test/e2e/specs/editor/various/behaviors.spec.js @@ -41,6 +41,7 @@ test.describe( 'Testing behaviors functionality', () => { } ); await page.getByRole( 'button', { name: 'Advanced' } ).click(); + await expect( page.getByLabel( 'Behavior' ) ).toHaveCount( 1 ); await expect( page.getByLabel( 'Behavior' ) ).toHaveValue( 'lightbox' ); } ); @@ -70,7 +71,7 @@ test.describe( 'Testing behaviors functionality', () => { } ); await page.getByRole( 'button', { name: 'Advanced' } ).click(); - // Right now the selector has an empty value. - await expect( page.getByLabel( 'Behavior' ) ).toHaveValue( '' ); + // Right now the selector should not appear. + await expect( page.getByLabel( 'Behavior' ) ).toHaveCount( 0 ); } ); } ); From 0a68bea257d4d8dd334469670f0837d622042729 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Fri, 12 May 2023 14:23:57 +0200 Subject: [PATCH 15/61] Update core-blocks doc with behaviors attribute --- docs/reference-guides/core-blocks.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 423676e2afe320..df852bf97b6664 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -321,7 +321,7 @@ Insert an image to make a visual statement. ([Source](https://github.com/WordPre - **Name:** core/image - **Category:** media - **Supports:** anchor, color (~~background~~, ~~text~~), filter (duotone) -- **Attributes:** align, alt, caption, height, href, id, linkClass, linkDestination, linkTarget, rel, sizeSlug, title, url, width +- **Attributes:** align, alt, behaviors, caption, height, href, id, linkClass, linkDestination, linkTarget, rel, sizeSlug, title, url, width ## Latest Comments From fd089285f89e7b1904b3547fa86e2ef9ca149408 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Sat, 13 May 2023 11:26:18 +0200 Subject: [PATCH 16/61] Update fixtures with new lightbox attribute --- .../blocks/core__gallery-with-caption.json | 10 +++++-- .../fixtures/blocks/core__gallery.json | 10 +++++-- .../blocks/core__gallery__columns.json | 10 +++++-- .../blocks/core__gallery__deprecated-1.json | 10 +++++-- .../blocks/core__gallery__deprecated-2.json | 10 +++++-- .../blocks/core__gallery__deprecated-3.json | 10 +++++-- .../blocks/core__gallery__deprecated-4.json | 15 ++++++++-- .../blocks/core__gallery__deprecated-5.json | 15 ++++++++-- .../blocks/core__gallery__deprecated-6.json | 15 ++++++++-- .../blocks/core__gallery__deprecated-7.json | 30 +++++++++++++++---- .../fixtures/blocks/core__image.json | 5 +++- .../blocks/core__image__attachment-link.json | 5 +++- .../blocks/core__image__center-caption.json | 5 +++- .../core__image__custom-link-class.json | 5 +++- .../blocks/core__image__custom-link-rel.json | 5 +++- .../blocks/core__image__custom-link.json | 5 +++- .../blocks/core__image__deprecated-3.json | 5 +++- .../blocks/core__image__media-link.json | 5 +++- 18 files changed, 140 insertions(+), 35 deletions(-) diff --git a/test/integration/fixtures/blocks/core__gallery-with-caption.json b/test/integration/fixtures/blocks/core__gallery-with-caption.json index 12b516606641d4..ac7adb1267d0c9 100644 --- a/test/integration/fixtures/blocks/core__gallery-with-caption.json +++ b/test/integration/fixtures/blocks/core__gallery-with-caption.json @@ -24,7 +24,10 @@ "caption": "", "id": 1421, "sizeSlug": "large", - "linkDestination": "none" + "linkDestination": "none", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] }, @@ -37,7 +40,10 @@ "caption": "", "id": 1440, "sizeSlug": "large", - "linkDestination": "none" + "linkDestination": "none", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__gallery.json b/test/integration/fixtures/blocks/core__gallery.json index 8b7a1000d37ccc..009e8c076fd08f 100644 --- a/test/integration/fixtures/blocks/core__gallery.json +++ b/test/integration/fixtures/blocks/core__gallery.json @@ -24,7 +24,10 @@ "caption": "", "id": 1421, "sizeSlug": "large", - "linkDestination": "none" + "linkDestination": "none", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] }, @@ -37,7 +40,10 @@ "caption": "", "id": 1440, "sizeSlug": "large", - "linkDestination": "none" + "linkDestination": "none", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__gallery__columns.json b/test/integration/fixtures/blocks/core__gallery__columns.json index d0c40b3d3a9a93..f464f7664abfd6 100644 --- a/test/integration/fixtures/blocks/core__gallery__columns.json +++ b/test/integration/fixtures/blocks/core__gallery__columns.json @@ -24,7 +24,10 @@ "caption": "", "id": 1421, "sizeSlug": "large", - "linkDestination": "none" + "linkDestination": "none", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] }, @@ -37,7 +40,10 @@ "caption": "", "id": 1440, "sizeSlug": "large", - "linkDestination": "none" + "linkDestination": "none", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__gallery__deprecated-1.json b/test/integration/fixtures/blocks/core__gallery__deprecated-1.json index 9e15ee7f1c7149..9a7f8bef587daa 100644 --- a/test/integration/fixtures/blocks/core__gallery__deprecated-1.json +++ b/test/integration/fixtures/blocks/core__gallery__deprecated-1.json @@ -16,7 +16,10 @@ "attributes": { "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==", "alt": "title", - "linkDestination": "none" + "linkDestination": "none", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] }, @@ -26,7 +29,10 @@ "attributes": { "url": "data:image/jpeg;base64,/9j/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/yQALCAABAAEBAREA/8wABgAQEAX/2gAIAQEAAD8A0s8g/9k=", "alt": "title", - "linkDestination": "none" + "linkDestination": "none", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__gallery__deprecated-2.json b/test/integration/fixtures/blocks/core__gallery__deprecated-2.json index a60776801eb337..f38d799d8c28f6 100644 --- a/test/integration/fixtures/blocks/core__gallery__deprecated-2.json +++ b/test/integration/fixtures/blocks/core__gallery__deprecated-2.json @@ -18,7 +18,10 @@ "alt": "title", "caption": "", "id": 1, - "linkDestination": "none" + "linkDestination": "none", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] }, @@ -30,7 +33,10 @@ "alt": "title", "caption": "", "id": 2, - "linkDestination": "none" + "linkDestination": "none", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__gallery__deprecated-3.json b/test/integration/fixtures/blocks/core__gallery__deprecated-3.json index 9ac2c197819ce5..8df5a76cf1a3d4 100644 --- a/test/integration/fixtures/blocks/core__gallery__deprecated-3.json +++ b/test/integration/fixtures/blocks/core__gallery__deprecated-3.json @@ -16,7 +16,10 @@ "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==", "alt": "title", "caption": "", - "linkDestination": "none" + "linkDestination": "none", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] }, @@ -27,7 +30,10 @@ "url": "data:image/jpeg;base64,/9j/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/yQALCAABAAEBAREA/8wABgAQEAX/2gAIAQEAAD8A0s8g/9k=", "alt": "title", "caption": "", - "linkDestination": "none" + "linkDestination": "none", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__gallery__deprecated-4.json b/test/integration/fixtures/blocks/core__gallery__deprecated-4.json index dad150cc96b48b..1849374547c6c2 100644 --- a/test/integration/fixtures/blocks/core__gallery__deprecated-4.json +++ b/test/integration/fixtures/blocks/core__gallery__deprecated-4.json @@ -18,7 +18,10 @@ "alt": "", "caption": "", "id": 1421, - "linkDestination": "none" + "linkDestination": "none", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] }, @@ -30,7 +33,10 @@ "alt": "", "caption": "", "id": 1440, - "linkDestination": "none" + "linkDestination": "none", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] }, @@ -42,7 +48,10 @@ "alt": "", "caption": "", "id": 1362, - "linkDestination": "none" + "linkDestination": "none", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__gallery__deprecated-5.json b/test/integration/fixtures/blocks/core__gallery__deprecated-5.json index 9ef1f03ba09349..78719d37808f2e 100644 --- a/test/integration/fixtures/blocks/core__gallery__deprecated-5.json +++ b/test/integration/fixtures/blocks/core__gallery__deprecated-5.json @@ -20,7 +20,10 @@ "href": "http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1-682x1024.jpg", "id": 705, "sizeSlug": "large", - "linkDestination": "media" + "linkDestination": "media", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] }, @@ -34,7 +37,10 @@ "href": "http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1024x682.jpg", "id": 704, "sizeSlug": "large", - "linkDestination": "media" + "linkDestination": "media", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] }, @@ -48,7 +54,10 @@ "href": "http://wptest.local/wp-content/uploads/2020/04/test-image-1024x683.jpg", "id": 703, "sizeSlug": "large", - "linkDestination": "media" + "linkDestination": "media", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__gallery__deprecated-6.json b/test/integration/fixtures/blocks/core__gallery__deprecated-6.json index 9ef1f03ba09349..78719d37808f2e 100644 --- a/test/integration/fixtures/blocks/core__gallery__deprecated-6.json +++ b/test/integration/fixtures/blocks/core__gallery__deprecated-6.json @@ -20,7 +20,10 @@ "href": "http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1-682x1024.jpg", "id": 705, "sizeSlug": "large", - "linkDestination": "media" + "linkDestination": "media", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] }, @@ -34,7 +37,10 @@ "href": "http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1024x682.jpg", "id": 704, "sizeSlug": "large", - "linkDestination": "media" + "linkDestination": "media", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] }, @@ -48,7 +54,10 @@ "href": "http://wptest.local/wp-content/uploads/2020/04/test-image-1024x683.jpg", "id": 703, "sizeSlug": "large", - "linkDestination": "media" + "linkDestination": "media", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__gallery__deprecated-7.json b/test/integration/fixtures/blocks/core__gallery__deprecated-7.json index 4eaf85b46906d6..440087e65fd55d 100644 --- a/test/integration/fixtures/blocks/core__gallery__deprecated-7.json +++ b/test/integration/fixtures/blocks/core__gallery__deprecated-7.json @@ -24,7 +24,10 @@ "href": "http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1-682x1024.jpg", "id": 705, "sizeSlug": "large", - "linkDestination": "media" + "linkDestination": "media", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] }, @@ -38,7 +41,10 @@ "href": "http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1024x682.jpg", "id": 704, "sizeSlug": "large", - "linkDestination": "media" + "linkDestination": "media", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] }, @@ -52,7 +58,10 @@ "href": "http://wptest.local/wp-content/uploads/2020/04/test-image-1024x683.jpg", "id": 703, "sizeSlug": "large", - "linkDestination": "media" + "linkDestination": "media", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] } @@ -83,7 +92,10 @@ "href": "http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1-682x1024.jpg", "id": 705, "sizeSlug": "large", - "linkDestination": "media" + "linkDestination": "media", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] }, @@ -97,7 +109,10 @@ "href": "http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1024x682.jpg", "id": 704, "sizeSlug": "large", - "linkDestination": "media" + "linkDestination": "media", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] }, @@ -111,7 +126,10 @@ "href": "http://wptest.local/wp-content/uploads/2020/04/test-image-1024x683.jpg", "id": 703, "sizeSlug": "large", - "linkDestination": "media" + "linkDestination": "media", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__image.json b/test/integration/fixtures/blocks/core__image.json index 3da5557bb05cad..1a7d3ea108b15a 100644 --- a/test/integration/fixtures/blocks/core__image.json +++ b/test/integration/fixtures/blocks/core__image.json @@ -5,7 +5,10 @@ "attributes": { "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==", "alt": "", - "caption": "" + "caption": "", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__image__attachment-link.json b/test/integration/fixtures/blocks/core__image__attachment-link.json index 055e4a61d2f383..ff29c929692a8f 100644 --- a/test/integration/fixtures/blocks/core__image__attachment-link.json +++ b/test/integration/fixtures/blocks/core__image__attachment-link.json @@ -7,7 +7,10 @@ "alt": "", "caption": "", "href": "http://localhost:8888/?attachment_id=7", - "linkDestination": "attachment" + "linkDestination": "attachment", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__image__center-caption.json b/test/integration/fixtures/blocks/core__image__center-caption.json index a369e433b4028e..527df0a23af3b4 100644 --- a/test/integration/fixtures/blocks/core__image__center-caption.json +++ b/test/integration/fixtures/blocks/core__image__center-caption.json @@ -6,7 +6,10 @@ "align": "center", "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==", "alt": "", - "caption": "Give it a try. Press the \"really wide\" button on the image toolbar." + "caption": "Give it a try. Press the \"really wide\" button on the image toolbar.", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__image__custom-link-class.json b/test/integration/fixtures/blocks/core__image__custom-link-class.json index 2b5a36e78bdb8b..51cf778064dc01 100644 --- a/test/integration/fixtures/blocks/core__image__custom-link-class.json +++ b/test/integration/fixtures/blocks/core__image__custom-link-class.json @@ -8,7 +8,10 @@ "caption": "", "href": "https://wordpress.org/", "linkClass": "custom-link", - "linkDestination": "custom" + "linkDestination": "custom", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__image__custom-link-rel.json b/test/integration/fixtures/blocks/core__image__custom-link-rel.json index dc3a04a932c415..ab4811c1beda57 100644 --- a/test/integration/fixtures/blocks/core__image__custom-link-rel.json +++ b/test/integration/fixtures/blocks/core__image__custom-link-rel.json @@ -8,7 +8,10 @@ "caption": "", "href": "https://wordpress.org/", "rel": "external", - "linkDestination": "custom" + "linkDestination": "custom", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__image__custom-link.json b/test/integration/fixtures/blocks/core__image__custom-link.json index 54cfa81f72b148..ad0dba78def177 100644 --- a/test/integration/fixtures/blocks/core__image__custom-link.json +++ b/test/integration/fixtures/blocks/core__image__custom-link.json @@ -7,7 +7,10 @@ "alt": "", "caption": "", "href": "https://wordpress.org/", - "linkDestination": "custom" + "linkDestination": "custom", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__image__deprecated-3.json b/test/integration/fixtures/blocks/core__image__deprecated-3.json index bae213510011ac..0fcb09638eef8b 100644 --- a/test/integration/fixtures/blocks/core__image__deprecated-3.json +++ b/test/integration/fixtures/blocks/core__image__deprecated-3.json @@ -8,7 +8,10 @@ "alt": "", "caption": "", "width": 100, - "height": 100 + "height": 100, + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__image__media-link.json b/test/integration/fixtures/blocks/core__image__media-link.json index 0d411f713203de..300f4ba7dadfa4 100644 --- a/test/integration/fixtures/blocks/core__image__media-link.json +++ b/test/integration/fixtures/blocks/core__image__media-link.json @@ -7,7 +7,10 @@ "alt": "", "caption": "", "href": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==", - "linkDestination": "media" + "linkDestination": "media", + "behaviors": { + "lightbox": true + } }, "innerBlocks": [] } From 4e03e0d0c4902d047221b979f816f2b42bb67d4a Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Mon, 15 May 2023 23:37:37 -0500 Subject: [PATCH 17/61] Add a new theme for e2e tests --- .../{behaviors => behaviors-disabled}/index.php | 0 .../{behaviors => behaviors-disabled}/style.css | 0 .../{behaviors => behaviors-disabled}/theme.json | 0 .../behaviors-false/index.php | 0 .../behaviors-false/style.css | 15 +++++++++++++++ .../behaviors-false/theme.json | 8 ++++++++ 6 files changed, 23 insertions(+) rename test/gutenberg-test-themes/{behaviors => behaviors-disabled}/index.php (100%) rename test/gutenberg-test-themes/{behaviors => behaviors-disabled}/style.css (100%) rename test/gutenberg-test-themes/{behaviors => behaviors-disabled}/theme.json (100%) create mode 100644 test/gutenberg-test-themes/behaviors-false/index.php create mode 100644 test/gutenberg-test-themes/behaviors-false/style.css create mode 100644 test/gutenberg-test-themes/behaviors-false/theme.json diff --git a/test/gutenberg-test-themes/behaviors/index.php b/test/gutenberg-test-themes/behaviors-disabled/index.php similarity index 100% rename from test/gutenberg-test-themes/behaviors/index.php rename to test/gutenberg-test-themes/behaviors-disabled/index.php diff --git a/test/gutenberg-test-themes/behaviors/style.css b/test/gutenberg-test-themes/behaviors-disabled/style.css similarity index 100% rename from test/gutenberg-test-themes/behaviors/style.css rename to test/gutenberg-test-themes/behaviors-disabled/style.css diff --git a/test/gutenberg-test-themes/behaviors/theme.json b/test/gutenberg-test-themes/behaviors-disabled/theme.json similarity index 100% rename from test/gutenberg-test-themes/behaviors/theme.json rename to test/gutenberg-test-themes/behaviors-disabled/theme.json diff --git a/test/gutenberg-test-themes/behaviors-false/index.php b/test/gutenberg-test-themes/behaviors-false/index.php new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/test/gutenberg-test-themes/behaviors-false/style.css b/test/gutenberg-test-themes/behaviors-false/style.css new file mode 100644 index 00000000000000..972dbacbb5cabd --- /dev/null +++ b/test/gutenberg-test-themes/behaviors-false/style.css @@ -0,0 +1,15 @@ +/* +Theme Name: Behaviors +Theme URI: https://github.com/wordpress/theme-experiments/ +Author: the WordPress team +Description: Behaviors test theme. +Requires at least: 5.3 +Tested up to: 6.2 +Requires PHP: 5.6 +Version: 1.0 +License: GNU General Public License v2 or later +License URI: http://www.gnu.org/licenses/gpl-2.0.html +Text Domain: behaviors +Behaviors WordPress Theme, (C) 2023 WordPress.org +Behaviors is distributed under the terms of the GNU GPL. +*/ \ No newline at end of file diff --git a/test/gutenberg-test-themes/behaviors-false/theme.json b/test/gutenberg-test-themes/behaviors-false/theme.json new file mode 100644 index 00000000000000..6b06b08098a9df --- /dev/null +++ b/test/gutenberg-test-themes/behaviors-false/theme.json @@ -0,0 +1,8 @@ +{ + "version": 2, + "settings": { + "behaviors": { + "lightbox": false + } + } +} From 92b6e6c67c9e28609f4f7a734130078733232b2e Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Mon, 15 May 2023 23:39:57 -0500 Subject: [PATCH 18/61] Change theme.json to include `settings.behaviors` --- lib/theme.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/theme.json b/lib/theme.json index 09949e539dcf53..c62fa3b7f28c19 100644 --- a/lib/theme.json +++ b/lib/theme.json @@ -1,6 +1,9 @@ { "version": 2, "settings": { + "behaviors": { + "lightbox": true + }, "appearanceTools": false, "useRootPaddingAwareAlignments": false, "border": { From ffa881e3e8f99d607fd5eb8163ae937c6b2693a3 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Mon, 15 May 2023 23:41:09 -0500 Subject: [PATCH 19/61] Remove default behavior value from block.json --- packages/block-library/src/image/block.json | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/block-library/src/image/block.json b/packages/block-library/src/image/block.json index f2e9be4e10e06d..791e09f73c8009 100644 --- a/packages/block-library/src/image/block.json +++ b/packages/block-library/src/image/block.json @@ -82,10 +82,7 @@ "attribute": "target" }, "behaviors": { - "type": "object", - "default": { - "lightbox": true - } + "type": "object" } }, "supports": { From 99c964b639f651220a7cd6f7ad8c3440b3be8ca4 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Mon, 15 May 2023 23:41:58 -0500 Subject: [PATCH 20/61] Minor fix to the implementation --- packages/block-editor/src/hooks/behaviors.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/packages/block-editor/src/hooks/behaviors.js b/packages/block-editor/src/hooks/behaviors.js index 0aae8fdbea48ba..3724bd126b759c 100644 --- a/packages/block-editor/src/hooks/behaviors.js +++ b/packages/block-editor/src/hooks/behaviors.js @@ -42,16 +42,15 @@ export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => { const { behaviors: blockBehaviors } = props.attributes; - // TODO: Here we should get the theme behaviors but it doesn't work ATM. - const themeBehaviors = settings?.behaviors; + // Get the theme behaviors from the theme.json. + // + // TODO: We probably want to use a top-level `behaviors` property of + // `theme.json` instead of `settings.behaviors` like we do now. + const themeBehaviors = settings?.__experimentalFeatures?.behaviors; // By default, use the block behaviors. - let behaviors = blockBehaviors; - // If the theme has behaviors, but the block does not, use the theme behaviors. - if ( ! blockBehaviors && themeBehaviors ) { - behaviors = themeBehaviors; - } + const behaviors = blockBehaviors || themeBehaviors || {}; return ( <> From 7f7db2c8394073f01158e3c6f58c88a9c457f03f Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Mon, 15 May 2023 23:42:14 -0500 Subject: [PATCH 21/61] Add more e2e tests --- .../specs/editor/various/behaviors.spec.js | 89 +++++++++++++++---- 1 file changed, 71 insertions(+), 18 deletions(-) diff --git a/test/e2e/specs/editor/various/behaviors.spec.js b/test/e2e/specs/editor/various/behaviors.spec.js index 5866721a05d5dc..97aa6300103a6c 100644 --- a/test/e2e/specs/editor/various/behaviors.spec.js +++ b/test/e2e/specs/editor/various/behaviors.spec.js @@ -9,6 +9,15 @@ const path = require( 'path' ); const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); test.describe( 'Testing behaviors functionality', () => { + const filename = '1024x768_e2e_test_image_size.jpeg'; + const filepath = path.join( './test/e2e/assets', filename ); + + const createMedia = async ( { admin, requestUtils } ) => { + await admin.createNewPost(); + const media = await requestUtils.uploadMedia( filepath ); + return media; + }; + test.afterAll( async ( { requestUtils } ) => { await requestUtils.activateTheme( 'twentytwentyone' ); await requestUtils.deleteAllPosts(); @@ -18,19 +27,14 @@ test.describe( 'Testing behaviors functionality', () => { await requestUtils.deleteAllMedia(); } ); - test( 'Lightbox option should be selected in an image block, as defined in theme.json', async ( { + test( 'Lightbox behavior should be selected by default as defined in the core theme.json', async ( { admin, editor, requestUtils, page, } ) => { - await admin.createNewPost(); - const filename = '1024x768_e2e_test_image_size.jpeg'; - const filepath = path.join( './test/e2e/assets', filename ); - - await admin.createNewPost(); - const media = await requestUtils.uploadMedia( filepath ); - + await requestUtils.activateTheme( 'twentytwentyone' ); + const media = await createMedia( { admin, requestUtils } ); await editor.insertBlock( { name: 'core/image', attributes: { @@ -45,21 +49,41 @@ test.describe( 'Testing behaviors functionality', () => { await expect( page.getByLabel( 'Behavior' ) ).toHaveValue( 'lightbox' ); } ); - test( 'None option should be selected in an image block, as defined in theme.json', async ( { + test( 'Behaviors UI can be disabled in the `theme.json`', async ( { admin, editor, requestUtils, page, } ) => { - // Lightbox is the default behavior, so we need a theme that has it disabled. Change if we change the default. - await requestUtils.activateTheme( 'behaviors' ); - await admin.createNewPost(); + // { "lightbox": true } is the default behavior, so we activate the + // `behaviors` theme where it is disabled by default. Change if we change + // the default value in the core theme.json file. + await requestUtils.activateTheme( 'behaviors-disabled' ); + const media = await createMedia( { admin, requestUtils } ); - const filename = '1024x768_e2e_test_image_size.jpeg'; - const filepath = path.join( './test/e2e/assets', filename ); + await editor.insertBlock( { + name: 'core/image', + attributes: { + alt: filename, + id: media.id, + url: media.source_url, + }, + } ); - await admin.createNewPost(); - const media = await requestUtils.uploadMedia( filepath ); + await page.getByRole( 'button', { name: 'Advanced' } ).click(); + + // No behaviors dropdown should be present. + await expect( page.getByLabel( 'Behavior' ) ).toHaveCount( 0 ); + } ); + + test( "Block's value for behaviors takes precedence over the theme's value", async ( { + admin, + editor, + requestUtils, + page, + } ) => { + await requestUtils.activateTheme( 'twentytwentyone' ); + const media = await createMedia( { admin, requestUtils } ); await editor.insertBlock( { name: 'core/image', @@ -67,11 +91,40 @@ test.describe( 'Testing behaviors functionality', () => { alt: filename, id: media.id, url: media.source_url, + // Explicitly set the value for behaviors to false. + behaviors: { lightbox: false }, }, } ); await page.getByRole( 'button', { name: 'Advanced' } ).click(); - // Right now the selector should not appear. - await expect( page.getByLabel( 'Behavior' ) ).toHaveCount( 0 ); + await expect( page.getByLabel( 'Behavior' ) ).toHaveCount( 1 ); + await expect( page.getByLabel( 'Behavior' ) ).toHaveValue( '' ); + } ); + + test( 'You can set the default value for the behaviors in the theme.json', async ( { + admin, + editor, + requestUtils, + page, + } ) => { + // In this theme, the default value for settings.behaviors.lightbox is `false`. + await requestUtils.activateTheme( 'behaviors-false' ); + const media = await createMedia( { admin, requestUtils } ); + + await editor.insertBlock( { + name: 'core/image', + attributes: { + alt: filename, + id: media.id, + url: media.source_url, + }, + } ); + + await page.getByRole( 'button', { name: 'Advanced' } ).click(); + await expect( page.getByLabel( 'Behavior' ) ).toHaveCount( 1 ); + + // The default value for behaviors in `theme.json` is `false`, so the + // dropdown should be present, but the value should be `""` (None). + await expect( page.getByLabel( 'Behavior' ) ).toHaveValue( '' ); } ); } ); From 5e5ff9a028daa940570baf9710a456ce53ee13f1 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Tue, 16 May 2023 19:47:03 -0500 Subject: [PATCH 22/61] Create a selector for behaviors --- packages/block-editor/src/store/selectors.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 59c36dca2b8237..1e4fb921275508 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -2490,6 +2490,17 @@ export function getSettings( state ) { return state.settings; } +/** + * Returns the behaviors. + * + * @param {Object} state Editor state. + * + * @return {Object} The editor behaviors object. + */ +export function getBehaviors( state ) { + return state.settings.behaviors; +} + /** * Returns true if the most recent block change is be considered persistent, or * false otherwise. A persistent change is one committed by BlockEditorProvider From ad20b642119eef5a6d73a2709617149f6a7e0204 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Tue, 16 May 2023 19:47:57 -0500 Subject: [PATCH 23/61] Add a filter to load behaviors on the server from the theme.json --- lib/class-wp-theme-json-gutenberg.php | 1 + lib/compat/wordpress-6.3/behaviors.php | 17 +++++++++++++++++ lib/load.php | 2 +- 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 lib/compat/wordpress-6.3/behaviors.php diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 5eb65062d5a5c5..ef69437b95142b 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -323,6 +323,7 @@ class WP_Theme_JSON_Gutenberg { 'templateParts', 'title', 'version', + 'behaviors', ); /** diff --git a/lib/compat/wordpress-6.3/behaviors.php b/lib/compat/wordpress-6.3/behaviors.php new file mode 100644 index 00000000000000..e4348948bebfd7 --- /dev/null +++ b/lib/compat/wordpress-6.3/behaviors.php @@ -0,0 +1,17 @@ +get_data()['behaviors']; + return $settings; + }, + PHP_INT_MAX +); diff --git a/lib/load.php b/lib/load.php index a97a7cdc881f5e..c99e0ec9e98b79 100644 --- a/lib/load.php +++ b/lib/load.php @@ -93,6 +93,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-6.3/html-api/class-gutenberg-html-tag-processor-6-3.php'; require __DIR__ . '/compat/wordpress-6.3/script-loader.php'; require __DIR__ . '/compat/wordpress-6.3/blocks.php'; +require __DIR__ . '/compat/wordpress-6.3/behaviors.php'; // Experimental features. remove_action( 'plugins_loaded', '_wp_theme_json_webfonts_handler' ); // Turns off WP 6.0's stopgap handler for Webfonts API. @@ -165,4 +166,3 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/block-supports/duotone.php'; require __DIR__ . '/block-supports/anchor.php'; require __DIR__ . '/block-supports/shadow.php'; - From d16266b4f5ad3c827e669c3c3b4899f6303abaf7 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Tue, 16 May 2023 19:48:40 -0500 Subject: [PATCH 24/61] Add behaviors on the top-level in core theme.json --- lib/theme.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/theme.json b/lib/theme.json index c62fa3b7f28c19..c030305314ba0b 100644 --- a/lib/theme.json +++ b/lib/theme.json @@ -1,9 +1,9 @@ { "version": 2, + "behaviors": { + "lightbox": true + }, "settings": { - "behaviors": { - "lightbox": true - }, "appearanceTools": false, "useRootPaddingAwareAlignments": false, "border": { From 0982d9f8988d1830f7580051e95ab62031cee574 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Tue, 16 May 2023 19:49:26 -0500 Subject: [PATCH 25/61] Use the behaviors in the hooks --- packages/block-editor/src/hooks/behaviors.js | 2 +- .../editor/src/components/provider/use-block-editor-settings.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/block-editor/src/hooks/behaviors.js b/packages/block-editor/src/hooks/behaviors.js index 3724bd126b759c..6cfac1936ff053 100644 --- a/packages/block-editor/src/hooks/behaviors.js +++ b/packages/block-editor/src/hooks/behaviors.js @@ -46,7 +46,7 @@ export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => { // // TODO: We probably want to use a top-level `behaviors` property of // `theme.json` instead of `settings.behaviors` like we do now. - const themeBehaviors = settings?.__experimentalFeatures?.behaviors; + const themeBehaviors = select( blockEditorStore ).getBehaviors(); // By default, use the block behaviors. // If the theme has behaviors, but the block does not, use the theme behaviors. diff --git a/packages/editor/src/components/provider/use-block-editor-settings.js b/packages/editor/src/components/provider/use-block-editor-settings.js index d8035f4f54fca1..33a28460c39cdc 100644 --- a/packages/editor/src/components/provider/use-block-editor-settings.js +++ b/packages/editor/src/components/provider/use-block-editor-settings.js @@ -194,6 +194,7 @@ function useBlockEditorSettings( settings, hasTemplate ) { BLOCK_EDITOR_SETTINGS.includes( key ) ) ), + behaviors: settings.behaviors, mediaUpload: hasUploadPermissions ? mediaUpload : undefined, __experimentalReusableBlocks: reusableBlocks, __experimentalBlockPatterns: blockPatterns, From 0ef7b5e0e709962d24ba66368394482303de7ef2 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Tue, 16 May 2023 19:51:22 -0500 Subject: [PATCH 26/61] Update the comment --- lib/compat/wordpress-6.3/behaviors.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.3/behaviors.php b/lib/compat/wordpress-6.3/behaviors.php index e4348948bebfd7..401a94bf338445 100644 --- a/lib/compat/wordpress-6.3/behaviors.php +++ b/lib/compat/wordpress-6.3/behaviors.php @@ -9,7 +9,7 @@ 'block_editor_settings_all', function( $settings ) { - // Make sure to also get the value from the core theme.json file. + // TODO: Make sure to also get the value from the core theme.json file. $settings['behaviors'] = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_data()['behaviors']; return $settings; }, From e75fd87a30b38572cd4c064509a745b4404e5faf Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Tue, 16 May 2023 19:52:25 -0500 Subject: [PATCH 27/61] Update the comment in behaviors.js --- packages/block-editor/src/hooks/behaviors.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/block-editor/src/hooks/behaviors.js b/packages/block-editor/src/hooks/behaviors.js index 6cfac1936ff053..d3ea7938d28846 100644 --- a/packages/block-editor/src/hooks/behaviors.js +++ b/packages/block-editor/src/hooks/behaviors.js @@ -43,9 +43,6 @@ export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => { const { behaviors: blockBehaviors } = props.attributes; // Get the theme behaviors from the theme.json. - // - // TODO: We probably want to use a top-level `behaviors` property of - // `theme.json` instead of `settings.behaviors` like we do now. const themeBehaviors = select( blockEditorStore ).getBehaviors(); // By default, use the block behaviors. From f6414f98964fb8b8da21965fdb0963d1caa20675 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Wed, 17 May 2023 14:56:25 +0200 Subject: [PATCH 28/61] Update fixtures and e2e --- .../data/data-core-block-editor.md | 12 ++++++++ .../specs/editor/various/behaviors.spec.js | 2 +- .../blocks/core__gallery-with-caption.json | 10 ++----- .../fixtures/blocks/core__gallery.json | 10 ++----- .../blocks/core__gallery__columns.json | 10 ++----- .../blocks/core__gallery__deprecated-1.json | 10 ++----- .../blocks/core__gallery__deprecated-2.json | 10 ++----- .../blocks/core__gallery__deprecated-3.json | 10 ++----- .../blocks/core__gallery__deprecated-4.json | 15 ++-------- .../blocks/core__gallery__deprecated-5.json | 15 ++-------- .../blocks/core__gallery__deprecated-6.json | 15 ++-------- .../blocks/core__gallery__deprecated-7.json | 30 ++++--------------- .../fixtures/blocks/core__image.json | 5 +--- .../blocks/core__image__attachment-link.json | 5 +--- .../blocks/core__image__center-caption.json | 5 +--- .../core__image__custom-link-class.json | 5 +--- .../blocks/core__image__custom-link-rel.json | 5 +--- .../blocks/core__image__custom-link.json | 5 +--- .../blocks/core__image__deprecated-3.json | 5 +--- .../blocks/core__image__media-link.json | 5 +--- 20 files changed, 48 insertions(+), 141 deletions(-) diff --git a/docs/reference-guides/data/data-core-block-editor.md b/docs/reference-guides/data/data-core-block-editor.md index a3d6fa25e97c89..d8a3e2259c36d9 100644 --- a/docs/reference-guides/data/data-core-block-editor.md +++ b/docs/reference-guides/data/data-core-block-editor.md @@ -168,6 +168,18 @@ _Returns_ - `Array?`: The list of allowed block types. +### getBehaviors + +Returns the behaviors. + +_Parameters_ + +- _state_ `Object`: Editor state. + +_Returns_ + +- `Object`: The editor behaviors object. + ### getBlock Returns a block given its client ID. This is a parsed copy of the block, containing its `blockName`, `clientId`, and current `attributes` state. This is not the block's registration settings, which must be retrieved from the blocks module registration store. diff --git a/test/e2e/specs/editor/various/behaviors.spec.js b/test/e2e/specs/editor/various/behaviors.spec.js index 97aa6300103a6c..c1babb6b846b72 100644 --- a/test/e2e/specs/editor/various/behaviors.spec.js +++ b/test/e2e/specs/editor/various/behaviors.spec.js @@ -46,7 +46,7 @@ test.describe( 'Testing behaviors functionality', () => { await page.getByRole( 'button', { name: 'Advanced' } ).click(); await expect( page.getByLabel( 'Behavior' ) ).toHaveCount( 1 ); - await expect( page.getByLabel( 'Behavior' ) ).toHaveValue( 'lightbox' ); + await expect( page.getByLabel( 'Behavior' ) ).toHaveValue( '' ); } ); test( 'Behaviors UI can be disabled in the `theme.json`', async ( { diff --git a/test/integration/fixtures/blocks/core__gallery-with-caption.json b/test/integration/fixtures/blocks/core__gallery-with-caption.json index ac7adb1267d0c9..12b516606641d4 100644 --- a/test/integration/fixtures/blocks/core__gallery-with-caption.json +++ b/test/integration/fixtures/blocks/core__gallery-with-caption.json @@ -24,10 +24,7 @@ "caption": "", "id": 1421, "sizeSlug": "large", - "linkDestination": "none", - "behaviors": { - "lightbox": true - } + "linkDestination": "none" }, "innerBlocks": [] }, @@ -40,10 +37,7 @@ "caption": "", "id": 1440, "sizeSlug": "large", - "linkDestination": "none", - "behaviors": { - "lightbox": true - } + "linkDestination": "none" }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__gallery.json b/test/integration/fixtures/blocks/core__gallery.json index 009e8c076fd08f..8b7a1000d37ccc 100644 --- a/test/integration/fixtures/blocks/core__gallery.json +++ b/test/integration/fixtures/blocks/core__gallery.json @@ -24,10 +24,7 @@ "caption": "", "id": 1421, "sizeSlug": "large", - "linkDestination": "none", - "behaviors": { - "lightbox": true - } + "linkDestination": "none" }, "innerBlocks": [] }, @@ -40,10 +37,7 @@ "caption": "", "id": 1440, "sizeSlug": "large", - "linkDestination": "none", - "behaviors": { - "lightbox": true - } + "linkDestination": "none" }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__gallery__columns.json b/test/integration/fixtures/blocks/core__gallery__columns.json index f464f7664abfd6..d0c40b3d3a9a93 100644 --- a/test/integration/fixtures/blocks/core__gallery__columns.json +++ b/test/integration/fixtures/blocks/core__gallery__columns.json @@ -24,10 +24,7 @@ "caption": "", "id": 1421, "sizeSlug": "large", - "linkDestination": "none", - "behaviors": { - "lightbox": true - } + "linkDestination": "none" }, "innerBlocks": [] }, @@ -40,10 +37,7 @@ "caption": "", "id": 1440, "sizeSlug": "large", - "linkDestination": "none", - "behaviors": { - "lightbox": true - } + "linkDestination": "none" }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__gallery__deprecated-1.json b/test/integration/fixtures/blocks/core__gallery__deprecated-1.json index 9a7f8bef587daa..9e15ee7f1c7149 100644 --- a/test/integration/fixtures/blocks/core__gallery__deprecated-1.json +++ b/test/integration/fixtures/blocks/core__gallery__deprecated-1.json @@ -16,10 +16,7 @@ "attributes": { "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==", "alt": "title", - "linkDestination": "none", - "behaviors": { - "lightbox": true - } + "linkDestination": "none" }, "innerBlocks": [] }, @@ -29,10 +26,7 @@ "attributes": { "url": "data:image/jpeg;base64,/9j/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/yQALCAABAAEBAREA/8wABgAQEAX/2gAIAQEAAD8A0s8g/9k=", "alt": "title", - "linkDestination": "none", - "behaviors": { - "lightbox": true - } + "linkDestination": "none" }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__gallery__deprecated-2.json b/test/integration/fixtures/blocks/core__gallery__deprecated-2.json index f38d799d8c28f6..a60776801eb337 100644 --- a/test/integration/fixtures/blocks/core__gallery__deprecated-2.json +++ b/test/integration/fixtures/blocks/core__gallery__deprecated-2.json @@ -18,10 +18,7 @@ "alt": "title", "caption": "", "id": 1, - "linkDestination": "none", - "behaviors": { - "lightbox": true - } + "linkDestination": "none" }, "innerBlocks": [] }, @@ -33,10 +30,7 @@ "alt": "title", "caption": "", "id": 2, - "linkDestination": "none", - "behaviors": { - "lightbox": true - } + "linkDestination": "none" }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__gallery__deprecated-3.json b/test/integration/fixtures/blocks/core__gallery__deprecated-3.json index 8df5a76cf1a3d4..9ac2c197819ce5 100644 --- a/test/integration/fixtures/blocks/core__gallery__deprecated-3.json +++ b/test/integration/fixtures/blocks/core__gallery__deprecated-3.json @@ -16,10 +16,7 @@ "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==", "alt": "title", "caption": "", - "linkDestination": "none", - "behaviors": { - "lightbox": true - } + "linkDestination": "none" }, "innerBlocks": [] }, @@ -30,10 +27,7 @@ "url": "data:image/jpeg;base64,/9j/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/yQALCAABAAEBAREA/8wABgAQEAX/2gAIAQEAAD8A0s8g/9k=", "alt": "title", "caption": "", - "linkDestination": "none", - "behaviors": { - "lightbox": true - } + "linkDestination": "none" }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__gallery__deprecated-4.json b/test/integration/fixtures/blocks/core__gallery__deprecated-4.json index 1849374547c6c2..dad150cc96b48b 100644 --- a/test/integration/fixtures/blocks/core__gallery__deprecated-4.json +++ b/test/integration/fixtures/blocks/core__gallery__deprecated-4.json @@ -18,10 +18,7 @@ "alt": "", "caption": "", "id": 1421, - "linkDestination": "none", - "behaviors": { - "lightbox": true - } + "linkDestination": "none" }, "innerBlocks": [] }, @@ -33,10 +30,7 @@ "alt": "", "caption": "", "id": 1440, - "linkDestination": "none", - "behaviors": { - "lightbox": true - } + "linkDestination": "none" }, "innerBlocks": [] }, @@ -48,10 +42,7 @@ "alt": "", "caption": "", "id": 1362, - "linkDestination": "none", - "behaviors": { - "lightbox": true - } + "linkDestination": "none" }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__gallery__deprecated-5.json b/test/integration/fixtures/blocks/core__gallery__deprecated-5.json index 78719d37808f2e..9ef1f03ba09349 100644 --- a/test/integration/fixtures/blocks/core__gallery__deprecated-5.json +++ b/test/integration/fixtures/blocks/core__gallery__deprecated-5.json @@ -20,10 +20,7 @@ "href": "http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1-682x1024.jpg", "id": 705, "sizeSlug": "large", - "linkDestination": "media", - "behaviors": { - "lightbox": true - } + "linkDestination": "media" }, "innerBlocks": [] }, @@ -37,10 +34,7 @@ "href": "http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1024x682.jpg", "id": 704, "sizeSlug": "large", - "linkDestination": "media", - "behaviors": { - "lightbox": true - } + "linkDestination": "media" }, "innerBlocks": [] }, @@ -54,10 +48,7 @@ "href": "http://wptest.local/wp-content/uploads/2020/04/test-image-1024x683.jpg", "id": 703, "sizeSlug": "large", - "linkDestination": "media", - "behaviors": { - "lightbox": true - } + "linkDestination": "media" }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__gallery__deprecated-6.json b/test/integration/fixtures/blocks/core__gallery__deprecated-6.json index 78719d37808f2e..9ef1f03ba09349 100644 --- a/test/integration/fixtures/blocks/core__gallery__deprecated-6.json +++ b/test/integration/fixtures/blocks/core__gallery__deprecated-6.json @@ -20,10 +20,7 @@ "href": "http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1-682x1024.jpg", "id": 705, "sizeSlug": "large", - "linkDestination": "media", - "behaviors": { - "lightbox": true - } + "linkDestination": "media" }, "innerBlocks": [] }, @@ -37,10 +34,7 @@ "href": "http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1024x682.jpg", "id": 704, "sizeSlug": "large", - "linkDestination": "media", - "behaviors": { - "lightbox": true - } + "linkDestination": "media" }, "innerBlocks": [] }, @@ -54,10 +48,7 @@ "href": "http://wptest.local/wp-content/uploads/2020/04/test-image-1024x683.jpg", "id": 703, "sizeSlug": "large", - "linkDestination": "media", - "behaviors": { - "lightbox": true - } + "linkDestination": "media" }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__gallery__deprecated-7.json b/test/integration/fixtures/blocks/core__gallery__deprecated-7.json index 440087e65fd55d..4eaf85b46906d6 100644 --- a/test/integration/fixtures/blocks/core__gallery__deprecated-7.json +++ b/test/integration/fixtures/blocks/core__gallery__deprecated-7.json @@ -24,10 +24,7 @@ "href": "http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1-682x1024.jpg", "id": 705, "sizeSlug": "large", - "linkDestination": "media", - "behaviors": { - "lightbox": true - } + "linkDestination": "media" }, "innerBlocks": [] }, @@ -41,10 +38,7 @@ "href": "http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1024x682.jpg", "id": 704, "sizeSlug": "large", - "linkDestination": "media", - "behaviors": { - "lightbox": true - } + "linkDestination": "media" }, "innerBlocks": [] }, @@ -58,10 +52,7 @@ "href": "http://wptest.local/wp-content/uploads/2020/04/test-image-1024x683.jpg", "id": 703, "sizeSlug": "large", - "linkDestination": "media", - "behaviors": { - "lightbox": true - } + "linkDestination": "media" }, "innerBlocks": [] } @@ -92,10 +83,7 @@ "href": "http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1-682x1024.jpg", "id": 705, "sizeSlug": "large", - "linkDestination": "media", - "behaviors": { - "lightbox": true - } + "linkDestination": "media" }, "innerBlocks": [] }, @@ -109,10 +97,7 @@ "href": "http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1024x682.jpg", "id": 704, "sizeSlug": "large", - "linkDestination": "media", - "behaviors": { - "lightbox": true - } + "linkDestination": "media" }, "innerBlocks": [] }, @@ -126,10 +111,7 @@ "href": "http://wptest.local/wp-content/uploads/2020/04/test-image-1024x683.jpg", "id": 703, "sizeSlug": "large", - "linkDestination": "media", - "behaviors": { - "lightbox": true - } + "linkDestination": "media" }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__image.json b/test/integration/fixtures/blocks/core__image.json index 1a7d3ea108b15a..3da5557bb05cad 100644 --- a/test/integration/fixtures/blocks/core__image.json +++ b/test/integration/fixtures/blocks/core__image.json @@ -5,10 +5,7 @@ "attributes": { "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==", "alt": "", - "caption": "", - "behaviors": { - "lightbox": true - } + "caption": "" }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__image__attachment-link.json b/test/integration/fixtures/blocks/core__image__attachment-link.json index ff29c929692a8f..055e4a61d2f383 100644 --- a/test/integration/fixtures/blocks/core__image__attachment-link.json +++ b/test/integration/fixtures/blocks/core__image__attachment-link.json @@ -7,10 +7,7 @@ "alt": "", "caption": "", "href": "http://localhost:8888/?attachment_id=7", - "linkDestination": "attachment", - "behaviors": { - "lightbox": true - } + "linkDestination": "attachment" }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__image__center-caption.json b/test/integration/fixtures/blocks/core__image__center-caption.json index 527df0a23af3b4..a369e433b4028e 100644 --- a/test/integration/fixtures/blocks/core__image__center-caption.json +++ b/test/integration/fixtures/blocks/core__image__center-caption.json @@ -6,10 +6,7 @@ "align": "center", "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==", "alt": "", - "caption": "Give it a try. Press the \"really wide\" button on the image toolbar.", - "behaviors": { - "lightbox": true - } + "caption": "Give it a try. Press the \"really wide\" button on the image toolbar." }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__image__custom-link-class.json b/test/integration/fixtures/blocks/core__image__custom-link-class.json index 51cf778064dc01..2b5a36e78bdb8b 100644 --- a/test/integration/fixtures/blocks/core__image__custom-link-class.json +++ b/test/integration/fixtures/blocks/core__image__custom-link-class.json @@ -8,10 +8,7 @@ "caption": "", "href": "https://wordpress.org/", "linkClass": "custom-link", - "linkDestination": "custom", - "behaviors": { - "lightbox": true - } + "linkDestination": "custom" }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__image__custom-link-rel.json b/test/integration/fixtures/blocks/core__image__custom-link-rel.json index ab4811c1beda57..dc3a04a932c415 100644 --- a/test/integration/fixtures/blocks/core__image__custom-link-rel.json +++ b/test/integration/fixtures/blocks/core__image__custom-link-rel.json @@ -8,10 +8,7 @@ "caption": "", "href": "https://wordpress.org/", "rel": "external", - "linkDestination": "custom", - "behaviors": { - "lightbox": true - } + "linkDestination": "custom" }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__image__custom-link.json b/test/integration/fixtures/blocks/core__image__custom-link.json index ad0dba78def177..54cfa81f72b148 100644 --- a/test/integration/fixtures/blocks/core__image__custom-link.json +++ b/test/integration/fixtures/blocks/core__image__custom-link.json @@ -7,10 +7,7 @@ "alt": "", "caption": "", "href": "https://wordpress.org/", - "linkDestination": "custom", - "behaviors": { - "lightbox": true - } + "linkDestination": "custom" }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__image__deprecated-3.json b/test/integration/fixtures/blocks/core__image__deprecated-3.json index 0fcb09638eef8b..bae213510011ac 100644 --- a/test/integration/fixtures/blocks/core__image__deprecated-3.json +++ b/test/integration/fixtures/blocks/core__image__deprecated-3.json @@ -8,10 +8,7 @@ "alt": "", "caption": "", "width": 100, - "height": 100, - "behaviors": { - "lightbox": true - } + "height": 100 }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__image__media-link.json b/test/integration/fixtures/blocks/core__image__media-link.json index 300f4ba7dadfa4..0d411f713203de 100644 --- a/test/integration/fixtures/blocks/core__image__media-link.json +++ b/test/integration/fixtures/blocks/core__image__media-link.json @@ -7,10 +7,7 @@ "alt": "", "caption": "", "href": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==", - "linkDestination": "media", - "behaviors": { - "lightbox": true - } + "linkDestination": "media" }, "innerBlocks": [] } From 31c0623d9cefd130591662bc92136819c06a31f3 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 18 May 2023 16:13:38 +0200 Subject: [PATCH 29/61] Prevent mobile test gutenberg error (temporary - not the best solution) --- lib/compat/wordpress-6.3/behaviors.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/compat/wordpress-6.3/behaviors.php b/lib/compat/wordpress-6.3/behaviors.php index 401a94bf338445..c83f1158deb3ac 100644 --- a/lib/compat/wordpress-6.3/behaviors.php +++ b/lib/compat/wordpress-6.3/behaviors.php @@ -8,10 +8,19 @@ add_filter( 'block_editor_settings_all', function( $settings ) { - + // Prevents testing error 'Undefined index: behaviors' in Gutenberg_REST_Block_Editor_Settings_Controller_Test. + // This should be removed once the REST API is updated to use the new theme.json file. + $is_mobile_context = ( + defined( 'REST_REQUEST' ) && + REST_REQUEST && + isset( $_GET['context'] ) && + 'mobile' === $_GET['context'] + ); + if (! $is_mobile_context) { + $settings['behaviors'] = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_data()['behaviors']; + } // TODO: Make sure to also get the value from the core theme.json file. - $settings['behaviors'] = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_data()['behaviors']; return $settings; }, - PHP_INT_MAX + 'post-editor' ); From ee3701f486d8ecd39592157b53ed0343f43b25a5 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 18 May 2023 16:14:10 +0200 Subject: [PATCH 30/61] Add priority on filter --- lib/compat/wordpress-6.3/behaviors.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.3/behaviors.php b/lib/compat/wordpress-6.3/behaviors.php index c83f1158deb3ac..6f85cd20fb6e6f 100644 --- a/lib/compat/wordpress-6.3/behaviors.php +++ b/lib/compat/wordpress-6.3/behaviors.php @@ -22,5 +22,5 @@ function( $settings ) { // TODO: Make sure to also get the value from the core theme.json file. return $settings; }, - 'post-editor' + PHP_INT_MAX ); From a0ac74806af4e04917e823616c2c353dd6662ad7 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 18 May 2023 16:28:58 +0200 Subject: [PATCH 31/61] Fix php standards --- lib/compat/wordpress-6.3/behaviors.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.3/behaviors.php b/lib/compat/wordpress-6.3/behaviors.php index 6f85cd20fb6e6f..cab95d11b9753e 100644 --- a/lib/compat/wordpress-6.3/behaviors.php +++ b/lib/compat/wordpress-6.3/behaviors.php @@ -16,7 +16,7 @@ function( $settings ) { isset( $_GET['context'] ) && 'mobile' === $_GET['context'] ); - if (! $is_mobile_context) { + if ( ! $is_mobile_context ) { $settings['behaviors'] = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_data()['behaviors']; } // TODO: Make sure to also get the value from the core theme.json file. From 1dd6df1397c00b4634ac4feeee7b098f6fc64dad Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 18 May 2023 17:47:47 +0200 Subject: [PATCH 32/61] Found a much better way to fix php tests --- lib/compat/wordpress-6.3/behaviors.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/compat/wordpress-6.3/behaviors.php b/lib/compat/wordpress-6.3/behaviors.php index cab95d11b9753e..da73d82a6c0ff5 100644 --- a/lib/compat/wordpress-6.3/behaviors.php +++ b/lib/compat/wordpress-6.3/behaviors.php @@ -8,15 +8,8 @@ add_filter( 'block_editor_settings_all', function( $settings ) { - // Prevents testing error 'Undefined index: behaviors' in Gutenberg_REST_Block_Editor_Settings_Controller_Test. - // This should be removed once the REST API is updated to use the new theme.json file. - $is_mobile_context = ( - defined( 'REST_REQUEST' ) && - REST_REQUEST && - isset( $_GET['context'] ) && - 'mobile' === $_GET['context'] - ); - if ( ! $is_mobile_context ) { + $theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_data(); + if ( array_key_exists( 'behaviors', $theme_data ) ) { $settings['behaviors'] = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_data()['behaviors']; } // TODO: Make sure to also get the value from the core theme.json file. From a7372007ef1492ef84cca90c7ef381c9d4cd48ac Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 18 May 2023 20:42:00 +0200 Subject: [PATCH 33/61] Small refactor --- lib/compat/wordpress-6.3/behaviors.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.3/behaviors.php b/lib/compat/wordpress-6.3/behaviors.php index da73d82a6c0ff5..c2c51e9a92a778 100644 --- a/lib/compat/wordpress-6.3/behaviors.php +++ b/lib/compat/wordpress-6.3/behaviors.php @@ -10,7 +10,7 @@ function( $settings ) { $theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_data(); if ( array_key_exists( 'behaviors', $theme_data ) ) { - $settings['behaviors'] = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_data()['behaviors']; + $settings['behaviors'] = $theme_data['behaviors']; } // TODO: Make sure to also get the value from the core theme.json file. return $settings; From 3fd9270c9f0a4701c7a87baef24b0231b47f111a Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 18 May 2023 15:42:44 -0500 Subject: [PATCH 34/61] Add `behaviors` as an allowed key to BLOCK_EDITOR_SETTINGS --- .../editor/src/components/provider/use-block-editor-settings.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/editor/src/components/provider/use-block-editor-settings.js b/packages/editor/src/components/provider/use-block-editor-settings.js index 33a28460c39cdc..8a71fb5deca65f 100644 --- a/packages/editor/src/components/provider/use-block-editor-settings.js +++ b/packages/editor/src/components/provider/use-block-editor-settings.js @@ -75,6 +75,7 @@ const BLOCK_EDITOR_SETTINGS = [ '__unstableIsPreviewMode', '__unstableResolvedAssets', '__unstableIsBlockBasedTheme', + 'behaviors', ]; /** @@ -194,7 +195,6 @@ function useBlockEditorSettings( settings, hasTemplate ) { BLOCK_EDITOR_SETTINGS.includes( key ) ) ), - behaviors: settings.behaviors, mediaUpload: hasUploadPermissions ? mediaUpload : undefined, __experimentalReusableBlocks: reusableBlocks, __experimentalBlockPatterns: blockPatterns, From 5a9748cac61ebe3e23bfa8968ee6d9b7130aaab4 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 18 May 2023 17:04:17 -0500 Subject: [PATCH 35/61] Move the behaviors to top level in the e2e test theme.json file --- test/gutenberg-test-themes/behaviors-false/theme.json | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/gutenberg-test-themes/behaviors-false/theme.json b/test/gutenberg-test-themes/behaviors-false/theme.json index 6b06b08098a9df..b448f0972deb72 100644 --- a/test/gutenberg-test-themes/behaviors-false/theme.json +++ b/test/gutenberg-test-themes/behaviors-false/theme.json @@ -1,8 +1,6 @@ { "version": 2, - "settings": { - "behaviors": { - "lightbox": false - } + "behaviors": { + "lightbox": false } } From 9a6b1f211c4cff9df032717b944474ca7556c08e Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 18 May 2023 17:06:03 -0500 Subject: [PATCH 36/61] Rename the `behaviors` setting to `behaviorsUIEnabled` --- lib/class-wp-theme-json-gutenberg.php | 4 +--- lib/theme.json | 4 +--- packages/block-editor/src/hooks/behaviors.js | 4 ++-- test/gutenberg-test-themes/behaviors-disabled/theme.json | 4 +--- 4 files changed, 5 insertions(+), 11 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index ef69437b95142b..afd4825b627bb5 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -405,9 +405,7 @@ class WP_Theme_JSON_Gutenberg { 'textDecoration' => null, 'textTransform' => null, ), - 'behaviors' => array( - 'lightbox' => null, - ), + 'behaviorsUIEnabled' => null, ); /** diff --git a/lib/theme.json b/lib/theme.json index c030305314ba0b..2c42f4cef04b3b 100644 --- a/lib/theme.json +++ b/lib/theme.json @@ -455,9 +455,7 @@ } }, "core/image": { - "behaviors": { - "lightbox": true - } + "behaviorsUIEnabled": true } } }, diff --git a/packages/block-editor/src/hooks/behaviors.js b/packages/block-editor/src/hooks/behaviors.js index d3ea7938d28846..76d04d20680d4f 100644 --- a/packages/block-editor/src/hooks/behaviors.js +++ b/packages/block-editor/src/hooks/behaviors.js @@ -30,12 +30,12 @@ export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => { return ; } - // Check the value of `settings.blocks.core/image.behavior.lightbox` in the + // Check the value of `settings.blocks.core/image.behaviorsUIEnabled` in the // theme.json. If false, do not add the behaviors inspector control. const settings = select( blockEditorStore ).getSettings(); if ( ! settings?.__experimentalFeatures?.blocks?.[ props.name ] - ?.behaviors?.lightbox + ?.behaviorsUIEnabled ) { return ; } diff --git a/test/gutenberg-test-themes/behaviors-disabled/theme.json b/test/gutenberg-test-themes/behaviors-disabled/theme.json index a9f920f6dd0abc..c137f7a7223926 100644 --- a/test/gutenberg-test-themes/behaviors-disabled/theme.json +++ b/test/gutenberg-test-themes/behaviors-disabled/theme.json @@ -3,9 +3,7 @@ "settings": { "blocks": { "core/image": { - "behaviors": { - "lightbox": false - } + "behaviorsUIEnabled": false } } } From 1c7f6d5b55a99d460f8b974e0217324a5f7be1c8 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 18 May 2023 17:07:16 -0500 Subject: [PATCH 37/61] Change "None" to "No behaviors" --- packages/block-editor/src/hooks/behaviors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-editor/src/hooks/behaviors.js b/packages/block-editor/src/hooks/behaviors.js index 76d04d20680d4f..832480c79236cf 100644 --- a/packages/block-editor/src/hooks/behaviors.js +++ b/packages/block-editor/src/hooks/behaviors.js @@ -65,7 +65,7 @@ export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => { } ) ) .concat( { value: '', - label: __( 'None' ), + label: __( 'No behaviors' ), } ) } onChange={ ( nextValue ) => { // If the user selects something, it means that they want to From 470f74fa2563e4104634e607b0cc858ba6a806ff Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 18 May 2023 17:45:19 -0500 Subject: [PATCH 38/61] Behaviors -> behavior --- packages/block-editor/src/hooks/behaviors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-editor/src/hooks/behaviors.js b/packages/block-editor/src/hooks/behaviors.js index 832480c79236cf..c74b8259d07c5d 100644 --- a/packages/block-editor/src/hooks/behaviors.js +++ b/packages/block-editor/src/hooks/behaviors.js @@ -65,7 +65,7 @@ export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => { } ) ) .concat( { value: '', - label: __( 'No behaviors' ), + label: __( 'No behavior' ), } ) } onChange={ ( nextValue ) => { // If the user selects something, it means that they want to From bba7c2dff98b4d3d3e89f262061ab13e57a9c392 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 18 May 2023 17:54:37 -0500 Subject: [PATCH 39/61] Fix redundant ternary --- packages/block-editor/src/hooks/behaviors.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-editor/src/hooks/behaviors.js b/packages/block-editor/src/hooks/behaviors.js index c74b8259d07c5d..457347461100b6 100644 --- a/packages/block-editor/src/hooks/behaviors.js +++ b/packages/block-editor/src/hooks/behaviors.js @@ -65,14 +65,14 @@ export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => { } ) ) .concat( { value: '', - label: __( 'No behavior' ), + label: __( 'No behaviors' ), } ) } onChange={ ( nextValue ) => { // If the user selects something, it means that they want to // change the default value (true) so we save it in the attributes. props.setAttributes( { behaviors: { - lightbox: nextValue === '' ? false : true, + lightbox: nextValue === 'lightbox', }, } ); } } From 2cb348c04ab368311ce0991a9abc761a53259158 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 18 May 2023 18:45:12 -0500 Subject: [PATCH 40/61] Improve the JSDoc for behaviors selector --- .../data/data-core-block-editor.md | 13 ++++++++++++- packages/block-editor/src/store/selectors.js | 15 ++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/docs/reference-guides/data/data-core-block-editor.md b/docs/reference-guides/data/data-core-block-editor.md index d8a3e2259c36d9..8b0f24e60fb7bf 100644 --- a/docs/reference-guides/data/data-core-block-editor.md +++ b/docs/reference-guides/data/data-core-block-editor.md @@ -170,7 +170,18 @@ _Returns_ ### getBehaviors -Returns the behaviors. +Returns the behaviors registered with the editor. + +Behaviors are named, reusable pieces of functionality that can be attached to blocks. They are registered with the editor using the `theme.json` file. + +_Usage_ + +```js +const behaviors = select( blockEditorStore ).getBehaviors(); +if ( behaviors?.lightbox ) { + // Do something with the lightbox. +} +``` _Parameters_ diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 1e4fb921275508..487d13db811d84 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -2491,7 +2491,20 @@ export function getSettings( state ) { } /** - * Returns the behaviors. + * Returns the behaviors registered with the editor. + * + * Behaviors are named, reusable pieces of functionality that can be + * attached to blocks. They are registered with the editor using the + * `theme.json` file. + * + * @example + * + * ```js + * const behaviors = select( blockEditorStore ).getBehaviors(); + * if ( behaviors?.lightbox ) { + * // Do something with the lightbox. + * } + *``` * * @param {Object} state Editor state. * From c65bc57f5a9ed1ddd47330e94bc115f236ed1742 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 18 May 2023 19:27:55 -0500 Subject: [PATCH 41/61] Rename the test themes to make more sense --- test/e2e/specs/editor/various/behaviors.spec.js | 2 +- test/gutenberg-test-themes/behaviors-false/style.css | 4 ++-- .../{behaviors-disabled => behaviors-ui-disabled}/index.php | 0 .../{behaviors-disabled => behaviors-ui-disabled}/style.css | 4 ++-- .../{behaviors-disabled => behaviors-ui-disabled}/theme.json | 0 5 files changed, 5 insertions(+), 5 deletions(-) rename test/gutenberg-test-themes/{behaviors-disabled => behaviors-ui-disabled}/index.php (100%) rename test/gutenberg-test-themes/{behaviors-disabled => behaviors-ui-disabled}/style.css (92%) rename test/gutenberg-test-themes/{behaviors-disabled => behaviors-ui-disabled}/theme.json (100%) diff --git a/test/e2e/specs/editor/various/behaviors.spec.js b/test/e2e/specs/editor/various/behaviors.spec.js index c1babb6b846b72..ab45012a6ebbfb 100644 --- a/test/e2e/specs/editor/various/behaviors.spec.js +++ b/test/e2e/specs/editor/various/behaviors.spec.js @@ -58,7 +58,7 @@ test.describe( 'Testing behaviors functionality', () => { // { "lightbox": true } is the default behavior, so we activate the // `behaviors` theme where it is disabled by default. Change if we change // the default value in the core theme.json file. - await requestUtils.activateTheme( 'behaviors-disabled' ); + await requestUtils.activateTheme( 'behaviors-ui-disabled' ); const media = await createMedia( { admin, requestUtils } ); await editor.insertBlock( { diff --git a/test/gutenberg-test-themes/behaviors-false/style.css b/test/gutenberg-test-themes/behaviors-false/style.css index 972dbacbb5cabd..9d093e7cd73bcc 100644 --- a/test/gutenberg-test-themes/behaviors-false/style.css +++ b/test/gutenberg-test-themes/behaviors-false/style.css @@ -1,5 +1,5 @@ /* -Theme Name: Behaviors +Theme Name: Behaviors (False) Theme URI: https://github.com/wordpress/theme-experiments/ Author: the WordPress team Description: Behaviors test theme. @@ -12,4 +12,4 @@ License URI: http://www.gnu.org/licenses/gpl-2.0.html Text Domain: behaviors Behaviors WordPress Theme, (C) 2023 WordPress.org Behaviors is distributed under the terms of the GNU GPL. -*/ \ No newline at end of file +*/ diff --git a/test/gutenberg-test-themes/behaviors-disabled/index.php b/test/gutenberg-test-themes/behaviors-ui-disabled/index.php similarity index 100% rename from test/gutenberg-test-themes/behaviors-disabled/index.php rename to test/gutenberg-test-themes/behaviors-ui-disabled/index.php diff --git a/test/gutenberg-test-themes/behaviors-disabled/style.css b/test/gutenberg-test-themes/behaviors-ui-disabled/style.css similarity index 92% rename from test/gutenberg-test-themes/behaviors-disabled/style.css rename to test/gutenberg-test-themes/behaviors-ui-disabled/style.css index 972dbacbb5cabd..2cf6387b490e91 100644 --- a/test/gutenberg-test-themes/behaviors-disabled/style.css +++ b/test/gutenberg-test-themes/behaviors-ui-disabled/style.css @@ -1,5 +1,5 @@ /* -Theme Name: Behaviors +Theme Name: Behaviors UI Disabled Theme URI: https://github.com/wordpress/theme-experiments/ Author: the WordPress team Description: Behaviors test theme. @@ -12,4 +12,4 @@ License URI: http://www.gnu.org/licenses/gpl-2.0.html Text Domain: behaviors Behaviors WordPress Theme, (C) 2023 WordPress.org Behaviors is distributed under the terms of the GNU GPL. -*/ \ No newline at end of file +*/ diff --git a/test/gutenberg-test-themes/behaviors-disabled/theme.json b/test/gutenberg-test-themes/behaviors-ui-disabled/theme.json similarity index 100% rename from test/gutenberg-test-themes/behaviors-disabled/theme.json rename to test/gutenberg-test-themes/behaviors-ui-disabled/theme.json From 1c655f7975f7d48ac4dbc9618e23d479cb432134 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Mon, 22 May 2023 14:51:13 -0500 Subject: [PATCH 42/61] Remove definition of `behaviors` attribute in core/image --- docs/reference-guides/core-blocks.md | 2 +- packages/block-library/src/image/block.json | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index df852bf97b6664..423676e2afe320 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -321,7 +321,7 @@ Insert an image to make a visual statement. ([Source](https://github.com/WordPre - **Name:** core/image - **Category:** media - **Supports:** anchor, color (~~background~~, ~~text~~), filter (duotone) -- **Attributes:** align, alt, behaviors, caption, height, href, id, linkClass, linkDestination, linkTarget, rel, sizeSlug, title, url, width +- **Attributes:** align, alt, caption, height, href, id, linkClass, linkDestination, linkTarget, rel, sizeSlug, title, url, width ## Latest Comments diff --git a/packages/block-library/src/image/block.json b/packages/block-library/src/image/block.json index 791e09f73c8009..92931455c1144c 100644 --- a/packages/block-library/src/image/block.json +++ b/packages/block-library/src/image/block.json @@ -80,9 +80,6 @@ "source": "attribute", "selector": "figure > a", "attribute": "target" - }, - "behaviors": { - "type": "object" } }, "supports": { From 697c04fde2a1710abce8a72cf9bb3fd99d0849f6 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Mon, 22 May 2023 16:49:31 -0500 Subject: [PATCH 43/61] Change default value for `behaviors.lightbox` to false and update e2e tests accordingly --- lib/theme.json | 2 +- .../specs/editor/various/behaviors.spec.js | 23 +++++++++++++------ .../index.php | 0 .../style.css | 3 ++- .../theme.json | 2 +- 5 files changed, 20 insertions(+), 10 deletions(-) rename test/gutenberg-test-themes/{behaviors-false => behaviors-enabled}/index.php (100%) rename test/gutenberg-test-themes/{behaviors-false => behaviors-enabled}/style.css (93%) rename test/gutenberg-test-themes/{behaviors-false => behaviors-enabled}/theme.json (65%) diff --git a/lib/theme.json b/lib/theme.json index 2c42f4cef04b3b..1672382f6d4d96 100644 --- a/lib/theme.json +++ b/lib/theme.json @@ -1,7 +1,7 @@ { "version": 2, "behaviors": { - "lightbox": true + "lightbox": false }, "settings": { "appearanceTools": false, diff --git a/test/e2e/specs/editor/various/behaviors.spec.js b/test/e2e/specs/editor/various/behaviors.spec.js index ab45012a6ebbfb..566acd3d69548d 100644 --- a/test/e2e/specs/editor/various/behaviors.spec.js +++ b/test/e2e/specs/editor/various/behaviors.spec.js @@ -27,7 +27,7 @@ test.describe( 'Testing behaviors functionality', () => { await requestUtils.deleteAllMedia(); } ); - test( 'Lightbox behavior should be selected by default as defined in the core theme.json', async ( { + test( 'Lightbox behavior should be false by default as defined in the core theme.json', async ( { admin, editor, requestUtils, @@ -91,14 +91,17 @@ test.describe( 'Testing behaviors functionality', () => { alt: filename, id: media.id, url: media.source_url, - // Explicitly set the value for behaviors to false. - behaviors: { lightbox: false }, + // Explicitly set the value for behaviors to true. + behaviors: { lightbox: true }, }, } ); await page.getByRole( 'button', { name: 'Advanced' } ).click(); await expect( page.getByLabel( 'Behavior' ) ).toHaveCount( 1 ); await expect( page.getByLabel( 'Behavior' ) ).toHaveValue( '' ); + + // Here we should also check that the block renders on the frontend with the + // lightbox even though the theme.json has it set to false. } ); test( 'You can set the default value for the behaviors in the theme.json', async ( { @@ -107,8 +110,8 @@ test.describe( 'Testing behaviors functionality', () => { requestUtils, page, } ) => { - // In this theme, the default value for settings.behaviors.lightbox is `false`. - await requestUtils.activateTheme( 'behaviors-false' ); + // In this theme, the default value for settings.behaviors.lightbox is `true`. + await requestUtils.activateTheme( 'behaviors-enabled' ); const media = await createMedia( { admin, requestUtils } ); await editor.insertBlock( { @@ -121,10 +124,16 @@ test.describe( 'Testing behaviors functionality', () => { } ); await page.getByRole( 'button', { name: 'Advanced' } ).click(); + + // The behaviors dropdown should be present and the value should be set to + // `lightbox`. await expect( page.getByLabel( 'Behavior' ) ).toHaveCount( 1 ); + await expect( page.getByLabel( 'Behavior' ) ).toHaveValue( 'lightbox' ); - // The default value for behaviors in `theme.json` is `false`, so the - // dropdown should be present, but the value should be `""` (None). + // Check that we can change the value of the behaviors dropdown to `No behavior`. + await page + .getByLabel( 'Behavior' ) + .selectOption( { label: 'No behaviors' } ); await expect( page.getByLabel( 'Behavior' ) ).toHaveValue( '' ); } ); } ); diff --git a/test/gutenberg-test-themes/behaviors-false/index.php b/test/gutenberg-test-themes/behaviors-enabled/index.php similarity index 100% rename from test/gutenberg-test-themes/behaviors-false/index.php rename to test/gutenberg-test-themes/behaviors-enabled/index.php diff --git a/test/gutenberg-test-themes/behaviors-false/style.css b/test/gutenberg-test-themes/behaviors-enabled/style.css similarity index 93% rename from test/gutenberg-test-themes/behaviors-false/style.css rename to test/gutenberg-test-themes/behaviors-enabled/style.css index 9d093e7cd73bcc..c8ae349f915bef 100644 --- a/test/gutenberg-test-themes/behaviors-false/style.css +++ b/test/gutenberg-test-themes/behaviors-enabled/style.css @@ -1,5 +1,6 @@ /* -Theme Name: Behaviors (False) +Theme Name: Behaviors Enabled + Theme URI: https://github.com/wordpress/theme-experiments/ Author: the WordPress team Description: Behaviors test theme. diff --git a/test/gutenberg-test-themes/behaviors-false/theme.json b/test/gutenberg-test-themes/behaviors-enabled/theme.json similarity index 65% rename from test/gutenberg-test-themes/behaviors-false/theme.json rename to test/gutenberg-test-themes/behaviors-enabled/theme.json index b448f0972deb72..436c60e1dc441b 100644 --- a/test/gutenberg-test-themes/behaviors-false/theme.json +++ b/test/gutenberg-test-themes/behaviors-enabled/theme.json @@ -1,6 +1,6 @@ { "version": 2, "behaviors": { - "lightbox": false + "lightbox": true } } From d4fd9d9b8efb87784259071e93d605df1ddd627b Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Mon, 22 May 2023 19:02:22 -0500 Subject: [PATCH 44/61] Change the way we get the data from `theme.json` and adjust e2e --- docs/reference-guides/core-blocks.md | 2 +- lib/compat/wordpress-6.3/behaviors.php | 2 +- packages/block-editor/src/hooks/behaviors.js | 9 ++-- packages/block-library/src/image/block.json | 3 ++ .../specs/editor/various/behaviors.spec.js | 48 ++++++++++++++----- 5 files changed, 45 insertions(+), 19 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 423676e2afe320..df852bf97b6664 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -321,7 +321,7 @@ Insert an image to make a visual statement. ([Source](https://github.com/WordPre - **Name:** core/image - **Category:** media - **Supports:** anchor, color (~~background~~, ~~text~~), filter (duotone) -- **Attributes:** align, alt, caption, height, href, id, linkClass, linkDestination, linkTarget, rel, sizeSlug, title, url, width +- **Attributes:** align, alt, behaviors, caption, height, href, id, linkClass, linkDestination, linkTarget, rel, sizeSlug, title, url, width ## Latest Comments diff --git a/lib/compat/wordpress-6.3/behaviors.php b/lib/compat/wordpress-6.3/behaviors.php index c2c51e9a92a778..ba92a4d9bcb764 100644 --- a/lib/compat/wordpress-6.3/behaviors.php +++ b/lib/compat/wordpress-6.3/behaviors.php @@ -8,7 +8,7 @@ add_filter( 'block_editor_settings_all', function( $settings ) { - $theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_data(); + $theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()->get_data(); if ( array_key_exists( 'behaviors', $theme_data ) ) { $settings['behaviors'] = $theme_data['behaviors']; } diff --git a/packages/block-editor/src/hooks/behaviors.js b/packages/block-editor/src/hooks/behaviors.js index 457347461100b6..b38e11f8d863ee 100644 --- a/packages/block-editor/src/hooks/behaviors.js +++ b/packages/block-editor/src/hooks/behaviors.js @@ -58,10 +58,11 @@ export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => { label={ __( 'Behaviors' ) } // At the moment we are only supporting one behavior (lightbox) value={ behaviors?.lightbox ? 'LIGHTBOX' : '' } - options={ Object.keys( behaviors ) - .map( ( behavior ) => ( { - value: behavior, - label: behavior.toUpperCase(), + options={ Object.entries( behaviors ) + .filter( ( [ , behaviorValue ] ) => behaviorValue ) // Filter out falsey values + .map( ( [ behaviorName ] ) => ( { + value: behaviorName, + label: behaviorName.toUpperCase(), } ) ) .concat( { value: '', diff --git a/packages/block-library/src/image/block.json b/packages/block-library/src/image/block.json index 92931455c1144c..791e09f73c8009 100644 --- a/packages/block-library/src/image/block.json +++ b/packages/block-library/src/image/block.json @@ -80,6 +80,9 @@ "source": "attribute", "selector": "figure > a", "attribute": "target" + }, + "behaviors": { + "type": "object" } }, "supports": { diff --git a/test/e2e/specs/editor/various/behaviors.spec.js b/test/e2e/specs/editor/various/behaviors.spec.js index 566acd3d69548d..030a31b1b3f669 100644 --- a/test/e2e/specs/editor/various/behaviors.spec.js +++ b/test/e2e/specs/editor/various/behaviors.spec.js @@ -27,7 +27,7 @@ test.describe( 'Testing behaviors functionality', () => { await requestUtils.deleteAllMedia(); } ); - test( 'Lightbox behavior should be false by default as defined in the core theme.json', async ( { + test( 'By default, thel Lightbox behavior should be false as defined in the core theme.json', async ( { admin, editor, requestUtils, @@ -45,8 +45,15 @@ test.describe( 'Testing behaviors functionality', () => { } ); await page.getByRole( 'button', { name: 'Advanced' } ).click(); - await expect( page.getByLabel( 'Behavior' ) ).toHaveCount( 1 ); - await expect( page.getByLabel( 'Behavior' ) ).toHaveValue( '' ); + const select = page.getByLabel( 'Behavior' ); + + // By default, no behaviors should be available. + await expect( select ).toHaveCount( 1 ); + await expect( select ).toHaveValue( '' ); + + // By default, you cannot select any behaviors. + const options = select.locator( 'option' ); + await expect( options ).toHaveCount( 1 ); } ); test( 'Behaviors UI can be disabled in the `theme.json`', async ( { @@ -97,8 +104,20 @@ test.describe( 'Testing behaviors functionality', () => { } ); await page.getByRole( 'button', { name: 'Advanced' } ).click(); - await expect( page.getByLabel( 'Behavior' ) ).toHaveCount( 1 ); - await expect( page.getByLabel( 'Behavior' ) ).toHaveValue( '' ); + const select = page.getByLabel( 'Behavior' ); + + // The lightbox should be selected because the value from the block's + // attributes takes precedence over the theme's value. + await expect( select ).toHaveCount( 1 ); + await expect( select ).toHaveValue( 'lightbox' ); + + // There should be 2 options available: `No behaviors` and `Lightbox`. + const options = select.locator( 'option' ); + await expect( options ).toHaveCount( 2 ); + + // We can change the value of the behaviors dropdown to `No behavior`. + await select.selectOption( { label: 'No behaviors' } ); + await expect( select ).toHaveValue( '' ); // Here we should also check that the block renders on the frontend with the // lightbox even though the theme.json has it set to false. @@ -124,16 +143,19 @@ test.describe( 'Testing behaviors functionality', () => { } ); await page.getByRole( 'button', { name: 'Advanced' } ).click(); + const select = page.getByLabel( 'Behavior' ); // The behaviors dropdown should be present and the value should be set to // `lightbox`. - await expect( page.getByLabel( 'Behavior' ) ).toHaveCount( 1 ); - await expect( page.getByLabel( 'Behavior' ) ).toHaveValue( 'lightbox' ); - - // Check that we can change the value of the behaviors dropdown to `No behavior`. - await page - .getByLabel( 'Behavior' ) - .selectOption( { label: 'No behaviors' } ); - await expect( page.getByLabel( 'Behavior' ) ).toHaveValue( '' ); + await expect( select ).toHaveCount( 1 ); + await expect( select ).toHaveValue( 'lightbox' ); + + // There should be 2 options available: `No behaviors` and `Lightbox`. + const options = select.locator( 'option' ); + await expect( options ).toHaveCount( 2 ); + + // We can change the value of the behaviors dropdown to `No behavior`. + await select.selectOption( { label: 'No behaviors' } ); + await expect( select ).toHaveValue( '' ); } ); } ); From a57d634d5a4f32ef15539b10bcc3b09fdbde26c6 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Mon, 22 May 2023 19:39:10 -0500 Subject: [PATCH 45/61] Capitalize behaviors' labels --- packages/block-editor/src/hooks/behaviors.js | 9 ++++++--- test/e2e/specs/editor/various/behaviors.spec.js | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/block-editor/src/hooks/behaviors.js b/packages/block-editor/src/hooks/behaviors.js index b38e11f8d863ee..7a2896348cfa42 100644 --- a/packages/block-editor/src/hooks/behaviors.js +++ b/packages/block-editor/src/hooks/behaviors.js @@ -56,13 +56,16 @@ export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => { behaviorValue ) // Filter out falsey values .map( ( [ behaviorName ] ) => ( { value: behaviorName, - label: behaviorName.toUpperCase(), + label: + // Capitalize the first letter of the behavior name. + behaviorName[ 0 ].toUpperCase() + + behaviorName.slice( 1 ).toLowerCase(), } ) ) .concat( { value: '', diff --git a/test/e2e/specs/editor/various/behaviors.spec.js b/test/e2e/specs/editor/various/behaviors.spec.js index 030a31b1b3f669..25290954bc040f 100644 --- a/test/e2e/specs/editor/various/behaviors.spec.js +++ b/test/e2e/specs/editor/various/behaviors.spec.js @@ -115,7 +115,7 @@ test.describe( 'Testing behaviors functionality', () => { const options = select.locator( 'option' ); await expect( options ).toHaveCount( 2 ); - // We can change the value of the behaviors dropdown to `No behavior`. + // We can change the value of the behaviors dropdown to `No behaviors`. await select.selectOption( { label: 'No behaviors' } ); await expect( select ).toHaveValue( '' ); @@ -154,7 +154,7 @@ test.describe( 'Testing behaviors functionality', () => { const options = select.locator( 'option' ); await expect( options ).toHaveCount( 2 ); - // We can change the value of the behaviors dropdown to `No behavior`. + // We can change the value of the behaviors dropdown to `No behaviors`. await select.selectOption( { label: 'No behaviors' } ); await expect( select ).toHaveValue( '' ); } ); From 234a8b666e8867382db402fdb419e5df5cde1a6b Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Mon, 22 May 2023 20:18:18 -0500 Subject: [PATCH 46/61] Move PHP code adding `theme.json` behaviors to `block-editor-settings` --- lib/block-editor-settings.php | 8 +++++++- lib/compat/wordpress-6.3/behaviors.php | 19 ------------------- 2 files changed, 7 insertions(+), 20 deletions(-) delete mode 100644 lib/compat/wordpress-6.3/behaviors.php diff --git a/lib/block-editor-settings.php b/lib/block-editor-settings.php index 53668e114e04cb..eff4c80d67359c 100644 --- a/lib/block-editor-settings.php +++ b/lib/block-editor-settings.php @@ -6,7 +6,7 @@ */ /** - * Replaces core 'styles' and '__experimentalFeatures' block editor settings from + * Replaces core 'styles', '__experimentalFeatures' and 'behaviors' block editor settings from * wordpress-develop/block-editor.php with the Gutenberg versions. Much of the * code is copied from get_block_editor_settings() in that file. * @@ -146,6 +146,12 @@ function gutenberg_get_block_editor_settings( $settings ) { ); } + // Add theme.json behaviors. + $theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()->get_data(); + if ( array_key_exists( 'behaviors', $theme_data ) ) { + $settings['behaviors'] = $theme_data['behaviors']; + } + return $settings; } add_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings', 0 ); diff --git a/lib/compat/wordpress-6.3/behaviors.php b/lib/compat/wordpress-6.3/behaviors.php deleted file mode 100644 index ba92a4d9bcb764..00000000000000 --- a/lib/compat/wordpress-6.3/behaviors.php +++ /dev/null @@ -1,19 +0,0 @@ -get_data(); - if ( array_key_exists( 'behaviors', $theme_data ) ) { - $settings['behaviors'] = $theme_data['behaviors']; - } - // TODO: Make sure to also get the value from the core theme.json file. - return $settings; - }, - PHP_INT_MAX -); From 4bef633dd52856247e7b05229c62191dc85a09f9 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Mon, 22 May 2023 20:19:31 -0500 Subject: [PATCH 47/61] Update comment --- lib/block-editor-settings.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/block-editor-settings.php b/lib/block-editor-settings.php index eff4c80d67359c..4456f2fddd7682 100644 --- a/lib/block-editor-settings.php +++ b/lib/block-editor-settings.php @@ -146,7 +146,8 @@ function gutenberg_get_block_editor_settings( $settings ) { ); } - // Add theme.json behaviors. + // Add the behaviors from the theme.json to the block editor settings. + // Behaviors are specific, named pieces of interactivity that can be applied to blocks. $theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()->get_data(); if ( array_key_exists( 'behaviors', $theme_data ) ) { $settings['behaviors'] = $theme_data['behaviors']; From 7aa72317e03155e1cd8ca75e62f7e592eec2f843 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Mon, 22 May 2023 20:22:35 -0500 Subject: [PATCH 48/61] Remove the behaviors require from load.php --- lib/load.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/load.php b/lib/load.php index c99e0ec9e98b79..8edd2dd321e60e 100644 --- a/lib/load.php +++ b/lib/load.php @@ -93,7 +93,6 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-6.3/html-api/class-gutenberg-html-tag-processor-6-3.php'; require __DIR__ . '/compat/wordpress-6.3/script-loader.php'; require __DIR__ . '/compat/wordpress-6.3/blocks.php'; -require __DIR__ . '/compat/wordpress-6.3/behaviors.php'; // Experimental features. remove_action( 'plugins_loaded', '_wp_theme_json_webfonts_handler' ); // Turns off WP 6.0's stopgap handler for Webfonts API. From 1d923ab2a8a919fa9010e397f05666f5a23ca99b Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Mon, 22 May 2023 20:41:37 -0500 Subject: [PATCH 49/61] Revert "Update comment" This reverts commit fc812bc6c04e8b3c5c7fe3450c1e39d1dd6f6f3b. --- lib/block-editor-settings.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/block-editor-settings.php b/lib/block-editor-settings.php index 4456f2fddd7682..eff4c80d67359c 100644 --- a/lib/block-editor-settings.php +++ b/lib/block-editor-settings.php @@ -146,8 +146,7 @@ function gutenberg_get_block_editor_settings( $settings ) { ); } - // Add the behaviors from the theme.json to the block editor settings. - // Behaviors are specific, named pieces of interactivity that can be applied to blocks. + // Add theme.json behaviors. $theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()->get_data(); if ( array_key_exists( 'behaviors', $theme_data ) ) { $settings['behaviors'] = $theme_data['behaviors']; From aee908a3a5e2778a96767e3879173b26b6b5be23 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Mon, 22 May 2023 20:41:44 -0500 Subject: [PATCH 50/61] Revert "Move PHP code adding `theme.json` behaviors to `block-editor-settings`" This reverts commit e8d16c8bb4977d1e27b758a067066a78fc21f794. --- lib/block-editor-settings.php | 6 ------ lib/compat/wordpress-6.3/behaviors.php | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 lib/compat/wordpress-6.3/behaviors.php diff --git a/lib/block-editor-settings.php b/lib/block-editor-settings.php index eff4c80d67359c..fe8d78ccc29130 100644 --- a/lib/block-editor-settings.php +++ b/lib/block-editor-settings.php @@ -146,12 +146,6 @@ function gutenberg_get_block_editor_settings( $settings ) { ); } - // Add theme.json behaviors. - $theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()->get_data(); - if ( array_key_exists( 'behaviors', $theme_data ) ) { - $settings['behaviors'] = $theme_data['behaviors']; - } - return $settings; } add_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings', 0 ); diff --git a/lib/compat/wordpress-6.3/behaviors.php b/lib/compat/wordpress-6.3/behaviors.php new file mode 100644 index 00000000000000..ba92a4d9bcb764 --- /dev/null +++ b/lib/compat/wordpress-6.3/behaviors.php @@ -0,0 +1,19 @@ +get_data(); + if ( array_key_exists( 'behaviors', $theme_data ) ) { + $settings['behaviors'] = $theme_data['behaviors']; + } + // TODO: Make sure to also get the value from the core theme.json file. + return $settings; + }, + PHP_INT_MAX +); From 33cb62d65483f80433fdc962407b99d61a1c439c Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Tue, 23 May 2023 11:33:20 -0500 Subject: [PATCH 51/61] Remove the comment that was added previously --- lib/block-editor-settings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/block-editor-settings.php b/lib/block-editor-settings.php index fe8d78ccc29130..53668e114e04cb 100644 --- a/lib/block-editor-settings.php +++ b/lib/block-editor-settings.php @@ -6,7 +6,7 @@ */ /** - * Replaces core 'styles', '__experimentalFeatures' and 'behaviors' block editor settings from + * Replaces core 'styles' and '__experimentalFeatures' block editor settings from * wordpress-develop/block-editor.php with the Gutenberg versions. Much of the * code is copied from get_block_editor_settings() in that file. * From 55e37a3ae85b0b5e4e3d0ca199cb9a4ba0ead0ff Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Tue, 23 May 2023 11:33:42 -0500 Subject: [PATCH 52/61] Update comments in `behaviors.php` --- lib/compat/wordpress-6.3/behaviors.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.3/behaviors.php b/lib/compat/wordpress-6.3/behaviors.php index ba92a4d9bcb764..62e7be7a252d49 100644 --- a/lib/compat/wordpress-6.3/behaviors.php +++ b/lib/compat/wordpress-6.3/behaviors.php @@ -2,6 +2,8 @@ /** * Behaviors. * + * Updates the block editor settings with the theme's behaviors. + * * @package gutenberg */ @@ -12,7 +14,6 @@ function( $settings ) { if ( array_key_exists( 'behaviors', $theme_data ) ) { $settings['behaviors'] = $theme_data['behaviors']; } - // TODO: Make sure to also get the value from the core theme.json file. return $settings; }, PHP_INT_MAX From d4d657ba98d7b9c676522879a67f2c07527fdcf0 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Tue, 23 May 2023 12:18:09 -0500 Subject: [PATCH 53/61] Add back the require_once in load.php --- lib/load.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/load.php b/lib/load.php index 8edd2dd321e60e..f66fe7f474b961 100644 --- a/lib/load.php +++ b/lib/load.php @@ -51,6 +51,7 @@ function gutenberg_is_experiment_enabled( $name ) { require_once __DIR__ . '/compat/wordpress-6.3/theme-previews.php'; require_once __DIR__ . '/compat/wordpress-6.3/navigation-block-preloading.php'; require_once __DIR__ . '/compat/wordpress-6.3/link-template.php'; + require_once __DIR__ . '/compat/wordpress-6.3/behaviors.php'; // Experimental. if ( ! class_exists( 'WP_Rest_Customizer_Nonces' ) ) { From 8d6b0d0c61f0787bde345644fbac6e58480f5832 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Tue, 23 May 2023 15:18:35 -0500 Subject: [PATCH 54/61] Use `settings.blocks.core/image.behaviors.lightbox` --- lib/class-wp-theme-json-gutenberg.php | 2 +- lib/theme.json | 4 +++- packages/block-editor/src/hooks/behaviors.js | 16 +++++++++------- test/e2e/specs/editor/various/behaviors.spec.js | 12 ++++++------ .../behaviors-ui-disabled/theme.json | 4 +++- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index afd4825b627bb5..c27572e735ee1e 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -405,7 +405,7 @@ class WP_Theme_JSON_Gutenberg { 'textDecoration' => null, 'textTransform' => null, ), - 'behaviorsUIEnabled' => null, + 'behaviors' => null, ); /** diff --git a/lib/theme.json b/lib/theme.json index 1672382f6d4d96..6b3e63d20dd026 100644 --- a/lib/theme.json +++ b/lib/theme.json @@ -455,7 +455,9 @@ } }, "core/image": { - "behaviorsUIEnabled": true + "behaviors": { + "lightbox": true + } } } }, diff --git a/packages/block-editor/src/hooks/behaviors.js b/packages/block-editor/src/hooks/behaviors.js index 7a2896348cfa42..c072eb8383411a 100644 --- a/packages/block-editor/src/hooks/behaviors.js +++ b/packages/block-editor/src/hooks/behaviors.js @@ -30,12 +30,14 @@ export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => { return ; } - // Check the value of `settings.blocks.core/image.behaviorsUIEnabled` in the - // theme.json. If false, do not add the behaviors inspector control. - const settings = select( blockEditorStore ).getSettings(); + const settings = + select( blockEditorStore ).getSettings()?.__experimentalFeatures + ?.blocks?.[ props.name ]?.behaviors; + if ( - ! settings?.__experimentalFeatures?.blocks?.[ props.name ] - ?.behaviorsUIEnabled + ! settings || + // If every behavior is disabled, do not show the behaviors inspector control. + Object.entries( settings ).every( ( [ , value ] ) => ! value ) ) { return ; } @@ -58,8 +60,8 @@ export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => { label={ __( 'Behaviors' ) } // At the moment we are only supporting one behavior (Lightbox) value={ behaviors?.lightbox ? 'lightbox' : '' } - options={ Object.entries( behaviors ) - .filter( ( [ , behaviorValue ] ) => behaviorValue ) // Filter out falsey values + options={ Object.entries( settings ) + .filter( ( [ , behaviorValue ] ) => behaviorValue ) // Filter out behaviors that are disabled. .map( ( [ behaviorName ] ) => ( { value: behaviorName, label: diff --git a/test/e2e/specs/editor/various/behaviors.spec.js b/test/e2e/specs/editor/various/behaviors.spec.js index 25290954bc040f..b5d80f8703ab90 100644 --- a/test/e2e/specs/editor/various/behaviors.spec.js +++ b/test/e2e/specs/editor/various/behaviors.spec.js @@ -27,7 +27,7 @@ test.describe( 'Testing behaviors functionality', () => { await requestUtils.deleteAllMedia(); } ); - test( 'By default, thel Lightbox behavior should be false as defined in the core theme.json', async ( { + test( '`No Behaviors` should be the default as defined in the core theme.json', async ( { admin, editor, requestUtils, @@ -47,13 +47,13 @@ test.describe( 'Testing behaviors functionality', () => { await page.getByRole( 'button', { name: 'Advanced' } ).click(); const select = page.getByLabel( 'Behavior' ); - // By default, no behaviors should be available. + // By default, no behaviors should be selected. await expect( select ).toHaveCount( 1 ); await expect( select ).toHaveValue( '' ); - // By default, you cannot select any behaviors. + // By default, you should be able to select the Lightbox behavior. const options = select.locator( 'option' ); - await expect( options ).toHaveCount( 1 ); + await expect( options ).toHaveCount( 2 ); } ); test( 'Behaviors UI can be disabled in the `theme.json`', async ( { @@ -62,8 +62,8 @@ test.describe( 'Testing behaviors functionality', () => { requestUtils, page, } ) => { - // { "lightbox": true } is the default behavior, so we activate the - // `behaviors` theme where it is disabled by default. Change if we change + // { "lightbox": true } is the default behavior setting, so we activate the + // `behaviors-ui-disabled` theme where it is disabled by default. Change if we change // the default value in the core theme.json file. await requestUtils.activateTheme( 'behaviors-ui-disabled' ); const media = await createMedia( { admin, requestUtils } ); diff --git a/test/gutenberg-test-themes/behaviors-ui-disabled/theme.json b/test/gutenberg-test-themes/behaviors-ui-disabled/theme.json index c137f7a7223926..a9f920f6dd0abc 100644 --- a/test/gutenberg-test-themes/behaviors-ui-disabled/theme.json +++ b/test/gutenberg-test-themes/behaviors-ui-disabled/theme.json @@ -3,7 +3,9 @@ "settings": { "blocks": { "core/image": { - "behaviorsUIEnabled": false + "behaviors": { + "lightbox": false + } } } } From 76b2a752994b2d3b3173ddb5967e484c8689214a Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Tue, 23 May 2023 18:12:12 -0500 Subject: [PATCH 55/61] Use `behaviors.blocks.core/image.lightbox` --- lib/theme.json | 6 +++++- package-lock.json | 21 ++++++++++++++++--- packages/block-editor/package.json | 1 + packages/block-editor/src/hooks/behaviors.js | 15 ++++++++----- .../specs/editor/various/behaviors.spec.js | 2 +- .../behaviors-enabled/theme.json | 6 +++++- 6 files changed, 40 insertions(+), 11 deletions(-) diff --git a/lib/theme.json b/lib/theme.json index 6b3e63d20dd026..6955d8f2f3016e 100644 --- a/lib/theme.json +++ b/lib/theme.json @@ -1,7 +1,11 @@ { "version": 2, "behaviors": { - "lightbox": false + "blocks": { + "core/image": { + "lightbox": false + } + } }, "settings": { "appearanceTools": false, diff --git a/package-lock.json b/package-lock.json index 5833c451c403be..2f674821f32951 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17195,6 +17195,7 @@ "change-case": "^4.1.2", "classnames": "^2.3.1", "colord": "^2.7.0", + "deepmerge": "^4.3.0", "diff": "^4.0.2", "dom-scroll-into-view": "^1.2.1", "fast-deep-equal": "^3.1.3", @@ -17205,6 +17206,13 @@ "rememo": "^4.0.2", "remove-accents": "^0.4.2", "traverse": "^0.6.6" + }, + "dependencies": { + "deepmerge": { + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.3.1.tgz", + "integrity": "sha512-3sUqbMEc77XqpdNO7FRyRog+eW3ph+GYCbj+rK+uYyRMuwsVy0rMiVtPn+QJlKFvWP/1PYpapqYn0Me2knFn+A==" + } } }, "@wordpress/block-library": { @@ -17298,6 +17306,13 @@ "showdown": "^1.9.1", "simple-html-tokenizer": "^0.5.7", "uuid": "^8.3.0" + }, + "dependencies": { + "deepmerge": { + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.3.1.tgz", + "integrity": "sha512-3sUqbMEc77XqpdNO7FRyRog+eW3ph+GYCbj+rK+uYyRMuwsVy0rMiVtPn+QJlKFvWP/1PYpapqYn0Me2knFn+A==" + } } }, "@wordpress/browserslist-config": { @@ -29227,7 +29242,7 @@ "co": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", - "integrity": "sha1-bqa989hTrlTMuOR7+gvz+QMfsYQ=", + "integrity": "sha512-QVb0dM5HvG+uaxitm8wONl7jltx8dqhfU33DcqtOZcLSVIKSDDLDi7+0LbAKiyI8hD9u42m2YxXSkMGWThaecQ==", "dev": true }, "code-point-at": { @@ -30804,7 +30819,7 @@ "css.escape": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/css.escape/-/css.escape-1.5.1.tgz", - "integrity": "sha512-YUifsXXuknHlUsmlgyY0PKzgPOr7/FjCePfHNt0jxm83wHZi44VDMQ7/fGNkjY3/jV1MC+1CmZbaHzugyeRtpg==", + "integrity": "sha1-QuJ9T6BK4y+TGktNQZH6nN3ul8s=", "dev": true }, "cssesc": { @@ -41365,7 +41380,7 @@ "lz-string": { "version": "1.4.4", "resolved": "https://registry.npmjs.org/lz-string/-/lz-string-1.4.4.tgz", - "integrity": "sha512-0ckx7ZHRPqb0oUm8zNr+90mtf9DQB60H1wMCjBtfi62Kl3a7JbHob6gA2bC+xRvZoOL+1hzUK8jeuEIQE8svEQ==", + "integrity": "sha1-wNjq82BZ9wV5bh40SBHPTEmNOiY=", "dev": true }, "macos-release": { diff --git a/packages/block-editor/package.json b/packages/block-editor/package.json index efd980cba06c66..9e934a68662c2e 100644 --- a/packages/block-editor/package.json +++ b/packages/block-editor/package.json @@ -65,6 +65,7 @@ "change-case": "^4.1.2", "classnames": "^2.3.1", "colord": "^2.7.0", + "deepmerge": "^4.3.0", "diff": "^4.0.2", "dom-scroll-into-view": "^1.2.1", "fast-deep-equal": "^3.1.3", diff --git a/packages/block-editor/src/hooks/behaviors.js b/packages/block-editor/src/hooks/behaviors.js index c072eb8383411a..e47e47b7b610ae 100644 --- a/packages/block-editor/src/hooks/behaviors.js +++ b/packages/block-editor/src/hooks/behaviors.js @@ -13,6 +13,11 @@ import { select } from '@wordpress/data'; import { InspectorControls } from '../components'; import { store as blockEditorStore } from '../store'; +/** + * External dependencies + */ +import merge from 'deepmerge'; + /** * Override the default edit UI to include a new block inspector control for * assigning behaviors to blocks if behaviors are enabled in the theme.json. @@ -44,12 +49,12 @@ export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => { const { behaviors: blockBehaviors } = props.attributes; - // Get the theme behaviors from the theme.json. - const themeBehaviors = select( blockEditorStore ).getBehaviors(); + // Get the theme behaviors for the block from the theme.json. + const themeBehaviors = + select( blockEditorStore ).getBehaviors()?.blocks?.[ props.name ]; - // By default, use the block behaviors. - // If the theme has behaviors, but the block does not, use the theme behaviors. - const behaviors = blockBehaviors || themeBehaviors || {}; + // Block behaviors take precedence over theme behaviors. + const behaviors = merge( themeBehaviors, blockBehaviors || {} ); return ( <> diff --git a/test/e2e/specs/editor/various/behaviors.spec.js b/test/e2e/specs/editor/various/behaviors.spec.js index b5d80f8703ab90..b219ebfb809c13 100644 --- a/test/e2e/specs/editor/various/behaviors.spec.js +++ b/test/e2e/specs/editor/various/behaviors.spec.js @@ -129,7 +129,7 @@ test.describe( 'Testing behaviors functionality', () => { requestUtils, page, } ) => { - // In this theme, the default value for settings.behaviors.lightbox is `true`. + // In this theme, the default value for settings.behaviors.blocks.core/image.lightbox is `true`. await requestUtils.activateTheme( 'behaviors-enabled' ); const media = await createMedia( { admin, requestUtils } ); diff --git a/test/gutenberg-test-themes/behaviors-enabled/theme.json b/test/gutenberg-test-themes/behaviors-enabled/theme.json index 436c60e1dc441b..f49129622d9f6d 100644 --- a/test/gutenberg-test-themes/behaviors-enabled/theme.json +++ b/test/gutenberg-test-themes/behaviors-enabled/theme.json @@ -1,6 +1,10 @@ { "version": 2, "behaviors": { - "lightbox": true + "blocks": { + "core/image": { + "lightbox": true + } + } } } From 70e51cd684d186668eea9ff9c3786f38209ffcd5 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Wed, 24 May 2023 12:44:06 +0200 Subject: [PATCH 56/61] Remove experimental setting for interactivity API --- lib/experiments-page.php | 12 ------------ lib/load.php | 6 ++---- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/lib/experiments-page.php b/lib/experiments-page.php index 9e31815f3f50ff..725aa1efcf0e28 100644 --- a/lib/experiments-page.php +++ b/lib/experiments-page.php @@ -101,18 +101,6 @@ function gutenberg_initialize_experiments_settings() { ) ); - add_settings_field( - 'gutenberg-interactivity-api-core-blocks', - __( 'Core blocks', 'gutenberg' ), - 'gutenberg_display_experiment_field', - 'gutenberg-experiments', - 'gutenberg_experiments_section', - array( - 'label' => __( 'Test the core blocks using the Interactivity API', 'gutenberg' ), - 'id' => 'gutenberg-interactivity-api-core-blocks', - ) - ); - add_settings_field( 'gutenberg-pattern-enhancements', __( 'Pattern enhancements', 'gutenberg' ), diff --git a/lib/load.php b/lib/load.php index f66fe7f474b961..1cfb13c54f2146 100644 --- a/lib/load.php +++ b/lib/load.php @@ -104,10 +104,8 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/experimental/kses.php'; require __DIR__ . '/experimental/l10n.php'; require __DIR__ . '/experimental/navigation-fallback.php'; -if ( gutenberg_is_experiment_enabled( 'gutenberg-interactivity-api-core-blocks' ) ) { - require __DIR__ . '/experimental/interactivity-api/script-loader.php'; - require __DIR__ . '/experimental/interactivity-api/blocks.php'; -} +require __DIR__ . '/experimental/interactivity-api/script-loader.php'; +require __DIR__ . '/experimental/interactivity-api/blocks.php'; // Fonts API. if ( ! class_exists( 'WP_Fonts' ) ) { From 69ebe6c7c0e27a3cc84eb0a8c4fc0a7a5ecedf58 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Wed, 24 May 2023 16:23:03 +0200 Subject: [PATCH 57/61] Revert "Remove experimental setting for interactivity API" This reverts commit 70e51cd684d186668eea9ff9c3786f38209ffcd5. --- lib/experiments-page.php | 12 ++++++++++++ lib/load.php | 6 ++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/experiments-page.php b/lib/experiments-page.php index 725aa1efcf0e28..9e31815f3f50ff 100644 --- a/lib/experiments-page.php +++ b/lib/experiments-page.php @@ -101,6 +101,18 @@ function gutenberg_initialize_experiments_settings() { ) ); + add_settings_field( + 'gutenberg-interactivity-api-core-blocks', + __( 'Core blocks', 'gutenberg' ), + 'gutenberg_display_experiment_field', + 'gutenberg-experiments', + 'gutenberg_experiments_section', + array( + 'label' => __( 'Test the core blocks using the Interactivity API', 'gutenberg' ), + 'id' => 'gutenberg-interactivity-api-core-blocks', + ) + ); + add_settings_field( 'gutenberg-pattern-enhancements', __( 'Pattern enhancements', 'gutenberg' ), diff --git a/lib/load.php b/lib/load.php index 1cfb13c54f2146..f66fe7f474b961 100644 --- a/lib/load.php +++ b/lib/load.php @@ -104,8 +104,10 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/experimental/kses.php'; require __DIR__ . '/experimental/l10n.php'; require __DIR__ . '/experimental/navigation-fallback.php'; -require __DIR__ . '/experimental/interactivity-api/script-loader.php'; -require __DIR__ . '/experimental/interactivity-api/blocks.php'; +if ( gutenberg_is_experiment_enabled( 'gutenberg-interactivity-api-core-blocks' ) ) { + require __DIR__ . '/experimental/interactivity-api/script-loader.php'; + require __DIR__ . '/experimental/interactivity-api/blocks.php'; +} // Fonts API. if ( ! class_exists( 'WP_Fonts' ) ) { From c32be86e150ccd8213167287b473774c6f80d6bc Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Wed, 24 May 2023 18:38:31 +0200 Subject: [PATCH 58/61] Move `interactivity.js` files to `block.json` --- lib/experimental/interactivity-api/blocks.php | 18 ----- packages/block-library/src/file/block.json | 2 +- packages/block-library/src/file/view.js | 9 --- .../block-library/src/navigation/block.json | 2 +- .../src/navigation/view-modal.js | 78 ------------------- packages/block-library/src/navigation/view.js | 74 ------------------ 6 files changed, 2 insertions(+), 181 deletions(-) delete mode 100644 packages/block-library/src/file/view.js delete mode 100644 packages/block-library/src/navigation/view-modal.js delete mode 100644 packages/block-library/src/navigation/view.js diff --git a/lib/experimental/interactivity-api/blocks.php b/lib/experimental/interactivity-api/blocks.php index 1c6e7d2b1fbb9b..871754ea3f0c17 100644 --- a/lib/experimental/interactivity-api/blocks.php +++ b/lib/experimental/interactivity-api/blocks.php @@ -228,21 +228,3 @@ function gutenberg_block_core_navigation_add_directives_to_submenu( $w ) { }; add_filter( 'render_block_core/navigation', 'gutenberg_block_core_navigation_add_directives_to_markup', 10, 1 ); - -/** - * Replaces view script for the File and Navigation blocks with version using Interactivity API. - * - * @param array $metadata Block metadata as read in via block.json. - * - * @return array Filtered block type metadata. - */ -function gutenberg_block_update_interactive_view_script( $metadata ) { - if ( - in_array( $metadata['name'], array( 'core/file', 'core/navigation' ), true ) && - str_contains( $metadata['file'], 'build/block-library/blocks' ) - ) { - $metadata['viewScript'] = array( 'file:./interactivity.min.js' ); - } - return $metadata; -} -add_filter( 'block_type_metadata', 'gutenberg_block_update_interactive_view_script', 10, 1 ); diff --git a/packages/block-library/src/file/block.json b/packages/block-library/src/file/block.json index 08a78f3a94ce42..6b7da0a5ab1651 100644 --- a/packages/block-library/src/file/block.json +++ b/packages/block-library/src/file/block.json @@ -67,7 +67,7 @@ } } }, - "viewScript": "file:./view.min.js", + "viewScript": "file:./interactivity.min.js", "editorStyle": "wp-block-file-editor", "style": "wp-block-file" } diff --git a/packages/block-library/src/file/view.js b/packages/block-library/src/file/view.js deleted file mode 100644 index 6d0b61fa51cb7c..00000000000000 --- a/packages/block-library/src/file/view.js +++ /dev/null @@ -1,9 +0,0 @@ -/** - * Internal dependencies - */ -import { hidePdfEmbedsOnUnsupportedBrowsers } from './utils'; - -document.addEventListener( - 'DOMContentLoaded', - hidePdfEmbedsOnUnsupportedBrowsers -); diff --git a/packages/block-library/src/navigation/block.json b/packages/block-library/src/navigation/block.json index ce2bed0d8837f6..bc37b98afd5d48 100644 --- a/packages/block-library/src/navigation/block.json +++ b/packages/block-library/src/navigation/block.json @@ -134,7 +134,7 @@ } } }, - "viewScript": [ "file:./view.min.js", "file:./view-modal.min.js" ], + "viewScript": [ "file:./interactivity.min.js" ], "editorStyle": "wp-block-navigation-editor", "style": "wp-block-navigation" } diff --git a/packages/block-library/src/navigation/view-modal.js b/packages/block-library/src/navigation/view-modal.js deleted file mode 100644 index 9477d262816d93..00000000000000 --- a/packages/block-library/src/navigation/view-modal.js +++ /dev/null @@ -1,78 +0,0 @@ -/** - * External dependencies - */ -import MicroModal from 'micromodal'; - -// Responsive navigation toggle. -function navigationToggleModal( modal ) { - const dialogContainer = modal.querySelector( - `.wp-block-navigation__responsive-dialog` - ); - - const isHidden = 'true' === modal.getAttribute( 'aria-hidden' ); - - modal.classList.toggle( 'has-modal-open', ! isHidden ); - dialogContainer.toggleAttribute( 'aria-modal', ! isHidden ); - - if ( isHidden ) { - dialogContainer.removeAttribute( 'role' ); - dialogContainer.removeAttribute( 'aria-modal' ); - } else { - dialogContainer.setAttribute( 'role', 'dialog' ); - dialogContainer.setAttribute( 'aria-modal', 'true' ); - } - - // Add a class to indicate the modal is open. - const htmlElement = document.documentElement; - htmlElement.classList.toggle( 'has-modal-open' ); -} - -function isLinkToAnchorOnCurrentPage( node ) { - return ( - node.hash && - node.protocol === window.location.protocol && - node.host === window.location.host && - node.pathname === window.location.pathname && - node.search === window.location.search - ); -} - -window.addEventListener( 'load', () => { - MicroModal.init( { - onShow: navigationToggleModal, - onClose: navigationToggleModal, - openClass: 'is-menu-open', - } ); - - // Close modal automatically on clicking anchor links inside modal. - const navigationLinks = document.querySelectorAll( - '.wp-block-navigation-item__content' - ); - - navigationLinks.forEach( function ( link ) { - // Ignore non-anchor links and anchor links which open on a new tab. - if ( - ! isLinkToAnchorOnCurrentPage( link ) || - link.attributes?.target === '_blank' - ) { - return; - } - - // Find the specific parent modal for this link - // since .close() won't work without an ID if there are - // multiple navigation menus in a post/page. - const modal = link.closest( - '.wp-block-navigation__responsive-container' - ); - const modalId = modal?.getAttribute( 'id' ); - - link.addEventListener( 'click', () => { - // check if modal exists and is open before trying to close it - // otherwise Micromodal will toggle the `has-modal-open` class - // on the html tag which prevents scrolling - if ( modalId && modal.classList.contains( 'has-modal-open' ) ) { - MicroModal.close( modalId ); - } - } ); - } ); -} ); diff --git a/packages/block-library/src/navigation/view.js b/packages/block-library/src/navigation/view.js deleted file mode 100644 index 19805a44ae4ae2..00000000000000 --- a/packages/block-library/src/navigation/view.js +++ /dev/null @@ -1,74 +0,0 @@ -// Open on click functionality. -function closeSubmenus( element ) { - element - .querySelectorAll( '[aria-expanded="true"]' ) - .forEach( function ( toggle ) { - toggle.setAttribute( 'aria-expanded', 'false' ); - } ); -} - -function toggleSubmenuOnClick( event ) { - const buttonToggle = event.target.closest( '[aria-expanded]' ); - const isSubmenuOpen = buttonToggle.getAttribute( 'aria-expanded' ); - - if ( isSubmenuOpen === 'true' ) { - closeSubmenus( buttonToggle.closest( '.wp-block-navigation-item' ) ); - } else { - // Close all sibling submenus. - const parentElement = buttonToggle.closest( - '.wp-block-navigation-item' - ); - const navigationParent = buttonToggle.closest( - '.wp-block-navigation__submenu-container, .wp-block-navigation__container, .wp-block-page-list' - ); - navigationParent - .querySelectorAll( '.wp-block-navigation-item' ) - .forEach( function ( child ) { - if ( child !== parentElement ) { - closeSubmenus( child ); - } - } ); - // Open submenu. - buttonToggle.setAttribute( 'aria-expanded', 'true' ); - } -} - -// Necessary for some themes such as TT1 Blocks, where -// scripts could be loaded before the body. -window.addEventListener( 'load', () => { - const submenuButtons = document.querySelectorAll( - '.wp-block-navigation-submenu__toggle' - ); - - submenuButtons.forEach( function ( button ) { - button.addEventListener( 'click', toggleSubmenuOnClick ); - } ); - - // Close on click outside. - document.addEventListener( 'click', function ( event ) { - const navigationBlocks = document.querySelectorAll( - '.wp-block-navigation' - ); - navigationBlocks.forEach( function ( block ) { - if ( ! block.contains( event.target ) ) { - closeSubmenus( block ); - } - } ); - } ); - // Close on focus outside or escape key. - document.addEventListener( 'keyup', function ( event ) { - const submenuBlocks = document.querySelectorAll( - '.wp-block-navigation-item.has-child' - ); - submenuBlocks.forEach( function ( block ) { - if ( ! block.contains( event.target ) ) { - closeSubmenus( block ); - } else if ( event.key === 'Escape' ) { - const toggle = block.querySelector( '[aria-expanded="true"]' ); - closeSubmenus( block ); - // Focus the submenu trigger so focus does not get trapped in the closed submenu. - toggle?.focus(); - } - } ); - } ); -} ); From b8dc7dd6ff6bec3b9a4576dd8341f27fca5b9dfb Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Wed, 24 May 2023 18:39:46 +0200 Subject: [PATCH 59/61] Remove experimental flag for the Interactivity API --- lib/experiments-page.php | 12 ------------ lib/load.php | 6 ++---- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/lib/experiments-page.php b/lib/experiments-page.php index 9e31815f3f50ff..725aa1efcf0e28 100644 --- a/lib/experiments-page.php +++ b/lib/experiments-page.php @@ -101,18 +101,6 @@ function gutenberg_initialize_experiments_settings() { ) ); - add_settings_field( - 'gutenberg-interactivity-api-core-blocks', - __( 'Core blocks', 'gutenberg' ), - 'gutenberg_display_experiment_field', - 'gutenberg-experiments', - 'gutenberg_experiments_section', - array( - 'label' => __( 'Test the core blocks using the Interactivity API', 'gutenberg' ), - 'id' => 'gutenberg-interactivity-api-core-blocks', - ) - ); - add_settings_field( 'gutenberg-pattern-enhancements', __( 'Pattern enhancements', 'gutenberg' ), diff --git a/lib/load.php b/lib/load.php index f66fe7f474b961..1cfb13c54f2146 100644 --- a/lib/load.php +++ b/lib/load.php @@ -104,10 +104,8 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/experimental/kses.php'; require __DIR__ . '/experimental/l10n.php'; require __DIR__ . '/experimental/navigation-fallback.php'; -if ( gutenberg_is_experiment_enabled( 'gutenberg-interactivity-api-core-blocks' ) ) { - require __DIR__ . '/experimental/interactivity-api/script-loader.php'; - require __DIR__ . '/experimental/interactivity-api/blocks.php'; -} +require __DIR__ . '/experimental/interactivity-api/script-loader.php'; +require __DIR__ . '/experimental/interactivity-api/blocks.php'; // Fonts API. if ( ! class_exists( 'WP_Fonts' ) ) { From 4800d91ccf1ca455dfaeb0969f85d716461bc1e4 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Wed, 24 May 2023 13:47:43 -0500 Subject: [PATCH 60/61] Revert "Move `interactivity.js` files to `block.json`" This reverts commit c32be86e150ccd8213167287b473774c6f80d6bc. --- lib/experimental/interactivity-api/blocks.php | 18 +++++ packages/block-library/src/file/block.json | 2 +- packages/block-library/src/file/view.js | 9 +++ .../block-library/src/navigation/block.json | 2 +- .../src/navigation/view-modal.js | 78 +++++++++++++++++++ packages/block-library/src/navigation/view.js | 74 ++++++++++++++++++ 6 files changed, 181 insertions(+), 2 deletions(-) create mode 100644 packages/block-library/src/file/view.js create mode 100644 packages/block-library/src/navigation/view-modal.js create mode 100644 packages/block-library/src/navigation/view.js diff --git a/lib/experimental/interactivity-api/blocks.php b/lib/experimental/interactivity-api/blocks.php index 871754ea3f0c17..1c6e7d2b1fbb9b 100644 --- a/lib/experimental/interactivity-api/blocks.php +++ b/lib/experimental/interactivity-api/blocks.php @@ -228,3 +228,21 @@ function gutenberg_block_core_navigation_add_directives_to_submenu( $w ) { }; add_filter( 'render_block_core/navigation', 'gutenberg_block_core_navigation_add_directives_to_markup', 10, 1 ); + +/** + * Replaces view script for the File and Navigation blocks with version using Interactivity API. + * + * @param array $metadata Block metadata as read in via block.json. + * + * @return array Filtered block type metadata. + */ +function gutenberg_block_update_interactive_view_script( $metadata ) { + if ( + in_array( $metadata['name'], array( 'core/file', 'core/navigation' ), true ) && + str_contains( $metadata['file'], 'build/block-library/blocks' ) + ) { + $metadata['viewScript'] = array( 'file:./interactivity.min.js' ); + } + return $metadata; +} +add_filter( 'block_type_metadata', 'gutenberg_block_update_interactive_view_script', 10, 1 ); diff --git a/packages/block-library/src/file/block.json b/packages/block-library/src/file/block.json index 6b7da0a5ab1651..08a78f3a94ce42 100644 --- a/packages/block-library/src/file/block.json +++ b/packages/block-library/src/file/block.json @@ -67,7 +67,7 @@ } } }, - "viewScript": "file:./interactivity.min.js", + "viewScript": "file:./view.min.js", "editorStyle": "wp-block-file-editor", "style": "wp-block-file" } diff --git a/packages/block-library/src/file/view.js b/packages/block-library/src/file/view.js new file mode 100644 index 00000000000000..6d0b61fa51cb7c --- /dev/null +++ b/packages/block-library/src/file/view.js @@ -0,0 +1,9 @@ +/** + * Internal dependencies + */ +import { hidePdfEmbedsOnUnsupportedBrowsers } from './utils'; + +document.addEventListener( + 'DOMContentLoaded', + hidePdfEmbedsOnUnsupportedBrowsers +); diff --git a/packages/block-library/src/navigation/block.json b/packages/block-library/src/navigation/block.json index bc37b98afd5d48..ce2bed0d8837f6 100644 --- a/packages/block-library/src/navigation/block.json +++ b/packages/block-library/src/navigation/block.json @@ -134,7 +134,7 @@ } } }, - "viewScript": [ "file:./interactivity.min.js" ], + "viewScript": [ "file:./view.min.js", "file:./view-modal.min.js" ], "editorStyle": "wp-block-navigation-editor", "style": "wp-block-navigation" } diff --git a/packages/block-library/src/navigation/view-modal.js b/packages/block-library/src/navigation/view-modal.js new file mode 100644 index 00000000000000..9477d262816d93 --- /dev/null +++ b/packages/block-library/src/navigation/view-modal.js @@ -0,0 +1,78 @@ +/** + * External dependencies + */ +import MicroModal from 'micromodal'; + +// Responsive navigation toggle. +function navigationToggleModal( modal ) { + const dialogContainer = modal.querySelector( + `.wp-block-navigation__responsive-dialog` + ); + + const isHidden = 'true' === modal.getAttribute( 'aria-hidden' ); + + modal.classList.toggle( 'has-modal-open', ! isHidden ); + dialogContainer.toggleAttribute( 'aria-modal', ! isHidden ); + + if ( isHidden ) { + dialogContainer.removeAttribute( 'role' ); + dialogContainer.removeAttribute( 'aria-modal' ); + } else { + dialogContainer.setAttribute( 'role', 'dialog' ); + dialogContainer.setAttribute( 'aria-modal', 'true' ); + } + + // Add a class to indicate the modal is open. + const htmlElement = document.documentElement; + htmlElement.classList.toggle( 'has-modal-open' ); +} + +function isLinkToAnchorOnCurrentPage( node ) { + return ( + node.hash && + node.protocol === window.location.protocol && + node.host === window.location.host && + node.pathname === window.location.pathname && + node.search === window.location.search + ); +} + +window.addEventListener( 'load', () => { + MicroModal.init( { + onShow: navigationToggleModal, + onClose: navigationToggleModal, + openClass: 'is-menu-open', + } ); + + // Close modal automatically on clicking anchor links inside modal. + const navigationLinks = document.querySelectorAll( + '.wp-block-navigation-item__content' + ); + + navigationLinks.forEach( function ( link ) { + // Ignore non-anchor links and anchor links which open on a new tab. + if ( + ! isLinkToAnchorOnCurrentPage( link ) || + link.attributes?.target === '_blank' + ) { + return; + } + + // Find the specific parent modal for this link + // since .close() won't work without an ID if there are + // multiple navigation menus in a post/page. + const modal = link.closest( + '.wp-block-navigation__responsive-container' + ); + const modalId = modal?.getAttribute( 'id' ); + + link.addEventListener( 'click', () => { + // check if modal exists and is open before trying to close it + // otherwise Micromodal will toggle the `has-modal-open` class + // on the html tag which prevents scrolling + if ( modalId && modal.classList.contains( 'has-modal-open' ) ) { + MicroModal.close( modalId ); + } + } ); + } ); +} ); diff --git a/packages/block-library/src/navigation/view.js b/packages/block-library/src/navigation/view.js new file mode 100644 index 00000000000000..19805a44ae4ae2 --- /dev/null +++ b/packages/block-library/src/navigation/view.js @@ -0,0 +1,74 @@ +// Open on click functionality. +function closeSubmenus( element ) { + element + .querySelectorAll( '[aria-expanded="true"]' ) + .forEach( function ( toggle ) { + toggle.setAttribute( 'aria-expanded', 'false' ); + } ); +} + +function toggleSubmenuOnClick( event ) { + const buttonToggle = event.target.closest( '[aria-expanded]' ); + const isSubmenuOpen = buttonToggle.getAttribute( 'aria-expanded' ); + + if ( isSubmenuOpen === 'true' ) { + closeSubmenus( buttonToggle.closest( '.wp-block-navigation-item' ) ); + } else { + // Close all sibling submenus. + const parentElement = buttonToggle.closest( + '.wp-block-navigation-item' + ); + const navigationParent = buttonToggle.closest( + '.wp-block-navigation__submenu-container, .wp-block-navigation__container, .wp-block-page-list' + ); + navigationParent + .querySelectorAll( '.wp-block-navigation-item' ) + .forEach( function ( child ) { + if ( child !== parentElement ) { + closeSubmenus( child ); + } + } ); + // Open submenu. + buttonToggle.setAttribute( 'aria-expanded', 'true' ); + } +} + +// Necessary for some themes such as TT1 Blocks, where +// scripts could be loaded before the body. +window.addEventListener( 'load', () => { + const submenuButtons = document.querySelectorAll( + '.wp-block-navigation-submenu__toggle' + ); + + submenuButtons.forEach( function ( button ) { + button.addEventListener( 'click', toggleSubmenuOnClick ); + } ); + + // Close on click outside. + document.addEventListener( 'click', function ( event ) { + const navigationBlocks = document.querySelectorAll( + '.wp-block-navigation' + ); + navigationBlocks.forEach( function ( block ) { + if ( ! block.contains( event.target ) ) { + closeSubmenus( block ); + } + } ); + } ); + // Close on focus outside or escape key. + document.addEventListener( 'keyup', function ( event ) { + const submenuBlocks = document.querySelectorAll( + '.wp-block-navigation-item.has-child' + ); + submenuBlocks.forEach( function ( block ) { + if ( ! block.contains( event.target ) ) { + closeSubmenus( block ); + } else if ( event.key === 'Escape' ) { + const toggle = block.querySelector( '[aria-expanded="true"]' ); + closeSubmenus( block ); + // Focus the submenu trigger so focus does not get trapped in the closed submenu. + toggle?.focus(); + } + } ); + } ); +} ); From 9124b58e6d591c2ce92386512a78dc727d98cd9d Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Wed, 24 May 2023 13:47:49 -0500 Subject: [PATCH 61/61] Revert "Remove experimental flag for the Interactivity API" This reverts commit b8dc7dd6ff6bec3b9a4576dd8341f27fca5b9dfb. --- lib/experiments-page.php | 12 ++++++++++++ lib/load.php | 6 ++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/experiments-page.php b/lib/experiments-page.php index 725aa1efcf0e28..9e31815f3f50ff 100644 --- a/lib/experiments-page.php +++ b/lib/experiments-page.php @@ -101,6 +101,18 @@ function gutenberg_initialize_experiments_settings() { ) ); + add_settings_field( + 'gutenberg-interactivity-api-core-blocks', + __( 'Core blocks', 'gutenberg' ), + 'gutenberg_display_experiment_field', + 'gutenberg-experiments', + 'gutenberg_experiments_section', + array( + 'label' => __( 'Test the core blocks using the Interactivity API', 'gutenberg' ), + 'id' => 'gutenberg-interactivity-api-core-blocks', + ) + ); + add_settings_field( 'gutenberg-pattern-enhancements', __( 'Pattern enhancements', 'gutenberg' ), diff --git a/lib/load.php b/lib/load.php index 1cfb13c54f2146..f66fe7f474b961 100644 --- a/lib/load.php +++ b/lib/load.php @@ -104,8 +104,10 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/experimental/kses.php'; require __DIR__ . '/experimental/l10n.php'; require __DIR__ . '/experimental/navigation-fallback.php'; -require __DIR__ . '/experimental/interactivity-api/script-loader.php'; -require __DIR__ . '/experimental/interactivity-api/blocks.php'; +if ( gutenberg_is_experiment_enabled( 'gutenberg-interactivity-api-core-blocks' ) ) { + require __DIR__ . '/experimental/interactivity-api/script-loader.php'; + require __DIR__ . '/experimental/interactivity-api/blocks.php'; +} // Fonts API. if ( ! class_exists( 'WP_Fonts' ) ) {