PluginProbe
Import WP – CSV & XML Import Export for WordPress / trunk
Import WP – CSV & XML Import Export for WordPress vtrunk
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 trunk 0.1.6 All 142 releases
jc-importer / class / Common / Exporter / File / JSONFile.php

JSONFile.php in Import WP – CSV & XML Import Export for WordPress trunk, at class/Common/Exporter/File/JSONFile.php

144 lines 4.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\Exporter\File;
4
5 class JSONFile extends File
6 {
7 private $counter = 0;
8 private $parts = [
9 'open' => '{' . PHP_EOL,
10 'close' => PHP_EOL . '}',
11 'parent' => 0
12 ];
13 private $setup = false;
14
15 public function start()
16 {
17 // write headers
18
19 // TODO: get the opening posts loop wrappers
20 $fields = $this->exporter->getFields();
21 $tmp = $this->generate_wrapper($fields);
22
23 foreach ($tmp as $i => $t) {
24 $is_last = count($tmp) - 1 == $i;
25 $open_tag = $is_last ? '[' : '{';
26 $close_tag = $is_last ? ']' : '}';
27
28 $this->parts['open'] .= sprintf('"%s" : %s', $t, $open_tag) . PHP_EOL;
29 $this->parts['close'] = PHP_EOL . sprintf('%s', $close_tag) . $this->parts['close'];
30 }
31
32 fputs($this->fh, $this->parts['open'] . PHP_EOL);
33 $this->counter = 0;
34
35 update_option('iwp_exporter_json_config', [
36 'parts' => $this->parts
37 ]);
38 }
39
40 public function loadConfig()
41 {
42 if (!$this->setup) {
43 $config = get_option('iwp_exporter_json_config', []);
44 $this->parts = $config['parts'];
45 $this->setup = true;
46 }
47 }
48
49 public function generate_wrapper($fields, $output = [], $parent = 0, $depth = 0)
50 {
51
52 $current_items = array_filter($fields, function ($item) use ($parent) {
53 return intval($item['parent']) == $parent;
54 });
55
56 if (!empty($current_items)) {
57 foreach ($current_items as $current_item) {
58 $label = $this->getFieldLabel($current_item);
59
60 if (isset($current_item['loop']) && ($current_item['loop'] === 'true' || $current_item['loop'] === true)) {
61 $output[] = $label;
62 $this->parts['parent'] = $current_item['id'];
63 break;
64 } else {
65 $output[] = $label;
66 $output = $this->generate_wrapper($fields, $output, $current_item['id'], $depth + 1);
67 }
68 }
69 }
70
71 return $output;
72 }
73
74 public function add($mapper)
75 {
76 $this->loadConfig();
77
78 // $data = array_map(function($value){
79 // if(is_array($value)){
80 // $value = implode('|', $value);
81 // }
82 // return $value;
83 // }, $data);
84
85 if ($this->counter > 0) {
86 fputs($this->fh, "," . PHP_EOL);
87 }
88
89 $fields = $this->exporter->getFields();
90 fputs($this->fh, "\t" . json_encode($this->generate_schema($fields, $mapper, $this->parts['parent'])));
91 $this->counter++;
92 }
93
94 public function end()
95 {
96 $this->loadConfig();
97
98 fputs($this->fh, PHP_EOL . $this->parts['close']);
99 fclose($this->fh);
100 }
101
102 public function get_file_name()
103 {
104 return $this->exporter->getId() . '.json';
105 }
106
107 public function generate_schema($fields, $mapper, $parent = 0, $loop_args = [], $depth = 0, $data = null)
108 {
109 $output = [];
110
111 $current_items = array_filter($fields, function ($item) use ($parent) {
112 return intval($item['parent']) == $parent;
113 });
114
115 if (!empty($current_items)) {
116 foreach ($current_items as $current_item) {
117 $label = $this->getFieldLabel($current_item);
118
119 if (isset($current_item['loop']) && ($current_item['loop'] === 'true' || $current_item['loop'] === true)) {
120
121 $loop_args = ['loop' => $current_item['selection']];
122
123 $loop_data = $mapper->data($loop_args);
124 $output[$label] = [];
125
126 // We are in a loop
127 foreach ($loop_data as $data) {
128 $output[$label][] = $this->generate_schema($fields, $mapper, $current_item['id'], $loop_args, $depth + 1, $data);
129 }
130 } else {
131
132 $output[$label] = $this->generate_schema($fields, $mapper, $current_item['id'], $loop_args, $depth + 1, $data);
133 if (empty($output[$label])) {
134
135 $output[$label] = $mapper->get_value(isset($current_item['selection']) ? $current_item['selection'] : '', $data);
136 }
137 }
138 }
139 }
140
141 return $output;
142 }
143 }
144