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/base.php +280 -200 1.0.1 → 3.6.6 View file →
@@ -1,200 +1,280 @@
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\Classes\Tools;
9 -use Jet_Form_Builder\Form_Preset;
10 -use Jet_Form_Builder\Live_Form;
11 -use Jet_Form_Builder\Plugin;
12 -
13 -// If this file is called directly, abort.
14 -
15 -if ( ! defined( 'WPINC' ) ) {
16 - die;
17 -}
18 -
19 -/**
20 - * Define Base_Type class
21 - */
22 -abstract class Base {
23 -
24 - use Attributes_Trait;
25 - use Get_Template_Trait;
26 -
27 - public $form_id;
28 - public $block_type;
29 - public $content;
30 - public $live_form;
31 - public $preset;
32 -
33 -
34 - public function __construct( $block_type ) {
35 - $this->form_id = Live_Form::instance()->form_id;
36 -
37 - $this->set_live_form();
38 - $this->set_block_type( $block_type );
39 - $this->set_form_preset();
40 - }
41 -
42 - abstract public function get_name();
43 -
44 - public function set_live_form() {
45 - $this->live_form = Live_Form::instance();
46 - }
47 -
48 - public function set_form_preset() {
49 - $this->preset = Form_Preset::instance();
50 - }
51 -
52 - public function set_block_type( $block_type ) {
53 - $this->block_type = $block_type;
54 - }
55 -
56 - private function is_field( $needle ) {
57 - return Plugin::instance()->form->is_field( $this->block_type->block_attrs['blockName'], $needle );
58 - }
59 -
60 -
61 - /**
62 - * Returns field label
63 - *
64 - * @return [type] [description]
65 - */
66 - public function get_field_label() {
67 -
68 - ob_start();
69 - if ( ! empty( $this->block_type->block_attrs['label'] ) && $this->label_allowed() ) {
70 - $args = $this->block_type->block_attrs;
71 - include $this->block_type->get_common_template( 'field-label.php' );
72 - }
73 -
74 - return ob_get_clean();
75 -
76 - }
77 -
78 - /**
79 - * Returns field description
80 - *
81 - * @return [type] [description]
82 - */
83 - public function get_field_desc() {
84 -
85 - ob_start();
86 - if ( ! empty( $this->block_type->block_attrs['desc'] ) && $this->label_allowed() ) {
87 - $args = $this->block_type->block_attrs;
88 - include $this->block_type->get_common_template( 'field-description.php' );
89 - }
90 -
91 - return ob_get_clean();
92 -
93 - }
94 -
95 - /**
96 - * Defines if this form control supports label
97 - *
98 - * @return [type] [description]
99 - */
100 - public function label_allowed() {
101 - return true;
102 - }
103 -
104 - public function render( $wp_block = null, $template = null ) {
105 -
106 - if ( ! $this->live_form->form_id ) {
107 - return;
108 - }
109 -
110 - $defaults = array(
111 - 'default' => '',
112 - 'name' => '',
113 - 'placeholder' => '',
114 - 'required' => false,
115 - );
116 -
117 - $sanitized_args = array();
118 -
119 - foreach ( $this->block_type->block_attrs as $key => $value ) {
120 - $sanitized_args[ $key ] = $value;
121 - }
122 - $args = wp_parse_args( $sanitized_args, $defaults );
123 -
124 - $template_name = $this->get_name();
125 -
126 - if ( is_null( $template ) ) {
127 - $template = $this->block_type->get_field_template( $template_name . '.php' );
128 - }
129 -
130 - $label = $this->get_field_label();
131 - $desc = $this->get_field_desc();
132 - $layout = $this->live_form ? $this->live_form->spec_data->fields_layout : 'column';
133 -
134 - $args['default'] = $this->get_default_from_preset( $args );
135 -
136 - if ( 'column' === $layout ) {
137 - ob_start();
138 - include $this->block_type->get_common_template( 'field-column.php' );
139 - $result_field = ob_get_clean();
140 - } else {
141 - ob_start();
142 - include $this->block_type->get_common_template( 'field-row.php' );
143 - $result_field = ob_get_clean();
144 - }
145 -
146 - return $result_field;
147 - }
148 -
149 - private function get_default_from_preset( $args ) {
150 - if ( ! $this->preset ) {
151 - return $args['default'];
152 - }
153 -
154 - $preset_value = $this->preset->get_field_value( $args['name'], $args );
155 - $result_value = '';
156 -
157 - if ( ! $this->live_form->current_repeater ) {
158 -
159 - if ( $preset_value['rewrite'] ) {
160 - $result_value = $preset_value['value'];
161 - } elseif ( ! $this->is_field( 'hidden' ) ) {
162 - $result_value = $this->preset->maybe_adjust_value( $args );
163 - }
164 -
165 - } elseif ( ! empty( $this->live_form->current_repeater['values'] )
166 - && isset( $this->live_form->current_repeater['values'][ $args['name'] ] ) ) {
167 - $result_value = $this->live_form->current_repeater['values'][ $args['name'] ];
168 - }
169 -
170 -
171 - if ( $result_value ) {
172 - return $result_value;
173 - } elseif ( $preset_value['rewrite'] ) { // if default was set by dynamic preset (json format)
174 - return '';
175 - } else {
176 - return $args['default'];
177 - }
178 - }
179 -
180 - public function render_disabled_message_form_break( $args ) {
181 - $format = '<div class="jet-form-builder__next-page-msg" %2$s>%1$s</div>';
182 -
183 - if ( ! empty( $args['page_break_disabled'] ) && ! Tools::is_editor() ) {
184 - return sprintf( $format, $args['page_break_disabled'], '' );
185 -
186 - } elseif ( Tools::is_editor() ) {
187 -
188 - $message = ! empty( $args['page_break_disabled'] ) ? $args['page_break_disabled'] : 'Disabled message';
189 -
190 - return sprintf( $format, $message, $this->get_style_attrs( array( 'display:block' ) ) );
191 - }
192 -
193 - return '';
194 - }
195 -
196 - public function get_style_attrs( $style ) {
197 - return 'style=' . implode( ';', $style );
198 - }
199 -
200 -}
1 +<?php
2 +
3 +namespace Jet_Form_Builder\Blocks\Render;
4 +
5 +use Jet_Form_Builder\Blocks\Button_Types\Button_Next;
6 +use Jet_Form_Builder\Blocks\Dynamic_Value;
7 +use Jet_Form_Builder\Blocks\Modules\Fields_Errors\Error_Handler;
8 +use Jet_Form_Builder\Classes\Attributes_Trait;
9 +use Jet_Form_Builder\Classes\Builder_Helper;
10 +use Jet_Form_Builder\Classes\Get_Template_Trait;
11 +use Jet_Form_Builder\Classes\Tools;
12 +use Jet_Form_Builder\Live_Form;
13 +use Jet_Form_Builder\Classes\Regexp_Tools;
14 +
15 +// If this file is called directly, abort.
16 +
17 +if ( ! defined( 'WPINC' ) ) {
18 + die;
19 +}
20 +
21 +/**
22 + * Define Base_Type class
23 + */
24 +abstract class Base {
25 +
26 + use Attributes_Trait;
27 + use Get_Template_Trait;
28 +
29 + public $form_id;
30 +
31 + /**
32 + * @var \Jet_Form_Builder\Blocks\Types\Base
33 + */
34 + public $block_type;
35 + public $content;
36 +
37 + /**
38 + * @var Live_Form
39 + */
40 + public $live_form;
41 +
42 + /**
43 + * @var array
44 + */
45 + public $args;
46 +
47 + const FIELD_ERROR_CLASS = 'field-has-error';
48 +
49 + public function __construct( $block_type ) {
50 + $this->form_id = Live_Form::instance()->form_id;
51 +
52 + $this->set_live_form();
53 + $this->set_block_type( $block_type );
54 + }
55 +
56 + abstract public function get_name();
57 +
58 + public function set_live_form() {
59 + $this->live_form = Live_Form::instance();
60 + }
61 +
62 + public function set_block_type( $block_type ) {
63 + $this->block_type = $block_type;
64 + }
65 +
66 + /**
67 + * Returns field label
68 + *
69 + * @return [type] [description]
70 + */
71 + public function get_field_label() {
72 + if ( empty( $this->block_type->block_attrs['label'] ) || ! $this->label_allowed() ) {
73 + return '';
74 + }
75 +
76 + $args = $this->block_type->block_attrs;
77 +
78 + $label_wrapper = new class() {
79 + use Attributes_Trait;
80 + };
81 + $label_text = clone $label_wrapper;
82 +
83 + if ( isset( $args['type'] ) && 'heading-field' === $args['type'] ) {
84 + $label_wrapper->add_attribute( 'class', 'jet-form-builder__heading' );
85 + $label_wrapper->add_attribute( 'class', $args['class_name'] );
86 + } else {
87 + $label_wrapper->add_attribute( 'class', 'jet-form-builder__label' );
88 + }
89 +
90 + $label_text->add_attribute( 'class', 'jet-form-builder__label-text' );
91 + $label_text_tag = esc_attr( $this->get_fields_label_tag() );
92 +
93 + if ( 'label' === $label_text_tag ) {
94 + $label_text->add_attribute( 'for', $this->block_type->get_field_id( $args, 'label' ) );
95 + }
96 +
97 + ob_start();
98 +
99 + include $this->block_type->get_common_template( 'field-label.php' );
100 +
101 + return ob_get_clean();
102 + }
103 +
104 + /**
105 + * @since 3.1.8
106 + *
107 + * @return string
108 + */
109 + protected function get_fields_label_tag(): string {
110 + return jet_fb_live_args()->fields_label_tag;
111 + }
112 +
113 + /**
114 + * Returns field description
115 + *
116 + * @return [type] [description]
117 + */
118 + public function get_field_desc() {
119 +
120 + ob_start();
121 + if ( ! empty( $this->block_type->block_attrs['desc'] ) && $this->label_allowed() ) {
122 + $args = $this->block_type->block_attrs;
123 + include $this->block_type->get_common_template( 'field-description.php' );
124 + }
125 +
126 + return ob_get_clean();
127 + }
128 +
129 + /**
130 + * Defines if this form control supports label
131 + *
132 + * @return [type] [description]
133 + */
134 + public function label_allowed() {
135 + return true;
136 + }
137 +
138 + public function render_without_layout( $template = null, $args = array() ) {
139 + $template_name = $this->get_name();
140 +
141 + if ( empty( $args ) ) {
142 + $args = $this->get_default_args_with_filter();
143 + }
144 +
145 + $this->args = $args;
146 + $this->before_render( $args );
147 +
148 + if ( is_null( $template ) ) {
149 + $template = $this->block_type->get_field_template( $template_name . '.php' );
150 + }
151 +
152 + if ( $template && Tools::is_readable( $template ) ) {
153 + ob_start();
154 + include $template;
155 + $template = ob_get_clean();
156 + }
157 +
158 + return $template;
159 + }
160 +
161 + public function get_default_args() {
162 + $defaults = array(
163 + 'default' => '',
164 + 'name' => '',
165 + 'placeholder' => '',
166 + 'required' => false,
167 + );
168 +
169 + $sanitized_args = array();
170 +
171 + foreach ( $this->block_type->block_attrs as $key => $value ) {
172 + $sanitized_args[ $key ] = $value;
173 + }
174 +
175 + return wp_parse_args( $sanitized_args, $defaults );
176 + }
177 +
178 + public function get_default_args_with_filter() {
179 + $args = $this->get_default_args();
180 +
181 + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
182 + return apply_filters( "jet-form-builder/render/{$args['type']}", $args, $this );
183 + }
184 +
185 + public function before_render( $args ) {
186 + }
187 +
188 + /**
189 + * @param null $wp_block
190 + * @param null $template
191 + *
192 + * @return false|string
193 + */
194 + public function render( $wp_block = null, $template = null ) {
195 + $args = $this->get_default_args_with_filter();
196 + $template = $this->render_without_layout( $template, $args );
197 +
198 + $label = $this->get_field_label();
199 + $desc = $this->get_field_desc();
200 + $layout = $this->live_form ? $this->live_form->spec_data->fields_layout : 'column';
201 +
202 + if ( 'column' === $layout ) {
203 + ob_start();
204 + include $this->block_type->get_common_template( 'field-column.php' );
205 + $result_field = ob_get_clean();
206 + } else {
207 + ob_start();
208 + include $this->block_type->get_common_template( 'field-row.php' );
209 + $result_field = ob_get_clean();
210 + }
211 +
212 + return $result_field;
213 + }
214 +
215 + public function render_disabled_message_form_break( $args ) {
216 + $format = '<div class="jet-form-builder__next-page-msg" %2$s>%1$s</div>';
217 +
218 + if ( ! empty( $args['page_break_disabled'] ) && ! Tools::is_editor() ) {
219 + $attrs = array(
220 + sprintf(
221 + 'data-jfb-conditional="%s"',
222 + Tools::esc_attr(
223 + array(
224 + array(
225 + 'page_state' => 'active',
226 + ),
227 + )
228 + )
229 + ),
230 + sprintf(
231 + 'data-jfb-func="%s"',
232 + Tools::esc_attr(
233 + array(
234 + 'show' => array( 'dom' => true ),
235 + )
236 + )
237 + ),
238 + );
239 +
240 + return sprintf( $format, $args['page_break_disabled'], implode( ' ', $attrs ) );
241 +
242 + } elseif ( Tools::is_editor() ) {
243 +
244 + $message = ! empty( $args['page_break_disabled'] ) ? $args['page_break_disabled'] : 'Disabled message';
245 +
246 + return sprintf( $format, $message, $this->get_style_attrs( array( 'display:block' ) ) );
247 + }
248 +
249 + return '';
250 + }
251 +
252 + public function get_style_attrs( $style ) {
253 + return 'style=' . implode( ';', $style );
254 + }
255 +
256 + /**
257 + * Render custom form item template
258 + *
259 + * @param int|string $object_id Object ID.
260 + * @param array $args Field arguments.
261 + * @param bool|string $checked
262 + *
263 + * @return string
264 + */
265 + public function get_custom_template( $object_id, $args, $checked = false ) {
266 + return ( new Builder_Helper() )->get_custom_template( $object_id, $args, $checked );
267 + }
268 +
269 + protected function set_value() {
270 + if ( ! jet_form_builder()->regexp->has_macro( $this->args['default'] ) ) {
271 + $this->add_attribute( 'value', $this->args['default'] );
272 +
273 + return;
274 + }
275 + wp_enqueue_script( Dynamic_Value::HANDLE );
276 +
277 + $this->add_attribute( 'data-value', $this->args['default'] );
278 + }
279 +
280 +}