PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 3.1.14
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v3.1.14
3.1.14 3.1.13 3.1.12 3.1.11 3.1.10 3.1.9 3.1.8 3.1.7 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 All 123 releases
firebox / Inc / Framework / Inc / Base / Fields / Repeater.php

Repeater.php in FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment 3.1.14, at Inc/Framework/Inc/Base/Fields/Repeater.php

301 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package FirePlugins Framework
4 * @version 1.2.6
5 *
6 * @author FirePlugins <[email protected]>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2026 FirePlugins All Rights Reserved
9 * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
10 */
11
12 namespace FPFramework\Base\Fields;
13
14 if (!defined('ABSPATH'))
15 {
16 exit; // Exit if accessed directly.
17 }
18
19 use FPFramework\Base\Field;
20 use FPFramework\Libs\Registry;
21 use FPFramework\Helpers\FieldsHelper;
22 use FPFramework\Base\FieldsParser;
23
24 class Repeater extends Field
25 {
26 /**
27 * Set specific field options
28 *
29 * @param array $options
30 *
31 * @return void
32 */
33 protected function setFieldOptions($options)
34 {
35 $options = new Registry($options);
36
37 $btn_label = $options->get('btn_label');
38 $btn_label = $btn_label === false ? '' : (!$btn_label ? 'FPF_ADD_ITEM' : $btn_label);
39
40 $this->field_options = [
41 'render_group' => $options->get('render_group', false),
42 'default_values' => $options->get('default_values'),
43 'default_html' => $options->get('default_html'),
44 'fields' => $options->get('fields', []),
45 'remove_action_class' => $options->get('remove_action_class', []),
46 'actions_class' => $options->get('actions_class', []),
47 'actions_prepend' => $options->get('actions_prepend', ''),
48 'actions_append' => $options->get('actions_append', ''),
49 'repeater_item_class' => $options->get('repeater_item_class', []),
50 'btn_label' => $btn_label,
51 'btn_class' => $options->get('btn_class', [])
52 ];
53 }
54
55 /**
56 * Returns the Repeater Default HTML
57 *
58 * @return string
59 */
60 public function getRepeaterDefaultHTML()
61 {
62 if ($this->options['value'])
63 {
64 return;
65 }
66
67 if (!$this->options['default_values'])
68 {
69 return;
70 }
71
72 $fields_data = $this->getRepeaterFieldsData($this->options['default_values']);
73
74 return $this->getRepeaterFieldsHTML($fields_data);
75 }
76
77 /**
78 * Returns the Repeater HTML
79 *
80 * @return string
81 */
82 public function getRepeaterHTML()
83 {
84 $fields_data = $this->getRepeaterFieldsData($this->options['value']);
85
86 return $this->getRepeaterFieldsHTML($fields_data);
87 }
88
89 /**
90 * Returns all HTML of the repeater item fields
91 *
92 * @param array $fields_data
93 *
94 * @return string
95 */
96 private function getRepeaterFieldsHTML($fields_data)
97 {
98 $options = new Registry($this->options);
99
100 $fieldsParser = new FieldsParser([
101 'fields_name_prefix' => $this->options['name_prefix']
102 ]);
103
104 $repeater_html = [];
105
106 $name = $options->get('name_key', '');
107
108 $index = 1;
109 foreach ($fields_data as $key => $fields)
110 {
111 foreach ($fields as &$field)
112 {
113 if (!isset($field['name']))
114 {
115 continue;
116 }
117
118 // update name of each field with correct index
119 $field['name'] = $name . '.' . $index . '.' . $field['name'];
120 }
121
122 ob_start();
123 $fieldsParser->renderFields($fields);
124 $html = ob_get_contents();
125 ob_end_clean();
126
127 $repeater_html[] = $html;
128
129 $index++;
130 }
131
132 return $repeater_html;
133 }
134
135 /**
136 * Returns all field data for the repeater field
137 *
138 * @param array $data
139 *
140 * @return array
141 */
142 private function getRepeaterFieldsData($data)
143 {
144 if (empty($data))
145 {
146 return [];
147 }
148
149 $data = (array) $data;
150
151 if (empty($data))
152 {
153 return [];
154 }
155
156 $fields = $this->options['fields'];
157
158 $index = 1;
159
160 $fields_data = [];
161
162 $data = array_values($data);
163
164 foreach ($data as $_key => $_values)
165 {
166 $repeater_fields = [];
167 foreach ($fields as $key => $field)
168 {
169 $name = isset($field['name']) ? $field['name'] : '';
170
171 // set name and value
172 if (!empty($name))
173 {
174 $default = isset($field['default']) ? $field['default'] : null;
175
176 $field['value'] = FieldsHelper::findFieldValueInArray($name, $default, (array) $_values);
177 }
178
179 // add index to showon attribute
180 if (isset($field['showon']))
181 {
182 $showon = $field['showon'];
183 $showon = str_replace('ITEM_ID', $index, $showon);
184 $field['showon'] = $showon;
185 }
186
187 // remove no-render classes
188 if (isset($field['input_class']) && ($key = array_search('no-render', $field['input_class'])) !== false)
189 {
190 unset($field['input_class'][$key]);
191 }
192
193 $repeater_fields[] = $field;
194 }
195
196 $fields_data[] = $repeater_fields;
197 $index++;
198 }
199
200 return $fields_data;
201 }
202
203 /**
204 * Returns the repeater fields template
205 *
206 * @return string
207 */
208 private function getTemplate()
209 {
210 $options = new Registry($this->options);
211
212 $html = '';
213
214 $name = $options->get('name_key', '');
215 $name_prefix = $options->get('name_prefix', '');
216
217 $fields = $options->get('fields', []);
218
219 // update name of fields
220 foreach ($fields as &$field)
221 {
222 if (!isset($field['name']))
223 {
224 continue;
225 }
226
227 $field['name'] = $name . '.ITEM_ID.' . $field['name'];
228 }
229
230 $fieldsParser = new FieldsParser([
231 'fields_name_prefix' => $name_prefix
232 ]);
233
234 ob_start();
235 $fieldsParser->renderFields($fields);
236 $html = ob_get_contents();
237 ob_end_clean();
238
239 // add brackets between ITEM_ID in order to be able to update IDs for each repeater item via JS
240 $html = str_replace('.ITEM_ID.', '[ITEM_ID]', $html);
241
242 return $html;
243 }
244
245 /**
246 * Runs before field renders.
247 * Sets the value and template before rendering the field
248 *
249 * @return void
250 */
251 public function onBeforeRender()
252 {
253 // get the saved repeater fields html
254 $this->options['value'] = $this->getRepeaterHTML();
255
256 $this->options['default_html'] = $this->getRepeaterDefaultHTML();
257
258 // get the template
259 $this->options['template'] = $this->getTemplate();
260
261 $this->loadMedia();
262 }
263
264 /**
265 * Loads media tied to this Field
266 *
267 * @return void
268 */
269 protected function loadMedia()
270 {
271 // CSS
272 wp_register_style(
273 'fpframework-repeater-field',
274 FPF_MEDIA_URL . 'admin/css/fpf_repeater.css',
275 [],
276 FPF_VERSION,
277 false
278 );
279 wp_enqueue_style('fpframework-repeater-field');
280
281 // Load Sortable
282 wp_register_script(
283 'fpframework-sortable-lib',
284 FPF_MEDIA_URL . 'admin/js/sortable.min.js',
285 [],
286 FPF_VERSION,
287 false
288 );
289 wp_enqueue_script('fpframework-sortable-lib');
290
291 // JS
292 wp_register_script(
293 'fpframework-repeater-field',
294 FPF_MEDIA_URL . 'admin/js/fpf_repeater.js',
295 [],
296 FPF_VERSION,
297 false
298 );
299 wp_enqueue_script('fpframework-repeater-field');
300 }
301 }