firstChild ) { return $content; } /** @var DOMElement $first_child */ $first_child = $dom->firstChild; $div = change_tag_name( $first_child, 'div' ); $span = change_tag_name( $div->firstChild, 'span' ); $allowed_classes = [ 'wp-block-image', 'wp-block-site-logo', 'is-style-icon', 'aligncenter', 'alignleft', 'alignright', 'aligncenter', 'aligncenter', ]; $all_classes = explode( ' ', $div->getAttribute( 'class' ) ); $div_classes = ''; $span_classes = 'blockify-icon'; foreach ( $all_classes as $class ) { if ( in_array( $class, $allowed_classes ) ) { $div_classes .= ' ' . $class; } } if ( str_contains( $div->getAttribute( 'class' ), 'has-box-shadow' ) ) { $span_classes .= ' has-box-shadow'; } $span_styles = []; $default_svg = ' '; if ( isset( $block['attrs']['width'] ) ) { $span_styles['--wp--custom--icon--size'] = $block['attrs']['width'] . 'px'; } if ( isset( $block['attrs']['height'] ) ) { $span_styles['--wp--custom--icon--height'] = $block['attrs']['height'] . 'px'; } if ( isset( $block['attrs']['backgroundColor'] ) ) { $background = 'var(--wp--preset--color--' . $block['attrs']['backgroundColor'] . ')'; $span_styles['--wp--custom--icon--color'] = $background; } if ( isset( $block['attrs']['gradient'] ) ) { $gradient = 'var(--wp--preset--gradient--' . $block['attrs']['gradient'] . ')'; $span_styles['--wp--custom--icon--color'] = $gradient; } if ( isset( $block['attrs']['textColor'] ) ) { $span_styles['--wp--custom--icon--color'] = 'var(--wp--preset--color--' . $block['attrs']['textColor'] . ')'; $span_styles['background'] = $gradient ?? $background ?? 'transparent'; } if ( isset( $block['attrs']['style']['color']['gradient'] ) && ! isset( $block['attrs']['textColor'] ) ) { $span_styles['--wp--custom--icon--color'] = $block['attrs']['style']['color']['gradient']; } if ( isset( $block['attrs']['style']['transform'] ) ) { // TODO. } foreach ( explode( ';', $div->getAttribute( 'style' ) ) as $rule ) { $explode = explode( ':', $rule ); $property = $explode[0] ?? null; $value = $explode[1] ?? null; if ( ! str_contains( $property, 'background' ) && $value ) { $span_styles[ $property ] = $value; } } $span_styles['--wp--custom--icon--url'] = "url('data:image/svg+xml;utf8," . ( $block['attrs']['svgString'] ?? $default_svg ) . "')"; $css = css_array_to_string( $span_styles ); if ( $span->getAttribute( 'href' ) ) { $span = change_tag_name( $span, 'a' ); } $dom->removeChild( $dom->firstChild ); $dom->appendChild( $div ); $div->removeChild( $dom->firstChild->firstChild ); $div->appendChild( $span ); $div->removeAttribute( 'style' ); $span->removeAttribute( 'src' ); $span->removeAttribute( 'width' ); $span->removeAttribute( 'height' ); $span->removeAttribute( 'alt' ); $div->setAttribute( 'class', trim( $div_classes ) ); $span->setAttribute( 'class', trim( $span_classes ) ); $span->setAttribute( 'style', $css ); if ( $span->firstChild ) { $span->removeChild( $span->firstChild ); } if ( isset( $block['attrs']['icon'] ) ) { $span->setAttribute( 'title', convert_case( $block['attrs']['icon'], SENTENCE_CASE ) ); } $content = $dom->saveHTML(); } return $content; }