actions
5 years ago
admin
5 years ago
blocks
5 years ago
classes
5 years ago
compatibility
5 years ago
dev-mode
5 years ago
exceptions
5 years ago
form-messages
5 years ago
form-patterns
5 years ago
form-response
5 years ago
gateways
5 years ago
generators
5 years ago
integrations
5 years ago
presets
5 years ago
request
5 years ago
shortcodes
5 years ago
widgets
5 years ago
autoloader.php
5 years ago
file-upload.php
5 years ago
form-handler.php
5 years ago
form-manager.php
5 years ago
form-messages-builder.php
5 years ago
form-messages-manager.php
5 years ago
form-preset.php
5 years ago
live-form.php
5 years ago
plugin.php
5 years ago
post-type.php
5 years ago
request-handler.php
5 years ago
live-form.php
367 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder; |
| 4 | |
| 5 | use Jet_Form_Builder\Blocks\Types\Base as Block_Type_Base; |
| 6 | use Jet_Form_Builder\Classes\Attributes_Trait; |
| 7 | use Jet_Form_Builder\Classes\Get_Template_Trait; |
| 8 | use Jet_Form_Builder\Classes\Instance_Trait; |
| 9 | use Jet_Form_Builder\Classes\Tools; |
| 10 | |
| 11 | // If this file is called directly, abort. |
| 12 | if ( ! defined( 'WPINC' ) ) { |
| 13 | die; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Main file |
| 18 | */ |
| 19 | class Live_Form { |
| 20 | |
| 21 | use Attributes_Trait; |
| 22 | use Get_Template_Trait; |
| 23 | use Instance_Trait; |
| 24 | |
| 25 | public $form_id = false; |
| 26 | private $form = false; |
| 27 | |
| 28 | |
| 29 | private $field_name; |
| 30 | private $current_field_data; |
| 31 | private $start_new_page = true; |
| 32 | public $pages = 0; |
| 33 | public $rendered_rows = 0; |
| 34 | |
| 35 | public $is_hidden_row; |
| 36 | public $is_submit_row; |
| 37 | public $is_page_break_row; |
| 38 | |
| 39 | public $current_repeater; |
| 40 | public $current_repeater_i; |
| 41 | public $preset; |
| 42 | public $spec_data; |
| 43 | |
| 44 | public $page = 0; |
| 45 | public $has_prev = false; |
| 46 | public $post; |
| 47 | |
| 48 | // for progress |
| 49 | public $form_breaks; |
| 50 | |
| 51 | /** |
| 52 | * Create form instance |
| 53 | * |
| 54 | * @param [type] $form_id [description] |
| 55 | */ |
| 56 | private function __construct() { |
| 57 | $this->post = get_post(); |
| 58 | } |
| 59 | |
| 60 | public function set_form_id( $form_id ) { |
| 61 | $this->form_id = $form_id; |
| 62 | |
| 63 | return $this; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | public function set_repeater( $item, $count = false ) { |
| 68 | $this->current_repeater = $item; |
| 69 | $this->current_repeater_i = $count; |
| 70 | |
| 71 | return $this; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * It turns out the inheritance of such an image |
| 76 | * Individual Form Arguments (if not) -> General Form Attributes (if not) -> Default Values |
| 77 | * |
| 78 | * @param array $attributes |
| 79 | * |
| 80 | * @return $this |
| 81 | */ |
| 82 | public function set_specific_data_for_render( $attributes = array() ) { |
| 83 | $jf_default_args = Plugin::instance()->post_type->get_default_args(); |
| 84 | $jf_args = Plugin::instance()->post_type->get_args( $this->form_id ); |
| 85 | |
| 86 | $spec_data = array_diff( $jf_args, $jf_default_args ); |
| 87 | $this->spec_data = ( object ) array_merge( $jf_default_args, $attributes, $spec_data );; |
| 88 | |
| 89 | return $this; |
| 90 | } |
| 91 | |
| 92 | public function is_not_field( $block ) { |
| 93 | return Plugin::instance()->form->is_not_field( $block['blockName'] ); |
| 94 | } |
| 95 | |
| 96 | public function is_field( $block, $needle ) { |
| 97 | return Plugin::instance()->form->is_field( $block['blockName'], $needle ); |
| 98 | } |
| 99 | |
| 100 | public function get_fields( $content ) { |
| 101 | return Plugin::instance()->form->get_fields( $content ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Setup fields prop |
| 106 | * |
| 107 | * @param $content |
| 108 | */ |
| 109 | public function setup_fields( $content ) { |
| 110 | $blocks = $this->get_fields( $content ); |
| 111 | |
| 112 | foreach ( $blocks as $field ) { |
| 113 | if ( $this->is_field( $field, 'form-break' ) ) { |
| 114 | $this->pages ++; |
| 115 | $this->form_breaks[] = Plugin::instance()->blocks->get_field_attrs( $field['blockName'], $field['attrs'] ); |
| 116 | } |
| 117 | } |
| 118 | if ( ! empty( $this->form_breaks ) ) { |
| 119 | $this->form_breaks[] = array( 'label' => __( 'Last Page' ) ); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | public function maybe_progress_pages() { |
| 124 | if ( ! $this->spec_data->enable_progress || 0 === $this->pages ) { |
| 125 | return ''; |
| 126 | } |
| 127 | |
| 128 | ob_start(); |
| 129 | include $this->get_global_template( 'common/progress-pages.php' ); |
| 130 | |
| 131 | return ob_get_clean(); |
| 132 | } |
| 133 | |
| 134 | public function get_progress_item_class( $index ) { |
| 135 | $classes = array( 'jet-form-builder-progress-pages__item--wrapper' ); |
| 136 | |
| 137 | if ( $index === $this->page ) { |
| 138 | $classes[] = 'active-page'; |
| 139 | } elseif ( -1 === $index ) { |
| 140 | $classes[] = 'passed-page'; |
| 141 | } |
| 142 | |
| 143 | return implode( ' ', $classes ); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | public static function force_render_field( $name, $arguments = array() ) { |
| 148 | if ( empty( $name ) ) { |
| 149 | return; |
| 150 | } |
| 151 | $field = jet_form_builder()->blocks->get_field_by_name( $name ); |
| 152 | |
| 153 | if ( ! $field instanceof Block_Type_Base ) { |
| 154 | return; |
| 155 | } |
| 156 | $field->set_block_data( $arguments ); |
| 157 | |
| 158 | return $field->get_block_renderer(); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Open form wrapper |
| 163 | * |
| 164 | * @param $field |
| 165 | * |
| 166 | * @return false|string [type] [description] |
| 167 | */ |
| 168 | public function start_form_row( $field ) { |
| 169 | |
| 170 | if ( $this->is_field( $field, 'hidden' ) ) { |
| 171 | return ''; |
| 172 | } |
| 173 | |
| 174 | if ( ! $this->is_hidden_row ) { |
| 175 | $this->rendered_rows ++; |
| 176 | } |
| 177 | |
| 178 | ob_start(); |
| 179 | |
| 180 | do_action( 'jet-form-builder/before-start-form-row', $this ); |
| 181 | |
| 182 | $this->add_attribute( 'class', 'jet-form-builder-row' ); |
| 183 | |
| 184 | if ( $this->is_hidden_row ) { |
| 185 | $this->add_attribute( 'class', 'jet-form-builder-row--hidden' ); |
| 186 | } |
| 187 | |
| 188 | if ( $this->is_field( $field, 'submit' ) ) { |
| 189 | $this->add_attribute( 'class', 'jet-form-builder-row--submit' ); |
| 190 | } |
| 191 | |
| 192 | if ( $this->is_field( $field, 'form-break' ) ) { |
| 193 | $this->add_attribute( 'class', 'jet-form-builder-row--page-break' ); |
| 194 | } |
| 195 | |
| 196 | if ( 1 === $this->rendered_rows ) { |
| 197 | $this->add_attribute( 'class', 'jet-form-builder-row--first-visible' ); |
| 198 | } |
| 199 | |
| 200 | include $this->get_global_template( 'common/start-form-row.php' ); |
| 201 | |
| 202 | do_action( 'jet-form-builder/after-start-form-row', $this ); |
| 203 | |
| 204 | return ob_get_clean(); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Close form wrapper |
| 209 | * |
| 210 | * @param $field |
| 211 | * |
| 212 | * @return false|string [type] [description] |
| 213 | */ |
| 214 | public function end_form_row( $field ) { |
| 215 | |
| 216 | if ( $this->is_field( $field, 'hidden' ) ) { |
| 217 | return ''; |
| 218 | } |
| 219 | |
| 220 | ob_start(); |
| 221 | do_action( 'jet-form-builder/before-end-form-row', $this ); |
| 222 | |
| 223 | include $this->get_global_template( 'common/end-form-row.php' ); |
| 224 | |
| 225 | do_action( 'jet-form-builder/after-end-form-row', $this ); |
| 226 | |
| 227 | return ob_get_clean(); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | /** |
| 232 | * Maybe start new page |
| 233 | * |
| 234 | * @return false|string|void |
| 235 | */ |
| 236 | public function maybe_start_page() { |
| 237 | |
| 238 | if ( 0 >= $this->pages ) { |
| 239 | return ''; |
| 240 | } |
| 241 | |
| 242 | if ( false === $this->start_new_page ) { |
| 243 | return ''; |
| 244 | } |
| 245 | |
| 246 | $this->start_new_page = false; |
| 247 | $this->page ++; |
| 248 | |
| 249 | ob_start(); |
| 250 | do_action( 'jet-form-builder/before-page-start', $this ); |
| 251 | |
| 252 | $hidden_class = ''; |
| 253 | |
| 254 | if ( 1 < $this->page ) { |
| 255 | $hidden_class = 'jet-form-page--hidden'; |
| 256 | } |
| 257 | |
| 258 | include $this->get_global_template( 'common/start-page.php' ); |
| 259 | |
| 260 | do_action( 'jet-form-builder/after-page-start', $this ); |
| 261 | |
| 262 | return ob_get_clean(); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Maybe start new page |
| 267 | * |
| 268 | * @param $is_last |
| 269 | * @param $field |
| 270 | * |
| 271 | * @return false|string|void [type] [description] |
| 272 | */ |
| 273 | public function maybe_end_page( $is_last = false, $field = false ) { |
| 274 | |
| 275 | if ( 0 >= $this->pages ) { |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | if ( ! $is_last && ! $this->is_field( $field, 'form-break' ) ) { |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | $this->start_new_page = true; |
| 284 | $this->has_prev = true; |
| 285 | |
| 286 | ob_start(); |
| 287 | do_action( 'jet-form-builder/before-page-end', $this ); |
| 288 | |
| 289 | include $this->get_global_template( 'common/end-page.php' ); |
| 290 | |
| 291 | do_action( 'jet-form-builder/after-page-end', $this ); |
| 292 | |
| 293 | return ob_get_clean(); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Returns true if field is visible |
| 298 | * |
| 299 | * @param $field |
| 300 | * |
| 301 | * @return boolean [description] |
| 302 | */ |
| 303 | public function is_field_visible( $field ) { |
| 304 | |
| 305 | // For backward compatibility and hidden fields |
| 306 | if ( empty( $field['visibility'] ) ) { |
| 307 | return true; |
| 308 | } |
| 309 | |
| 310 | // If is visible for all - show field |
| 311 | if ( 'all' === $field['visibility'] ) { |
| 312 | return true; |
| 313 | } |
| 314 | |
| 315 | // If is visible for logged in users and user is logged in - show field |
| 316 | if ( 'logged_id' === $field['visibility'] && is_user_logged_in() ) { |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | // If is visible for not logged in users and user is not logged in - show field |
| 321 | if ( 'not_logged_in' === $field['visibility'] && ! is_user_logged_in() ) { |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | return false; |
| 326 | |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Returns field ID with repeater prefix if needed |
| 331 | */ |
| 332 | public function get_field_id( $name ) { |
| 333 | |
| 334 | if ( is_array( $name ) ) { |
| 335 | $name = $name['name']; |
| 336 | } |
| 337 | //Find some solution for the repeater field |
| 338 | |
| 339 | if ( $this->current_repeater ) { |
| 340 | $repeater_name = ! empty( $this->current_repeater['name'] ) ? $this->current_repeater['name'] : 'repeater'; |
| 341 | $index = ( false !== $this->current_repeater_i ) ? $this->current_repeater_i : '__i__'; |
| 342 | $name = sprintf( '%1$s_%2$s_%3$s', $repeater_name, $index, $name ); |
| 343 | } |
| 344 | |
| 345 | return $name; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Returns field name with repeater prefix if needed |
| 350 | */ |
| 351 | public function get_field_name( $name ) { |
| 352 | |
| 353 | //Find some solution for the repeater field |
| 354 | if ( $this->current_repeater ) { |
| 355 | $repeater_name = ! empty( $this->current_repeater['name'] ) ? $this->current_repeater['name'] : '_undefined_repeater_name'; |
| 356 | $index = ( false !== $this->current_repeater_i ) ? $this->current_repeater_i : '__i__'; |
| 357 | |
| 358 | $name = sprintf( '%1$s[%2$s][%3$s]', $repeater_name, $index, $name ); |
| 359 | } |
| 360 | |
| 361 | return $name; |
| 362 | |
| 363 | } |
| 364 | |
| 365 | |
| 366 | } |
| 367 |