Making WordPress.org

Changeset 11355


Ignore:
Timestamp:
12/06/2021 04:04:19 AM (2 years ago)
Author:
dd32
Message:

Plugin Directory: Blocks: Support the object notation for block icons, which adds support for background/foreground colours.

This prevents a fatal where we were trying to treat the object as a string.

See https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/#icon-optional

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-plugins/template-parts/section-blocks.php

    r10440 r11355  
    5151                continue;
    5252            }
    53             $block_icon = isset( $block->icon ) ? $block->icon : '';
     53
     54            $block_icon = '';
     55            $block_styles = [];
     56            if ( isset( $block->icon->src ) ) {
     57                $block_icon = $block->icon->src;
     58
     59                if ( isset( $block->icon->foreground ) ) {
     60                    $block_styles[] = 'color: ' . sanitize_hex_color( $block->icon->foreground );
     61                }
     62
     63                if ( isset( $block->icon->background ) ) {
     64                    $block_styles[] = 'background-color: ' . sanitize_hex_color( $block->icon->background );
     65                }
     66
     67            } elseif ( isset( $block->icon ) && is_string( $block->icon ) ) {
     68                $block_icon = $block->icon;
     69            }
     70
    5471            $block_classes = 'plugin-blocks-list-item';
    5572            $block_classes .= isset( $block->description ) ? ' has-description' : '';
     73            $block_style   = $block_styles ? 'style="' . implode('; ', $block_styles ) . '"' : '';
    5674            ?>
    5775            <li class="<?php echo esc_attr( $block_classes ); ?>">
    5876                <?php if ( false !== strpos( $block_icon, '<svg' ) ) : ?>
    59                     <span class="block-icon">
     77                    <span class="block-icon" <?php echo $block_style; ?>>
    6078                        <?php echo wp_kses( str_replace( '<svg ', '<svg role="img" aria-hidden="true" focusable="false" ', $block_icon ), $allowed_svg ); ?>
    6179                    </span>
    6280                <?php elseif ( $block_icon ) : ?>
    63                     <span class="block-icon dashicons dashicons-<?php echo esc_attr( $block->icon ); ?>"></span>
     81                    <span class="block-icon dashicons dashicons-<?php echo esc_attr( $block_icon ); ?>" <?php echo $block_style; ?>></span>
    6482                <?php else : ?>
    6583                    <span class="block-icon dashicons dashicons-block-default"></span>
Note: See TracChangeset for help on using the changeset viewer.