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

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