roles ); } /** * Add SVG to the allowed file types, for allowed users. * * @param array $mimes Extension regex => MIME type. * @return array */ public function allow_mime( $mimes ) { if ( self::user_allowed() ) { $mimes['svg'] = self::MIME; } return $mimes; } /** * WordPress double-checks a file's real type; SVG files read as text * on some servers, so confirm them here (the content itself is checked * by the sanitizer). * * @param array $data Detected ext, type, and proper filename. * @param string $file Full path to the file. * @param string $filename The file's name. * @param string[]|null $mimes Allowed MIME types. * @return array */ public function check_filetype( $data, $file, $filename, $mimes ) { if ( ! empty( $data['ext'] ) || 'svg' !== strtolower( pathinfo( (string) $filename, PATHINFO_EXTENSION ) ) ) { return $data; } if ( ! self::user_allowed() || ( is_array( $mimes ) && ! in_array( self::MIME, $mimes, true ) ) ) { return $data; } $data['ext'] = 'svg'; $data['type'] = self::MIME; return $data; } /** * Clean an uploaded SVG in place, or refuse it. * * @param array $file Upload ($_FILES entry). * @return array */ public function sanitize_upload( $file ) { $name = (string) ( $file['name'] ?? '' ); if ( ! empty( $file['error'] ) || 'svg' !== strtolower( pathinfo( $name, PATHINFO_EXTENSION ) ) ) { return $file; } if ( ! self::user_allowed() ) { $file['error'] = __( 'Sorry, you are not allowed to upload SVG files.', 'all-in-one-utilities' ); return $file; } $path = (string) ( $file['tmp_name'] ?? '' ); $raw = is_readable( $path ) ? (string) file_get_contents( $path ) : ''; // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- a local uploaded temp file. $clean = Sanitizer::sanitize( $raw ); if ( null === $clean ) { $file['error'] = __( 'This SVG file could not be read safely, so it was not uploaded. Try saving it again as a plain SVG from your design program.', 'all-in-one-utilities' ); return $file; } file_put_contents( $path, $clean ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents -- rewriting the upload's own temp file before WordPress moves it. $file['size'] = strlen( $clean ); return $file; } /** * Width and height from an SVG's attributes or viewBox. * * @param string $path File path. * @return array{0:int,1:int}|null */ public static function dimensions( string $path ): ?array { if ( ! is_readable( $path ) ) { return null; } $svg = (string) file_get_contents( $path, false, null, 0, 4096 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- a local attachment file; only the opening tag is needed. if ( ! preg_match( '/]*>/is', $svg, $tag ) ) { return null; } $attr = static function ( string $name ) use ( $tag ): string { return preg_match( '/\s' . $name . '\s*=\s*(["\'])(.*?)\1/is', $tag[0], $m ) ? trim( $m[2] ) : ''; }; $width = (float) $attr( 'width' ); $height = (float) $attr( 'height' ); // Percent or missing sizes: use the viewBox. if ( ( $width <= 0 || $height <= 0 || str_contains( $attr( 'width' ) . $attr( 'height' ), '%' ) ) && preg_match( '/^[-\d.e]+[\s,]+[-\d.e]+[\s,]+([\d.e]+)[\s,]+([\d.e]+)$/i', $attr( 'viewBox' ), $box ) ) { $width = (float) $box[1]; $height = (float) $box[2]; } return $width > 0 && $height > 0 ? array( (int) round( $width ), (int) round( $height ) ) : null; } /** * Store an SVG's dimensions in its attachment metadata. * * @param array $metadata Metadata. * @param int $attachment_id Attachment ID. * @return array */ public function add_dimensions( $metadata, $attachment_id ) { if ( self::MIME !== get_post_mime_type( $attachment_id ) ) { return $metadata; } $path = (string) get_attached_file( $attachment_id ); $size = self::dimensions( $path ); $metadata = (array) $metadata; $metadata['file'] = _wp_relative_upload_path( $path ); $metadata['width'] = $size ? $size[0] : 0; $metadata['height'] = $size ? $size[1] : 0; $metadata['sizes'] = array(); return $metadata; } /** * SVGs have no resized copies; report the real dimensions instead of * WordPress's 1×1 fallback. * * @param array|false $image [ url, width, height, is_intermediate ]. * @param int $attachment_id Attachment ID. * @return array|false */ public function image_src_dimensions( $image, $attachment_id ) { if ( ! is_array( $image ) || self::MIME !== get_post_mime_type( $attachment_id ) ) { return $image; } $meta = wp_get_attachment_metadata( $attachment_id ); if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) { $image[1] = (int) $meta['width']; $image[2] = (int) $meta['height']; } return $image; } /** * Give the Media Library a preview and sizes for SVGs. * * @param array $response Attachment data for JS. * @param \WP_Post $attachment Attachment. * @return array */ public function prepare_for_js( $response, $attachment ) { if ( self::MIME !== ( $response['mime'] ?? '' ) ) { return $response; } $meta = wp_get_attachment_metadata( $attachment->ID ); $width = (int) ( $meta['width'] ?? 0 ); $height = (int) ( $meta['height'] ?? 0 ); $url = (string) ( $response['url'] ?? '' ); $size = array( 'url' => $url, 'width' => $width, 'height' => $height, 'orientation' => $height > $width ? 'portrait' : 'landscape', ); $response['image'] = array( 'src' => $url, 'width' => $width, 'height' => $height, ); $response['width'] = $width; $response['height'] = $height; $response['sizes'] = array( 'full' => $size, 'medium' => $size, 'thumbnail' => $size, ); return $response; } /** * SVG previews in wp-admin have no intrinsic size; let them fill * their box. * * @return void */ public function admin_css(): void { echo ''; } }