base-select-radio-check.php
5 years ago
base.php
5 years ago
calculated-field-render.php
5 years ago
checkbox-field-render.php
5 years ago
date-field-render.php
5 years ago
form-break-field-render.php
5 years ago
form-builder.php
5 years ago
group-break-field-render.php
5 years ago
heading-field-render.php
5 years ago
hidden-field-render.php
5 years ago
media-field-render.php
5 years ago
number-field-render.php
5 years ago
radio-field-render.php
5 years ago
range-field-render.php
5 years ago
repeater-field-render.php
5 years ago
select-field-render.php
5 years ago
submit-field-render.php
5 years ago
text-field-render.php
5 years ago
textarea-field-render.php
5 years ago
time-field-render.php
5 years ago
wysiwyg-field-render.php
5 years ago
form-builder.php
246 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Blocks\Render; |
| 4 | |
| 5 | |
| 6 | use Jet_Form_Builder\Classes\Arguments_Trait; |
| 7 | use Jet_Form_Builder\Classes\Attributes_Trait; |
| 8 | use Jet_Form_Builder\Classes\Get_Template_Trait; |
| 9 | use Jet_Form_Builder\Compatibility\Jet_Style_Manager; |
| 10 | use Jet_Form_Builder\Fields_Factory; |
| 11 | use Jet_Form_Builder\File_Upload; |
| 12 | use Jet_Form_Builder\Form_Preset; |
| 13 | use Jet_Form_Builder\Live_Form; |
| 14 | use Jet_Form_Builder\Plugin; |
| 15 | |
| 16 | // If this file is called directly, abort. |
| 17 | |
| 18 | if ( ! defined( 'WPINC' ) ) { |
| 19 | die; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Form builder class |
| 24 | */ |
| 25 | class Form_Builder { |
| 26 | |
| 27 | use Attributes_Trait; |
| 28 | |
| 29 | use Get_Template_Trait; |
| 30 | |
| 31 | |
| 32 | public $form_id; |
| 33 | public $post; |
| 34 | public $args = array(); |
| 35 | |
| 36 | private $blocks; |
| 37 | |
| 38 | /** |
| 39 | * Constructor for the class |
| 40 | * |
| 41 | * @param null $form_id |
| 42 | * @param bool $post |
| 43 | * @param array $args |
| 44 | */ |
| 45 | function __construct( $form_id = null, $post = false, $args = array() ) { |
| 46 | |
| 47 | if ( ! $form_id ) { |
| 48 | return; |
| 49 | } |
| 50 | $this->form_id = $form_id; |
| 51 | $this->set_form_args( $args ); |
| 52 | |
| 53 | if ( empty( $post ) ) { |
| 54 | $this->post = get_post(); |
| 55 | } |
| 56 | |
| 57 | $this->blocks = Plugin::instance()->form->get_form_by_id( $form_id ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param $arguments |
| 62 | */ |
| 63 | public function set_form_args( $arguments ) { |
| 64 | $this->args = array_intersect_key( $arguments, Plugin::instance()->post_type->get_default_args() ); |
| 65 | |
| 66 | return $this; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | /** |
| 71 | * Returns form action url |
| 72 | * |
| 73 | * @return [type] [description] |
| 74 | */ |
| 75 | public function get_form_action_url() { |
| 76 | |
| 77 | $action = add_query_arg( |
| 78 | array( |
| 79 | Plugin::instance()->form_handler->hook_key => Plugin::instance()->form_handler->hook_val, |
| 80 | ), |
| 81 | home_url( '/' ) |
| 82 | ); |
| 83 | |
| 84 | return apply_filters( 'jet-form-builder/form-action-url', $action, $this ); |
| 85 | |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns form refer url |
| 90 | * |
| 91 | * @return [type] [description] |
| 92 | */ |
| 93 | public function get_form_refer_url() { |
| 94 | |
| 95 | global $wp; |
| 96 | |
| 97 | $refer = home_url( $wp->request ); |
| 98 | |
| 99 | if ( ! empty( $_SERVER['QUERY_STRING'] ) ) { |
| 100 | $refer = trailingslashit( $refer ) . '?' . $_SERVER['QUERY_STRING']; |
| 101 | } |
| 102 | |
| 103 | return apply_filters( 'jet-form-builder/form-refer-url', $refer, $this ); |
| 104 | |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @return mixed|void |
| 109 | */ |
| 110 | public function pre_render() { |
| 111 | return apply_filters( 'jet-form-builder/pre-render/' . $this->form_id, false ); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /** |
| 116 | * Open form wrapper |
| 117 | * |
| 118 | * @return string [type] [description] |
| 119 | */ |
| 120 | public function start_form() { |
| 121 | |
| 122 | $start_form = apply_filters( 'jet-form-builder/before-start-form', '', $this ); |
| 123 | |
| 124 | $this->add_attribute( 'class', 'jet-form' ); |
| 125 | $this->add_attribute( 'class', 'layout-' . $this->args['fields_layout'] ); |
| 126 | $this->add_attribute( 'class', 'submit-type-' . $this->args['submit_type'] ); |
| 127 | $this->add_attribute( 'action', $this->get_form_action_url() ); |
| 128 | $this->add_attribute( 'method', 'POST' ); |
| 129 | $this->add_attribute( 'data-form-id', $this->form_id ); |
| 130 | |
| 131 | ob_start(); |
| 132 | include $this->get_global_template( 'common/start-form.php' ); |
| 133 | $start_form .= ob_get_clean(); |
| 134 | |
| 135 | $start_form .= apply_filters( 'jet-form-builder/after-start-form', '', $this ); |
| 136 | |
| 137 | return $start_form; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Close form wrapper |
| 142 | * |
| 143 | * @return string [type] [description] |
| 144 | */ |
| 145 | public function end_form() { |
| 146 | |
| 147 | $end_form = apply_filters( 'jet-form-builder/before-end-form', '', $this ); |
| 148 | $form_id = $this->form_id; |
| 149 | |
| 150 | ob_start(); |
| 151 | |
| 152 | if ( Plugin::instance()->captcha ) { |
| 153 | Plugin::instance()->captcha->render( $this->form_id ); |
| 154 | } |
| 155 | |
| 156 | include $this->get_global_template( 'common/end-form.php' ); |
| 157 | |
| 158 | echo $this->maybe_render_styles_block(); |
| 159 | |
| 160 | Plugin::instance()->blocks->register_frontend_assets(); |
| 161 | File_Upload::instance()->enqueue_scripts(); |
| 162 | |
| 163 | $end_form .= ob_get_clean(); |
| 164 | |
| 165 | $end_form .= apply_filters( 'jet-form-builder/after-end-form', '', $this ); |
| 166 | |
| 167 | return $end_form; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | /** |
| 172 | * Render from HTML |
| 173 | * |
| 174 | * @param bool $echo |
| 175 | * |
| 176 | * @return false|string [type] [description] |
| 177 | */ |
| 178 | public function render_form( $echo = true ) { |
| 179 | |
| 180 | if ( $this->pre_render() ) { |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | if ( ! $this->preset()->sanitize_source() ) { |
| 185 | echo 'You are not permitted to submit this form!'; |
| 186 | |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | $form = $this->start_form(); |
| 191 | |
| 192 | Live_Form::instance() |
| 193 | ->set_form_id( $this->form_id ) |
| 194 | ->set_specific_data_for_render( $this->args ) |
| 195 | ->setup_fields( $this->blocks ); |
| 196 | |
| 197 | $form .= Live_Form::force_render_field( 'hidden-field', |
| 198 | array( |
| 199 | 'field_value' => $this->form_id, |
| 200 | 'name' => '_jet_engine_booking_form_id', |
| 201 | ) |
| 202 | ); |
| 203 | |
| 204 | $form .= Live_Form::force_render_field( 'hidden-field', |
| 205 | array( |
| 206 | 'field_value' => $this->get_form_refer_url(), |
| 207 | 'name' => '_jet_engine_refer', |
| 208 | ) |
| 209 | ); |
| 210 | $form .= Live_Form::instance()->maybe_start_page(); |
| 211 | |
| 212 | foreach ( $this->blocks as $block ) { |
| 213 | $form .= render_block( $block ); |
| 214 | } |
| 215 | |
| 216 | $form .= Live_Form::instance()->maybe_end_page( true ); |
| 217 | $form .= $this->end_form(); |
| 218 | |
| 219 | if ( $echo ) { |
| 220 | echo $form; |
| 221 | } else { |
| 222 | return $form; |
| 223 | } |
| 224 | |
| 225 | } |
| 226 | |
| 227 | private function maybe_render_styles_block() { |
| 228 | if ( ! Jet_Style_Manager::is_activated() ) { |
| 229 | return ''; |
| 230 | } |
| 231 | $result = '<div id="jet-sm-gb-style"><style>'; |
| 232 | $result .= Plugin::instance()->post_type->maybe_get_jet_sm_ready_styles( $this->form_id ); |
| 233 | |
| 234 | return $result . '</style></div>'; |
| 235 | } |
| 236 | |
| 237 | public function preset() { |
| 238 | Form_Preset::instance()->set_form_id( $this->form_id ); |
| 239 | |
| 240 | return Form_Preset::instance(); |
| 241 | } |
| 242 | |
| 243 | } |
| 244 | |
| 245 | |
| 246 |