{{panel}}
'; /** * @param array $items Accordion items. * @param string $template Block template id (4wp-faq, core-accordion). * @param array $attrs Optional forwp/faq attrs. */ public function build_accordion_section( array $items, string $template = '4wp-faq', array $attrs = array() ): string { if ( empty( $items ) ) { return ''; } $items_markup = ''; foreach ( $items as $item ) { $question = esc_html( wp_strip_all_tags( (string) ( $item['question'] ?? '' ) ) ); if ( '' === $question ) { continue; } $answer_html = (string) ( $item['answer_html'] ?? '' ); $panel = $this->html_to_inner_blocks_markup( $answer_html ); if ( '' === trim( wp_strip_all_tags( $panel ) ) ) { $panel = '

'; } $items_markup .= strtr( self::ACCORDION_ITEM_TEMPLATE, array( '{{question}}' => $question, '{{panel}}' => $panel, ) ); } if ( '' === $items_markup ) { return ''; } $accordion = '
' . $items_markup . '
'; if ( Block_Template_Registry::TEMPLATE_CORE_ACCORDION === sanitize_key( $template ) ) { return $accordion; } $attrs_part = ''; if ( ! empty( $attrs ) ) { $encoded = wp_json_encode( $attrs, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); if ( is_string( $encoded ) && '{}' !== $encoded ) { $attrs_part = ' ' . $encoded; } } return sprintf( '%s', $attrs_part, $accordion ); } /** * Convert answer HTML into block comments for accordion panels. */ public function html_to_inner_blocks_markup( string $html ): string { $html = trim( $html ); if ( '' === $html ) { return ''; } $nodes = Html_Body_Parser::nodes( $html ); if ( empty( $nodes ) ) { return sprintf( '

%s

', wp_kses_post( $html ) ); } $parts = array(); foreach ( $nodes as $node ) { $tag = (string) $node['tag']; if ( 'p' === $tag ) { $parts[] = sprintf( '%s', wp_kses_post( (string) $node['html'] ) ); continue; } if ( 'ul' === $tag || 'ol' === $tag ) { $parts[] = sprintf( '%s', wp_kses_post( (string) $node['html'] ) ); continue; } if ( 'h' === substr( $tag, 0, 1 ) && strlen( $tag ) === 2 ) { $level = (int) substr( $tag, 1 ); $parts[] = sprintf( '%2$s', $level, wp_kses_post( (string) $node['html'] ) ); continue; } if ( 'table' === $tag || 'figure' === $tag ) { $converted = Gutenberg_Content::from_html( (string) $node['html'] ); if ( '' !== $converted ) { $parts[] = $converted; continue; } } $text = trim( (string) $node['text'] ); if ( '' === $text ) { continue; } $parts[] = sprintf( '

%s

', esc_html( $text ) ); } return implode( "\n\n", $parts ); } /** * Build a core/image block for an uploaded attachment. * * @param string $align left|center|right or empty. */ public function build_image_block( int $attachment_id, string $url, string $alt = '', string $align = '' ): string { if ( $attachment_id <= 0 || '' === $url ) { return ''; } $align = sanitize_key( $align ); if ( ! in_array( $align, array( 'left', 'center', 'right' ), true ) ) { $align = ''; } $attrs = array( 'id' => $attachment_id, 'sizeSlug' => 'large', 'linkDestination' => 'none', ); if ( '' !== $align ) { $attrs['align'] = $align; } $encoded = wp_json_encode( $attrs, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); if ( ! is_string( $encoded ) ) { $encoded = '{}'; } $class = 'wp-block-image size-large'; if ( '' !== $align ) { $class .= ' align' . $align; } return sprintf( '
%4$s
', $encoded, esc_attr( $class ), esc_url( $url ), esc_attr( $alt ), $attachment_id ); } }