Skip to content

Instantly share code, notes, and snippets.

@davidperezgar
Created September 22, 2024 09:17
Show Gist options
  • Save davidperezgar/866c9e89b7a669aab739ff93aee4d99e to your computer and use it in GitHub Desktop.
Save davidperezgar/866c9e89b7a669aab739ff93aee4d99e to your computer and use it in GitHub Desktop.
Escape SVG
<?php
public function escapeSvg( string $svg ): string {
$svg_args = array(
'svg' => array(
'xmlns' => true,
'id' => true,
'class' => true,
'style' => true,
'x' => true,
'y' => true,
'width' => true,
'height' => true,
'viewbox' => true,
'preserveaspectratio' => true,
'fill' => true,
'fill-opacity' => true,
'fill-rule' => true,
'stroke' => true,
'stroke-width' => true,
'stroke-opacity' => true,
'stroke-linecap' => true,
'stroke-linejoin' => true,
'stroke-dasharray' => true,
'stroke-dashoffset' => true,
'stroke-miterlimit' => true,
),
'line' => array(
'x1' => true,
'y1' => true,
'x2' => true,
'y2' => true,
'id' => true,
'class' => true,
'style' => true,
'fill' => true,
'fill-opacity' => true,
'fill-rule' => true,
'stroke' => true,
'stroke-width' => true,
'stroke-opacity' => true,
'stroke-linecap' => true,
'stroke-linejoin' => true,
'stroke-dasharray' => true,
'stroke-dashoffset' => true,
'stroke-miterlimit' => true,
),
'polyline' => array(
'points' => true,
'id' => true,
'class' => true,
'style' => true,
'fill' => true,
'fill-opacity' => true,
'fill-rule' => true,
'stroke' => true,
'stroke-width' => true,
'stroke-opacity' => true,
'stroke-linecap' => true,
'stroke-linejoin' => true,
'stroke-dasharray' => true,
'stroke-dashoffset' => true,
'stroke-miterlimit' => true,
),
'polygon' => array(
'points' => true,
'id' => true,
'class' => true,
'style' => true,
'fill' => true,
'fill-opacity' => true,
'fill-rule' => true,
'stroke' => true,
'stroke-width' => true,
'stroke-opacity' => true,
'stroke-linecap' => true,
'stroke-linejoin' => true,
'stroke-dasharray' => true,
'stroke-dashoffset' => true,
'stroke-miterlimit' => true,
),
'path' => array(
'd' => true,
'id' => true,
'class' => true,
'style' => true,
'fill' => true,
'fill-opacity' => true,
'fill-rule' => true,
'stroke' => true,
'stroke-width' => true,
'stroke-opacity' => true,
'stroke-linecap' => true,
'stroke-linejoin' => true,
'stroke-dasharray' => true,
'stroke-dashoffset' => true,
'stroke-miterlimit' => true,
),
'rect' => array(
'x' => true,
'y' => true,
'rx' => true,
'ry' => true,
'width' => true,
'height' => true,
'id' => true,
'class' => true,
'style' => true,
'fill' => true,
'fill-opacity' => true,
'fill-rule' => true,
'stroke' => true,
'stroke-width' => true,
'stroke-opacity' => true,
'stroke-linecap' => true,
'stroke-linejoin' => true,
'stroke-dasharray' => true,
'stroke-dashoffset' => true,
'stroke-miterlimit' => true,
),
'circle' => array(
'r' => true,
'cx' => true,
'cy' => true,
'id' => true,
'class' => true,
'style' => true,
'fill' => true,
'fill-opacity' => true,
'fill-rule' => true,
'stroke' => true,
'stroke-width' => true,
'stroke-opacity' => true,
'stroke-linecap' => true,
'stroke-linejoin' => true,
'stroke-dasharray' => true,
'stroke-dashoffset' => true,
'stroke-miterlimit' => true,
),
'ellipse' => array(
'rx' => true,
'ry' => true,
'cx' => true,
'cy' => true,
'id' => true,
'class' => true,
'style' => true,
'fill' => true,
'fill-opacity' => true,
'fill-rule' => true,
'stroke' => true,
'stroke-width' => true,
'stroke-opacity' => true,
'stroke-linecap' => true,
'stroke-linejoin' => true,
'stroke-dasharray' => true,
'stroke-dashoffset' => true,
'stroke-miterlimit' => true,
),
'title' => array(
'id' => true,
'class' => true,
'style' => true,
),
'desc' => array(
'id' => true,
'class' => true,
'style' => true,
),
);
return wp_kses( $svg, $svg_args );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment