PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.6
JetFormBuilder — Dynamic Blocks Form Builder v3.6.6
3.6.6.1 3.6.6 3.6.5.4 3.6.5.3 3.6.5.2 3.6.5.1 3.6.5 3.6.4.2 3.6.4.1 3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 All 134 releases
← All changes | includes/blocks/render/form-builder.php +248 -241 1.0.0 → 3.6.6 View file →
@@ -1,241 +1,248 @@
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\Form_Preset;
12 -use Jet_Form_Builder\Live_Form;
13 -use Jet_Form_Builder\Plugin;
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 -
28 - use Get_Template_Trait;
29 -
30 -
31 - public $form_id;
32 - public $post;
33 - public $args = array();
34 -
35 - private $blocks;
36 -
37 - /**
38 - * Constructor for the class
39 - *
40 - * @param null $form_id
41 - * @param bool $post
42 - * @param array $args
43 - */
44 - function __construct( $form_id = null, $post = false, $args = array() ) {
45 -
46 - if ( ! $form_id ) {
47 - return;
48 - }
49 - $this->form_id = $form_id;
50 - $this->set_form_args( $args );
51 -
52 - if ( empty( $post ) ) {
53 - $this->post = get_post();
54 - }
55 -
56 - $this->blocks = Plugin::instance()->form->get_form_by_id( $form_id );
57 - }
58 -
59 - /**
60 - * @param $arguments
61 - */
62 - public function set_form_args( $arguments ) {
63 - $this->args = array_intersect_key( $arguments, Plugin::instance()->post_type->get_default_args() );
64 -
65 - return $this;
66 - }
67 -
68 -
69 - /**
70 - * Returns form action url
71 - *
72 - * @return [type] [description]
73 - */
74 - public function get_form_action_url() {
75 -
76 - $action = add_query_arg(
77 - array(
78 - Plugin::instance()->form_handler->hook_key => Plugin::instance()->form_handler->hook_val,
79 - ),
80 - home_url( '/' )
81 - );
82 -
83 - return apply_filters( 'jet-form-builder/form-action-url', $action, $this );
84 -
85 - }
86 -
87 - /**
88 - * Returns form refer url
89 - *
90 - * @return [type] [description]
91 - */
92 - public function get_form_refer_url() {
93 -
94 - global $wp;
95 -
96 - $refer = home_url( $wp->request );
97 -
98 - if ( ! empty( $_SERVER['QUERY_STRING'] ) ) {
99 - $refer = trailingslashit( $refer ) . '?' . $_SERVER['QUERY_STRING'];
100 - }
101 -
102 - return apply_filters( 'jet-form-builder/form-refer-url', $refer, $this );
103 -
104 - }
105 -
106 - /**
107 - * @return mixed|void
108 - */
109 - public function pre_render() {
110 - return apply_filters( 'jet-form-builder/pre-render/' . $this->form_id, false );
111 - }
112 -
113 -
114 - /**
115 - * Open form wrapper
116 - *
117 - * @return string [type] [description]
118 - */
119 - public function start_form() {
120 -
121 - $start_form = apply_filters( 'jet-form-builder/before-start-form', '', $this );
122 -
123 - $this->add_attribute( 'class', 'jet-form' );
124 - $this->add_attribute( 'class', 'layout-' . $this->args['fields_layout'] );
125 - $this->add_attribute( 'class', 'submit-type-' . $this->args['submit_type'] );
126 - $this->add_attribute( 'action', $this->get_form_action_url() );
127 - $this->add_attribute( 'method', 'POST' );
128 - $this->add_attribute( 'data-form-id', $this->form_id );
129 -
130 - ob_start();
131 - include $this->get_global_template( 'common/start-form.php' );
132 - $start_form .= ob_get_clean();
133 -
134 - $start_form .= apply_filters( 'jet-form-builder/after-start-form', '', $this );
135 -
136 - return $start_form;
137 - }
138 -
139 - /**
140 - * Close form wrapper
141 - *
142 - * @return string [type] [description]
143 - */
144 - public function end_form() {
145 -
146 - $end_form = apply_filters( 'jet-form-builder/before-end-form', '', $this );
147 - $form_id = $this->form_id;
148 -
149 - ob_start();
150 -
151 - if ( Plugin::instance()->captcha ) {
152 - Plugin::instance()->captcha->render( $this->form_id );
153 - }
154 -
155 - include $this->get_global_template( 'common/end-form.php' );
156 -
157 - echo $this->maybe_render_styles_block();
158 -
159 - $end_form .= ob_get_clean();
160 -
161 - $end_form .= apply_filters( 'jet-form-builder/after-end-form', '', $this );
162 -
163 - return $end_form;
164 - }
165 -
166 -
167 - /**
168 - * Render from HTML
169 - *
170 - * @param bool $echo
171 - *
172 - * @return false|string [type] [description]
173 - */
174 - public function render_form( $echo = true ) {
175 -
176 - if ( $this->pre_render() ) {
177 - return;
178 - }
179 -
180 - if ( ! $this->preset()->sanitize_source() ) {
181 - echo 'You are not permitted to submit this form!';
182 -
183 - return;
184 - }
185 -
186 - $form = $this->start_form();
187 -
188 - Live_Form::instance()
189 - ->set_form_id( $this->form_id )
190 - ->set_specific_data_for_render( $this->args )
191 - ->setup_fields( $this->blocks );
192 -
193 - $form .= Live_Form::force_render_field( 'hidden-field',
194 - array(
195 - 'field_value' => $this->form_id,
196 - 'name' => '_jet_engine_booking_form_id',
197 - )
198 - );
199 -
200 - $form .= Live_Form::force_render_field( 'hidden-field',
201 - array(
202 - 'field_value' => $this->get_form_refer_url(),
203 - 'name' => '_jet_engine_refer',
204 - )
205 - );
206 - $form .= Live_Form::instance()->maybe_start_page();
207 -
208 - foreach ( $this->blocks as $block ) {
209 - $form .= render_block( $block );
210 - }
211 -
212 - $form .= Live_Form::instance()->maybe_end_page( true );
213 - $form .= $this->end_form();
214 -
215 - if ( $echo ) {
216 - echo $form;
217 - } else {
218 - return $form;
219 - }
220 -
221 - }
222 -
223 - private function maybe_render_styles_block() {
224 - if ( ! Jet_Style_Manager::is_activated() ) {
225 - return '';
226 - }
227 - $result = '<div id="jet-sm-gb-style"><style>';
228 - $result .= Plugin::instance()->post_type->maybe_get_jet_sm_ready_styles( $this->form_id );
229 -
230 - return $result . '</style></div>';
231 - }
232 -
233 - public function preset() {
234 - Form_Preset::instance()->set_form_id( $this->form_id );
235 -
236 - return Form_Preset::instance();
237 - }
238 -
239 -}
240 -
241 -
1 +<?php
2 +
3 +namespace Jet_Form_Builder\Blocks\Render;
4 +
5 +use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager;
6 +use Jet_Form_Builder\Blocks\Module;
7 +use Jet_Form_Builder\Classes\Arguments\Form_Arguments;
8 +use Jet_Form_Builder\Classes\Attributes_Trait;
9 +use Jet_Form_Builder\Classes\Builder_Helper;
10 +use Jet_Form_Builder\Classes\Compatibility;
11 +use Jet_Form_Builder\Classes\Get_Template_Trait;
12 +use Jet_Form_Builder\Classes\Http\Http_Tools;
13 +use Jet_Form_Builder\Classes\Tools;
14 +use Jet_Form_Builder\Live_Form;
15 +use Jet_Form_Builder\Plugin;
16 +use JET_SM\Gutenberg\Style_Manager;
17 +
18 +// If this file is called directly, abort.
19 +
20 +if ( ! defined( 'WPINC' ) ) {
21 + die;
22 +}
23 +
24 +/**
25 + * Form builder class
26 + */
27 +class Form_Builder {
28 +
29 + use Attributes_Trait;
30 + use Get_Template_Trait;
31 +
32 + public $form_id;
33 + public $post;
34 + public $args = array();
35 +
36 + private $form_content;
37 +
38 + /**
39 + * Constructor for the class
40 + *
41 + * @param null $form_id
42 + * @param array $args
43 + */
44 + public function __construct( $form_id = null, $args = array() ) {
45 +
46 + if ( ! $form_id ) {
47 + return;
48 + }
49 + $this->form_id = $form_id;
50 + $this->set_form_args( $args );
51 + }
52 +
53 + /**
54 + * Render from HTML
55 + *
56 + * @return false|string [type] [description]
57 + */
58 + public function render_form() {
59 +
60 + if ( $this->pre_render() ) {
61 + return '';
62 + }
63 +
64 + if ( ! jet_fb_preset( $this->form_id )->general()->sanitize_source() ) {
65 + echo 'You are not permitted to submit this form!';
66 +
67 + return '';
68 + }
69 +
70 + $blocks = Live_Form::instance()
71 + ->set_form_id( $this->form_id )
72 + ->set_specific_data_for_render( $this->args )
73 + ->setup_fields();
74 +
75 + $form = $this->start_form();
76 +
77 + $form .= Form_Hidden_Fields::render();
78 +
79 + $form .= jet_fb_live()->maybe_progress_pages();
80 + $form .= jet_fb_live()->maybe_start_page( true );
81 +
82 + foreach ( $blocks as $block ) {
83 + $form .= render_block( $block );
84 + }
85 +
86 + $form .= jet_fb_live()->maybe_end_page( true );
87 + $form .= $this->end_form();
88 +
89 + return $form;
90 + }
91 +
92 +
93 + /**
94 + * @param $arguments
95 + *
96 + * @return Form_Builder
97 + */
98 + public function set_form_args( $arguments ): Form_Builder {
99 + $this->args = array_intersect_key( $arguments, Form_Arguments::arguments() );
100 +
101 + $this->args['className'] = $arguments['className'] ?? '';
102 + $this->args['anchor'] = $arguments['anchor'] ?? '';
103 +
104 + return $this;
105 + }
106 +
107 +
108 + /**
109 + * @return mixed|void
110 + */
111 + public function pre_render() {
112 + return apply_filters( 'jet-form-builder/pre-render/' . $this->form_id, false );
113 + }
114 +
115 + /**
116 + * Open form wrapper
117 + *
118 + * @return string [type] [description]
119 + */
120 + public function start_form() {
121 + Plugin::instance()->blocks->enqueue_frontend_styles();
122 +
123 + $start_form = apply_filters( 'jet-form-builder/before-start-form', '', $this );
124 +
125 + $start_form .= $this->maybe_render_fonts_block();
126 + $start_form .= $this->render_styles();
127 +
128 + $base_classes = sprintf(
129 + 'jet-form-builder layout-%1$s submit-type-%2$s',
130 + jet_fb_live_args()->fields_layout,
131 + jet_fb_live_args()->submit_type
132 + );
133 +
134 + /**
135 + * We use form builder methods to add attributes, as the form can be displayed
136 + * not only with the Gutenberg block, but also with a third-party builder widget
137 + */
138 + $this->add_attribute( 'id', $this->args['anchor'] );
139 + $this->add_attribute( 'class', $base_classes );
140 + $this->add_attribute( 'action', Http_Tools::get_form_action_url() );
141 + $this->add_attribute( 'method', \WP_REST_Server::CREATABLE );
142 + $this->add_attribute( 'data-form-id', $this->form_id );
143 + $this->add_attribute( 'data-layout', jet_fb_live_args()->fields_layout );
144 + $this->add_attribute( 'enctype', 'multipart/form-data' );
145 + $this->add_attribute( 'data-validation-type', jet_fb_live_args()->validation_type );
146 + $this->add_attribute( 'data-clear', jet_fb_live_args()->clear ? 1 : '' );
147 + $this->add_attribute( 'novalidate' );
148 +
149 + $options_tab = Tab_Handler_Manager::instance()->tab( 'options-tab' );
150 + $options = $options_tab->get_options();
151 + $method = $options['ssr_validation_method'] ?? 'rest';
152 + $this->add_attribute( 'ssr_validation_method', $method );
153 +
154 + /**
155 + * Backward compatibility.
156 + * We leave only the basic ones in the classes, because the value
157 + * from className is added "from above"
158 + *
159 + * Without it, custom classes will be on the <form> tag and the outer <div>
160 + */
161 + $this->add_attribute(
162 + 'class',
163 + Compatibility::has_jet_sm() ? '' : $this->args['className']
164 + );
165 +
166 + $start_form .= sprintf(
167 + '<form %1$s>',
168 + $this->get_attributes_string()
169 + );
170 +
171 + $start_form .= apply_filters( 'jet-form-builder/after-start-form', '', $this );
172 +
173 + return $start_form;
174 + }
175 +
176 + public function end_form() {
177 + /** @var Module $blocks */
178 + /** @noinspection PhpUnhandledExceptionInspection */
179 + $blocks = jet_form_builder()->module( 'blocks' );
180 +
181 + $blocks->enqueue_frontend_assets();
182 +
183 + $end_form = apply_filters( 'jet-form-builder/before-end-form', '', $this );
184 + $form_id = $this->form_id;
185 +
186 + ob_start();
187 +
188 + include $this->get_global_template( 'common/end-form.php' );
189 +
190 + $end_form .= ob_get_clean();
191 +
192 + $end_form .= apply_filters( 'jet-form-builder/after-end-form', '', $this );
193 +
194 + return $end_form;
195 + }
196 +
197 + private function render_styles(): string {
198 + if ( wp_doing_ajax() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
199 + return $this->get_inline_styles();
200 + }
201 +
202 + if ( Tools::is_elementor_editor() ) {
203 + return $this->get_inline_styles();
204 + }
205 +
206 + Builder_Helper::enqueue_global_styles();
207 + Builder_Helper::enqueue_style_form( $this->form_id );
208 +
209 + return '';
210 + }
211 +
212 + private function get_inline_styles(): string {
213 + return sprintf(
214 + '<style id="jet-form-builder-%s-inline-css">%s</style>',
215 + $this->form_id,
216 + Plugin::instance()->post_type->maybe_get_jet_sm_ready_styles( $this->form_id )
217 + );
218 + }
219 +
220 +
221 + private function maybe_render_fonts_block(): string {
222 + if (
223 + ! Compatibility::has_jet_sm()
224 + || ! method_exists( Style_Manager::get_instance(), 'get_blocks_fonts' )
225 + ) {
226 + return '';
227 + }
228 + $fonts = Style_Manager::get_instance()->get_blocks_fonts( $this->form_id );
229 +
230 + if ( ! $fonts ) {
231 + return '';
232 + }
233 +
234 + $fonts = trim( $fonts, '"' );
235 + $fonts = wp_unslash( $fonts );
236 +
237 + return wp_kses(
238 + $fonts,
239 + array(
240 + 'link' => array(
241 + 'href' => true,
242 + 'rel' => true,
243 + ),
244 + )
245 + );
246 + }
247 +
248 +}