PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.7.1
Import WP – CSV & XML Import Export for WordPress v2.7.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 / Exporter / File / JSONFile.php

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

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