actions
3 years ago
addons
3 years ago
admin
3 years ago
blocks
3 years ago
classes
3 years ago
compatibility
3 years ago
db-queries
3 years ago
dev-mode
3 years ago
exceptions
3 years ago
form-actions
3 years ago
form-messages
3 years ago
form-patterns
3 years ago
form-response
3 years ago
gateways
3 years ago
generators
3 years ago
integrations
3 years ago
migrations
3 years ago
presets
3 years ago
request
3 years ago
rest-api
3 years ago
shortcodes
3 years ago
widgets
3 years ago
wp-cli
3 years ago
autoloader.php
3 years ago
file-upload.php
3 years ago
form-break.php
3 years ago
form-handler.php
3 years ago
form-manager.php
3 years ago
live-form.php
3 years ago
plugin.php
3 years ago
post-type.php
3 years ago
live-form.php
181 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder; |
| 4 | |
| 5 | use Jet_Form_Builder\Blocks\Block_Helper; |
| 6 | use Jet_Form_Builder\Blocks\Types\Base as Block_Type_Base; |
| 7 | use Jet_Form_Builder\Classes\Arguments\Form_Arguments; |
| 8 | use Jet_Form_Builder\Classes\Attributes_Trait; |
| 9 | use Jet_Form_Builder\Classes\Get_Template_Trait; |
| 10 | use Jet_Form_Builder\Classes\Instance_Trait; |
| 11 | use Jet_Form_Builder\Classes\Post\Post_Tools; |
| 12 | |
| 13 | // If this file is called directly, abort. |
| 14 | if ( ! defined( 'WPINC' ) ) { |
| 15 | die; |
| 16 | } |
| 17 | |
| 18 | |
| 19 | /** |
| 20 | * @method static Live_Form instance() |
| 21 | * |
| 22 | * Class Live_Form |
| 23 | * @package Jet_Form_Builder |
| 24 | */ |
| 25 | class Live_Form { |
| 26 | |
| 27 | use Attributes_Trait; |
| 28 | use Get_Template_Trait; |
| 29 | use Instance_Trait; |
| 30 | |
| 31 | public $form_id = false; |
| 32 | /** @var Form_Arguments */ |
| 33 | public $spec_data; |
| 34 | public $post; |
| 35 | public $_conditional_blocks = array(); |
| 36 | public $_repeaters = array(); |
| 37 | public $blocks = array(); |
| 38 | |
| 39 | // for progress |
| 40 | public $form_break; |
| 41 | |
| 42 | /** |
| 43 | * Create form instance |
| 44 | * |
| 45 | * @param [type] $form_id [description] |
| 46 | */ |
| 47 | private function __construct() { |
| 48 | $this->post = $this->current_post(); |
| 49 | $this->spec_data = new Form_Arguments(); |
| 50 | } |
| 51 | |
| 52 | public function set_form_id( $form_id ) { |
| 53 | $this->form_id = $form_id; |
| 54 | |
| 55 | return $this; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param array $incoming_attributes |
| 60 | * |
| 61 | * @return $this |
| 62 | */ |
| 63 | public function set_specific_data_for_render( $incoming_attributes = array() ) { |
| 64 | $this->spec_data |
| 65 | ->set_form_id( $this->form_id ) |
| 66 | ->load( $incoming_attributes ); |
| 67 | |
| 68 | return $this; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Setup fields prop |
| 73 | * |
| 74 | * @return array[] |
| 75 | */ |
| 76 | public function setup_fields(): array { |
| 77 | $this->blocks = $this->get_form_break()->set_pages( |
| 78 | Block_Helper::get_blocks_by_post( $this->form_id ) |
| 79 | ); |
| 80 | |
| 81 | return $this->blocks; |
| 82 | } |
| 83 | |
| 84 | public function maybe_progress_pages() { |
| 85 | if ( ! $this->spec_data->enable_progress || 0 === $this->get_form_break()->get_pages() ) { |
| 86 | return ''; |
| 87 | } |
| 88 | |
| 89 | return $this->get_form_break()->render_progress( |
| 90 | 'default', |
| 91 | array( |
| 92 | 'jet-form-builder-progress-pages--global', |
| 93 | ) |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | public static function force_render_field( $name, $arguments = array() ) { |
| 99 | if ( empty( $name ) ) { |
| 100 | return ''; |
| 101 | } |
| 102 | $field = jet_form_builder()->blocks->get_field_by_name( $name ); |
| 103 | |
| 104 | if ( ! $field instanceof Block_Type_Base ) { |
| 105 | return ''; |
| 106 | } |
| 107 | $arguments['_static_value'] = true; |
| 108 | |
| 109 | $field->set_block_data( $arguments ); |
| 110 | |
| 111 | return $field->get_block_renderer(); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /** |
| 116 | * Maybe start new page |
| 117 | * |
| 118 | * @param bool $force_first |
| 119 | * |
| 120 | * @return false|string|void |
| 121 | */ |
| 122 | public function maybe_start_page( $force_first = false ) { |
| 123 | return $this->get_form_break()->maybe_start_page( $force_first ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Maybe start new page |
| 128 | * |
| 129 | * @param $is_last |
| 130 | * @param $field |
| 131 | * |
| 132 | * @return false|string|void [type] [description] |
| 133 | */ |
| 134 | public function maybe_end_page( $is_last = false, $field = false ) { |
| 135 | return $this->get_form_break()->maybe_end_page( $is_last, $field ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @param string $name |
| 140 | * |
| 141 | * @return Form_Break |
| 142 | */ |
| 143 | public function get_form_break( $name = '' ) { |
| 144 | if ( ! $name && ! $this->form_break ) { |
| 145 | $this->form_break = new Form_Break(); |
| 146 | } |
| 147 | if ( $name && ! $this->isset_form_break( $name ) ) { |
| 148 | $this->_conditional_blocks[ $name ] = array( 'break' => new Form_Break() ); |
| 149 | } |
| 150 | |
| 151 | return $name ? $this->_conditional_blocks[ $name ]['break'] : $this->form_break; |
| 152 | } |
| 153 | |
| 154 | public function isset_form_break( $name ) { |
| 155 | return isset( $this->_conditional_blocks[ $name ] ); |
| 156 | } |
| 157 | |
| 158 | public function get_repeater( $name ) { |
| 159 | return $this->_repeaters[ $name ] ?? array(); |
| 160 | } |
| 161 | |
| 162 | public function set_repeater( $name, $attrs ) { |
| 163 | $this->_repeaters[ $name ] = array_merge( $this->get_repeater( $name ), $attrs ); |
| 164 | } |
| 165 | |
| 166 | private function current_post() { |
| 167 | global $post; |
| 168 | |
| 169 | if ( wp_doing_ajax() && empty( $post->ID ) ) { |
| 170 | $url = wp_get_referer(); |
| 171 | $post_id = url_to_postid( $url ); |
| 172 | |
| 173 | return get_post( $post_id ); |
| 174 | } else { |
| 175 | return $post; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | |
| 180 | } |
| 181 |