sanitize_svg_content( $svg_content ); if ( $sanitized_content !== $svg_content ) { $data['error'] = 'SVG file contains disallowed content.'; } return $data; } /** * Sanitize SVG content to remove potentially harmful elements. * * @param string $content * @return string */ private function sanitize_svg_content( $content ) { if ( is_wp_error( $content ) ) { // Handle the error, for example, return an empty string or log the error return ''; // or use error_log($content->get_error_message()); } // Remove XML processing instructions $content = preg_replace( '/<\?xml.*?\?>/i', '', $content ); return $content; } /** * Modify MIME types to allow SVG uploads. * * @param array $existingMimes * @return array */ public function modify_svg_mimes( $existingMimes ) { $existingMimes['svg'] = 'image/svg+xml'; return $existingMimes; } /** * Flush rewrite rules. */ public function add_flush_rewrite_rules() { flush_rewrite_rules(); } /** * Filter user capabilities to allow SVG uploads. * * @param array $allcaps * @param array $cap * @param array $args * @return array */ public function filter_user_capabilities( $allcaps, $cap, $args ) { if ( $args[0] === 'upload_files' && isset( $allcaps['upload_files'] ) && ! isset( $allcaps['upload_svg'] ) ) { $allcaps['upload_svg'] = $allcaps['upload_files']; } return $allcaps; } /** * Add SVG support to media library. */ public function add_svg_support_style() { ?>