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 / Addon / AddonBasePanel.php

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

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