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

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