action-button-render.php
2 years ago
base-select-radio-check.php
2 years ago
base.php
2 years ago
calculated-field-render.php
2 years ago
checkbox-field-render.php
2 years ago
date-field-render.php
2 years ago
datetime-field-render.php
2 years ago
form-builder.php
2 years ago
form-hidden-fields.php
2 years ago
group-break-field-render.php
2 years ago
heading-field-render.php
2 years ago
media-field-render.php
2 years ago
number-field-render.php
2 years ago
radio-field-render.php
2 years ago
range-field-render.php
2 years ago
repeater-field-render.php
2 years ago
select-field-render.php
2 years ago
text-field-render.php
2 years ago
textarea-field-render.php
2 years ago
time-field-render.php
2 years ago
wysiwyg-field-render.php
2 years ago
base.php
281 lines
| 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 | } |
| 281 |