| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side customizations for the `core/details` block. |
| 4 |
* |
| 5 |
* @package twentig |
| 6 |
*/ |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* Filters the details block output. |
| 12 |
* |
| 13 |
* @param string $block_content Rendered block content. |
| 14 |
* @param array $block Block object. |
| 15 |
* @return string Filtered block content. |
| 16 |
*/ |
| 17 |
function twentig_filter_details_block( $block_content, $block ) { |
| 18 |
$icon_type = $block['attrs']['twIcon'] ?? ''; |
| 19 |
|
| 20 |
if ( empty( $icon_type ) ) { |
| 21 |
return $block_content; |
| 22 |
} |
| 23 |
|
| 24 |
$icon_position = $block['attrs']['twIconPosition'] ?? 'right'; |
| 25 |
$tag_processor = new WP_HTML_Tag_Processor( $block_content ); |
| 26 |
$tag_processor->next_tag(); |
| 27 |
$tag_processor->add_class( 'tw-has-icon' ); |
| 28 |
$tag_processor->add_class( 'tw-icon-' . sanitize_html_class( $icon_type ) ); |
| 29 |
|
| 30 |
if ( 'left' === $icon_position ) { |
| 31 |
$tag_processor->add_class( 'tw-has-icon-left' ); |
| 32 |
} |
| 33 |
|
| 34 |
$block_content = $tag_processor->get_updated_html(); |
| 35 |
|
| 36 |
$icon_svg = '<svg class="details-arrow" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" version="1.1" aria-hidden="true" focusable="false"><path d="m12 15.4-6-6L7.4 8l4.6 4.6L16.6 8 18 9.4z"></path></svg>'; |
| 37 |
if ( 'plus' === $icon_type ) { |
| 38 |
$icon_svg = '<svg class="details-plus" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" version="1.1" aria-hidden="true" focusable="false"><path class="plus-vertical" d="M11 6h2v12h-2z"/><path d="M6 11h12v2H6z"/></svg>'; |
| 39 |
} elseif ( 'plus-circle' === $icon_type ) { |
| 40 |
$icon_svg = '<svg class="details-plus" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" version="1.1" aria-hidden="true" focusable="false"><path d="M12 2c5.523 0 10 4.477 10 10s-4.477 10-10 10S2 17.523 2 12 6.477 2 12 2m0 1.75a8.25 8.25 0 1 0 0 16.5 8.25 8.25 0 0 0 0-16.5" /><path d="M11.125 7.5h1.75v9h-1.75z" class="plus-vertical" /><path d="M7.5 11.125h9v1.75h-9z" /></svg>'; |
| 41 |
} |
| 42 |
return str_replace( '</summary>', $icon_svg . '</summary>', $block_content ); |
| 43 |
} |
| 44 |
add_filter( 'render_block_core/details', 'twentig_filter_details_block', 10, 2 ); |
| 45 |
|