PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.8.3
Import WP – CSV & XML Import Export for WordPress v2.8.3
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 / Addon / AddonBasePanel.php

AddonBasePanel.php in Import WP – CSV & XML Import Export for WordPress 2.8.3, at class/Common/Addon/AddonBasePanel.php

283 lines 8.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\Addon;
4
5 class AddonBasePanel extends AddonBaseContainer implements AddonPanelInterface
6 {
7 /**
8 * @var callable
9 */
10 protected $_process_callback;
11
12 /**
13 * @var string
14 */
15 protected $_id;
16
17 /**
18 * @var callable
19 */
20 protected $_register_callback;
21
22 /**
23 * Stores ParsedData only during panel process
24 *
25 * @var \ImportWP\Common\Importer\ParsedData
26 */
27 protected $_process_data;
28
29 public function __construct($addon, $callback, $id, $data)
30 {
31 $this->_id = $id;
32 $this->_register_callback = $callback;
33 parent::__construct($addon, $data);
34 }
35
36 public function _register($importer_model)
37 {
38 call_user_func_array($this->_register_callback, [$this, $importer_model]);
39 }
40
41 public function _trigger_process_callback($object_id, $data, $importer_model, $template)
42 {
43 if (!is_null($this->_process_callback) && is_callable($this->_process_callback)) {
44 call_user_func($this->_process_callback, new AddonPanelDataApi($this->addon(), $this, $object_id, $this->get_id(), $data, $importer_model, $template));
45 }
46 }
47
48 public function save($callback)
49 {
50 $this->_process_callback = $callback;
51 return $this;
52 }
53
54 public function get_id()
55 {
56 return $this->_id;
57 }
58
59 /**
60 * @return AddonBaseField[]
61 */
62 public function fields()
63 {
64 return isset($this->_data['fields']) ? $this->_data['fields'] : [];
65 }
66
67 public function settings()
68 {
69 return isset($this->_data['settings']) ? $this->_data['settings'] : [];
70 }
71
72 public function is_repeatable()
73 {
74 return isset($this->_data['settings'], $this->_data['settings']['type']) && $this->_data['settings']['type'] == 'repeatable';
75 }
76
77 /**
78 * @param \ImportWP\Common\Importer\ParsedData $data
79 * @param \ImportWP\Common\Model\ImporterModel $importer_model
80 *
81 * @return array
82 */
83 public function _pre_process($data, $importer_model)
84 {
85 $section_id = $this->get_id();
86 $group_fields = $this->fields();
87 $section_data = $data->getData($section_id);
88
89 if ($this->is_repeatable()) {
90 $output = $this->_pre_process_repeatable_fields($group_fields, $data, $section_id, $section_data);
91 } else {
92 $output = $this->_pre_process_fields($group_fields, $data, $section_id, $section_data, $importer_model);
93 }
94
95 return $output;
96 }
97
98 /**
99 * @param AddonBaseField[] $group_fields
100 * @param ParsedData $data
101 * @param string $section_id
102 * @param array $data
103 * @param \ImportWP\Common\Model\ImporterModel $importer_model
104 * @param array $output
105 *
106 * @return array
107 */
108 public function _pre_process_fields($group_fields, $data, $section_id, $section_data, $importer_model, $output = [], $enable_id = false)
109 {
110 if (empty($group_fields)) {
111 return $output;
112 }
113
114 foreach ($group_fields as $group_field) {
115
116 /**
117 * @var AddonBaseField $group_field
118 */
119
120 $field = $group_field->data();
121
122 // section.group_id or section.field_id
123 $field_enable_id = false === $enable_id ? "{$section_id}.{$field['id']}" : $enable_id;
124
125 if ($importer_model->isEnabledField($field_enable_id) || (isset($field['settings'], $field['settings']['core']) && $field['settings']['core'] === true)) {
126
127 // Mark field for processing
128 $group_field->_enable_processing();
129
130 if ($field['type'] === 'group') {
131
132 /**
133 * @var AddonBaseGroup $group_field
134 */
135 $output = $this->_pre_process_fields($group_field->fields(), $data, "{$section_id}.{$field['id']}", $section_data, $importer_model, $output, $field_enable_id);
136 } else {
137
138 foreach ($section_data as $field_id => $field_value) {
139 if (preg_match('/^' . str_replace('.', '\.', $section_id) . '\.(' . $field['id'] . '(\.\S+)?)$/', $field_id, $matches) !== 1) {
140 continue;
141 }
142 $output[$matches[1]] = $field_value;
143 }
144 }
145 }
146 }
147
148 return $output;
149 }
150
151 /**
152 * @param AddonBaseField[] $group_fields
153 * @param ParsedData $data
154 * @param string $section_id
155 * @param array $data
156 * @param \ImportWP\Common\Model\ImporterModel $importer_model
157 * @param array $output
158 *
159 * @return array
160 */
161 public function _pre_process_repeatable_fields($group_fields, $data, $section_id, $section_data)
162 {
163 $row_count = isset($section_data["{$section_id}._index"]) ? $section_data["{$section_id}._index"] : 0;
164
165 $rows = [];
166
167 for ($i = 0; $i < $row_count; $i++) {
168
169 $row = [];
170 $prefix = "{$section_id}.{$i}";
171
172 if (isset($section_data["{$prefix}.row_base"]) && !empty($section_data["{$prefix}.row_base"])) {
173 $sub_rows = $data->getData($prefix);
174 } else {
175 $sub_rows = [$section_data];
176 }
177
178 foreach ($sub_rows as $custom_field_row) {
179 $row = [];
180 foreach ($group_fields as $group_field) {
181
182 $field = $group_field->data();
183 $group_field->_enable_processing();
184
185 foreach ($custom_field_row as $field_id => $field_value) {
186 if (preg_match('/^' . str_replace('.', '\.', $prefix) . '\.(' . $field['id'] . '(\.\S+)?)$/', $field_id, $matches) !== 1) {
187 continue;
188 }
189
190 $row[$matches[1]] = $field_value;
191 }
192 }
193
194 $rows[] = $row;
195 }
196 }
197
198 return $rows;
199 }
200
201 /**
202 * @param integer $id
203 * @param \ImportWP\Common\Importer\ParsedData $data
204 * @param \ImportWP\Common\Model\ImporterModel $importer_model
205 * @param \ImportWP\Common\Importer\Template\Template $template
206 */
207 public function _process($id, $data, $importer_model, $template)
208 {
209 $section_id = $this->get_id();
210 $group_fields = $this->fields();
211 $repeatable = $this->is_repeatable();
212
213 $this->_process_data = $data;
214
215 $rows = $data->getData($section_id);
216 if ($repeatable) {
217 foreach ($rows as $i => $row) {
218 $this->_process_fields($id, $group_fields, $section_id, $row, $importer_model, $template, $i);
219 }
220 } else {
221 $this->_process_fields($id, $group_fields, $section_id, $rows, $importer_model, $template, false);
222 }
223
224 // check permissions before calling save
225 $permission_key = $section_id;
226 $allowed = $this->_process_data->permission()->validate([$permission_key => ''], $this->_process_data->getMethod(), $section_id);
227 $is_allowed = isset($allowed[$permission_key]) ? true : false;
228
229 if ($is_allowed) {
230 $this->_trigger_process_callback($id, $rows, $importer_model, $template);
231 }
232
233 $this->_process_data = null;
234 }
235
236 /**
237 * @param integer $id
238 * @param AddonBaseField[] $group_fields
239 * @param string $section_id
240 * @param mixed $data
241 * @param \ImportWP\Common\Model\ImporterModel $importer_model
242 * @param \ImportWP\Common\Importer\Template\Template $template
243 * @return void
244 */
245 public function _process_fields($id, $group_fields, $section_id, $data, $importer_model, $template, $i = false)
246 {
247 $group_fields = array_values(array_filter($group_fields, function ($item) use ($template, $importer_model) {
248 /**
249 * @var AddonBaseField $item
250 */
251 return $item->_is_allowed($template, $importer_model) && $item->_is_processing_enabled();
252 }));
253
254 foreach ($group_fields as $group_field) {
255
256 $field = $group_field->data();
257
258 if ($field['type'] === 'group') {
259 /**
260 * @var AddonBaseGroup $group_field
261 */
262 $this->_process_fields($id, $group_field->fields(), $section_id, $data, $importer_model, $template);
263 continue;
264 }
265
266 $permission_key = $section_id . '.' . $field['id'];
267 $allowed = $this->_process_data->permission()->validate([$permission_key => ''], $this->_process_data->getMethod(), $section_id);
268 $is_allowed = isset($allowed[$permission_key]) ? true : false;
269 if (!$is_allowed) {
270 continue;
271 }
272
273 $group_field->_process($this->addon(), $id, $section_id, $data, $importer_model, $template, $i);
274 }
275 }
276
277 public function clear()
278 {
279 // Removed due to clearing setitngs and other important data.
280 // unset($this->_data);
281 }
282 }
283