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