base-select-radio-check.php
5 years ago
base.php
5 years ago
calculated-field-render.php
5 years ago
checkbox-field-render.php
5 years ago
conditional-block-render.php
5 years ago
date-field-render.php
5 years ago
datetime-field-render.php
5 years ago
form-break-field-render.php
5 years ago
form-builder.php
5 years ago
group-break-field-render.php
5 years ago
heading-field-render.php
5 years ago
hidden-field-render.php
5 years ago
media-field-render.php
5 years ago
number-field-render.php
5 years ago
radio-field-render.php
5 years ago
range-field-render.php
5 years ago
repeater-field-render.php
5 years ago
select-field-render.php
5 years ago
submit-field-render.php
5 years ago
text-field-render.php
5 years ago
textarea-field-render.php
5 years ago
time-field-render.php
5 years ago
wysiwyg-field-render.php
5 years ago
base.php
196 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Blocks\Render; |
| 4 | |
| 5 | |
| 6 | use Jet_Form_Builder\Blocks\Modules\Fields_Errors\Error_Handler; |
| 7 | use Jet_Form_Builder\Classes\Attributes_Trait; |
| 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 | use Jet_Form_Builder\Presets\Preset_Manager; |
| 13 | |
| 14 | // If this file is called directly, abort. |
| 15 | |
| 16 | if ( ! defined( 'WPINC' ) ) { |
| 17 | die; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Define Base_Type class |
| 22 | */ |
| 23 | abstract class Base { |
| 24 | |
| 25 | use Attributes_Trait; |
| 26 | use Get_Template_Trait; |
| 27 | |
| 28 | public $form_id; |
| 29 | public $block_type; |
| 30 | public $content; |
| 31 | public $live_form; |
| 32 | |
| 33 | const FIELD_ERROR_CLASS = 'field-has-error'; |
| 34 | |
| 35 | |
| 36 | public function __construct( $block_type ) { |
| 37 | $this->form_id = Live_Form::instance()->form_id; |
| 38 | |
| 39 | $this->set_live_form(); |
| 40 | $this->set_block_type( $block_type ); |
| 41 | } |
| 42 | |
| 43 | abstract public function get_name(); |
| 44 | |
| 45 | public function set_live_form() { |
| 46 | $this->live_form = Live_Form::instance(); |
| 47 | } |
| 48 | |
| 49 | public function set_block_type( $block_type ) { |
| 50 | $this->block_type = $block_type; |
| 51 | } |
| 52 | |
| 53 | private function is_field( $needle ) { |
| 54 | return Plugin::instance()->form->is_field( $this->block_type->block_attrs['blockName'], $needle ); |
| 55 | } |
| 56 | |
| 57 | public function maybe_add_error_class( $args ) { |
| 58 | if ( $this->has_error( $args ) ) { |
| 59 | $this->add_attribute( 'class', self::FIELD_ERROR_CLASS ); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public function maybe_render_error( $args ) { |
| 64 | if ( $this->has_error( $args ) ) { |
| 65 | return "<div class='error-message'>". Error_Handler::instance()->error_by_name( $args['name'] ) ."</div>"; |
| 66 | } |
| 67 | return ''; |
| 68 | } |
| 69 | |
| 70 | public function maybe_get_error_class( $args ) { |
| 71 | if ( $this->has_error( $args ) ) { |
| 72 | return self::FIELD_ERROR_CLASS; |
| 73 | } |
| 74 | return ''; |
| 75 | } |
| 76 | |
| 77 | private function has_error( $args ) { |
| 78 | return Error_Handler::instance()->has_error_by_name( $args['name'] ); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /** |
| 83 | * Returns field label |
| 84 | * |
| 85 | * @return [type] [description] |
| 86 | */ |
| 87 | public function get_field_label() { |
| 88 | |
| 89 | ob_start(); |
| 90 | if ( ! empty( $this->block_type->block_attrs['label'] ) && $this->label_allowed() ) { |
| 91 | $args = $this->block_type->block_attrs; |
| 92 | include $this->block_type->get_common_template( 'field-label.php' ); |
| 93 | } |
| 94 | |
| 95 | return ob_get_clean(); |
| 96 | |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns field description |
| 101 | * |
| 102 | * @return [type] [description] |
| 103 | */ |
| 104 | public function get_field_desc() { |
| 105 | |
| 106 | ob_start(); |
| 107 | if ( ! empty( $this->block_type->block_attrs['desc'] ) && $this->label_allowed() ) { |
| 108 | $args = $this->block_type->block_attrs; |
| 109 | include $this->block_type->get_common_template( 'field-description.php' ); |
| 110 | } |
| 111 | |
| 112 | return ob_get_clean(); |
| 113 | |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Defines if this form control supports label |
| 118 | * |
| 119 | * @return [type] [description] |
| 120 | */ |
| 121 | public function label_allowed() { |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | public function render( $wp_block = null, $template = null ) { |
| 126 | |
| 127 | if ( ! $this->live_form->form_id ) { |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | $defaults = array( |
| 132 | 'default' => '', |
| 133 | 'name' => '', |
| 134 | 'placeholder' => '', |
| 135 | 'required' => false, |
| 136 | ); |
| 137 | |
| 138 | $sanitized_args = array(); |
| 139 | |
| 140 | foreach ( $this->block_type->block_attrs as $key => $value ) { |
| 141 | $sanitized_args[ $key ] = $value; |
| 142 | } |
| 143 | $args = wp_parse_args( $sanitized_args, $defaults ); |
| 144 | |
| 145 | $template_name = $this->get_name(); |
| 146 | |
| 147 | $this->maybe_add_error_class( $args ); |
| 148 | |
| 149 | if ( is_null( $template ) ) { |
| 150 | $template = $this->block_type->get_field_template( $template_name . '.php' ); |
| 151 | } |
| 152 | |
| 153 | $label = $this->get_field_label(); |
| 154 | $desc = $this->get_field_desc(); |
| 155 | $layout = $this->live_form ? $this->live_form->spec_data->fields_layout : 'column'; |
| 156 | |
| 157 | if ( 'hidden-field' === $args['type'] ) { |
| 158 | ob_start(); |
| 159 | include $template; |
| 160 | $result_field = ob_get_clean(); |
| 161 | |
| 162 | } else if ( 'column' === $layout ) { |
| 163 | ob_start(); |
| 164 | include $this->block_type->get_common_template( 'field-column.php' ); |
| 165 | $result_field = ob_get_clean(); |
| 166 | } else { |
| 167 | ob_start(); |
| 168 | include $this->block_type->get_common_template( 'field-row.php' ); |
| 169 | $result_field = ob_get_clean(); |
| 170 | } |
| 171 | |
| 172 | return $result_field; |
| 173 | } |
| 174 | |
| 175 | public function render_disabled_message_form_break( $args ) { |
| 176 | $format = '<div class="jet-form-builder__next-page-msg" %2$s>%1$s</div>'; |
| 177 | |
| 178 | if ( ! empty( $args['page_break_disabled'] ) && ! Tools::is_editor() ) { |
| 179 | return sprintf( $format, $args['page_break_disabled'], '' ); |
| 180 | |
| 181 | } elseif ( Tools::is_editor() ) { |
| 182 | |
| 183 | $message = ! empty( $args['page_break_disabled'] ) ? $args['page_break_disabled'] : 'Disabled message'; |
| 184 | |
| 185 | return sprintf( $format, $message, $this->get_style_attrs( array( 'display:block' ) ) ); |
| 186 | } |
| 187 | |
| 188 | return ''; |
| 189 | } |
| 190 | |
| 191 | public function get_style_attrs( $style ) { |
| 192 | return 'style=' . implode( ';', $style ); |
| 193 | } |
| 194 | |
| 195 | } |
| 196 |