Safe SVG - Version 1.8.0

Version Description

  • Pull SVG dimensions from the width/height or viewbox attributes of the SVG.
  • Add the role="img" attribute to SVGs
Download this release

Release Info

Developer enshrined
Plugin Icon 128x128 Safe SVG
Version 1.8.0
Comparing to
See all releases

Code changes from version 1.7.1 to 1.8.0

Files changed (2) hide show
  1. readme.txt +5 -1
  2. safe-svg.php +87 -11
readme.txt CHANGED
@@ -5,7 +5,7 @@ Tags: svg, sanitize, upload, sanitise, security, svg upload, image, vector, file
5
  Requires at least: 4.0
6
  Tested up to: 4.9.1
7
  Requires PHP: 5.6
8
- Stable tag: 1.7.1
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
@@ -62,6 +62,10 @@ They take one argument that must be returned. See below for examples:
62
 
63
  == Changelog ==
64
 
 
 
 
 
65
  = 1.7.1 =
66
  * Updated underlying lib and added new filters for filtering allowed tags and attributes
67
 
5
  Requires at least: 4.0
6
  Tested up to: 4.9.1
7
  Requires PHP: 5.6
8
+ Stable tag: 1.8.0
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
62
 
63
  == Changelog ==
64
 
65
+ = 1.8.0 =
66
+ * Pull SVG dimensions from the width/height or viewbox attributes of the SVG.
67
+ * Add the role="img" attribute to SVGs
68
+
69
  = 1.7.1 =
70
  * Updated underlying lib and added new filters for filtering allowed tags and attributes
71
 
safe-svg.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Safe SVG
4
  Plugin URI: https://wpsvg.com/
5
  Description: Allows SVG uploads into WordPress and sanitizes the SVG before saving it
6
- Version: 1.7.1
7
  Author: Daryll Doyle
8
  Author URI: http://enshrined.co.uk
9
  Text Domain: safe-svg
@@ -48,6 +48,7 @@ if ( ! class_exists( 'safe_svg' ) ) {
48
  add_filter( 'wp_generate_attachment_metadata', array( $this, 'skip_svg_regeneration' ), 10, 2 );
49
  add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_upgrade_link' ) );
50
  add_filter( 'wp_get_attachment_metadata', array( $this, 'metadata_error_fix' ), 10, 2 );
 
51
  }
52
 
53
  /**
@@ -184,18 +185,30 @@ if ( ! class_exists( 'safe_svg' ) ) {
184
 
185
  if ( $response['mime'] == 'image/svg+xml' ) {
186
  $possible_sizes = apply_filters( 'image_size_names_choose', array(
 
187
  'thumbnail' => __( 'Thumbnail' ),
188
  'medium' => __( 'Medium' ),
189
  'large' => __( 'Large' ),
190
- 'full' => __( 'Full Size' ),
191
  ) );
192
 
193
  $sizes = array();
194
 
195
  foreach ( $possible_sizes as $size => $label ) {
 
 
 
 
 
 
 
 
 
 
 
 
196
  $sizes[ $size ] = array(
197
- 'height' => get_option( "{$size}_size_w", 2000 ),
198
- 'width' => get_option( "{$size}_size_h", 2000 ),
199
  'url' => $response['url'],
200
  'orientation' => 'portrait',
201
  );
@@ -269,27 +282,32 @@ if ( ! class_exists( 'safe_svg' ) ) {
269
  * @return mixed
270
  */
271
  function get_image_tag_override( $html, $id, $alt, $title, $align, $size ) {
272
- $mime = get_post_mime_type( $id );
273
 
274
- if ( 'image/svg+xml' === $mime ) {
275
- if( is_array( $size ) ) {
276
- $width = $size[0];
277
  $height = $size[1];
 
 
 
278
  } else {
279
  $width = get_option( "{$size}_size_w", false );
280
  $height = get_option( "{$size}_size_h", false );
281
  }
282
 
283
- if( $height && $width ) {
284
  $html = str_replace( 'width="1" ', sprintf( 'width="%s" ', $width ), $html );
285
  $html = str_replace( 'height="1" ', sprintf( 'height="%s" ', $height ), $html );
286
  } else {
287
  $html = str_replace( 'width="1" ', '', $html );
288
  $html = str_replace( 'height="1" ', '', $html );
289
  }
290
- }
291
 
292
- return $html;
 
 
 
293
  }
294
 
295
  /**
@@ -341,6 +359,64 @@ if ( ! class_exists( 'safe_svg' ) ) {
341
  return $data;
342
  }
343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
344
  }
345
  }
346
 
3
  Plugin Name: Safe SVG
4
  Plugin URI: https://wpsvg.com/
5
  Description: Allows SVG uploads into WordPress and sanitizes the SVG before saving it
6
+ Version: 1.8.0
7
  Author: Daryll Doyle
8
  Author URI: http://enshrined.co.uk
9
  Text Domain: safe-svg
48
  add_filter( 'wp_generate_attachment_metadata', array( $this, 'skip_svg_regeneration' ), 10, 2 );
49
  add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_upgrade_link' ) );
50
  add_filter( 'wp_get_attachment_metadata', array( $this, 'metadata_error_fix' ), 10, 2 );
51
+ add_filter( 'wp_get_attachment_image_attributes',array( $this, 'fix_direct_image_output' ), 10, 3 );
52
  }
53
 
54
  /**
185
 
186
  if ( $response['mime'] == 'image/svg+xml' ) {
187
  $possible_sizes = apply_filters( 'image_size_names_choose', array(
188
+ 'full' => __( 'Full Size' ),
189
  'thumbnail' => __( 'Thumbnail' ),
190
  'medium' => __( 'Medium' ),
191
  'large' => __( 'Large' ),
 
192
  ) );
193
 
194
  $sizes = array();
195
 
196
  foreach ( $possible_sizes as $size => $label ) {
197
+ $default_height = 2000;
198
+ $default_width = 2000;
199
+
200
+ if ( 'full' === $size ) {
201
+ $dimensions = $this->svg_dimensions( get_attached_file( $attachment->ID ) );
202
+
203
+ if ( $dimensions ) {
204
+ $default_height = $dimensions['height'];
205
+ $default_width = $dimensions['width'];
206
+ }
207
+ }
208
+
209
  $sizes[ $size ] = array(
210
+ 'height' => get_option( "{$size}_size_w", $default_height ),
211
+ 'width' => get_option( "{$size}_size_h", $default_width ),
212
  'url' => $response['url'],
213
  'orientation' => 'portrait',
214
  );
282
  * @return mixed
283
  */
284
  function get_image_tag_override( $html, $id, $alt, $title, $align, $size ) {
285
+ $mime = get_post_mime_type( $id );
286
 
287
+ if ( 'image/svg+xml' === $mime ) {
288
+ if ( is_array( $size ) ) {
289
+ $width = $size[0];
290
  $height = $size[1];
291
+ } elseif ( 'full' == $size && $dimensions = $this->svg_dimensions( get_attached_file( $id ) ) ) {
292
+ $width = $dimensions['width'];
293
+ $height = $dimensions['height'];
294
  } else {
295
  $width = get_option( "{$size}_size_w", false );
296
  $height = get_option( "{$size}_size_h", false );
297
  }
298
 
299
+ if ( $height && $width ) {
300
  $html = str_replace( 'width="1" ', sprintf( 'width="%s" ', $width ), $html );
301
  $html = str_replace( 'height="1" ', sprintf( 'height="%s" ', $height ), $html );
302
  } else {
303
  $html = str_replace( 'width="1" ', '', $html );
304
  $html = str_replace( 'height="1" ', '', $html );
305
  }
 
306
 
307
+ $html = str_replace( '/>', ' role="img" />', $html );
308
+ }
309
+
310
+ return $html;
311
  }
312
 
313
  /**
359
  return $data;
360
  }
361
 
362
+ /**
363
+ * Get SVG size from the width/height or viewport.
364
+ *
365
+ * @param $svg
366
+ *
367
+ * @return array|bool
368
+ */
369
+ protected function svg_dimensions( $svg ) {
370
+ $svg = simplexml_load_file( $svg );
371
+ $width = 0;
372
+ $height = 0;
373
+ if ( $svg ) {
374
+ $attributes = $svg->attributes();
375
+ if ( isset( $attributes->width, $attributes->height ) ) {
376
+ $width = floatval( $attributes->width );
377
+ $height = floatval( $attributes->height );
378
+ } elseif ( isset( $attributes->viewBox ) ) {
379
+ $sizes = explode( ' ', $attributes->viewBox );
380
+ if ( isset( $sizes[2], $sizes[3] ) ) {
381
+ $width = floatval( $sizes[2] );
382
+ $height = floatval( $sizes[3] );
383
+ }
384
+ } else {
385
+ return false;
386
+ }
387
+ }
388
+
389
+ return array( 'width' => $width, 'height' => $height );
390
+ }
391
+
392
+ /**
393
+ * Fix the output of images using wp_get_attachment_image
394
+ *
395
+ * @param array $attr Attributes for the image markup.
396
+ * @param WP_Post $attachment Image attachment post.
397
+ * @param string|array $size Requested size. Image size or array of width and height values
398
+ * (in that order). Default 'thumbnail'.
399
+ */
400
+ public function fix_direct_image_output( $attr, $attachment, $size ) {
401
+ $mime = get_post_mime_type( $attachment->ID );
402
+ if ( 'image/svg+xml' === $mime ) {
403
+ $default_height = 100;
404
+ $default_width = 100;
405
+
406
+ $dimensions = $this->svg_dimensions( get_attached_file( $attachment->ID ) );
407
+
408
+ if ( $dimensions ) {
409
+ $default_height = $dimensions['height'];
410
+ $default_width = $dimensions['width'];
411
+ }
412
+
413
+ $attr['height'] = $default_height;
414
+ $attr['width'] = $default_width;
415
+ }
416
+
417
+ return $attr;
418
+ }
419
+
420
  }
421
  }
422