Skip to content

Commit

Permalink
feat: Add Title and Class support to SVG Files. Fixes #366.
Browse files Browse the repository at this point in the history
  • Loading branch information
danimalweb committed Jan 3, 2024
1 parent bc3e2bc commit 05c88ad
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion inc/lib/shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,44 @@ function svg($atts)
}

if (file_exists($file)) {
return file_get_contents($file);
$file_contents = file_get_contents($file);

if ($file_contents) {
if ($atts['class']) {
// Check if class already exists on the svg tag, if found merge the classes
if (preg_match('/<svg[^>]*class="([^"]*)"/', $file_contents, $matches)) {
$classes = array_filter(array_map('trim', explode(' ', $matches[1])));

if (!in_array($atts['class'], $classes)) {
$classes[] = $atts['class'];
}

// Keep any existing attributes on the svg tag
$file_contents = str_replace($matches[0], '<svg class="' . implode(' ', $classes) . '"', $file_contents);

// $file_contents = str_replace($matches[0], '<svg class="' . implode(' ', $classes) . '"', $file_contents);
} else {
// Class doesn't exist, add it to the svg tag after the existing attributes
if (preg_match('/<svg([^>]*)>/', $file_contents, $matches)) {
$file_contents = str_replace($matches[0], '<svg' . $matches[1] . ' class="' . $atts['class'] . '">', $file_contents);
}
}
}

if ($atts['title']) {
// Check if a title tag already exists, if found replace the title
if (preg_match('/<title[^>]*>([^<]*)<\/title>/', $file_contents, $matches)) {
$file_contents = str_replace($matches[0], '<title>' . $atts['title'] . '</title>', $file_contents);
} else {
// Title doesn't exist, add it inside the svg tag after the existing attributes
if (preg_match('/<svg([^>]*)>/', $file_contents, $matches)) {
$file_contents = str_replace($matches[0], '<svg' . $matches[1] . '><title>' . $atts['title'] . '</title>', $file_contents);
}
}
}

return $file_contents;
}
}
}

Expand Down

0 comments on commit 05c88ad

Please sign in to comment.