Skip to content

Commit

Permalink
fix: Prevent using simplexml_load_file() on missing file (#83)
Browse files Browse the repository at this point in the history
* fix: Prevent using simplexml_load_file() on missing file

* fix: Add test

* chore: Update testing matrix
  • Loading branch information
gchtr authored Aug 27, 2024
1 parent 59b1f82 commit f45456e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
fail-fast: false
matrix:
php: ['7.4']
wp: ['5.5', 'latest']
wp: ['6.1', 'latest']
multisite: ['0', '1']
dependency-version: [prefer-stable] # prefer-lowest
webp: [false]
Expand Down
4 changes: 4 additions & 0 deletions lib/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,10 @@ public static function get_mime_types() {
* @return array|null
*/
public static function get_svg_dimensions( $svg ) {
if ( ! file_exists( $svg ) ) {
return null;
}

$svg = simplexml_load_file( $svg );
$width = 0;
$height = 0;
Expand Down
4 changes: 2 additions & 2 deletions lib/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Image {
*
* @var array
*/
protected $svg_dimensions = [];
protected array $svg_dimensions = [];

final protected function __construct() {
}
Expand Down Expand Up @@ -991,7 +991,7 @@ public function description() {
*/
public function svg_dimensions() {
if ( empty( $this->svg_dimensions ) ) {
$this->svg_dimensions = Helper::get_svg_dimensions( $this->path() );
$this->svg_dimensions = Helper::get_svg_dimensions( $this->path() ) ?: [];
}

return $this->svg_dimensions;
Expand Down
19 changes: 19 additions & 0 deletions tests/test-svg.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ public function test_get_timber_image_full_with_svg() {
$this->assertEquals( $image, $result );
}

/**
* Tests whether we still get the image src without an error, even if the
* SVG file is missing.
*
* @since 2.1.0
*/
public function test_get_timber_image_full_with_svg_inexistent() {
$attachment = $this->create_image( [ 'file' => 'sveegee.svg' ] );

if (file_exists($attachment->file_loc())) {
unlink($attachment->file_loc());
}

$result = get_timber_image( $attachment, 'full' );
$image = ' src="' . $this->get_upload_url() . '/sveegee.svg" alt=""';

$this->assertEquals( $image, $result );
}

/**
* Tests whether we get the full src of an SVG with size large.
*
Expand Down

0 comments on commit f45456e

Please sign in to comment.