actions
4 years ago
addons
4 years ago
admin
4 years ago
blocks
4 years ago
classes
4 years ago
db-queries
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
rest-api
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
215 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\Attributes_Trait; |
| 8 | use Jet_Form_Builder\Classes\Get_Template_Trait; |
| 9 | use Jet_Form_Builder\Classes\Instance_Trait; |
| 10 | use Jet_Form_Builder\Classes\Post\Post_Tools; |
| 11 | |
| 12 | // If this file is called directly, abort. |
| 13 | if ( ! defined( 'WPINC' ) ) { |
| 14 | die; |
| 15 | } |
| 16 | |
| 17 | |
| 18 | /** |
| 19 | * @method static Live_Form instance() |
| 20 | * |
| 21 | * Class Live_Form |
| 22 | * @package Jet_Form_Builder |
| 23 | */ |
| 24 | class Live_Form { |
| 25 | |
| 26 | use Attributes_Trait; |
| 27 | use Get_Template_Trait; |
| 28 | use Instance_Trait; |
| 29 | |
| 30 | public $form_id = false; |
| 31 | public $spec_data; |
| 32 | public $post; |
| 33 | public $_conditional_blocks = array(); |
| 34 | public $_repeaters = array(); |
| 35 | public $blocks = array(); |
| 36 | |
| 37 | // for progress |
| 38 | public $form_break; |
| 39 | |
| 40 | /** |
| 41 | * Create form instance |
| 42 | * |
| 43 | * @param [type] $form_id [description] |
| 44 | */ |
| 45 | private function __construct() { |
| 46 | $this->post = $this->current_post(); |
| 47 | } |
| 48 | |
| 49 | public function set_form_id( $form_id ) { |
| 50 | $this->form_id = $form_id; |
| 51 | |
| 52 | return $this; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * It turns out the inheritance of such an image |
| 57 | * Incoming Form Attributes (from block/widget/shortcode) |
| 58 | * (if not) -> Individual Form Arguments |
| 59 | * (if not) -> Default PHP Values |
| 60 | * |
| 61 | * @param array $incoming_attributes |
| 62 | * |
| 63 | * @return $this |
| 64 | */ |
| 65 | public function set_specific_data_for_render( $incoming_attributes = array() ) { |
| 66 | $jf_default_args = Plugin::instance()->post_type->get_default_args(); |
| 67 | $jf_args = Plugin::instance()->post_type->get_args( $this->form_id ); |
| 68 | |
| 69 | $attributes_from_post_type = array_diff( $jf_args, $jf_default_args ); |
| 70 | $form_block_or_widget = array_diff( $incoming_attributes, $jf_default_args ); |
| 71 | |
| 72 | $render_attributes = array_merge( |
| 73 | ...apply_filters( |
| 74 | 'jet-form-builder/form-render/attributes', |
| 75 | array( |
| 76 | Plugin::instance()->post_type->get_default_args_on_render(), |
| 77 | $attributes_from_post_type, |
| 78 | $form_block_or_widget, |
| 79 | ) |
| 80 | ) |
| 81 | ); |
| 82 | |
| 83 | $this->spec_data = (object) $render_attributes; |
| 84 | |
| 85 | return $this; |
| 86 | } |
| 87 | |
| 88 | public function get_spec( $property, $default = '' ) { |
| 89 | return ( $this->spec_data->{$property} ?? $default ) ?: $default; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Setup fields prop |
| 94 | * |
| 95 | * @return array[] |
| 96 | */ |
| 97 | public function setup_fields(): array { |
| 98 | $form = get_post( $this->form_id ); |
| 99 | |
| 100 | $this->blocks = $this->get_form_break()->set_pages( |
| 101 | parse_blocks( $form->post_content ) |
| 102 | ); |
| 103 | |
| 104 | return $this->blocks; |
| 105 | } |
| 106 | |
| 107 | public function maybe_progress_pages() { |
| 108 | if ( ! $this->spec_data->enable_progress || 0 === $this->get_form_break()->get_pages() ) { |
| 109 | return ''; |
| 110 | } |
| 111 | |
| 112 | return $this->get_form_break()->render_progress( |
| 113 | 'default', |
| 114 | array( |
| 115 | 'jet-form-builder-progress-pages--global', |
| 116 | ) |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | public static function force_render_field( $name, $arguments = array() ) { |
| 122 | if ( empty( $name ) ) { |
| 123 | return ''; |
| 124 | } |
| 125 | $field = jet_form_builder()->blocks->get_field_by_name( $name ); |
| 126 | |
| 127 | if ( ! $field instanceof Block_Type_Base ) { |
| 128 | return ''; |
| 129 | } |
| 130 | |
| 131 | $field->set_block_data( $arguments ); |
| 132 | |
| 133 | return $field->get_block_renderer(); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | /** |
| 138 | * Maybe start new page |
| 139 | * |
| 140 | * @param bool $force_first |
| 141 | * |
| 142 | * @return false|string|void |
| 143 | */ |
| 144 | public function maybe_start_page( $force_first = false ) { |
| 145 | return $this->get_form_break()->maybe_start_page( $force_first ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Maybe start new page |
| 150 | * |
| 151 | * @param $is_last |
| 152 | * @param $field |
| 153 | * |
| 154 | * @return false|string|void [type] [description] |
| 155 | */ |
| 156 | public function maybe_end_page( $is_last = false, $field = false ) { |
| 157 | return $this->get_form_break()->maybe_end_page( $is_last, $field ); |
| 158 | } |
| 159 | |
| 160 | public function get_nonce_id() { |
| 161 | return "jet-form-builder-wp-nonce-{$this->form_id}"; |
| 162 | } |
| 163 | |
| 164 | public function get_nonce_field(): string { |
| 165 | if ( 'render' !== $this->spec_data->load_nonce ) { |
| 166 | return ''; |
| 167 | } |
| 168 | |
| 169 | return wp_nonce_field( $this->get_nonce_id(), '_wpnonce', true, false ); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @param string $name |
| 174 | * |
| 175 | * @return Form_Break |
| 176 | */ |
| 177 | public function get_form_break( $name = '' ) { |
| 178 | if ( ! $name && ! $this->form_break ) { |
| 179 | $this->form_break = new Form_Break(); |
| 180 | } |
| 181 | if ( $name && ! $this->isset_form_break( $name ) ) { |
| 182 | $this->_conditional_blocks[ $name ] = array( 'break' => new Form_Break() ); |
| 183 | } |
| 184 | |
| 185 | return $name ? $this->_conditional_blocks[ $name ]['break'] : $this->form_break; |
| 186 | } |
| 187 | |
| 188 | public function isset_form_break( $name ) { |
| 189 | return isset( $this->_conditional_blocks[ $name ] ); |
| 190 | } |
| 191 | |
| 192 | public function get_repeater( $name ) { |
| 193 | return $this->_repeaters[ $name ] ?? array(); |
| 194 | } |
| 195 | |
| 196 | public function set_repeater( $name, $attrs ) { |
| 197 | $this->_repeaters[ $name ] = array_merge( $this->get_repeater( $name ), $attrs ); |
| 198 | } |
| 199 | |
| 200 | private function current_post() { |
| 201 | global $post; |
| 202 | |
| 203 | if ( wp_doing_ajax() && empty( $post->ID ) ) { |
| 204 | $url = wp_get_referer(); |
| 205 | $post_id = url_to_postid( $url ); |
| 206 | |
| 207 | return get_post( $post_id ); |
| 208 | } else { |
| 209 | return $post; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | |
| 214 | } |
| 215 |