/** * Get the minimum version of PHP required by this plugin. * * @since 2.1.1 * * @return string Minimum version required. */ function minimum_php_requirement() { return '7.4'; }
/** * Custom function to check if user can upload svg. * * Use core caps if setting hasn't every been updated. * * @return bool */ public function current_user_can_upload_svg() { $upload_roles = get_option( 'safe_svg_upload_roles', [] );
// Fallback to upload_files check for backwards compatibility. if ( empty( $upload_roles ) ) { return current_user_can( 'upload_files' ); }
/** * Setup the blocks. */ public function setup_blocks() { // Setup blocks. Blocks\setup(); }
/** * Allow SVG Uploads * * @param array $mimes Mime types keyed by the file extension regex corresponding to those types. * * @return mixed */ public function allow_svg( $mimes ) { if ( $this->current_user_can_upload_svg() ) { $mimes['svg'] = 'image/svg+xml'; $mimes['svgz'] = 'image/svg+xml'; }
return $mimes; }
/** * Fixes the issue in WordPress 4.7.1 being unable to correctly identify SVGs * * @thanks @lewiscowles * * @param array $data Values for the extension, mime type, and corrected filename. * @param string $file Full path to the file. * @param string $filename The name of the file. * @param string[] $mimes Array of mime types keyed by their file extension regex. * * @return null */ public function fix_mime_type_svg( $data = null, $file = null, $filename = null, $mimes = null ) { $ext = isset( $data['ext'] ) ? $data['ext'] : ''; if ( strlen( $ext ) < 1 ) { $exploded = explode( '.', $filename ); $ext = strtolower( end( $exploded ) ); } if ( 'svg' === $ext ) { $data['type'] = 'image/svg+xml'; $data['ext'] = 'svg'; } elseif ( 'svgz' === $ext ) { $data['type'] = 'image/svg+xml'; $data['ext'] = 'svgz'; }
return $data; }
/** * Check if the file is an SVG, if so handle appropriately * * @param array $file An array of data for a single file. * * @return mixed */ public function check_for_svg( $file ) {
// Ensure we have a proper file path before processing if ( ! isset( $file['tmp_name'] ) ) { return $file; }
if ( 'image/svg+xml' === $type ) { if ( ! $this->current_user_can_upload_svg() ) { $file['error'] = __( 'Sorry, you are not allowed to upload SVG files.', 'safe-svg' );
return $file; }
if ( ! $this->sanitize( $file['tmp_name'] ) ) { $file['error'] = __( "Sorry, this file couldn't be sanitized so for security reasons wasn't uploaded", 'safe-svg' ); } }
// Is the SVG gzipped? If so we try and decode the string $is_zipped = $this->is_gzipped( $dirty ); if ( $is_zipped ) { $dirty = gzdecode( $dirty );
// If decoding fails, bail as we're not secure if ( false === $dirty ) { return false; } }
/** * Load extra filters to allow devs to access the safe tags and attrs by themselves. */ $this->sanitizer->setAllowedTags( new SafeSvgTags\safe_svg_tags() ); $this->sanitizer->setAllowedAttrs( new SafeSvgAttr\safe_svg_attributes() );
$clean = $this->sanitizer->sanitize( $dirty );
if ( false === $clean ) { return false; }
// If we were gzipped, we need to re-zip if ( $is_zipped ) { $clean = gzencode( $clean ); }
/** * Filters the attachment data prepared for JavaScript to add the sizes array to the response * * @param array $response Array of prepared attachment data. * @param int|object $attachment Attachment ID or object. * @param array $meta Array of attachment meta data. * * @return array */ public function fix_admin_preview( $response, $attachment, $meta ) {
/** * Filters the image src result. * If the image size doesn't exist, set a default size of 100 for width and height * * @param array|false $image Either array with src, width & height, icon src, or false. * @param int $attachment_id Image attachment ID. * @param string|array $size Size of image. Image size or array of width and height values * (in that order). Default 'thumbnail'. * @param bool $icon Whether the image should be treated as an icon. Default false. * * @return array */ public function one_pixel_fix( $image, $attachment_id, $size, $icon ) { if ( get_post_mime_type( $attachment_id ) === 'image/svg+xml' ) { $dimensions = $this->svg_dimensions( get_attached_file( $attachment_id ) );
/** * If the featured image is an SVG we wrap it in an SVG class so we can apply our CSS fix. * * @param string $content Admin post thumbnail HTML markup. * @param int $post_id Post ID. * @param int $thumbnail_id Thumbnail ID. * * @return string */ public function featured_image_fix( $content, $post_id, $thumbnail_id ) { $mime = get_post_mime_type( $thumbnail_id );
/** * Override the default height and width string on an SVG * * @param string $html HTML content for the image. * @param int $id Attachment ID. * @param string $alt Alternate text. * @param string $title Attachment title. * @param string $align Part of the class name for aligning the image. * @param string|array $size Size of image. Image size or array of width and height values (in that order). * Default 'medium'. * * @return mixed */ public function get_image_tag_override( $html, $id, $alt, $title, $align, $size ) { $mime = get_post_mime_type( $id );
// Might come handy to create the sizes array too - But it's not needed for this workaround! Always links to original svg-file => Hey, it's a vector graphic! ;) $sizes = array(); foreach ( get_intermediate_image_sizes() as $s ) { $sizes[ $s ] = array( 'width' => '', 'height' => '', 'crop' => false, );
if ( isset( $additional_image_sizes[ $s ]['width'] ) ) { // For theme-added sizes $sizes[ $s ]['width'] = intval( $additional_image_sizes[ $s ]['width'] ); } else { // For default sizes set in options $sizes[ $s ]['width'] = get_option( "{$s}_size_w" ); }
if ( isset( $additional_image_sizes[ $s ]['height'] ) ) { // For theme-added sizes $sizes[ $s ]['height'] = intval( $additional_image_sizes[ $s ]['height'] ); } else { // For default sizes set in options $sizes[ $s ]['height'] = get_option( "{$s}_size_h" ); }
if ( isset( $additional_image_sizes[ $s ]['crop'] ) ) { // For theme-added sizes $sizes[ $s ]['crop'] = intval( $additional_image_sizes[ $s ]['crop'] ); } else { // For default sizes set in options $sizes[ $s ]['crop'] = get_option( "{$s}_crop" ); }
/** * Filters the attachment meta data. * * @param array|bool $data Array of meta data for the given attachment, or false * if the object does not exist. * @param int $post_id Attachment ID. */ public function metadata_error_fix( $data, $post_id ) {
// If it's a WP_Error regenerate metadata and save it if ( is_wp_error( $data ) ) { $data = wp_generate_attachment_metadata( $post_id, get_attached_file( $post_id ) ); wp_update_attachment_metadata( $post_id, $data ); }
return $data; }
/** * Get SVG size from the width/height or viewport. * * @param string|false $svg The file path to where the SVG file should be, false otherwise. * * @return array|bool */ protected function svg_dimensions( $svg ) { if ( ! function_exists( 'simplexml_load_file' ) ) { return false; }
/** * Decide which attributes of the SVG we use first for image tag dimensions. * * We default to using the parameters in the viewbox attribute but * that can be overridden using this filter if you'd prefer to use * the width and height attributes. * * @hook safe_svg_use_width_height_attributes * * @param bool $use_width_height_attributes If the width & height attributes should be used first. Default false. * @param string $svg The file path to the SVG. * * @return bool If we should use the width & height attributes first or not. */ $use_width_height = (bool) apply_filters( 'safe_svg_use_width_height_attributes', false, $svg );
/** * Disable the creation of srcset on SVG images. * * @param array $image_meta The image meta data. * @param int[] $size_array { * An array of requested width and height values. * * @type int $0 The width in pixels. * @type int $1 The height in pixels. * } * @param string $image_src The 'src' of the image. * @param int $attachment_id The image attachment ID. */ public function disable_srcset( $image_meta, $size_array, $image_src, $attachment_id ) { if ( $attachment_id && 'image/svg+xml' === get_post_mime_type( $attachment_id ) ) { $image_meta['sizes'] = array(); }
return $image_meta; }
/** * Polyfill for `str_ends_with()` function added in PHP 8.0. * * Performs a case-sensitive check indicating if * the haystack ends with needle. * * @param string $haystack The string to search in. * @param string $needle The substring to search for in the `$haystack`. * @return bool True if `$haystack` ends with `$needle`, otherwise false. */ protected function str_ends_with( $haystack, $needle ) { if ( function_exists( 'str_ends_with' ) ) { return str_ends_with( $haystack, $needle ); }