← All changes
|
modules/mcp/abilities/build-composition/xml-parser.php
+18
-2
4.3.0
→
4.3.3
View file →
| @@ -21,18 +21,34 @@ | ||
| 21 | 21 | libxml_clear_errors(); |
| 22 | 22 | libxml_use_internal_errors( $previous ); |
| 23 | 23 | |
| 24 | 24 | if ( ! $loaded ) { |
| 25 | - $message = $errors ? $errors[0]->message : 'Unknown XML error.'; | |
| 25 | + $message = $errors ? $this->describe_libxml_error( trim( $errors[0]->message ) ) : 'Unknown XML error.'; | |
| 26 | 26 | return new \WP_Error( |
| 27 | 27 | 'invalid_xml', |
| 28 | 28 | /* translators: %s: XML parse error message */ |
| 29 | - sprintf( __( 'Failed to parse xml_structure: %s', 'elementor' ), trim( $message ) ), | |
| 29 | + sprintf( __( 'Failed to parse xml_structure: %s', 'elementor' ), $message ), | |
| 30 | 30 | [ 'status' => \WP_Http::BAD_REQUEST ] |
| 31 | 31 | ); |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | return $dom; |
| 35 | + } | |
| 36 | + | |
| 37 | + private function describe_libxml_error( string $message ): string { | |
| 38 | + $root = preg_quote( self::COMPOSITION_ROOT_TAG, '/' ); | |
| 39 | + | |
| 40 | + if ( preg_match( '/tag mismatch: ' . $root . ' line \d+ and (\S+)$/', $message, $matches ) ) { | |
| 41 | + /* translators: %s: XML tag name */ | |
| 42 | + return sprintf( __( 'Closing tag </%s> has no matching opening tag.', 'elementor' ), $matches[1] ); | |
| 43 | + } | |
| 44 | + | |
| 45 | + if ( preg_match( '/tag mismatch: (\S+) line \d+ and ' . $root . '$/', $message, $matches ) ) { | |
| 46 | + /* translators: %s: XML tag name */ | |
| 47 | + return sprintf( __( 'Element <%s> is not closed.', 'elementor' ), $matches[1] ); | |
| 48 | + } | |
| 49 | + | |
| 50 | + return $message; | |
| 35 | 51 | } |
| 36 | 52 | |
| 37 | 53 | public function get_root( \DOMDocument $dom ): ?\DOMElement { |
| 38 | 54 | return $dom->documentElement ?? null; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- DOMDocument API. |