actions
4 years ago
addons
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
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-break.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
209 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 $rendered_rows = 0; |
| 36 | |
| 37 | public $is_hidden_row; |
| 38 | public $is_submit_row; |
| 39 | public $is_page_break_row; |
| 40 | |
| 41 | public $current_repeater; |
| 42 | public $current_repeater_i; |
| 43 | public $preset; |
| 44 | public $spec_data; |
| 45 | public $post; |
| 46 | public $_conditional_blocks = array(); |
| 47 | public $_repeaters = array(); |
| 48 | public $blocks = array(); |
| 49 | |
| 50 | // for progress |
| 51 | public $form_break; |
| 52 | |
| 53 | /** |
| 54 | * Create form instance |
| 55 | * |
| 56 | * @param [type] $form_id [description] |
| 57 | */ |
| 58 | private function __construct() { |
| 59 | $this->post = get_post(); |
| 60 | } |
| 61 | |
| 62 | public function set_form_id( $form_id ) { |
| 63 | $this->form_id = $form_id; |
| 64 | |
| 65 | return $this; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * It turns out the inheritance of such an image |
| 70 | * Incoming Form Attributes (from block/widget/shortcode) |
| 71 | * (if not) -> Individual Form Arguments |
| 72 | * (if not) -> Default PHP Values |
| 73 | * |
| 74 | * @param array $incoming_attributes |
| 75 | * |
| 76 | * @return $this |
| 77 | */ |
| 78 | public function set_specific_data_for_render( $incoming_attributes = array() ) { |
| 79 | $jf_default_args = Plugin::instance()->post_type->get_default_args(); |
| 80 | $jf_args = Plugin::instance()->post_type->get_args( $this->form_id ); |
| 81 | |
| 82 | $attributes_from_post_type = array_diff( $jf_args, $jf_default_args ); |
| 83 | $form_block_or_widget = array_diff( $incoming_attributes, $jf_default_args ); |
| 84 | |
| 85 | $render_attributes = array_merge( ...apply_filters( |
| 86 | 'jet-form-builder/form-render/attributes', |
| 87 | array( |
| 88 | Plugin::instance()->post_type->get_default_args_on_render(), |
| 89 | $attributes_from_post_type, |
| 90 | $form_block_or_widget |
| 91 | ) |
| 92 | ) ); |
| 93 | |
| 94 | $this->spec_data = ( object ) $render_attributes; |
| 95 | |
| 96 | return $this; |
| 97 | } |
| 98 | |
| 99 | public function is_not_field( $block ) { |
| 100 | return Plugin::instance()->form->is_not_field( $block['blockName'] ); |
| 101 | } |
| 102 | |
| 103 | public function is_field( $block, $needle ) { |
| 104 | return Plugin::instance()->form->is_field( $block['blockName'], $needle ); |
| 105 | } |
| 106 | |
| 107 | public function get_fields( $content ) { |
| 108 | return Plugin::instance()->form->get_fields( $content ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Setup fields prop |
| 113 | * |
| 114 | * @param $content |
| 115 | * |
| 116 | * @return array[] |
| 117 | */ |
| 118 | public function setup_fields( $content ) { |
| 119 | $blocks = parse_blocks( $content ); |
| 120 | $this->blocks = $this->get_form_break()->set_pages( $blocks ); |
| 121 | |
| 122 | return $this->blocks; |
| 123 | } |
| 124 | |
| 125 | public function maybe_progress_pages() { |
| 126 | if ( ! $this->spec_data->enable_progress || 0 === $this->get_form_break()->get_pages() ) { |
| 127 | return ''; |
| 128 | } |
| 129 | |
| 130 | return $this->get_form_break()->render_progress( 'default', array( |
| 131 | 'jet-form-builder-progress-pages--global' |
| 132 | ) ); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | public static function force_render_field( $name, $arguments = array() ) { |
| 137 | if ( empty( $name ) ) { |
| 138 | return ''; |
| 139 | } |
| 140 | $field = jet_form_builder()->blocks->get_field_by_name( $name ); |
| 141 | |
| 142 | if ( ! $field instanceof Block_Type_Base ) { |
| 143 | return ''; |
| 144 | } |
| 145 | |
| 146 | $field->set_block_data( $arguments, null ); |
| 147 | |
| 148 | return $field->get_block_renderer(); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /** |
| 153 | * Maybe start new page |
| 154 | * |
| 155 | * @param bool $force_first |
| 156 | * |
| 157 | * @return false|string|void |
| 158 | */ |
| 159 | public function maybe_start_page( $force_first = false ) { |
| 160 | return $this->get_form_break()->maybe_start_page( $force_first ); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Maybe start new page |
| 165 | * |
| 166 | * @param $is_last |
| 167 | * @param $field |
| 168 | * |
| 169 | * @return false|string|void [type] [description] |
| 170 | */ |
| 171 | public function maybe_end_page( $is_last = false, $field = false ) { |
| 172 | return $this->get_form_break()->maybe_end_page( $is_last, $field ); |
| 173 | } |
| 174 | |
| 175 | public function get_nonce_id() { |
| 176 | return "jet-form-builder-wp-nonce-{$this->form_id}"; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * @param string $name |
| 181 | * |
| 182 | * @return Form_Break |
| 183 | */ |
| 184 | public function get_form_break( $name = '' ) { |
| 185 | if ( ! $name && ! $this->form_break ) { |
| 186 | $this->form_break = new Form_Break(); |
| 187 | } |
| 188 | if ( $name && ! $this->isset_form_break( $name ) ) { |
| 189 | $this->_conditional_blocks[ $name ] = array( 'break' => new Form_Break() ); |
| 190 | } |
| 191 | |
| 192 | return $name ? $this->_conditional_blocks[ $name ]['break'] : $this->form_break; |
| 193 | } |
| 194 | |
| 195 | public function isset_form_break( $name ) { |
| 196 | return isset( $this->_conditional_blocks[ $name ] ); |
| 197 | } |
| 198 | |
| 199 | public function get_repeater( $name ) { |
| 200 | return $this->_repeaters[ $name ] ?? array(); |
| 201 | } |
| 202 | |
| 203 | public function set_repeater( $name, $attrs ) { |
| 204 | $this->_repeaters[ $name ] = array_merge( $this->get_repeater( $name ), $attrs ); |
| 205 | } |
| 206 | |
| 207 | |
| 208 | } |
| 209 |