rule may set. Every other declaration is dropped. */ const STYLE_PROPERTIES = array( 'fill', 'stroke', 'stroke-width', 'stroke-linecap', 'stroke-linejoin', 'stroke-dasharray', 'stroke-opacity', 'fill-opacity', 'fill-rule', 'opacity', 'stop-color', 'stop-opacity', 'font-family', 'font-size', 'font-weight', 'font-style', 'text-anchor', 'letter-spacing', 'display', 'visibility', 'transform', 'clip-rule', 'mix-blend-mode', 'dominant-baseline', 'marker-start', 'marker-mid', 'marker-end', ); /** * Clean SVG markup. * * @param string $xml SVG markup. * @return string|\WP_Error Clean markup with an XML declaration, or an error. */ public static function sanitize( $xml ) { $error = new \WP_Error( 'svg_rejected', __( 'The vector image could not be checked safely, so it was not saved.', 'ai-featured-image-generator' ) ); $xml = is_string( $xml ) ? trim( $xml ) : ''; if ( '' === $xml || ! class_exists( 'DOMDocument' ) ) { return $error; } // A DOCTYPE can declare entities; a stylesheet instruction can load outside files. if ( preg_match( '/loadXML( $xml, LIBXML_NONET ); libxml_clear_errors(); libxml_use_internal_errors( $previous ); if ( ! $loaded || ! $doc->documentElement || 'svg' !== strtolower( $doc->documentElement->localName ) ) { return $error; } // The root must be an SVG element. Without any namespace it is given the SVG one. $root = $doc->documentElement; if ( null === $root->namespaceURI || '' === $root->namespaceURI ) { $root->setAttributeNS( 'http://www.w3.org/2000/xmlns/', 'xmlns', self::SVG_NS ); } elseif ( self::SVG_NS !== $root->namespaceURI ) { return $error; } // Processing instructions other than the XML declaration (which DOM does not keep). $xpath = new \DOMXPath( $doc ); foreach ( $xpath->query( '//processing-instruction()' ) as $pi ) { $pi->parentNode->removeChild( $pi ); } foreach ( $xpath->query( '//comment()' ) as $comment ) { $comment->parentNode->removeChild( $comment ); } // Read the