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
238 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\Builder_Helper; |
| 9 | use Jet_Form_Builder\Classes\Get_Template_Trait; |
| 10 | use Jet_Form_Builder\Classes\Tools; |
| 11 | use Jet_Form_Builder\Live_Form; |
| 12 | use Jet_Form_Builder\Plugin; |
| 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 | |
| 30 | /** |
| 31 | * @var \Jet_Form_Builder\Blocks\Types\Base |
| 32 | */ |
| 33 | public $block_type; |
| 34 | public $content; |
| 35 | public $live_form; |
| 36 | |
| 37 | const FIELD_ERROR_CLASS = 'field-has-error'; |
| 38 | |
| 39 | |
| 40 | public function __construct( $block_type ) { |
| 41 | $this->form_id = Live_Form::instance()->form_id; |
| 42 | |
| 43 | $this->set_live_form(); |
| 44 | $this->set_block_type( $block_type ); |
| 45 | } |
| 46 | |
| 47 | abstract public function get_name(); |
| 48 | |
| 49 | public function set_live_form() { |
| 50 | $this->live_form = Live_Form::instance(); |
| 51 | } |
| 52 | |
| 53 | public function set_block_type( $block_type ) { |
| 54 | $this->block_type = $block_type; |
| 55 | } |
| 56 | |
| 57 | private function is_field( $needle ) { |
| 58 | return Plugin::instance()->form->is_field( $this->block_type->block_attrs['blockName'], $needle ); |
| 59 | } |
| 60 | |
| 61 | public function maybe_add_error_class( $args ) { |
| 62 | if ( $this->has_error( $args ) ) { |
| 63 | $this->add_attribute( 'class', self::FIELD_ERROR_CLASS ); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public function maybe_render_error( $args ) { |
| 68 | if ( $this->has_error( $args ) ) { |
| 69 | return '<div class="error-message">' . Error_Handler::instance()->error_by_name( $args['name'] ) . '</div>'; |
| 70 | } |
| 71 | |
| 72 | return ''; |
| 73 | } |
| 74 | |
| 75 | public function maybe_get_error_class( $args ) { |
| 76 | if ( $this->has_error( $args ) ) { |
| 77 | return self::FIELD_ERROR_CLASS; |
| 78 | } |
| 79 | |
| 80 | return ''; |
| 81 | } |
| 82 | |
| 83 | private function has_error( $args ) { |
| 84 | return Error_Handler::instance()->has_error_by_name( $args['name'] ); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | /** |
| 89 | * Returns field label |
| 90 | * |
| 91 | * @return [type] [description] |
| 92 | */ |
| 93 | public function get_field_label() { |
| 94 | |
| 95 | ob_start(); |
| 96 | if ( ! empty( $this->block_type->block_attrs['label'] ) && $this->label_allowed() ) { |
| 97 | $args = $this->block_type->block_attrs; |
| 98 | include $this->block_type->get_common_template( 'field-label.php' ); |
| 99 | } |
| 100 | |
| 101 | return ob_get_clean(); |
| 102 | |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Returns field description |
| 107 | * |
| 108 | * @return [type] [description] |
| 109 | */ |
| 110 | public function get_field_desc() { |
| 111 | |
| 112 | ob_start(); |
| 113 | if ( ! empty( $this->block_type->block_attrs['desc'] ) && $this->label_allowed() ) { |
| 114 | $args = $this->block_type->block_attrs; |
| 115 | include $this->block_type->get_common_template( 'field-description.php' ); |
| 116 | } |
| 117 | |
| 118 | return ob_get_clean(); |
| 119 | |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Defines if this form control supports label |
| 124 | * |
| 125 | * @return [type] [description] |
| 126 | */ |
| 127 | public function label_allowed() { |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | public function render_without_layout( $template = null, $args = array() ) { |
| 132 | $template_name = $this->get_name(); |
| 133 | |
| 134 | if ( empty( $args ) ) { |
| 135 | $args = $this->get_default_args_with_filter(); |
| 136 | } |
| 137 | |
| 138 | if ( is_null( $template ) ) { |
| 139 | $template = $this->block_type->get_field_template( $template_name . '.php' ); |
| 140 | } |
| 141 | |
| 142 | if ( Tools::is_readable( $template ) ) { |
| 143 | ob_start(); |
| 144 | include $template; |
| 145 | $template = ob_get_clean(); |
| 146 | } |
| 147 | |
| 148 | return $template; |
| 149 | } |
| 150 | |
| 151 | public function get_default_args() { |
| 152 | $defaults = array( |
| 153 | 'default' => '', |
| 154 | 'name' => '', |
| 155 | 'placeholder' => '', |
| 156 | 'required' => false, |
| 157 | ); |
| 158 | |
| 159 | $sanitized_args = array(); |
| 160 | |
| 161 | foreach ( $this->block_type->block_attrs as $key => $value ) { |
| 162 | $sanitized_args[ $key ] = $value; |
| 163 | } |
| 164 | |
| 165 | return wp_parse_args( $sanitized_args, $defaults ); |
| 166 | } |
| 167 | |
| 168 | public function get_default_args_with_filter() { |
| 169 | $args = $this->get_default_args(); |
| 170 | |
| 171 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 172 | return apply_filters( "jet-form-builder/render/{$args['type']}", $args, $this ); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * @param null $wp_block |
| 177 | * @param null $template |
| 178 | * |
| 179 | * @return false|string |
| 180 | */ |
| 181 | public function render( $wp_block = null, $template = null ) { |
| 182 | $args = $this->get_default_args_with_filter(); |
| 183 | $template = $this->render_without_layout( $template, $args ); |
| 184 | |
| 185 | $this->maybe_add_error_class( $args ); |
| 186 | |
| 187 | $label = $this->get_field_label(); |
| 188 | $desc = $this->get_field_desc(); |
| 189 | $layout = $this->live_form ? $this->live_form->spec_data->fields_layout : 'column'; |
| 190 | |
| 191 | if ( 'column' === $layout ) { |
| 192 | ob_start(); |
| 193 | include $this->block_type->get_common_template( 'field-column.php' ); |
| 194 | $result_field = ob_get_clean(); |
| 195 | } else { |
| 196 | ob_start(); |
| 197 | include $this->block_type->get_common_template( 'field-row.php' ); |
| 198 | $result_field = ob_get_clean(); |
| 199 | } |
| 200 | |
| 201 | return $result_field; |
| 202 | } |
| 203 | |
| 204 | public function render_disabled_message_form_break( $args ) { |
| 205 | $format = '<div class="jet-form-builder__next-page-msg" %2$s>%1$s</div>'; |
| 206 | |
| 207 | if ( ! empty( $args['page_break_disabled'] ) && ! Tools::is_editor() ) { |
| 208 | return sprintf( $format, $args['page_break_disabled'], '' ); |
| 209 | |
| 210 | } elseif ( Tools::is_editor() ) { |
| 211 | |
| 212 | $message = ! empty( $args['page_break_disabled'] ) ? $args['page_break_disabled'] : 'Disabled message'; |
| 213 | |
| 214 | return sprintf( $format, $message, $this->get_style_attrs( array( 'display:block' ) ) ); |
| 215 | } |
| 216 | |
| 217 | return ''; |
| 218 | } |
| 219 | |
| 220 | public function get_style_attrs( $style ) { |
| 221 | return 'style=' . implode( ';', $style ); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Render custom form item template |
| 226 | * |
| 227 | * @param int|string $object_id Object ID |
| 228 | * @param array $args Field arguments |
| 229 | * @param bool|string $checked |
| 230 | * |
| 231 | * @return string |
| 232 | */ |
| 233 | public function get_custom_template( $object_id, $args, $checked = false ) { |
| 234 | return ( new Builder_Helper() )->get_custom_template( $object_id, $args, $checked ); |
| 235 | } |
| 236 | |
| 237 | } |
| 238 |