action-button-render.php
4 years ago
base-select-radio-check.php
4 years ago
base.php
4 years ago
calculated-field-render.php
4 years ago
checkbox-field-render.php
4 years ago
date-field-render.php
4 years ago
datetime-field-render.php
4 years ago
form-break-field-render.php
4 years ago
form-builder.php
4 years ago
group-break-field-render.php
4 years ago
heading-field-render.php
4 years ago
media-field-render.php
4 years ago
number-field-render.php
4 years ago
radio-field-render.php
4 years ago
range-field-render.php
4 years ago
repeater-field-render.php
4 years ago
select-field-render.php
4 years ago
submit-field-render.php
4 years ago
text-field-render.php
4 years ago
textarea-field-render.php
4 years ago
time-field-render.php
4 years ago
wysiwyg-field-render.php
4 years ago
form-builder.php
296 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Blocks\Render; |
| 4 | |
| 5 | use Jet_Form_Builder\Classes\Attributes_Trait; |
| 6 | use Jet_Form_Builder\Classes\Get_Template_Trait; |
| 7 | use Jet_Form_Builder\Classes\Tools; |
| 8 | use Jet_Form_Builder\Compatibility\Jet_Style_Manager; |
| 9 | use Jet_Form_Builder\File_Upload; |
| 10 | use Jet_Form_Builder\Live_Form; |
| 11 | use Jet_Form_Builder\Plugin; |
| 12 | use Jet_Form_Builder\Presets\Preset_Manager; |
| 13 | use JET_SM\Gutenberg\Style_Manager; |
| 14 | |
| 15 | // If this file is called directly, abort. |
| 16 | |
| 17 | if ( ! defined( 'WPINC' ) ) { |
| 18 | die; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Form builder class |
| 23 | */ |
| 24 | class Form_Builder { |
| 25 | |
| 26 | use Attributes_Trait; |
| 27 | use Get_Template_Trait; |
| 28 | |
| 29 | public $form_id; |
| 30 | public $post; |
| 31 | public $args = array(); |
| 32 | |
| 33 | private $form_content; |
| 34 | |
| 35 | /** |
| 36 | * Constructor for the class |
| 37 | * |
| 38 | * @param null $form_id |
| 39 | * @param bool $post |
| 40 | * @param array $args |
| 41 | */ |
| 42 | function __construct( $form_id = null, $post = false, $args = array() ) { |
| 43 | |
| 44 | if ( ! $form_id ) { |
| 45 | return; |
| 46 | } |
| 47 | $this->form_id = $form_id; |
| 48 | $this->set_form_args( $args ); |
| 49 | |
| 50 | if ( empty( $post ) ) { |
| 51 | $this->post = get_post(); |
| 52 | } |
| 53 | |
| 54 | $this->form_content = Plugin::instance()->form->get_form_content( $form_id ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @param $arguments |
| 59 | */ |
| 60 | public function set_form_args( $arguments ) { |
| 61 | $this->args = array_intersect_key( $arguments, Plugin::instance()->post_type->get_default_args() ); |
| 62 | |
| 63 | return $this; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | /** |
| 68 | * Returns form action url |
| 69 | * |
| 70 | * @return [type] [description] |
| 71 | */ |
| 72 | public function get_form_action_url() { |
| 73 | |
| 74 | $action = add_query_arg( |
| 75 | array( |
| 76 | Plugin::instance()->form_handler->hook_key => Plugin::instance()->form_handler->hook_val, |
| 77 | ), |
| 78 | home_url( '/' ) |
| 79 | ); |
| 80 | |
| 81 | return apply_filters( 'jet-form-builder/form-action-url', $action, $this ); |
| 82 | |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns form refer url |
| 87 | * |
| 88 | * @return [type] [description] |
| 89 | */ |
| 90 | public function get_form_refer_url() { |
| 91 | |
| 92 | global $wp; |
| 93 | |
| 94 | $refer = home_url( $wp->request ); |
| 95 | |
| 96 | if ( ! empty( $_SERVER['QUERY_STRING'] ) ) { |
| 97 | $refer = trailingslashit( $refer ) . '?' . $_SERVER['QUERY_STRING']; |
| 98 | } |
| 99 | |
| 100 | return apply_filters( 'jet-form-builder/form-refer-url', $refer, $this ); |
| 101 | |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @return mixed|void |
| 106 | */ |
| 107 | public function pre_render() { |
| 108 | return apply_filters( 'jet-form-builder/pre-render/' . $this->form_id, false ); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * Open form wrapper |
| 114 | * |
| 115 | * @return string [type] [description] |
| 116 | */ |
| 117 | public function start_form() { |
| 118 | |
| 119 | Plugin::instance()->blocks->enqueue_frontend_assets(); |
| 120 | |
| 121 | $start_form = apply_filters( 'jet-form-builder/before-start-form', '', $this ); |
| 122 | |
| 123 | $start_form .= $this->maybe_render_fonts_block(); |
| 124 | $start_form .= $this->render_styles(); |
| 125 | |
| 126 | $this->add_attribute( 'class', 'jet-form-builder' ); |
| 127 | $this->add_attribute( 'class', 'layout-' . Live_Form::instance()->spec_data->fields_layout ); |
| 128 | $this->add_attribute( 'class', 'submit-type-' . Live_Form::instance()->spec_data->submit_type ); |
| 129 | $this->add_attribute( 'action', $this->get_form_action_url() ); |
| 130 | $this->add_attribute( 'method', 'POST' ); |
| 131 | $this->add_attribute( 'data-form-id', $this->form_id ); |
| 132 | $this->add_attribute( 'data-layout', Live_Form::instance()->spec_data->fields_layout ); |
| 133 | |
| 134 | ob_start(); |
| 135 | include $this->get_global_template( 'common/start-form.php' ); |
| 136 | $start_form .= ob_get_clean(); |
| 137 | |
| 138 | $start_form .= apply_filters( 'jet-form-builder/after-start-form', '', $this ); |
| 139 | |
| 140 | return $start_form; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Close form wrapper |
| 145 | * |
| 146 | * @return string [type] [description] |
| 147 | */ |
| 148 | public function end_form() { |
| 149 | |
| 150 | $end_form = apply_filters( 'jet-form-builder/before-end-form', '', $this ); |
| 151 | $form_id = $this->form_id; |
| 152 | |
| 153 | ob_start(); |
| 154 | |
| 155 | if ( Plugin::instance()->captcha ) { |
| 156 | Plugin::instance()->captcha->render( $this->form_id ); |
| 157 | } |
| 158 | |
| 159 | include $this->get_global_template( 'common/end-form.php' ); |
| 160 | |
| 161 | $end_form .= ob_get_clean(); |
| 162 | |
| 163 | $end_form .= apply_filters( 'jet-form-builder/after-end-form', '', $this ); |
| 164 | |
| 165 | return $end_form; |
| 166 | } |
| 167 | |
| 168 | private function render_styles() { |
| 169 | if ( wp_doing_ajax() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { |
| 170 | return $this->get_inline_styles(); |
| 171 | } |
| 172 | |
| 173 | if ( Tools::is_elementor_editor() ) { |
| 174 | return $this->get_inline_styles(); |
| 175 | } |
| 176 | wp_enqueue_style( 'jet-form-builder-frontend' ); |
| 177 | |
| 178 | return $this->get_inline_styles(); |
| 179 | } |
| 180 | |
| 181 | private function get_inline_styles(): string { |
| 182 | return sprintf( |
| 183 | '<style id="jet-form-builder-%s-inline-css">%s</style>', |
| 184 | $this->form_id, |
| 185 | Plugin::instance()->post_type->maybe_get_jet_sm_ready_styles( $this->form_id ) |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | private function maybe_render_fonts_block(): string { |
| 190 | if ( |
| 191 | ! Jet_Style_Manager::is_activated() |
| 192 | || ! method_exists( Style_Manager::get_instance(), 'get_blocks_fonts' ) |
| 193 | ) { |
| 194 | return ''; |
| 195 | } |
| 196 | $fonts = Style_Manager::get_instance()->get_blocks_fonts( $this->form_id ); |
| 197 | |
| 198 | if ( ! $fonts ) { |
| 199 | return ''; |
| 200 | } |
| 201 | |
| 202 | $fonts = trim( $fonts, '"' ); |
| 203 | $fonts = wp_unslash( $fonts ); |
| 204 | |
| 205 | return wp_kses( |
| 206 | $fonts, |
| 207 | array( |
| 208 | 'link' => array( |
| 209 | 'href' => true, |
| 210 | 'rel' => true, |
| 211 | ), |
| 212 | ) |
| 213 | ); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | /** |
| 218 | * Render from HTML |
| 219 | * |
| 220 | * @param bool $echo |
| 221 | * |
| 222 | * @return false|string [type] [description] |
| 223 | */ |
| 224 | public function render_form() { |
| 225 | |
| 226 | if ( $this->pre_render() ) { |
| 227 | return ''; |
| 228 | } |
| 229 | |
| 230 | if ( ! $this->preset()->general()->sanitize_source() ) { |
| 231 | echo 'You are not permitted to submit this form!'; |
| 232 | |
| 233 | return ''; |
| 234 | } |
| 235 | |
| 236 | $this->form_content = Live_Form::instance() |
| 237 | ->set_form_id( $this->form_id ) |
| 238 | ->set_specific_data_for_render( $this->args ) |
| 239 | ->setup_fields( $this->form_content ); |
| 240 | |
| 241 | $form = $this->start_form(); |
| 242 | |
| 243 | $form .= wp_nonce_field( Live_Form::instance()->get_nonce_id(), '_wpnonce', true, false ); |
| 244 | |
| 245 | $form .= Live_Form::force_render_field( |
| 246 | 'hidden-field', |
| 247 | array( |
| 248 | 'field_value' => $this->form_id, |
| 249 | 'name' => Plugin::instance()->form_handler->form_key, |
| 250 | '_static_value' => true, |
| 251 | ) |
| 252 | ); |
| 253 | |
| 254 | $form .= Live_Form::force_render_field( |
| 255 | 'hidden-field', |
| 256 | array( |
| 257 | 'field_value' => $this->get_form_refer_url(), |
| 258 | 'name' => Plugin::instance()->form_handler->refer_key, |
| 259 | '_static_value' => true, |
| 260 | ) |
| 261 | ); |
| 262 | $form .= Live_Form::force_render_field( |
| 263 | 'hidden-field', |
| 264 | array( |
| 265 | 'field_value' => Live_Form::instance()->post->ID ?? - 1, |
| 266 | 'name' => Plugin::instance()->form_handler->post_id_key, |
| 267 | '_static_value' => true, |
| 268 | ) |
| 269 | ); |
| 270 | $form .= Live_Form::instance()->maybe_progress_pages(); |
| 271 | |
| 272 | $form .= Live_Form::instance()->maybe_start_page( true ); |
| 273 | |
| 274 | foreach ( $this->form_content as $block ) { |
| 275 | $form .= render_block( $block ); |
| 276 | } |
| 277 | |
| 278 | $form .= Live_Form::instance()->maybe_end_page( true ); |
| 279 | $form .= $this->end_form(); |
| 280 | |
| 281 | Live_Form::clear(); |
| 282 | Preset_Manager::clear(); |
| 283 | |
| 284 | return $form; |
| 285 | } |
| 286 | |
| 287 | public function preset() { |
| 288 | Preset_Manager::instance()->set_form_id( $this->form_id ); |
| 289 | |
| 290 | return Preset_Manager::instance(); |
| 291 | } |
| 292 | |
| 293 | } |
| 294 | |
| 295 | |
| 296 |