Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
965a51c
feat: add block binding support for icon attribute to core/icon block
yashjawale Jul 27, 2026
8ec6fe6
feat: add backport entry
yashjawale Jul 27, 2026
4967ab1
fix: update backport file name
yashjawale Jul 27, 2026
518722f
Merge branch 'trunk' into feat/icon-block-pattern-overrides-80696
yashjawale Jul 27, 2026
3523df2
Merge branch 'trunk' into feat/icon-block-pattern-overrides-80696
t-hamano Oct 3, 2026
fb8a31c
Merge branch 'trunk' into feat/icon-block-pattern-overrides-80696
yashjawale Oct 6, 2026
f1d7852
Merge branch 'trunk' into feat/icon-block-pattern-overrides-80696
yashjawale Oct 6, 2026
415e00b
fix: move backport to 7.2 compat
yashjawale Oct 6, 2026
e36687a
fix: remove extra line in lib/compat/wordpress-7.1/block-bindings.php
yashjawale Oct 6, 2026
922ee22
docs: add filter note to 7.2/block-bindings.php
yashjawale Oct 6, 2026
fea6729
fix: disable replace button for bound icon block instances
yashjawale Oct 6, 2026
adb0ef9
feat: add dynamic tooltip specifying reason for replace button being
yashjawale Oct 6, 2026
5f632d9
tests: add cases to check icon block binding & status of replace button
yashjawale Oct 6, 2026
8bf4509
Merge branch 'trunk' into feat/icon-block-pattern-overrides-80696
yashjawale Oct 6, 2026
908e21e
fix: lint php
yashjawale Oct 6, 2026
52caa27
fix: locator ambiguity
yashjawale Oct 6, 2026
9d1f911
fix: update binding label
yashjawale Oct 6, 2026
63c7c4c
Merge branch 'trunk' into feat/icon-block-pattern-overrides-80696
yashjawale Oct 7, 2026
579ed43
fix: add guard for adding icon to supported attributes in compat filter
yashjawale Oct 7, 2026
708f950
fix: prevent invalid REST requests by matching icon name format
yashjawale Oct 7, 2026
5baeaca
fix: consolidate attributes destructure
yashjawale Oct 7, 2026
1076c70
Merge branch 'trunk' into feat/icon-block-pattern-overrides-80696
yashjawale Oct 7, 2026
e5fbaec
fix: add safeguard for icon attribute value
yashjawale Oct 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backport-changelog/7.2/12708.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/12708

* https://github.com/WordPress/gutenberg/pull/80730
19 changes: 19 additions & 0 deletions lib/compat/wordpress-7.2/block-bindings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Block bindings additions for WordPress 7.2.
*
* @since 7.2.0
* @package gutenberg
* @subpackage Block Bindings
*/

// The following filter can be removed once the minimum required WordPress version is 7.2 or newer.
add_filter(
'block_bindings_supported_attributes_core/icon',
function ( $supported_attributes ) {
if ( ! in_array( 'icon', $supported_attributes, true ) ) {
$supported_attributes[] = 'icon';
}
return $supported_attributes;
}
);
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ function gutenberg_get_experiment_default( $name ) {
require __DIR__ . '/compat/wordpress-7.1/block-comments.php';

// WordPress 7.2 compat.
require __DIR__ . '/compat/wordpress-7.2/block-bindings.php';
require __DIR__ . '/compat/wordpress-7.2/class-gutenberg-rest-templates-controller-7-2.php';
require __DIR__ . '/compat/wordpress-7.2/view-config-api.php';
require __DIR__ . '/compat/wordpress-7.2/class-gutenberg-rest-view-config-controller-7-2.php';
Expand Down
75 changes: 68 additions & 7 deletions packages/block-library/src/icon/edit.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import clsx from 'clsx';
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import {
DropdownMenu,
TextControl,
Expand All @@ -22,6 +22,7 @@ import {
__experimentalGetSpacingClassesAndStyles as useSpacingProps,
getDimensionsClassesAndStyles as useDimensionsProps,
} from '@wordpress/block-editor';
import { getBlockBindingsSource } from '@wordpress/blocks';
import { useState } from '@wordpress/element';
import { SVG, Rect, Path } from '@wordpress/primitives';
import { useSelect } from '@wordpress/data';
Expand All @@ -30,6 +31,10 @@ import { useToolsPanelDropdownMenuProps } from '../utils/hooks';
import HtmlRenderer from '../utils/html-renderer';
import { CustomInserterModal } from './components';

// Matches the icon name pattern accepted by /wp/v2/icons/<name> REST route.
const ICON_NAME_REGEX =
/^[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?\/[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?$/;

const IconPlaceholder = ( { className, style } ) => (
<SVG
xmlns="http://www.w3.org/2000/svg"
Expand All @@ -50,13 +55,50 @@ const IconPlaceholder = ( { className, style } ) => (
</SVG>
);

export function Edit( { attributes, setAttributes } ) {
const { icon, ariaLabel, flipHorizontal, flipVertical, rotation } =
attributes;
export function Edit( { attributes, setAttributes, isSelected, context } ) {
const {
icon,
ariaLabel,
flipHorizontal,
flipVertical,
rotation,
metadata,
} = attributes;

const [ isInserterOpen, setInserterOpen ] = useState( false );

const isContentOnlyMode = useBlockEditingMode() === 'contentOnly';
const { isIconBindingReadOnly = false, iconBindingReadOnlyMessage } =
useSelect(
( select ) => {
if ( ! isSelected ) {
return {};
}

const iconBinding = metadata?.bindings?.icon;
const blockBindingsSource = getBlockBindingsSource(
iconBinding?.source
);

return {
isIconBindingReadOnly:
!! iconBinding &&
! blockBindingsSource?.canUserEditValue?.( {
select,
context,
args: iconBinding.args,
} ),
iconBindingReadOnlyMessage: blockBindingsSource?.label
? sprintf(
/* translators: %s: Label of the bindings source. */
__( 'Connected to %s' ),
blockBindingsSource.label
)
: __( 'Connected to dynamic data' ),
};
},
[ context, isSelected, metadata?.bindings?.icon ]
);

const colorProps = useColorProps( attributes );
// Only padding is applied to the inner SVG element, matching the front
Expand All @@ -73,8 +115,14 @@ export function Edit( { attributes, setAttributes } ) {

const selectedIcon = useSelect(
( select ) => {
const { getEntityRecord } = select( coreDataStore );
return icon ? getEntityRecord( 'root', 'icon', icon ) : null;
if ( typeof icon !== 'string' || ! ICON_NAME_REGEX.test( icon ) ) {
return null;
}
return select( coreDataStore ).getEntityRecord(
'root',
'icon',
icon
);
},
[ icon ]
);
Expand All @@ -87,6 +135,7 @@ export function Edit( { attributes, setAttributes } ) {
};

const rotationStyle = rotation ? { rotate: `${ rotation }deg` } : {};
const iconToolbarButtonLabel = icon ? __( 'Replace' ) : __( 'Choose icon' );

const blockControls = (
<>
Expand Down Expand Up @@ -125,11 +174,23 @@ export function Edit( { attributes, setAttributes } ) {
) }
<BlockControls group="other">
<ToolbarButton
disabled={ isIconBindingReadOnly }
label={
isIconBindingReadOnly
? iconBindingReadOnlyMessage
: undefined
}
showTooltip={ isIconBindingReadOnly }
aria-label={
isIconBindingReadOnly
? iconToolbarButtonLabel
: undefined
}
onClick={ () => {
setInserterOpen( true );
} }
>
{ icon ? __( 'Replace' ) : __( 'Choose icon' ) }
{ iconToolbarButtonLabel }
</ToolbarButton>
{ isContentOnlyMode && icon && (
// Add some extra controls for content attributes when content only mode is active.
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/icon/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @return string Returns the Icon.
*/
function render_block_core_icon( $attributes ) {
if ( empty( $attributes['icon'] ) ) {
if ( empty( $attributes['icon'] ) || ! is_string( $attributes['icon'] ) ) {
return;
}

Expand Down
5 changes: 5 additions & 0 deletions packages/e2e-tests/plugins/block-bindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ function gutenberg_test_block_bindings_registration() {
'value' => $testing_url,
'type' => 'string',
),
'icon_field' => array(
'label' => 'Icon Field Label',
'value' => 'core/arrow-right',
'type' => 'string',
),
'empty_field' => array(
'label' => 'Empty Field Label',
'value' => '',
Expand Down
30 changes: 30 additions & 0 deletions phpunit/block-bindings-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,36 @@ public function test_update_block_with_value_from_source_image_placeholder() {
);
}

/**
* Tests if the Icon block's icon attribute is updated with the value returned by the source.
*
* @covers ::register_block_bindings_source
*/
public function test_update_icon_block_with_value_from_source() {
register_block_bindings_source(
self::SOURCE_NAME,
array(
'label' => self::SOURCE_LABEL,
'get_value_callback' => function () {
return 'core/arrow-right';
},
)
);

$block_content = <<<HTML
<!-- wp:icon {"icon":"core/plus","metadata":{"bindings":{"icon":{"source":"test/source"}}}} /-->
HTML;
$parsed_blocks = parse_blocks( $block_content );
$block = new WP_Block( $parsed_blocks[0] );
$block->render();

$this->assertSame(
'core/arrow-right',
$block->attributes['icon'],
"The 'icon' attribute should be updated with the value returned by the source."
);
}

/**
* Tests if the `__default` attribute is replaced with real attributes for
* pattern overrides.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,31 @@ test.describe( 'Registered sources', () => {
);
} );

test( 'icon', async ( { editor, page } ) => {
await editor.insertBlock( {
name: 'core/icon',
attributes: {
icon: 'core/arrow-right',
metadata: {
bindings: {
icon: {
source: 'testing/can-user-edit-false',
args: { key: 'icon_field' },
},
},
},
},
} );
const replaceButton = page
.getByRole( 'toolbar', { name: 'Block tools' } )
.getByRole( 'button', { name: 'Replace' } );
await expect( replaceButton ).toBeDisabled();
await replaceButton.focus();
await expect( replaceButton ).toHaveAccessibleDescription(
'Connected to Can User Edit: False'
);
} );

test( 'button', async ( { editor, page } ) => {
await editor.insertBlock( {
name: 'core/buttons',
Expand Down Expand Up @@ -1086,6 +1111,43 @@ test.describe( 'Registered sources', () => {
await expect( contentAttribute ).toBeVisible();
} );

test( 'should be possible to connect the icon block icon', async ( {
editor,
page,
} ) => {
await editor.insertBlock( {
name: 'core/icon',
attributes: {
icon: 'core/arrow-right',
},
} );
await page.getByLabel( 'Attributes options' ).click();
const iconAttribute = page.getByRole( 'menuitemcheckbox', {
name: 'Show icon',
} );
await expect( iconAttribute ).toBeVisible();
await iconAttribute.click();
await page
.getByRole( 'button', {
name: 'icon Not connected',
exact: true,
} )
.click();
await page
.getByRole( 'menuitem', { name: 'Complete Source' } )
.click();
await page
.getByRole( 'menuitemcheckbox' )
.filter( { hasText: 'Icon Field Label' } )
.click();
await expect(
page.getByRole( 'button', {
name: 'icon Icon Field Label',
exact: true,
} )
).toContainText( 'Icon Field Label' );
} );

test( 'should be possible to connect the button supported attributes', async ( {
editor,
page,
Expand Down
Loading