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