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