PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.4.0
JetFormBuilder — Dynamic Blocks Form Builder v1.4.0
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 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / blocks / render / base.php
jetformbuilder / includes / blocks / render Last commit date
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
base.php
246 lines
1 <?php
2
3 namespace Jet_Form_Builder\Blocks\Render;
4
5 use Jet_Form_Builder\Blocks\Modules\Fields_Errors\Error_Handler;
6 use Jet_Form_Builder\Classes\Attributes_Trait;
7 use Jet_Form_Builder\Classes\Builder_Helper;
8 use Jet_Form_Builder\Classes\Get_Template_Trait;
9 use Jet_Form_Builder\Classes\Tools;
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
29 /**
30 * @var \Jet_Form_Builder\Blocks\Types\Base
31 */
32 public $block_type;
33 public $content;
34
35 /**
36 * @var Live_Form
37 */
38 public $live_form;
39
40 const FIELD_ERROR_CLASS = 'field-has-error';
41
42
43 public function __construct( $block_type ) {
44 $this->form_id = Live_Form::instance()->form_id;
45
46 $this->set_live_form();
47 $this->set_block_type( $block_type );
48 }
49
50 abstract public function get_name();
51
52 public function set_live_form() {
53 $this->live_form = Live_Form::instance();
54 }
55
56 public function set_block_type( $block_type ) {
57 $this->block_type = $block_type;
58 }
59
60 private function is_field( $needle ) {
61 return Plugin::instance()->form->is_field( $this->block_type->block_attrs['blockName'], $needle );
62 }
63
64 public function maybe_add_error_class( $args ) {
65 if ( $this->has_error( $args ) ) {
66 $this->add_attribute( 'class', self::FIELD_ERROR_CLASS );
67 }
68 }
69
70 public function maybe_render_error( $args ) {
71 if ( $this->has_error( $args ) ) {
72 return '<div class="error-message">' . Error_Handler::instance()->error_by_name( $args['name'] ) . '</div>';
73 }
74
75 return '';
76 }
77
78 public function maybe_get_error_class( $args ) {
79 if ( $this->has_error( $args ) ) {
80 return self::FIELD_ERROR_CLASS;
81 }
82
83 return '';
84 }
85
86 private function has_error( $args ) {
87 return Error_Handler::instance()->has_error_by_name( $args['name'] );
88 }
89
90
91 /**
92 * Returns field label
93 *
94 * @return [type] [description]
95 */
96 public function get_field_label() {
97
98 ob_start();
99 if ( ! empty( $this->block_type->block_attrs['label'] ) && $this->label_allowed() ) {
100 $args = $this->block_type->block_attrs;
101 include $this->block_type->get_common_template( 'field-label.php' );
102 }
103
104 return ob_get_clean();
105
106 }
107
108 /**
109 * Returns field description
110 *
111 * @return [type] [description]
112 */
113 public function get_field_desc() {
114
115 ob_start();
116 if ( ! empty( $this->block_type->block_attrs['desc'] ) && $this->label_allowed() ) {
117 $args = $this->block_type->block_attrs;
118 include $this->block_type->get_common_template( 'field-description.php' );
119 }
120
121 return ob_get_clean();
122
123 }
124
125 /**
126 * Defines if this form control supports label
127 *
128 * @return [type] [description]
129 */
130 public function label_allowed() {
131 return true;
132 }
133
134 public function render_without_layout( $template = null, $args = array() ) {
135 $template_name = $this->get_name();
136
137 if ( empty( $args ) ) {
138 $args = $this->get_default_args_with_filter();
139 }
140
141 $this->before_render( $args );
142
143 if ( is_null( $template ) ) {
144 $template = $this->block_type->get_field_template( $template_name . '.php' );
145 }
146
147 if ( Tools::is_readable( $template ) ) {
148 ob_start();
149 include $template;
150 $template = ob_get_clean();
151 }
152
153 return $template;
154 }
155
156 public function get_default_args() {
157 $defaults = array(
158 'default' => '',
159 'name' => '',
160 'placeholder' => '',
161 'required' => false,
162 );
163
164 $sanitized_args = array();
165
166 foreach ( $this->block_type->block_attrs as $key => $value ) {
167 $sanitized_args[ $key ] = $value;
168 }
169
170 return wp_parse_args( $sanitized_args, $defaults );
171 }
172
173 public function get_default_args_with_filter() {
174 $args = $this->get_default_args();
175
176 // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
177 return apply_filters( "jet-form-builder/render/{$args['type']}", $args, $this );
178 }
179
180 public function before_render( $args ) {
181 }
182
183 /**
184 * @param null $wp_block
185 * @param null $template
186 *
187 * @return false|string
188 */
189 public function render( $wp_block = null, $template = null ) {
190 $args = $this->get_default_args_with_filter();
191 $template = $this->render_without_layout( $template, $args );
192
193 $this->maybe_add_error_class( $args );
194
195 $label = $this->get_field_label();
196 $desc = $this->get_field_desc();
197 $layout = $this->live_form ? $this->live_form->spec_data->fields_layout : 'column';
198
199 if ( 'column' === $layout ) {
200 ob_start();
201 include $this->block_type->get_common_template( 'field-column.php' );
202 $result_field = ob_get_clean();
203 } else {
204 ob_start();
205 include $this->block_type->get_common_template( 'field-row.php' );
206 $result_field = ob_get_clean();
207 }
208
209 return $result_field;
210 }
211
212 public function render_disabled_message_form_break( $args ) {
213 $format = '<div class="jet-form-builder__next-page-msg" %2$s>%1$s</div>';
214
215 if ( ! empty( $args['page_break_disabled'] ) && ! Tools::is_editor() ) {
216 return sprintf( $format, $args['page_break_disabled'], '' );
217
218 } elseif ( Tools::is_editor() ) {
219
220 $message = ! empty( $args['page_break_disabled'] ) ? $args['page_break_disabled'] : 'Disabled message';
221
222 return sprintf( $format, $message, $this->get_style_attrs( array( 'display:block' ) ) );
223 }
224
225 return '';
226 }
227
228 public function get_style_attrs( $style ) {
229 return 'style=' . implode( ';', $style );
230 }
231
232 /**
233 * Render custom form item template
234 *
235 * @param int|string $object_id Object ID.
236 * @param array $args Field arguments.
237 * @param bool|string $checked
238 *
239 * @return string
240 */
241 public function get_custom_template( $object_id, $args, $checked = false ) {
242 return ( new Builder_Helper() )->get_custom_template( $object_id, $args, $checked );
243 }
244
245 }
246