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