| 1 |
<?php |
| 2 |
|
| 3 |
namespace Jet_Form_Builder\Blocks\Render; |
| 4 |
|
| 5 |
|
| 6 |
use Jet_Form_Builder\Classes\Attributes_Trait; |
| 7 |
use Jet_Form_Builder\Classes\Get_Template_Trait; |
| 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 function Crontrol\Schedule\delete; |
| 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 |
$start_form = apply_filters( 'jet-form-builder/before-start-form', '', $this ); |
| 120 |
|
| 121 |
$this->add_attribute( 'class', 'jet-form-builder' ); |
| 122 |
$this->add_attribute( 'class', 'layout-' . $this->args['fields_layout'] ); |
| 123 |
$this->add_attribute( 'class', 'submit-type-' . $this->args['submit_type'] ); |
| 124 |
$this->add_attribute( 'action', $this->get_form_action_url() ); |
| 125 |
$this->add_attribute( 'method', 'POST' ); |
| 126 |
$this->add_attribute( 'data-form-id', $this->form_id ); |
| 127 |
$this->add_attribute( 'data-layout', $this->args['fields_layout'] ); |
| 128 |
|
| 129 |
ob_start(); |
| 130 |
echo $this->maybe_render_styles_block(); |
| 131 |
|
| 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 |
Plugin::instance()->blocks->enqueue_frontend_assets(); |
| 159 |
File_Upload::instance()->enqueue_scripts(); |
| 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 |
|
| 169 |
/** |
| 170 |
* Render from HTML |
| 171 |
* |
| 172 |
* @param bool $echo |
| 173 |
* |
| 174 |
* @return false|string [type] [description] |
| 175 |
*/ |
| 176 |
public function render_form( $echo = true ) { |
| 177 |
|
| 178 |
if ( $this->pre_render() ) { |
| 179 |
return ''; |
| 180 |
} |
| 181 |
|
| 182 |
if ( ! $this->preset()->sanitize_source() ) { |
| 183 |
echo 'You are not permitted to submit this form!'; |
| 184 |
return ''; |
| 185 |
} |
| 186 |
|
| 187 |
$form = $this->start_form(); |
| 188 |
|
| 189 |
Live_Form::instance() |
| 190 |
->set_form_id( $this->form_id ) |
| 191 |
->set_specific_data_for_render( $this->args ) |
| 192 |
->setup_fields( $this->form_content ); |
| 193 |
|
| 194 |
$form .= Live_Form::force_render_field( 'hidden-field', |
| 195 |
array( |
| 196 |
'field_value' => $this->form_id, |
| 197 |
'name' => Plugin::instance()->form_handler->form_key, |
| 198 |
) |
| 199 |
); |
| 200 |
|
| 201 |
$form .= Live_Form::force_render_field( 'hidden-field', |
| 202 |
array( |
| 203 |
'field_value' => $this->get_form_refer_url(), |
| 204 |
'name' => Plugin::instance()->form_handler->refer_key, |
| 205 |
) |
| 206 |
); |
| 207 |
$form .= Live_Form::force_render_field( 'hidden-field', |
| 208 |
array( |
| 209 |
'field_value' => Live_Form::instance()->post->ID, |
| 210 |
'name' => Plugin::instance()->form_handler->post_id_key, |
| 211 |
) |
| 212 |
); |
| 213 |
$form .= Live_Form::instance()->maybe_progress_pages(); |
| 214 |
|
| 215 |
$form .= Live_Form::instance()->maybe_start_page(); |
| 216 |
|
| 217 |
$form .= do_blocks( $this->form_content ); |
| 218 |
|
| 219 |
$form .= Live_Form::instance()->maybe_end_page( true ); |
| 220 |
$form .= $this->end_form(); |
| 221 |
|
| 222 |
Live_Form::clear(); |
| 223 |
|
| 224 |
if ( $echo ) { |
| 225 |
echo $form; |
| 226 |
} else { |
| 227 |
return $form; |
| 228 |
} |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
private function maybe_render_styles_block() { |
| 233 |
if ( ! Jet_Style_Manager::is_activated() ) { |
| 234 |
return ''; |
| 235 |
} |
| 236 |
$result = '<div id="jet-sm-gb-style--fb"><style>'; |
| 237 |
$result .= Plugin::instance()->post_type->maybe_get_jet_sm_ready_styles( $this->form_id ); |
| 238 |
|
| 239 |
return $result . '</style></div>'; |
| 240 |
} |
| 241 |
|
| 242 |
public function preset() { |
| 243 |
Preset_Manager::instance()->set_form_id( $this->form_id ); |
| 244 |
|
| 245 |
return Preset_Manager::instance(); |
| 246 |
} |
| 247 |
|
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|