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
date-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
201 lines
| 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 | } |
| 201 |