From 397ee44e5cabcf4c5dd871892203c9fa93a5d0ab Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Tue, 26 May 2020 10:10:16 +0100 Subject: [PATCH 1/2] Check vars before use them --- .../php/class-media.php | 5 ++++- .../php/sync/class-push-sync.php | 2 +- .../php/sync/class-upload-sync.php | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php index cd966dc35..737e09088 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php @@ -574,7 +574,10 @@ public function cloudinary_url( $attachment_id, $size = array(), $transformation // Check size and correct if string or size. if ( is_string( $size ) || ( is_array( $size ) && 3 === count( $size ) ) ) { $intermediate = image_get_intermediate_size( $attachment_id, $size ); - $size = $this->get_crop( $intermediate['url'], $attachment_id ); + if ( ! is_array( $intermediate ) ) { + return false; + } + $size = $this->get_crop( $intermediate['url'], $attachment_id ); } /** diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php index f69020e31..90e0b0779 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php @@ -358,7 +358,7 @@ public function prepare_upload( $post, $down_sync = false ) { $resource_type = $this->get_resource_type( $post ); $max_size = ( 'image' === $resource_type ? 'max_image_size' : 'max_video_size' ); - if ( $file_size > $this->plugin->components['connect']->usage[ $max_size ] ) { + if ( ! empty( $this->plugin->components['connect']->usage[ $max_size ] ) && $file_size > $this->plugin->components['connect']->usage[ $max_size ] ) { $max_size_hr = size_format( $this->plugin->components['connect']->usage[ $max_size ] ); // translators: variable is file size. $error = sprintf( __( 'File size exceeds the maximum of %s. This media asset will be served from WordPress.', 'cloudinary' ), $max_size_hr ); diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-upload-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-upload-sync.php index 2c4bc7f62..8187af545 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-upload-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-upload-sync.php @@ -117,7 +117,7 @@ public function prep_on_demand_upload( $cloudinary_id, $attachment_id ) { $max_size = ( wp_attachment_is_image( $attachment_id ) ? 'max_image_size' : 'max_video_size' ); $file = get_attached_file( $attachment_id ); // Get the file size to make sure it can exist in cloudinary. - if ( file_exists( $file ) && filesize( $file ) < $this->plugin->components['connect']->usage[ $max_size ] ) { + if ( ! empty( $this->plugin->components['connect']->usage[ $max_size ] ) && file_exists( $file ) && filesize( $file ) < $this->plugin->components['connect']->usage[ $max_size ] ) { $this->add_to_sync( $attachment_id ); } else { // Check if the src is a url. From 1fe6402b7364ff18c01b57386f736109cd23ca62 Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Tue, 26 May 2020 10:10:38 +0100 Subject: [PATCH 2/2] Try to set SVGs size --- .../php/media/class-filter.php | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php index 5c2b0890a..553b4117a 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php @@ -671,6 +671,45 @@ public function filter_image_block_pre_render( $block, $source_block ) { return $block; } + /** + * Attempt to set the width and height for SVGs. + * + * @param array|false $image The image details. + * @param int $attachment_id The attachment ID. + * @param string|int[] $size The requested image size. + * + * @return array|false + */ + public function filter_svg_image_size( $image, $attachment_id, $size ) { + if ( is_array( $image ) && preg_match('/\.svg$/i', $image[0] ) && $image[1] <= 1 ) { + $image[1] = $image[2] = null; + + if ( is_array( $size ) ) { + $image[1] = $size[0]; + $image[2] = $size[1]; + } elseif ( false !== ( $xml = simplexml_load_file( $image[0] ) ) ) { + $attr = $xml->attributes(); + $viewbox = explode( ' ', $attr->viewBox ); + + // Get width + if ( isset( $attr->width ) && preg_match( '/\d+/', $attr->width, $value ) ) { + $image[1] = (int) $value[0]; + } elseif ( 4 === count( $viewbox ) ) { + $image[1] = (int) $viewbox[2]; + } + + // Get height + if ( isset( $attr->height ) && preg_match( '/\d+/', $attr->height, $value ) ) { + $image[2] = (int) $value[0]; + } elseif ( 4 === count( $viewbox ) ) { + $image[2] = (int) $viewbox[3]; + } + } + } + + return $image; + } + /** * Setup hooks for the filters. */ @@ -709,5 +748,7 @@ function ( $type ) use ( $filter ) { // Filter for block rendering. add_filter( 'render_block_data', array( $this, 'filter_image_block_pre_render' ), 10, 2 ); + // Try to get SVGs size. + add_filter( 'wp_get_attachment_image_src', array( $this, 'filter_svg_image_size' ), 10, 3 ); } }