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 / AddonAPI / Importer / PanelData.php

PanelData.php in Import WP – CSV & XML Import Export for WordPress trunk, at class/Common/AddonAPI/Importer/PanelData.php

166 lines 5.2 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\AddonAPI\Importer;
4
5 use ImportWP\Common\AddonAPI\Importer\Template\Field;
6 use ImportWP\Common\AddonAPI\Importer\Template\FieldGroup;
7
8 class PanelData
9 {
10 /**
11 * @var \ImportWP\Common\AddonAPI\Importer\Template\Panel
12 */
13 private $_panel;
14
15 /**
16 * @var \ImportWP\Common\AddonAPI\Importer\ImporterData
17 */
18 private $_addon_data;
19
20 private $_fields = [];
21
22 /**
23 * @param \ImportWP\Common\AddonAPI\Importer\Template\Panel $panel
24 * @param \ImportWP\Common\AddonAPI\Importer\ImporterData $addon_data
25 * @return void
26 */
27 public function __construct($panel, $addon_data)
28 {
29 $this->_panel = $panel;
30 $this->_addon_data = $addon_data;
31 }
32
33 public function get_field_prefix()
34 {
35 return $this->_panel->get_id();
36 }
37
38 public function get_data_group_id()
39 {
40 return $this->_panel->get_id();
41 }
42
43 public function get_value($field_id = null)
44 {
45 $field_prefix = $this->get_field_prefix();
46 $group_id = $this->get_data_group_id();
47
48 // Check group permission
49 $permission_key = $group_id;
50 $allowed = $this->_addon_data->get_data()->permission()->validate([$permission_key => ''], $this->_addon_data->get_data()->getMethod(), $group_id);
51 $is_allowed = isset($allowed[$permission_key]) ? true : false;
52 if (!$is_allowed) {
53 return false;
54 }
55
56 if ($this->_panel->is_repeater()) {
57
58 $max_rows = intval($this->_addon_data->get_data()->getValue($field_prefix . '._index', $group_id));
59 $output = [];
60 for ($i = 0; $i < $max_rows; $i++) {
61 $row = [];
62 foreach ($this->_panel->get_fields() as $field) {
63
64 $args = [
65 'data_group' => $this->get_data_group_id(),
66 'field_prefix' => $this->get_field_prefix() . '.' . $i,
67 ];
68
69 if (is_a($field, \ImportWP\Common\AddonAPI\Importer\Template\Field::class)) {
70
71 $field_data = new FieldData($field, $this->_addon_data, $args);
72 $row[$field->get_id()] = $field_data->get_value();
73 } elseif (is_a($field, \ImportWP\Common\AddonAPI\Importer\Template\FieldGroup::class)) {
74 $field_group_data = new FieldGroupData($field, $this->_addon_data, $args);
75 $row[$field->get_id()] = $field_group_data->get_value();
76 }
77 }
78 $output[] = $row;
79 }
80
81 return $output;
82 } else {
83
84 $output = [];
85 foreach ($this->_panel->get_fields() as $field) {
86
87 // dont fetch data if we are only requesting a single element
88 if (!is_null($field_id) && $field->get_id() !== $field_id) {
89 continue;
90 }
91
92 $args = [
93 'data_group' => $this->get_data_group_id(),
94 'field_prefix' => $this->get_field_prefix(),
95 ];
96
97 if (is_a($field, \ImportWP\Common\AddonAPI\Importer\Template\Field::class)) {
98
99 $field_data = new FieldData($field, $this->_addon_data, $args);
100 $output[$field->get_id()] = $field_data->get_value();
101 } elseif (is_a($field, \ImportWP\Common\AddonAPI\Importer\Template\FieldGroup::class)) {
102
103 $field_group = new FieldGroupData($field, $this->_addon_data, $args);
104 $output[$field->get_id()] = $field_group->get_value();
105 }
106
107 if (!is_null($field_id)) {
108 return isset($output[$field_id]) ? $output[$field_id] : false;
109 }
110 }
111
112 return $output;
113 }
114
115 return false;
116 }
117
118 /**
119 * @param Field|FieldGroup $field
120 * @return FieldData|FieldGroupData
121 */
122 private function get_field_data($field)
123 {
124 $field_id = $field->get_id();
125 if (isset($this->_fields[$field_id])) {
126 return $this->_fields[$field_id];
127 }
128
129 if (is_a($field, Field::class)) {
130 $this->_fields[$field_id] = new FieldData($field, $this->_addon_data, [
131 'data_group' => $this->get_data_group_id(),
132 'field_prefix' => $this->get_field_prefix(),
133 ]);
134 } elseif (is_a($field, FieldGroup::class)) {
135 $this->_fields[$field_id] = new FieldGroupData($field, $this->_addon_data, [
136 'data_group' => $this->get_data_group_id(),
137 'field_prefix' => $this->get_field_prefix(),
138 ]);
139 }
140
141 return $this->_fields[$field_id];
142 }
143
144 public function get_field($field_id)
145 {
146 $current_field = null;
147 foreach ($this->_panel->get_fields() as $field) {
148 if ($field->get_id() === $field_id) {
149 $current_field = $field;
150 break;
151 }
152 }
153
154 if (!$current_field) {
155 throw new \Exception("Missing field: " . $field_id);
156 }
157
158 return $this->get_field_data($field);
159 }
160
161 public function log($field_id)
162 {
163 $this->_addon_data->log($field_id, $this->_panel->get_id());
164 }
165 }
166