| 1 |
<?php |
| 2 |
namespace Templately\Modules\FullSiteImport\Parsers; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly. |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* WordPress extended RSS file parser implementations, |
| 10 |
* Originally made by WordPress part of WordPress/Importer. |
| 11 |
* https://plugins.trac.wordpress.org/browser/wordpress-importer/trunk/parsers/class-wxr-parser.php |
| 12 |
* |
| 13 |
* What was done (by Elementor): |
| 14 |
* Reformat of the code. |
| 15 |
* Changed text domain. |
| 16 |
*/ |
| 17 |
|
| 18 |
/** |
| 19 |
* WordPress Importer class for managing parsing of WXR files. |
| 20 |
*/ |
| 21 |
class WXR_Parser { |
| 22 |
|
| 23 |
public function parse( $file ) { |
| 24 |
// Attempt to use proper XML parsers first. |
| 25 |
// Note: WXR_Parser_SimpleXML relies on DOMDocument (the `dom` extension), |
| 26 |
// which is DISTINCT from `simplexml` — a server can have one without the |
| 27 |
// other. Require DOMDocument here so the chain falls through to the expat |
| 28 |
// (`xml`) / always-available regex parser instead of fataling. |
| 29 |
if ( extension_loaded( 'simplexml' ) && class_exists( 'DOMDocument' ) ) { |
| 30 |
$parser = new WXR_Parser_SimpleXML(); |
| 31 |
$result = $parser->parse( $file ); |
| 32 |
|
| 33 |
// If SimpleXML succeeds or this is an invalid WXR file then return the results. |
| 34 |
if ( ! is_wp_error( $result ) || 'SimpleXML_parse_error' != $result->get_error_code() ) { |
| 35 |
return $result; |
| 36 |
} |
| 37 |
} elseif ( extension_loaded( 'xml' ) ) { |
| 38 |
$parser = new WXR_Parser_XML(); |
| 39 |
$result = $parser->parse( $file ); |
| 40 |
|
| 41 |
// If XMLParser succeeds or this is an invalid WXR file then return the results. |
| 42 |
if ( ! is_wp_error( $result ) || 'XML_parse_error' != $result->get_error_code() ) { |
| 43 |
return $result; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
// Use regular expressions if nothing else available or this is bad XML. |
| 48 |
$parser = new WXR_Parser_Regex(); |
| 49 |
|
| 50 |
return $parser->parse( $file ); |
| 51 |
} |
| 52 |
} |
| 53 |
|