Skip to content

Commit

Permalink
Merge branch 'bugfix/support-children-when-rendering-svg'
Browse files Browse the repository at this point in the history
  • Loading branch information
oakesjosh committed Aug 1, 2024
2 parents 67d6859 + 8e9f5a5 commit 83dd22f
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions includes/class-kadence-blocks-svg.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,7 @@ public static function render( $name, $fill = 'currentColor', $stroke_width = fa
$svg .= '<title>' . $title . '</title>';
}
if ( ! empty( $icon['cD'] ) ) {
foreach ( $icon['cD'] as $cd ) {
$nE = $cd['nE'];
$aBs = $cd['aBs'];
$tmpAttr = array();

foreach ( $aBs as $key => $attribute ) {
if ( ! in_array( $key, array( 'fill', 'stroke', 'none' ) ) ) {
$tmpAttr[ $key ] = $key . '="' . $attribute . '"';
}
}

if ( isset( $aBs['fill'], $aBs['stroke'] ) && $aBs['fill'] === 'none' ) {
$tmpAttr['stroke'] = 'stroke="currentColor"';
}

$svg .= '<' . $nE . ' ' . implode( ' ', $tmpAttr ) . '/>';
}
$svg .= self::generate_svg_elements($icon['cD']);
}

$svg .= '</svg>';
Expand All @@ -193,6 +177,43 @@ public static function render( $name, $fill = 'currentColor', $stroke_width = fa
return $svg;

}

/**
* Recursively generate SVG elements
* Out native SVGs do not have children, but user uploaded SVGs in pro can contain children elements.
*
* @param $elements
*
* @return string
*/
private static function generate_svg_elements( $elements ) {
$output = '';
foreach ( $elements as $element ) {
$nE = $element['nE'];
$aBs = $element['aBs'];
$children = ! empty( $element['children'] ) ? $element['children'] : [];
$tmpAttr = array();

foreach ( $aBs as $key => $attribute ) {
if ( ! in_array( $key, array( 'fill', 'stroke', 'none' ) ) ) {
$tmpAttr[ $key ] = $key . '="' . esc_attr( $attribute ) . '"';
}
}

if ( isset( $aBs['fill'], $aBs['stroke'] ) && $aBs['fill'] === 'none' ) {
$tmpAttr['stroke'] = 'stroke="currentColor"';
}

$output .= '<' . $nE . ' ' . implode( ' ', $tmpAttr );
if ( ! empty( $children ) ) {
$output .= '>' . self::generate_svg_elements( $children ) . '</' . $nE . '>';
} else {
$output .= '/>';
}
}

return $output;
}
/**
* Return an array of icons.
*
Expand Down

0 comments on commit 83dd22f

Please sign in to comment.