PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.32.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.32.1
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / helpers / FrmFieldGridHelper.php

FrmFieldGridHelper.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.32.1, at classes/helpers/FrmFieldGridHelper.php

317 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 class FrmFieldGridHelper {
7
8 /**
9 * @var bool
10 */
11 private $parent_li;
12
13 /**
14 * @var int
15 */
16 private $current_list_size;
17
18 /**
19 * @var int
20 */
21 private $current_field_count;
22
23 /**
24 * @var string
25 */
26 private $field_layout_class;
27
28 /**
29 * @var int
30 */
31 private $active_field_size;
32
33 /**
34 * @var bool flagged while calling get_field_layout_class, true if classes contain frm_first class.
35 */
36 private $is_frm_first;
37
38 /**
39 * @var stdClass|null
40 */
41 private $field;
42
43 /**
44 * @var FrmFieldGridHelper|null
45 */
46 private $section_helper;
47
48 /**
49 * @var bool
50 */
51 private $nested;
52
53 /**
54 * @var int
55 */
56 private $section_size;
57
58 /**
59 * @var bool
60 */
61 private $section_is_open = false;
62
63 /**
64 * @param bool $nested
65 */
66 public function __construct( $nested = false ) {
67 $this->parent_li = false;
68 $this->current_list_size = 0;
69 $this->current_field_count = 0;
70 $this->nested = $nested;
71 }
72
73 /**
74 * @param stdClass $field
75 *
76 * @return void
77 */
78 public function set_field( $field ) {
79 $this->field = $field;
80
81 if ( $this->section_helper && 'end_divider' !== $field->type ) {
82 $this->section_helper->set_field( $field );
83 return;
84 }
85
86 if ( 'end_divider' === $field->type ) {
87 $this->field_layout_class = '';
88 $this->active_field_size = $this->section_size;
89 $this->section_is_open = false;
90 $this->maybe_close_section_helper();
91 } else {
92 $this->field_layout_class = $this->get_field_layout_class();
93 $this->active_field_size = self::get_size_of_class( $this->field_layout_class );
94 }
95
96 if ( 'divider' !== $field->type || $this->nested ) {
97 return;
98 }
99
100 $this->section_size = $this->active_field_size;
101 $this->active_field_size = 0;
102 $this->section_helper = new self( true );
103 }
104
105 /**
106 * @return void
107 */
108 private function maybe_close_section_helper() {
109 if ( ! $this->section_helper ) {
110 return;
111 }
112 $this->section_helper->force_close_field_wrapper();
113 $this->section_helper = null;
114 }
115
116 /**
117 * @return string
118 */
119 public function get_field_layout_class() {
120 $field = FrmFieldsHelper::setup_edit_vars( $this->field );
121
122 if ( empty( $field['classes'] ) ) {
123 return '';
124 }
125
126 $split = explode( ' ', $field['classes'] );
127 $this->is_frm_first = in_array( 'frm_first', $split, true );
128
129 foreach ( self::get_grid_classes() as $class ) {
130 if ( in_array( $class, $split, true ) ) {
131 return $class;
132 }
133 }
134
135 return '';
136 }
137
138 /**
139 * @return void
140 */
141 public function maybe_begin_field_wrapper() {
142 if ( $this->should_first_close_the_active_field_wrapper() ) {
143 $this->close_field_wrapper();
144 }
145
146 if ( false === $this->parent_li && 'end_divider' !== $this->field->type ) {
147 $this->begin_field_wrapper();
148 }
149
150 if ( $this->section_helper && $this->section_is_open ) {
151 $this->section_helper->maybe_begin_field_wrapper();
152 }
153 }
154
155 /**
156 * @return bool
157 */
158 private function should_first_close_the_active_field_wrapper() {
159 if ( false === $this->parent_li || $this->section_helper ) {
160 return false;
161 }
162
163 if ( 'end_divider' === $this->field->type ) {
164 return false;
165 }
166
167 return ! $this->can_support_current_layout() || $this->is_frm_first;
168 }
169
170 /**
171 * @return void
172 */
173 private function begin_field_wrapper() {
174 echo '<li class="frm_field_box"><ul class="frm_grid_container frm_sorting">';
175 $this->parent_li = true;
176 $this->current_list_size = 0;
177 $this->current_field_count = 0;
178 }
179
180 /**
181 * @param string $class
182 *
183 * @return int
184 */
185 private static function get_size_of_class( $class ) {
186 switch ( $class ) {
187 case 'frm_half':
188 return 6;
189 case 'frm_third':
190 return 4;
191 case 'frm_two_thirds':
192 return 8;
193 case 'frm_fourth':
194 return 3;
195 case 'frm_three_fourths':
196 return 9;
197 case 'frm_sixth':
198 return 2;
199 }
200
201 if ( str_starts_with( $class, 'frm' ) ) {
202 $substr = substr( $class, 3 );
203
204 if ( is_numeric( $substr ) ) {
205 return (int) $substr;
206 }
207 }
208
209 // Anything missing a layout class should be a full width row.
210 return 12;
211 }
212
213 /**
214 * @return void
215 */
216 public function sync_list_size() {
217 if ( ! $this->field ) {
218 return;
219 }
220
221 if ( 'divider' === $this->field->type ) {
222 $this->section_is_open = true;
223 }
224
225 if ( $this->section_helper ) {
226 $this->section_helper->sync_list_size();
227
228 if ( 'end_divider' === $this->field->type ) {
229 $this->maybe_close_section_helper();
230 }
231
232 return;
233 }
234
235 if ( false === $this->parent_li ) {
236 return;
237 }
238
239 ++$this->current_field_count;
240 $this->current_list_size += $this->active_field_size;
241
242 if ( 12 === $this->current_list_size ) {
243 $this->close_field_wrapper();
244 }
245 }
246
247 /**
248 * It is possible that there was still space for another field so the wrapper could still be open after looping the fields.
249 * If it is, make sure it's closed now.
250 *
251 * @return void
252 */
253 public function force_close_field_wrapper() {
254 if ( false !== $this->parent_li ) {
255 $this->close_field_wrapper();
256 }
257 }
258
259 /**
260 * @return void
261 */
262 private function close_field_wrapper() {
263 $this->maybe_close_section_helper();
264 echo '</ul></li>';
265 $this->parent_li = false;
266 $this->current_list_size = 0;
267 $this->current_field_count = 0;
268 }
269
270 /**
271 * @return bool
272 */
273 private function can_support_current_layout() {
274 if ( 'end_divider' === $this->field->type ) {
275 return true;
276 }
277 return $this->can_support_an_additional_layout( $this->field_layout_class );
278 }
279
280 /**
281 * @param string $class
282 *
283 * @return bool
284 */
285 private function can_support_an_additional_layout( $class ) {
286 $size = self::get_size_of_class( $class );
287 return $this->current_list_size + $size <= 12;
288 }
289
290 /**
291 * @return array<string>
292 */
293 private static function get_grid_classes() {
294 return array(
295 'frm_full',
296 'frm_half',
297 'frm_third',
298 'frm_two_thirds',
299 'frm_fourth',
300 'frm_three_fourths',
301 'frm_sixth',
302 'frm1',
303 'frm2',
304 'frm3',
305 'frm4',
306 'frm5',
307 'frm6',
308 'frm7',
309 'frm8',
310 'frm9',
311 'frm10',
312 'frm11',
313 'frm12',
314 );
315 }
316 }
317