Make WordPress Core

Changeset 64225


Ignore:
Timestamp:
10/07/2026 09:28:31 AM (10 hours ago)
Author:
wildworks
Message:

Block Bindings: Add support for Icon block's icon attribute.

Enable Block Bindings support for the Icon block's icon attribute, so the displayed icon can be supplied by a block bindings source. This also makes pattern overrides available for the Icon block.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/12708

Props wildworks, yashjawale.
Fixes #65725.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-bindings.php

    r62518 r64225  
    136136 * @since 6.9.0
    137137 * @since 7.1.0 Added support for the List Item block.
     138 * @since 7.2.0 Added support for the Icon block.
    138139 *
    139140 * @param string $block_type The block type whose supported attributes are being retrieved.
    … …  
    150151                'core/navigation-link'    => array( 'url' ),
    151152                'core/navigation-submenu' => array( 'url' ),
     153                'core/icon'               => array( 'icon' ),
    152154        );
    153155
  • trunk/tests/phpunit/tests/block-bindings/render.php

    r62522 r64225  
    353353
    354354        /**
     355         * Tests if the Icon block's icon attribute is updated with the value
     356         * returned by a block bindings source.
     357         *
     358         * @ticket 65725
     359         *
     360         * @covers ::register_block_bindings_source
     361         */
     362        public function test_update_icon_block_with_value_from_source() {
     363                $get_value_callback = function () {
     364                        return 'test-icons/test-icon';
     365                };
     366
     367                wp_register_icon_collection(
     368                        'test-icons',
     369                        array( 'label' => 'Test Icons' )
     370                );
     371
     372                wp_register_icon(
     373                        'test-icons/test-icon',
     374                        array(
     375                                'label'   => 'Test Icon',
     376                                'content' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M0 0h24v24H0z" fill="currentColor"/></svg>',
     377                        )
     378                );
     379
     380                register_block_bindings_source(
     381                        self::SOURCE_NAME,
     382                        array(
     383                                'label'              => self::SOURCE_LABEL,
     384                                'get_value_callback' => $get_value_callback,
     385                        )
     386                );
     387
     388                try {
     389                        $block_content = <<<HTML
     390<!-- wp:icon {"metadata":{"bindings":{"icon":{"source":"test/source"}}}} -->
     391<div class="wp-block-icon"></div>
     392<!-- /wp:icon -->
     393HTML;
     394                        $parsed_blocks = parse_blocks( $block_content );
     395                        $block         = new WP_Block( $parsed_blocks[0] );
     396                        $result        = $block->render();
     397
     398                        $this->assertSame(
     399                                'test-icons/test-icon',
     400                                $block->attributes['icon'],
     401                                "The 'icon' attribute should be updated with the value returned by the source."
     402                        );
     403                        $this->assertStringContainsString(
     404                                '<svg',
     405                                $result,
     406                                'The rendered output should contain an SVG element.'
     407                        );
     408                        $this->assertStringContainsString(
     409                                'wp-block-icon',
     410                                $result,
     411                                'The rendered output should contain the wp-block-icon class.'
     412                        );
     413                } finally {
     414                        wp_unregister_icon( 'test-icons/test-icon' );
     415                        wp_unregister_icon_collection( 'test-icons' );
     416                }
     417        }
     418
     419        /**
    355420         * Tests if the `__default` attribute is replaced with real attributes for
    356421         * pattern overrides.
Note: See TracChangeset for help on using the changeset viewer.