PluginProbe ʕ •ᴥ•ʔ
WP All Import – Drag & Drop Import for CSV, XML, Excel & Google Sheets / 4.1.1
WP All Import – Drag & Drop Import for CSV, XML, Excel & Google Sheets v4.1.1
4.1.1 3.9.5 3.9.6 4.0.0 4.0.1 4.1.0 trunk 2.12 2.13 2.14 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8 3.4.9 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 3.5.5 3.5.6 3.5.7 3.5.8 3.5.9 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.6.9 3.7.0 3.7.1 3.7.2 3.7.3 3.7.3-beta-1.0 3.7.4 3.7.4-beta-1.0 3.7.5 3.7.6 3.7.7 3.7.8 3.7.9 3.8.0 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4
wp-all-import / libraries / XmlImportParser.php
wp-all-import / libraries Last commit date
ast 1 month ago XmlImportConfig.php 1 month ago XmlImportCsvParse.php 1 month ago XmlImportException.php 1 month ago XmlImportParser.php 1 month ago XmlImportReaderInterface.php 1 month ago XmlImportSQLParse.php 1 month ago XmlImportStringReader.php 1 month ago XmlImportTemplate.php 1 month ago XmlImportTemplateCodeGenerator.php 1 month ago XmlImportTemplateParser.php 1 month ago XmlImportTemplateScanner.php 3 days ago XmlImportToken.php 1 month ago XmlImportXLSParse.php 1 month ago wpaipclzip.lib.php 1 month ago
XmlImportParser.php
116 lines
1 <?php
2 // phpcs:disable WordPress.NamingConventions.PrefixAllGlobals
3 /**
4 * @author Olexandr Zanichkovsky <olexandr.zanichkovsky@zophiatech.com>
5 * @author Pavel Kulbakin <p.kulbakin@gmail.com>
6 * @package General
7 */
8
9 require_once dirname(__FILE__) . '/XmlImportTemplate.php';
10
11 /**
12 * Is used to parse XML using specified template and root node
13 */
14 class XmlImportParser {
15 /**
16 * SimpleXmlElement instance for the xml file
17 *
18 * @var SimpleXMLElement
19 */
20 protected $xml;
21
22 /**
23 * XPath expression for selecting the root node of the record
24 *
25 * @var string
26 */
27 protected $rootNodeXPath;
28
29 /**
30 * Path to cached template
31 *
32 * @var string
33 */
34 protected $cachedTemplate;
35
36 /**
37 * Creates new parser instance
38 *
39 * @param string $xml
40 * @param string $rootNodeXPath XPath for the record root node
41 * @param string $cachedTemplate path to cached template
42 * @param bool $isURL whether xml is URL or path
43 */
44 public function __construct($xml, $rootNodeXPath, $cachedTemplate, $isURL = false)
45 {
46 if ($isURL) {
47 $xml = file_get_contents($xml);
48 }
49
50 // FIX: remove default namespace because libxml xpath implementation doesn't handle it properly
51 $xml and $xml = preg_replace('%xmlns\s*=\s*([\'"]).*\1%sU', '', $xml);
52
53 libxml_use_internal_errors(true);
54 try{
55 $this->xml = new SimpleXMLElement($xml);
56 } catch (Exception $e){
57 try{
58 $this->xml = new SimpleXMLElement(mb_convert_encoding( $xml, 'UTF-8', 'ISO-8859-1' ));
59 } catch (Exception $e){
60 throw new XmlImportException(esc_html($e->getMessage()));
61 }
62 }
63
64 $this->rootNodeXPath = $rootNodeXPath;
65 $this->cachedTemplate = $cachedTemplate;
66 }
67
68 /**
69 * Gets the parser results for all records or
70 * the part of them if $start and $count parameters are passed
71 *
72 * @param array[optional] $records Sequence numbers of records to import (first record corresponds to 1)
73 * @return array
74 */
75 public function parse($records = array())
76 {
77 empty($records) or is_array($records) or $records = array($records);
78
79 $result = array();
80
81 $rootNodes = $this->xml->xpath($this->rootNodeXPath);
82 if ($rootNodes === false)
83 throw new XmlImportException(esc_html('Invalid root node XPath \'' . $this->rootNodeXPath . '\' specified'));
84
85 for ($i = 0; $i < count($rootNodes); $i++) {
86 if (empty($records) or in_array($i + 1, $records)) {
87 $rootNode = apply_filters('wpallimport_xml_row', $rootNodes[$i]);
88 $template = new XmlImportTemplate($rootNode, $this->cachedTemplate);
89 $result[] = $template->parse();
90 }
91 }
92 return $result;
93 }
94
95 /**
96 * Creates new parser instance for text template specified
97 *
98 * @param string $xml
99 * @param string $rootNodeXPath XPath for the record root node
100 * @param string $template template
101 * @param string &$file path to the cached template
102 * @return XmlImportParser
103 */
104 public static function factory($xml, $rootNodeXPath, $template, &$file = NULL)
105 {
106
107 $scanner = new XmlImportTemplateScanner();
108 $tokens = $scanner->scan(new XmlImportStringReader($template));
109 $t_parser = new XmlImportTemplateParser($tokens);
110 $tree = $t_parser->parse();
111 $codegenerator = new XmlImportTemplateCodeGenerator($tree);
112 $file = $codegenerator->generate();
113
114 return new self($xml, $rootNodeXPath, $file);
115 }
116 }