PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.15.0
Import WP – CSV & XML Import Export for WordPress v2.15.0
2.15.1 2.15.0 2.14.24 2.14.23 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 2.9.1 All 144 releases
jc-importer / class / Common / Importer / Parser / XMLParser.php

XMLParser.php in Import WP – CSV & XML Import Export for WordPress 2.15.0, at class/Common/Importer/Parser/XMLParser.php

167 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImportWP\Common\Importer\Parser;
4
5 use ImportWP\Common\Importer\ParserInterface;
6
7 class XMLParser extends AbstractParser implements ParserInterface
8 {
9 /**
10 * XML Record
11 *
12 * @var \SimpleXMLElement
13 */
14 private $record_xml;
15
16 /**
17 * Base path to alter query
18 *
19 * @var string
20 */
21 private $query_base;
22
23 /**
24 * Query Group of XML Data
25 *
26 * @param $group
27 *
28 * @return array
29 */
30 public function queryGroup($group)
31 {
32 if (isset($group['base'])) {
33
34 $output = [];
35 $sub_records = $this->query($group['base'], false);
36 $this->setQueryBase($group['base']);
37
38 if (!empty($sub_records)) {
39 foreach ($sub_records as $record) {
40 $this->record_xml = $record;
41 $output[] = parent::queryGroup($group);
42 }
43 }
44
45 $this->resetQueryBase();
46 } else {
47 $output = parent::queryGroup($group);
48 }
49
50 return $output;
51 }
52
53 /**
54 * Run XML Query
55 *
56 * @param $query
57 *
58 * @param bool $as_string
59 *
60 * @return bool|\SimpleXMLElement[]
61 */
62 public function query($query, $as_string = true)
63 {
64 $xpath_prefix = "/record/*";
65 if ($this->getQueryBase() !== "") {
66 $xpath_prefix = '.';
67 }
68
69 if (false === $this->record_xml) {
70 return false;
71 }
72
73 $results = $this->record_xml->xpath($xpath_prefix . $query);
74
75 // make sure the result has records
76 if (is_array($results) && count($results) > 0) {
77 if ($as_string) {
78 $temp = [];
79 foreach ($results as $result) {
80 $temp[] = (string) $result;
81 }
82
83 return implode(',', $temp);
84 }
85
86 return $results;
87 }
88
89 return false;
90 }
91
92 /**
93 * Get Query Base
94 *
95 * @return string
96 */
97 private function getQueryBase()
98 {
99 if ($this->query_base) {
100 return $this->query_base;
101 }
102
103 return '';
104 }
105
106 /**
107 * Set Query Base
108 *
109 * @param string $base
110 */
111 private function setQueryBase($base)
112 {
113 $this->query_base = $base;
114 }
115
116 /**
117 * Reset Query Base
118 */
119 private function resetQueryBase()
120 {
121 $this->query_base = null;
122 $this->onRecordLoaded();
123 }
124
125 /**
126 * Load record string into XML format
127 */
128 protected function onRecordLoaded()
129 {
130 $this->record_xml = simplexml_load_string($this->record);
131 }
132
133 public function record()
134 {
135 return $this->record_xml;
136 }
137
138 /**
139 * Return list of SimpleXMLElements
140 *
141 * @param string $sub_path
142 * @param \SimpleXMLElement $record_xml
143 * @return \SimpleXMLElement[]
144 */
145 public function getSubRecords($sub_path, $record_xml = null)
146 {
147 $xpath_prefix = '.';
148
149 if ($record_xml === null) {
150 $record_xml = $this->record_xml;
151 $xpath_prefix = "/record/*";
152 }
153
154 return $record_xml->xpath($xpath_prefix . $sub_path);
155 }
156
157 public function getString($query, $record_xml = null)
158 {
159 $result = $this->getSubRecords($query, $record_xml);
160 $output = [];
161 foreach ($result as $a) {
162 $output[] = (string) $a;
163 }
164 return implode(',', $output);
165 }
166 }
167