Skip to content

Commit

Permalink
Fixes #31
Browse files Browse the repository at this point in the history
Signed-off-by: Lewis Cowles <[email protected]>
  • Loading branch information
Lewiscowles1986 committed Mar 19, 2018
1 parent f31e6a6 commit 11a3f2f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "lewiscowles1986/lc-enable-svg",
"type": "wordpress-plugin",
"description": "A small WordPress plugin to enable SVG uploads in Media Library and other file upload fields.",
"version": "1.8.4",
"version": "1.9.1",
"license": "GPL-3.0",
"homepage": "https://github.com/Lewiscowles1986/WordPressSVGPlugin",
"authors": [
Expand Down
40 changes: 39 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin Name: Enable SVG Uploads
* Plugin URI: https://github.com/Lewiscowles1986/WordPressSVGPlugin
* Description: Enable SVG uploads in Media Library and other file upload fields.
* Version: 1.8.4
* Version: 1.9.1
* Author: Lewis Cowles
* Author URI: https://www.lewiscowles.co.uk/
* License: GPL-3.0
Expand All @@ -27,6 +27,7 @@ function __construct() {
add_action( 'load-post-new.php', [ $this, 'add_editor_styles' ], 75 );
add_action( 'after_setup_theme', [ $this, 'theme_prefix_setup' ], 75 );
add_filter( 'wp_check_filetype_and_ext', [ $this, 'fix_mime_type_svg' ], 75, 4 );
add_filter( 'wp_update_attachment_metadata', [ $this, 'ensure_svg_metadata' ], 10, 2 );
}

public function theme_prefix_setup() {
Expand Down Expand Up @@ -133,5 +134,42 @@ public function fix_template( $content = '' ) {
);
return $content;
}

public function ensure_svg_metadata( $data, $id ) {
$attachment = get_post( $id );
$mime_type = $attachment->post_mime_type;

if ( $mime_type == 'image/svg+xml' ) {
if( $this->missingOrInvalidSVGDimensions( $data ) ) {
$xml = simplexml_load_file( wp_get_attachment_url( $id ) );
$attr = $xml->attributes();
$viewbox = explode( ' ', $attr->viewBox );

$this->fillSVGDimensions( $viewbox, $attr, $data, 'width', 2 );
$this->fillSVGDimensions( $viewbox, $attr, $data, 'height', 3 );
}
}
return $data;
}

protected function missingOrInvalidSVGDimensions( $data ) {
return (
empty( $data ) || empty( $data['width'] ) || empty( $data['height'] )
||
intval($data['width'] < 1) || intval($data['height'] < 1)
);
}

protected function fillSVGDimensions( $viewbox, $attr, &$data, $dimension, $viewboxoffset ) {
if ( isset( $attr->{ $dimension } ) ) {
$data[ $dimension ] = intval( $attr->{ $dimension } );
}
if ( is_nan( $data[ $dimension ] ) || !isset( $data[ $dimension ] ) ) {
$data[ $dimension ] = 0;
}
if ( $data[ $dimension ] < 1 ) {
$data[ $dimension ] = count($viewbox) == 4 ? intval( $viewbox[$viewboxoffset] ) : null;
}
}
}
new SVGSupport();
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tags: svg,upload,media library,mime
Requires at least: 4.0
Tested up to: 4.9
Requires PHP: 5.6
Stable tag: 1.8.4
Stable tag: 1.9.1
License: GPL-3.0
License URI: https://www.gnu.org/licenses/gpl-3.0.html

Expand All @@ -25,6 +25,9 @@ Automatic updates are currently supported via [GitHub Updater](https://github.co
Download and extract the zip file or clone this repo to your WordPress plugins directory.

== Changelog ==
= 1.9.1 =
* Added filter to deduce dimensions (if present) from regular SVG uploads

= 1.8.4 =
* Fixed a typo leading to warnings on some hosts

Expand Down

0 comments on commit 11a3f2f

Please sign in to comment.