Skip to content

Commit

Permalink
fixed displaying block icon in admin posts list when no icon selected
Browse files Browse the repository at this point in the history
  • Loading branch information
nk-o committed Oct 8, 2024
1 parent a4a61ce commit ce99d22
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
44 changes: 28 additions & 16 deletions classes/class-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,15 +541,16 @@ public function manage_posts_custom_column( $column_name = false ) {
global $post;

if ( 'lazyblocks_post_icon' === $column_name ) {
$icon = $this->get_meta_value_by_id( 'lazyblocks_icon' );
$icon = $this->prepare_block_icon( $this->get_meta_value_by_id( 'lazyblocks_icon' ) );
$admin_url = get_edit_post_link( $post->ID );

echo '<a class="lzb-admin-block-icon" href="' . esc_url( $admin_url ) . '">';

if ( $icon && strpos( $icon, 'dashicons' ) === 0 ) {
echo '<span class="dashicons ' . esc_attr( $icon ) . '"></span>';
} elseif ( $icon ) {
echo wp_kses( $icon, $this->kses_svg );
// XSS:OK - this variable is already escaped in the `prepare_block_icon` function.
echo $icon;
}

echo '</a>';
Expand Down Expand Up @@ -896,6 +897,30 @@ public function marshal_block_data( $block_data ) {
return $this->marshal_block_data_with_controls( null, null, $block_data, $all_controls );
}

/**
* Summary of prepare_block_icon
*
* @param string $icon - icon string.
*
* @return string
*/
public function prepare_block_icon( $icon ) {
// add default icon.
if ( ! $icon ) {
// phpcs:ignore
$icon = file_get_contents( lazyblocks()->plugin_path() . 'assets/svg/icon-lazyblocks.svg' );
$icon = str_replace( 'fill="white"', 'fill="currentColor"', $icon );
}

if ( $icon && strpos( $icon, 'dashicons' ) === 0 ) {
$icon = esc_attr( str_replace( 'dashicons-', 'dashicons dashicons-', $icon ) );
} elseif ( $icon ) {
$icon = wp_kses( $icon, $this->kses_svg );
}

return $icon;
}

/**
* Convert block format.
*
Expand All @@ -920,20 +945,7 @@ public function marshal_block_data_with_controls( $id = null, $post_title = null
}
};

$icon = $get_meta_value( 'lazyblocks_icon' );

// add default icon.
if ( ! $icon ) {
// phpcs:ignore
$icon = file_get_contents( lazyblocks()->plugin_path() . 'assets/svg/icon-lazyblocks.svg' );
$icon = str_replace( 'fill="white"', 'fill="currentColor"', $icon );
}

if ( $icon && strpos( $icon, 'dashicons' ) === 0 ) {
$icon = esc_attr( str_replace( 'dashicons-', 'dashicons dashicons-', $icon ) );
} elseif ( $icon ) {
$icon = wp_kses( $icon, $this->kses_svg );
}
$icon = $this->prepare_block_icon( $get_meta_value( 'lazyblocks_icon' ) );

$keywords = esc_attr( $get_meta_value( 'lazyblocks_keywords' ) );
if ( $keywords ) {
Expand Down
2 changes: 1 addition & 1 deletion classes/class-dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static function add() {
),
),
'lazyblocks_slug' => 'example-block',
'lazyblocks_icon' => '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><rect opacity="0.25" width="15" height="15" rx="4" transform="matrix(-1 0 0 1 22 7)" fill="currentColor" /><rect width="15" height="15" rx="4" transform="matrix(-1 0 0 1 17 2)" fill="currentColor" /></svg>',
'lazyblocks_icon' => '',
'lazyblocks_description' => esc_html__( 'Example block that helps you to get started with Lazy Blocks plugin', 'lazy-blocks' ),
'lazyblocks_keywords' => 'example,sample,template',
'lazyblocks_category' => 'design',
Expand Down

0 comments on commit ce99d22

Please sign in to comment.