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

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