PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.15.1
Import WP – CSV & XML Import Export for WordPress v2.15.1
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 / Preview / JSONPreview.php

JSONPreview.php in Import WP – CSV & XML Import Export for WordPress 2.15.1, at class/Common/Importer/Preview/JSONPreview.php

164 lines 4.2 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\Preview;
4
5 use ImportWP\Common\Importer\File\JSONFile;
6 use ImportWP\Common\Importer\PreviewInterface;
7
8 class JSONPreview implements PreviewInterface
9 {
10 /**
11 * @var JSONFile
12 */
13 private $file;
14
15 /**
16 * @var string
17 */
18 private $record_path;
19
20 /**
21 * @param JSONFile $file
22 * @param string $record_path
23 */
24 public function __construct(JSONFile $file, $record_path = '/')
25 {
26 $this->file = $file;
27 $this->record_path = $record_path;
28 // Do not enable processing mode — preview navigation needs the full record index.
29 }
30
31 public function output()
32 {
33 $data = $this->data();
34 return wp_json_encode($data);
35 }
36
37 /**
38 * Build a preview tree for a record under the given path.
39 *
40 * Shape mirrors XMLPreview nodes so the React tree can reuse similar rendering:
41 * [ { node, xpath, attr, value } ]
42 *
43 * @param int $record_index
44 * @return array
45 */
46 public function data($record_index = 0)
47 {
48 $this->file->setRecordPath($this->record_path);
49 $record_index = max(0, intval($record_index));
50
51 // Prefer stream-indexed record so any preview index can be loaded.
52 if ($this->file->getRecordCount() > $record_index) {
53 $raw = $this->file->getRecord($record_index);
54 $decoded = json_decode($raw, true);
55 if (is_array($decoded)) {
56 return [[
57 'node' => 'record',
58 'xpath' => '',
59 'attr' => [],
60 'value' => $this->buildNodes($decoded, ''),
61 ]];
62 }
63 }
64
65 // Fallback to decoded JSON for the first record — avoids sticky/empty stream indexes.
66 if ($record_index === 0) {
67 $records = $this->file->getDecodedRecords(1);
68 if (!empty($records) && is_array($records[0])) {
69 return [[
70 'node' => 'record',
71 'xpath' => '',
72 'attr' => [],
73 'value' => $this->buildNodes($records[0], ''),
74 ]];
75 }
76 }
77
78 return [[
79 'node' => 'record',
80 'xpath' => '',
81 'attr' => [],
82 'value' => [],
83 ]];
84 }
85
86 /**
87 * @param mixed $data
88 * @param string $path
89 * @return array
90 */
91 private function buildNodes($data, $path)
92 {
93 if (!is_array($data)) {
94 return [[
95 'node' => 'text',
96 'xpath' => $path === '' ? '/' : $path,
97 'value' => $this->stringify($data),
98 'type' => 'text',
99 'attr' => [],
100 ]];
101 }
102
103 $nodes = [];
104
105 if ($this->isList($data)) {
106 foreach ($data as $index => $item) {
107 $child_path = $path . '/' . $index;
108 $nodes[] = [
109 'node' => (string) $index,
110 'xpath' => $child_path,
111 'attr' => [],
112 'value' => is_array($item)
113 ? $this->buildNodes($item, $child_path)
114 : $this->stringify($item),
115 ];
116 }
117 return $nodes;
118 }
119
120 foreach ($data as $key => $value) {
121 $child_path = $path . '/' . $key;
122 $nodes[] = [
123 'node' => (string) $key,
124 'xpath' => $child_path,
125 'attr' => [],
126 'value' => is_array($value)
127 ? $this->buildNodes($value, $child_path)
128 : $this->stringify($value),
129 ];
130 }
131
132 return $nodes;
133 }
134
135 /**
136 * @param mixed $value
137 * @return string
138 */
139 private function stringify($value)
140 {
141 if ($value === null) {
142 return '';
143 }
144 if (is_bool($value)) {
145 return $value ? 'true' : 'false';
146 }
147
148 return (string) $value;
149 }
150
151 /**
152 * @param array $data
153 * @return bool
154 */
155 private function isList(array $data)
156 {
157 if ($data === []) {
158 return true;
159 }
160
161 return array_keys($data) === range(0, count($data) - 1);
162 }
163 }
164