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