* * @package FWREN */ namespace FWREN; use FWREN_BC\Block_Base; use FWREN_BC\Controls; use FWREN_BC\Group_Controls; defined( 'ABSPATH' ) || exit; abstract class Field_Block extends Block_Base { const NAME_PREFIX = 'fwren/'; /** Custom control types drawn by assets/editor/controls.js. */ const OPTIONS = 'fwren_options'; const RULES = 'fwren_rules'; const FIELD_PICK = 'fwren_field'; const SMARTCODES = 'fwren_smartcodes'; /* ------------------------------------------------------------------ * * Identity * ------------------------------------------------------------------ */ /** @return string */ public function get_block_name() { return self::NAME_PREFIX . $this->get_name(); } /** @return string Inserter category: fwren-fields | fwren-choice | fwren-advanced | fwren-layout | fwren-payment. */ public function get_category() { return 'fwren-fields'; } /** @return string */ public function get_wrapper_prefix() { return 'fwren-block-'; } /** @return string Field type id used in the schema (defaults to the block slug). */ public function get_field_type() { return $this->get_name(); } /** @return bool Whether the block collects a value. */ public function is_input() { return true; } /** @return bool Whether the field shows a placeholder control. */ protected function has_placeholder() { return true; } /** @return bool Whether the field shows a default value control. */ protected function has_default() { return true; } /** @return string Default label for a new block. */ protected function default_label() { return $this->get_title(); } /** @return string Default key prefix for auto-generated field keys. */ public function key_prefix() { return str_replace( '-', '_', $this->get_name() ); } /** @return array */ public function get_block_supports() { return array( 'html' => false, 'className' => false, 'customClassName' => false, 'reusable' => false, ); } /** @return string[]|null Block names this block may only be inserted into. */ public function get_parent() { return null; } /** Inner blocks setup for layout blocks; null = leaf. */ public function get_inner_blocks() { return null; } /** @return bool */ public function supports_inner_blocks() { return null !== $this->get_inner_blocks(); } /** @return string[]|null */ public function get_allowed_blocks() { $inner = $this->get_inner_blocks(); return $inner['allowed'] ?? null; } /** @return array Editor schema plus inner block template. */ public function get_schema() { $schema = parent::get_schema(); $inner = $this->get_inner_blocks(); if ( null !== $inner ) { $schema['inner_template'] = $inner['template'] ?? array(); $schema['inner_template_lock'] = $inner['template_lock'] ?? false; $schema['inner_orientation'] = $inner['orientation'] ?? 'vertical'; } $schema['fwren'] = array( 'is_input' => $this->is_input(), 'key_prefix' => $this->key_prefix(), 'type' => $this->get_field_type(), ); return $schema; } /* ------------------------------------------------------------------ * * Controls * ------------------------------------------------------------------ */ /** Type-specific Content controls, printed inside the Field section after the shared ones. */ protected function register_type_controls() {} /** Type-specific Validation controls (inside the Validation section). */ protected function register_validation_controls() {} /** Extra sections (Content tab) after Field / Validation. */ protected function register_extra_sections() {} /** @return bool Whether to print the Validation section. */ protected function has_validation() { return false; } /** @return bool Whether the block gets the Conditional logic section. */ protected function has_conditions() { return true; } /** @return bool Whether to add the shared input Style sections. */ protected function has_input_style() { return $this->is_input(); } /** Controls. */ protected function register_controls() { if ( $this->is_input() ) { $this->start_controls_section( 'section_field', array( 'label' => __( 'Field', 'fieldwren' ) ) ); $this->add_control( 'label', array( 'label' => __( 'Label', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => $this->default_label(), ) ); $this->add_control( 'name', array( 'label' => __( 'Field key', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => '', 'description' => __( 'Unique name used in entries, smartcodes ({inputs.key}) and integrations. Letters, numbers and underscores. Keep it unchanged once the form has entries.', 'fieldwren' ), ) ); if ( $this->has_placeholder() ) { $this->add_control( 'placeholder', array( 'label' => __( 'Placeholder', 'fieldwren' ), 'type' => Controls::TEXT, ) ); } $this->register_type_controls(); if ( $this->has_default() ) { $this->add_control( 'default_value', array( 'label' => __( 'Default value', 'fieldwren' ), 'type' => Controls::TEXT, 'merge_tags' => true, 'description' => __( 'Smartcodes work here, e.g. {user.email}, {get.utm_source}, {embed_post.title}.', 'fieldwren' ), ) ); } $this->add_control( 'help', array( 'label' => __( 'Help text', 'fieldwren' ), 'type' => Controls::TEXTAREA, 'rows' => 2, ) ); $this->add_control( 'required', array( 'label' => __( 'Required', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => '', 'separator' => 'before', ) ); $this->add_control( 'required_message', array( 'label' => __( 'Required message', 'fieldwren' ), 'type' => Controls::TEXT, 'placeholder' => __( 'This field is required.', 'fieldwren' ), 'condition' => array( 'required' => 'yes' ), ) ); $this->add_control( 'width', array( 'label' => __( 'Width', 'fieldwren' ), 'type' => Controls::SELECT, 'default' => '100', 'separator' => 'before', 'options' => array( '100' => '100%', '75' => '75%', '66' => '66%', '50' => '50%', '33' => '33%', '25' => '25%', ), ) ); $this->add_control( 'label_placement', array( 'label' => __( 'Label placement', 'fieldwren' ), 'type' => Controls::SELECT, 'default' => '', 'options' => array( '' => __( 'Form default', 'fieldwren' ), 'top' => __( 'Top', 'fieldwren' ), 'left' => __( 'Left', 'fieldwren' ), 'right' => __( 'Right', 'fieldwren' ), 'floating' => __( 'Floating', 'fieldwren' ), 'hidden' => __( 'Hidden', 'fieldwren' ), ), ) ); $this->add_control( 'admin_label', array( 'label' => __( 'Admin label', 'fieldwren' ), 'type' => Controls::TEXT, 'description' => __( 'Shown in entries and emails instead of the label.', 'fieldwren' ), ) ); $this->add_control( 'readonly', array( 'label' => __( 'Read only', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => '', ) ); $this->end_controls_section(); } else { $this->register_layout_controls(); } if ( $this->has_validation() ) { $this->start_controls_section( 'section_validation', array( 'label' => __( 'Validation', 'fieldwren' ) ) ); $this->register_validation_controls(); $this->end_controls_section(); } $this->register_extra_sections(); if ( $this->has_conditions() ) { $this->register_condition_controls(); } if ( $this->has_input_style() ) { $this->register_input_style_controls(); } $this->register_style_controls(); } /** Content controls of non-input blocks. */ protected function register_layout_controls() {} /** Style sections of non-input blocks / extra styles. */ protected function register_style_controls() {} /** Conditional logic section. */ protected function register_condition_controls() { $this->start_controls_section( 'section_conditions', array( 'label' => __( 'Conditional logic', 'fieldwren' ) ) ); $this->add_control( 'cl_enabled', array( 'label' => __( 'Enable conditional logic', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => '', ) ); $this->add_control( 'cl_action', array( 'label' => __( 'Action', 'fieldwren' ), 'type' => Controls::SELECT, 'default' => 'show', 'options' => array( 'show' => __( 'Show this block', 'fieldwren' ), 'hide' => __( 'Hide this block', 'fieldwren' ), ), 'condition' => array( 'cl_enabled' => 'yes' ), ) ); $this->add_control( 'cl_match', array( 'label' => __( 'When', 'fieldwren' ), 'type' => Controls::SELECT, 'default' => 'all', 'options' => array( 'all' => __( 'All rules match', 'fieldwren' ), 'any' => __( 'Any rule matches', 'fieldwren' ), ), 'condition' => array( 'cl_enabled' => 'yes' ), ) ); $this->add_control( 'cl_rules', array( 'label' => __( 'Rules', 'fieldwren' ), 'type' => self::RULES, 'default' => array(), 'condition' => array( 'cl_enabled' => 'yes' ), ) ); $this->end_controls_section(); } /** Label / input / help style sections. */ protected function register_input_style_controls() { $this->start_controls_section( 'section_style_label', array( 'label' => __( 'Label', 'fieldwren' ), 'tab' => Controls::TAB_STYLE, ) ); $this->add_control( 'label_color', array( 'label' => __( 'Color', 'fieldwren' ), 'type' => Controls::COLOR, 'selectors' => array( '{{WRAPPER}} .fwren-label' => 'color: {{VALUE}};' ), ) ); $this->add_group_control( Group_Controls::TYPOGRAPHY, array( 'name' => 'label_typography', 'selector' => '{{WRAPPER}} .fwren-label', ) ); $this->add_control( 'label_spacing', array( 'label' => __( 'Spacing', 'fieldwren' ), 'type' => Controls::SLIDER, 'size_units' => array( 'px' ), 'range' => array( 'px' => array( 'min' => 0, 'max' => 40, 'step' => 1 ) ), 'selectors' => array( '{{WRAPPER}} .fwren-label' => 'margin-bottom: {{SIZE}}{{UNIT}};' ), ) ); $this->end_controls_section(); $input = '{{WRAPPER}} .fwren-input'; $this->start_controls_section( 'section_style_input', array( 'label' => __( 'Input', 'fieldwren' ), 'tab' => Controls::TAB_STYLE, ) ); $this->add_group_control( Group_Controls::TYPOGRAPHY, array( 'name' => 'input_typography', 'selector' => $input, ) ); $this->start_controls_tabs( 'input_tabs' ); $this->start_controls_tab( 'input_normal', array( 'label' => __( 'Normal', 'fieldwren' ) ) ); $this->add_control( 'input_color', array( 'label' => __( 'Text color', 'fieldwren' ), 'type' => Controls::COLOR, 'selectors' => array( $input => 'color: {{VALUE}};' ), ) ); $this->add_control( 'input_bg', array( 'label' => __( 'Background', 'fieldwren' ), 'type' => Controls::COLOR, 'selectors' => array( $input => 'background-color: {{VALUE}};' ), ) ); $this->add_control( 'input_border_color', array( 'label' => __( 'Border color', 'fieldwren' ), 'type' => Controls::COLOR, 'selectors' => array( $input => 'border-color: {{VALUE}};' ), ) ); $this->end_controls_tab(); $this->start_controls_tab( 'input_focus', array( 'label' => __( 'Focus', 'fieldwren' ) ) ); $this->add_control( 'input_focus_border', array( 'label' => __( 'Border color', 'fieldwren' ), 'type' => Controls::COLOR, 'selectors' => array( $input . ':focus' => 'border-color: {{VALUE}}; box-shadow: 0 0 0 3px color-mix(in srgb, {{VALUE}} 20%, transparent);' ), ) ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->add_control( 'input_border_width', array( 'label' => __( 'Border width', 'fieldwren' ), 'type' => Controls::SLIDER, 'size_units' => array( 'px' ), 'range' => array( 'px' => array( 'min' => 0, 'max' => 6, 'step' => 1 ) ), 'separator' => 'before', 'selectors' => array( $input => 'border-width: {{SIZE}}{{UNIT}};' ), ) ); $this->add_control( 'input_radius', array( 'label' => __( 'Border radius', 'fieldwren' ), 'type' => Controls::SLIDER, 'size_units' => array( 'px' ), 'range' => array( 'px' => array( 'min' => 0, 'max' => 40, 'step' => 1 ) ), 'selectors' => array( $input => 'border-radius: {{SIZE}}{{UNIT}};' ), ) ); $this->add_responsive_control( 'input_padding', array( 'label' => __( 'Padding', 'fieldwren' ), 'type' => Controls::DIMENSIONS, 'size_units' => array( 'px', 'em' ), 'selectors' => array( $input => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ), ) ); $this->end_controls_section(); $this->start_controls_section( 'section_style_help', array( 'label' => __( 'Help text', 'fieldwren' ), 'tab' => Controls::TAB_STYLE, ) ); $this->add_control( 'help_color', array( 'label' => __( 'Color', 'fieldwren' ), 'type' => Controls::COLOR, 'selectors' => array( '{{WRAPPER}} .fwren-help' => 'color: {{VALUE}};' ), ) ); $this->end_controls_section(); } /** * Fix list values: the framework merges array defaults into saved arrays, which for lists * (options, rules) would append the default rows. Lists are taken as saved. */ public function prepare_settings( array $attributes ) { $settings = parent::prepare_settings( $attributes ); foreach ( $this->get_controls() as $id => $control ) { if ( in_array( $control['type'], array( self::OPTIONS, self::RULES ), true ) && array_key_exists( $id, $attributes ) && is_array( $attributes[ $id ] ) ) { $settings[ $id ] = array_values( $attributes[ $id ] ); } } return $settings; } /* ------------------------------------------------------------------ * * Schema (server side field definition) * ------------------------------------------------------------------ */ /** * Field key from settings, sanitized. * * @param array $s Settings. * @return string */ public function field_key( array $s ) { $key = sanitize_key( str_replace( '-', '_', (string) ( $s['name'] ?? '' ) ) ); if ( '' === $key ) { $key = $this->key_prefix() . '_' . preg_replace( '/[^a-z0-9]/', '', strtolower( (string) ( $s['_id'] ?? '' ) ) ); } return $key; } /** * Schema entry of the field. Subclasses add keys through schema_extra(). * * @param array $s Prepared settings. * @return array */ public function schema( array $s ) { $label = trim( wp_strip_all_tags( (string) ( $s['label'] ?? '' ) ) ); $f = array( 'name' => $this->field_key( $s ), 'type' => $this->get_field_type(), 'block' => $this->get_block_name(), 'id' => (string) ( $s['_id'] ?? '' ), 'label' => $label, 'admin_label' => trim( (string) ( $s['admin_label'] ?? '' ) ), 'required' => 'yes' === ( $s['required'] ?? '' ), 'required_message' => (string) ( $s['required_message'] ?? '' ), 'default' => (string) ( $s['default_value'] ?? '' ), 'readonly' => 'yes' === ( $s['readonly'] ?? '' ), 'conditions' => self::conditions_of( $s ), 'is_input' => $this->is_input(), ); return array_merge( $f, $this->schema_extra( $s ) ); } /** @return array Extra schema keys. */ protected function schema_extra( array $s ) { return array(); } /** * Conditional logic of a block's settings, normalized. * * @param array $s Settings. * @return array|null null when off. */ public static function conditions_of( array $s ) { if ( 'yes' !== ( $s['cl_enabled'] ?? '' ) ) { return null; } $rules = array(); foreach ( (array) ( $s['cl_rules'] ?? array() ) as $r ) { if ( ! is_array( $r ) || empty( $r['field'] ) ) { continue; } $rules[] = array( 'field' => preg_replace( '/[^a-z0-9_.\-]/', '', strtolower( (string) $r['field'] ) ), 'operator' => (string) ( $r['operator'] ?? '=' ), 'value' => is_scalar( $r['value'] ?? '' ) ? (string) ( $r['value'] ?? '' ) : '', ); } if ( ! $rules ) { return null; } return array( 'action' => 'hide' === ( $s['cl_action'] ?? 'show' ) ? 'hide' : 'show', 'match' => 'any' === ( $s['cl_match'] ?? 'all' ) ? 'any' : 'all', 'rules' => $rules, ); } /* ------------------------------------------------------------------ * * Values * ------------------------------------------------------------------ */ /** * Clean a submitted value. * * @param mixed $raw Raw request value (unslashed). * @param array $f Field schema. * @return mixed */ public function sanitize( $raw, array $f ) { if ( is_array( $raw ) ) { return array_map( 'sanitize_text_field', array_map( 'strval', array_filter( $raw, 'is_scalar' ) ) ); } return sanitize_text_field( (string) $raw ); } /** * Type-specific validation (required is checked by the Validator). * * @param mixed $value Clean value. * @param array $f Field schema. * @return string[] Error messages. */ public function validate( $value, array $f ) { return array(); } /** @return bool Whether a clean value counts as empty. */ public function is_empty( $value ) { if ( is_array( $value ) ) { foreach ( $value as $v ) { if ( ! $this->is_empty( $v ) ) { return false; } } return true; } return '' === trim( (string) $value ); } /** * Display a value. * * @param mixed $value Value. * @param array $f Field schema. * @param string $mode html | text. * @return string */ public function format( $value, array $f, $mode = 'html' ) { if ( is_array( $value ) ) { $value = implode( ', ', array_map( 'strval', array_filter( $value, 'is_scalar' ) ) ); } $value = (string) $value; return 'html' === $mode ? nl2br( esc_html( $value ) ) : $value; } /** * Numeric value for calculations. * * @param mixed $value Clean value. * @param array $f Field schema. * @return float */ public function numeric( $value, array $f ) { if ( is_array( $value ) ) { $sum = 0; foreach ( $value as $v ) { $sum += is_numeric( $v ) ? (float) $v : 0; } return $sum; } return is_numeric( $value ) ? (float) $value : 0.0; } /* ------------------------------------------------------------------ * * Rendering * ------------------------------------------------------------------ */ /** Wrapper classes and data attributes. */ protected function add_render_attributes() { parent::add_render_attributes(); $s = $this->settings; if ( $this->is_input() ) { $this->add_render_attribute( '_wrapper', 'class', 'fwren-field' ); $this->add_render_attribute( '_wrapper', 'class', 'fwren-field--' . $this->get_field_type() ); $this->add_render_attribute( '_wrapper', 'data-fwren-name', $this->field_key( $s ) ); $this->add_render_attribute( '_wrapper', 'data-fwren-type', $this->get_field_type() ); if ( 'yes' === ( $s['required'] ?? '' ) ) { $this->add_render_attribute( '_wrapper', 'class', 'is-required' ); } $lp = (string) ( $s['label_placement'] ?? '' ); if ( $lp ) { $this->add_render_attribute( '_wrapper', 'class', 'fwren-lp-' . sanitize_html_class( $lp ) ); } } $w = (string) ( $s['width'] ?? '100' ); $this->add_render_attribute( '_wrapper', 'class', 'fwren-col fwren-col-' . sanitize_html_class( $w ? $w : '100' ) ); $cond = self::conditions_of( $s ); if ( $cond ) { $this->add_render_attribute( '_wrapper', 'data-fwren-cond', wp_json_encode( $cond ) ); if ( ! Renderer::is_editor() ) { $this->add_render_attribute( '_wrapper', 'class', 'fwren-cond' ); } } } /** Print the field: label, control, help, error slot. */ protected function render( $content = '' ) { if ( ! $this->is_input() ) { $this->render_layout( $content ); return; } $s = $this->settings; $key = $this->field_key( $s ); $id = $this->input_id( $key ); echo '
'; $this->render_label( $s, $id ); echo '
'; $this->render_input( $s, $key, $id ); echo '
'; if ( '' !== trim( (string) ( $s['help'] ?? '' ) ) ) { echo '
' . wp_kses_post( $s['help'] ) . '
'; } echo ''; echo '
'; } /** Non-input blocks print here. */ protected function render_layout( $content ) { echo $content; // phpcs:ignore WordPress.Security.EscapeOutput } /** Label markup. */ protected function render_label( array $s, $id, $tag = 'label' ) { $label = (string) ( $s['label'] ?? '' ); if ( '' === trim( $label ) ) { return; } $req = 'yes' === ( $s['required'] ?? '' ) ? '' : ''; $attr = 'label' === $tag ? ' for="' . esc_attr( $id ) . '"' : ' id="' . esc_attr( $id ) . '-label"'; printf( '<%1$s class="fwren-label"%2$s>%3$s%4$s', esc_html( $tag ), $attr, wp_kses_post( $label ), $req ); // phpcs:ignore } /** * The control itself. * * @param array $s Settings. * @param string $key Field key (input name). * @param string $id DOM id. */ protected function render_input( array $s, $key, $id ) { $this->print_input( 'text', $s, $key, $id ); } /** @return string DOM id of the input, unique per rendered form instance. */ public function input_id( $key ) { return 'fwren-' . Renderer::instance_id() . '-' . $key; } /** Shared attributes (aria, required, readonly, placeholder) as a string. */ protected function common_attrs( array $s, $id, $extra = array() ) { $a = array( 'id' => $id, 'class' => 'fwren-input', 'aria-describedby' => $id . '-error' . ( '' !== trim( (string) ( $s['help'] ?? '' ) ) ? ' ' . $id . '-help' : '' ), ); if ( 'yes' === ( $s['required'] ?? '' ) ) { $a['required'] = 'required'; $a['aria-required'] = 'true'; } if ( 'yes' === ( $s['readonly'] ?? '' ) ) { $a['readonly'] = 'readonly'; } if ( '' !== (string) ( $s['placeholder'] ?? '' ) ) { $a['placeholder'] = $s['placeholder']; } $a = array_merge( $a, $extra ); return self::attrs( $a ); } /** @return string HTML attributes from an array (false/null skipped, true = boolean attribute). */ public static function attrs( array $a ) { $out = ''; foreach ( $a as $k => $v ) { if ( false === $v || null === $v ) { continue; } if ( true === $v ) { $out .= ' ' . esc_attr( $k ); continue; } $out .= ' ' . esc_attr( $k ) . '="' . esc_attr( (string) $v ) . '"'; } return $out; } /** with the shared attributes. */ protected function print_input( $type, array $s, $key, $id, array $extra = array() ) { $value = $this->default_for_display( $s ); echo 'common_attrs( $s, $id, $extra ) . ' />'; // phpcs:ignore } /** @return string Default value with smartcodes resolved (empty in the editor). */ protected function default_for_display( array $s ) { $d = (string) ( $s['default_value'] ?? '' ); if ( '' === $d ) { return ''; } return Renderer::is_editor() ? ( false !== strpos( $d, '{' ) ? '' : $d ) : Smartcodes::parse_default( $d, Renderer::current_form_id() ); } /** @return bool Whether this request renders the editor preview. */ public function is_editor() { return Renderer::is_editor(); } /** * Options of a choice control, normalized: [ label, value, calc, image, selected ]. * * @param mixed $raw Control value. * @return array */ public static function normalize_options( $raw ) { $out = array(); foreach ( (array) $raw as $o ) { if ( is_string( $o ) ) { $o = array( 'label' => $o, 'value' => $o ); } if ( ! is_array( $o ) ) { continue; } $label = (string) ( $o['label'] ?? '' ); $value = (string) ( $o['value'] ?? '' ); if ( '' === $value ) { $value = $label; } if ( '' === $label && '' === $value ) { continue; } $out[] = array( 'label' => $label, 'value' => $value, 'calc' => (string) ( $o['calc'] ?? '' ), 'image' => (string) ( $o['image'] ?? '' ), 'selected' => ! empty( $o['selected'] ), 'stock' => isset( $o['stock'] ) && '' !== $o['stock'] ? (int) $o['stock'] : null, 'disabled' => ! empty( $o['disabled'] ), ); } return $out; } }