PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.9.0
Import WP – CSV & XML Import Export for WordPress v2.9.0
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.9.0, at class/Common/Addon/AddonBasePanel.php

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