self::MAX_BYTES ) { $file['error'] = __( 'This SVG is too large for an icon (500 KB maximum).', 'karma-docs' ); return $file; } $clean = self::sanitize( (string) file_get_contents( $file['tmp_name'] ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents if ( '' === $clean ) { $file['error'] = __( 'This SVG could not be made safe, so it was not uploaded.', 'karma-docs' ); return $file; } file_put_contents( $file['tmp_name'], $clean ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents $file['size'] = strlen( $clean ); return $file; } /** * Cleans SVG markup with the allow-list sanitizer. * * @param string $svg Markup. * @return string Clean markup, or '' when it isn't a usable SVG. */ public static function sanitize( $svg ) { self::load_library(); if ( ! class_exists( '\enshrined\svgSanitize\Sanitizer' ) || '' === trim( $svg ) ) { return ''; } $sanitizer = new \enshrined\svgSanitize\Sanitizer(); $sanitizer->removeRemoteReferences( true ); $sanitizer->removeXMLTag( true ); $sanitizer->minify( true ); $clean = $sanitizer->sanitize( $svg ); if ( ! is_string( $clean ) || false === stripos( $clean, '