PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.7.7
Import WP – CSV & XML Import Export for WordPress v2.7.7
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.7.7, at class/Common/Importer/Parser/XMLParser.php

163 lines 3.3 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 foreach ($sub_records as $record) {
39 $this->record_xml = $record;
40 $output[] = parent::queryGroup($group);
41 }
42
43 $this->resetQueryBase();
44 } else {
45 $output = parent::queryGroup($group);
46 }
47
48 return $output;
49 }
50
51 /**
52 * Run XML Query
53 *
54 * @param $query
55 *
56 * @param bool $as_string
57 *
58 * @return bool|\SimpleXMLElement[]
59 */
60 public function query($query, $as_string = true)
61 {
62 $xpath_prefix = "/record/*";
63 if ($this->getQueryBase() !== "") {
64 $xpath_prefix = '.';
65 }
66
67 if (false === $this->record_xml) {
68 return false;
69 }
70
71 $results = $this->record_xml->xpath($xpath_prefix . $query);
72 if (count($results) > 0) {
73 if ($as_string) {
74 $temp = [];
75 foreach ($results as $result) {
76 $temp[] = (string) $result;
77 }
78
79 return implode(',', $temp);
80 }
81
82 return $results;
83 }
84
85 return false;
86 }
87
88 /**
89 * Get Query Base
90 *
91 * @return string
92 */
93 private function getQueryBase()
94 {
95 if ($this->query_base) {
96 return $this->query_base;
97 }
98
99 return '';
100 }
101
102 /**
103 * Set Query Base
104 *
105 * @param string $base
106 */
107 private function setQueryBase($base)
108 {
109 $this->query_base = $base;
110 }
111
112 /**
113 * Reset Query Base
114 */
115 private function resetQueryBase()
116 {
117 $this->query_base = null;
118 $this->onRecordLoaded();
119 }
120
121 /**
122 * Load record string into XML format
123 */
124 protected function onRecordLoaded()
125 {
126 $this->record_xml = simplexml_load_string($this->record);
127 }
128
129 public function record()
130 {
131 return $this->record_xml;
132 }
133
134 /**
135 * Return list of SimpleXMLElements
136 *
137 * @param string $sub_path
138 * @param \SimpleXMLElement $record_xml
139 * @return \SimpleXMLElement[]
140 */
141 public function getSubRecords($sub_path, $record_xml = null)
142 {
143 $xpath_prefix = '.';
144
145 if ($record_xml === null) {
146 $record_xml = $this->record_xml;
147 $xpath_prefix = "/record/*";
148 }
149
150 return $record_xml->xpath($xpath_prefix . $sub_path);
151 }
152
153 public function getString($query, $record_xml = null)
154 {
155 $result = $this->getSubRecords($query, $record_xml);
156 $output = [];
157 foreach ($result as $a) {
158 $output[] = (string) $a;
159 }
160 return implode(',', $output);
161 }
162 }
163