PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.28
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.28
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.28, at classes/helpers/FrmFieldGridHelper.php

318 lines 6.1 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 ( ! empty( $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 || ! empty( $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 ( empty( $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 $classes = self::get_grid_classes();
129
130 foreach ( $classes as $class ) {
131 if ( in_array( $class, $split, true ) ) {
132 return $class;
133 }
134 }
135
136 return '';
137 }
138
139 /**
140 * @return void
141 */
142 public function maybe_begin_field_wrapper() {
143 if ( $this->should_first_close_the_active_field_wrapper() ) {
144 $this->close_field_wrapper();
145 }
146
147 if ( false === $this->parent_li && 'end_divider' !== $this->field->type ) {
148 $this->begin_field_wrapper();
149 }
150
151 if ( ! empty( $this->section_helper ) && $this->section_is_open ) {
152 $this->section_helper->maybe_begin_field_wrapper();
153 }
154 }
155
156 /**
157 * @return bool
158 */
159 private function should_first_close_the_active_field_wrapper() {
160 if ( false === $this->parent_li || ! empty( $this->section_helper ) ) {
161 return false;
162 }
163
164 if ( 'end_divider' === $this->field->type ) {
165 return false;
166 }
167
168 return ! $this->can_support_current_layout() || $this->is_frm_first;
169 }
170
171 /**
172 * @return void
173 */
174 private function begin_field_wrapper() {
175 echo '<li class="frm_field_box"><ul class="frm_grid_container frm_sorting">';
176 $this->parent_li = true;
177 $this->current_list_size = 0;
178 $this->current_field_count = 0;
179 }
180
181 /**
182 * @param string $class
183 *
184 * @return int
185 */
186 private static function get_size_of_class( $class ) {
187 switch ( $class ) {
188 case 'frm_half':
189 return 6;
190 case 'frm_third':
191 return 4;
192 case 'frm_two_thirds':
193 return 8;
194 case 'frm_fourth':
195 return 3;
196 case 'frm_three_fourths':
197 return 9;
198 case 'frm_sixth':
199 return 2;
200 }
201
202 if ( str_starts_with( $class, 'frm' ) ) {
203 $substr = substr( $class, 3 );
204
205 if ( is_numeric( $substr ) ) {
206 return (int) $substr;
207 }
208 }
209
210 // Anything missing a layout class should be a full width row.
211 return 12;
212 }
213
214 /**
215 * @return void
216 */
217 public function sync_list_size() {
218 if ( empty( $this->field ) ) {
219 return;
220 }
221
222 if ( 'divider' === $this->field->type ) {
223 $this->section_is_open = true;
224 }
225
226 if ( ! empty( $this->section_helper ) ) {
227 $this->section_helper->sync_list_size();
228
229 if ( 'end_divider' === $this->field->type ) {
230 $this->maybe_close_section_helper();
231 }
232
233 return;
234 }
235
236 if ( false === $this->parent_li ) {
237 return;
238 }
239
240 ++$this->current_field_count;
241 $this->current_list_size += $this->active_field_size;
242
243 if ( 12 === $this->current_list_size ) {
244 $this->close_field_wrapper();
245 }
246 }
247
248 /**
249 * It is possible that there was still space for another field so the wrapper could still be open after looping the fields.
250 * If it is, make sure it's closed now.
251 *
252 * @return void
253 */
254 public function force_close_field_wrapper() {
255 if ( false !== $this->parent_li ) {
256 $this->close_field_wrapper();
257 }
258 }
259
260 /**
261 * @return void
262 */
263 private function close_field_wrapper() {
264 $this->maybe_close_section_helper();
265 echo '</ul></li>';
266 $this->parent_li = false;
267 $this->current_list_size = 0;
268 $this->current_field_count = 0;
269 }
270
271 /**
272 * @return bool
273 */
274 private function can_support_current_layout() {
275 if ( 'end_divider' === $this->field->type ) {
276 return true;
277 }
278 return $this->can_support_an_additional_layout( $this->field_layout_class );
279 }
280
281 /**
282 * @param string $class
283 *
284 * @return bool
285 */
286 private function can_support_an_additional_layout( $class ) {
287 $size = self::get_size_of_class( $class );
288 return $this->current_list_size + $size <= 12;
289 }
290
291 /**
292 * @return array<string>
293 */
294 private static function get_grid_classes() {
295 return array(
296 'frm_full',
297 'frm_half',
298 'frm_third',
299 'frm_two_thirds',
300 'frm_fourth',
301 'frm_three_fourths',
302 'frm_sixth',
303 'frm1',
304 'frm2',
305 'frm3',
306 'frm4',
307 'frm5',
308 'frm6',
309 'frm7',
310 'frm8',
311 'frm9',
312 'frm10',
313 'frm11',
314 'frm12',
315 );
316 }
317 }
318