__( 'Choose an option', 'fillnova' ), 'options' => array( array( 'label' => __( 'Option 1', 'fillnova' ), 'value' => 'Option 1' ), array( 'label' => __( 'Option 2', 'fillnova' ), 'value' => 'Option 2' ), array( 'label' => __( 'Option 3', 'fillnova' ), 'value' => 'Option 3' ), ), 'required' => false, ); $types = array( // Basic. 'text' => array( 'label' => __( 'Short Text', 'fillnova' ), 'icon' => 'editor-textcolor', 'group' => 'basic', 'defaults' => array( 'label' => __( 'Short text', 'fillnova' ) ) ), 'textarea' => array( 'label' => __( 'Long Text', 'fillnova' ), 'icon' => 'editor-paragraph', 'group' => 'basic', 'defaults' => array( 'label' => __( 'Message', 'fillnova' ), 'rows' => 5 ) ), 'email' => array( 'label' => __( 'Email', 'fillnova' ), 'icon' => 'email', 'group' => 'basic', 'defaults' => array( 'label' => __( 'Email address', 'fillnova' ), 'placeholder' => 'you@example.com' ) ), 'name' => array( 'label' => __( 'Full Name', 'fillnova' ), 'icon' => 'admin-users', 'group' => 'basic', 'defaults' => array( 'label' => __( 'Your name', 'fillnova' ), 'subfields' => 'first,last' ) ), 'phone' => array( 'label' => __( 'Phone', 'fillnova' ), 'icon' => 'phone', 'group' => 'basic', 'defaults' => array( 'label' => __( 'Phone number', 'fillnova' ) ) ), 'number' => array( 'label' => __( 'Number', 'fillnova' ), 'icon' => 'calculator', 'group' => 'basic', 'defaults' => array( 'label' => __( 'Number', 'fillnova' ) ) ), 'url' => array( 'label' => __( 'Website / URL', 'fillnova' ), 'icon' => 'admin-links', 'group' => 'basic', 'defaults' => array( 'label' => __( 'Website', 'fillnova' ), 'placeholder' => 'https://' ) ), 'address' => array( 'label' => __( 'Address', 'fillnova' ), 'icon' => 'location', 'group' => 'basic', 'defaults' => array( 'label' => __( 'Address', 'fillnova' ), 'subfields' => 'line1,line2,city,state,zip,country' ) ), // Choice. 'select' => array( 'label' => __( 'Dropdown', 'fillnova' ), 'icon' => 'arrow-down-alt2', 'group' => 'choice', 'defaults' => $choice_defaults ), 'radio' => array( 'label' => __( 'Single Choice', 'fillnova' ), 'icon' => 'marker', 'group' => 'choice', 'defaults' => $choice_defaults ), 'checkbox' => array( 'label' => __( 'Multiple Choice', 'fillnova' ), 'icon' => 'yes-alt', 'group' => 'choice', 'defaults' => $choice_defaults ), 'yesno' => array( 'label' => __( 'Yes / No', 'fillnova' ), 'icon' => 'thumbs-up', 'group' => 'choice', 'defaults' => array( 'label' => __( 'Do you agree?', 'fillnova' ) ) ), 'rating' => array( 'label' => __( 'Star Rating', 'fillnova' ), 'icon' => 'star-filled', 'group' => 'choice', 'defaults' => array( 'label' => __( 'How would you rate us?', 'fillnova' ), 'stars' => 5 ) ), 'range' => array( 'label' => __( 'Slider', 'fillnova' ), 'icon' => 'leftright', 'group' => 'choice', 'defaults' => array( 'label' => __( 'Select a value', 'fillnova' ), 'min' => 0, 'max' => 100, 'step' => 1 ) ), 'consent' => array( 'label' => __( 'Consent / GDPR', 'fillnova' ), 'icon' => 'shield', 'group' => 'choice', 'defaults' => array( 'label' => __( 'Consent', 'fillnova' ), 'consent_text' => __( 'I agree to the privacy policy and terms.', 'fillnova' ), 'required' => true ) ), // Advanced. 'date' => array( 'label' => __( 'Date', 'fillnova' ), 'icon' => 'calendar-alt', 'group' => 'advanced', 'defaults' => array( 'label' => __( 'Date', 'fillnova' ) ) ), 'time' => array( 'label' => __( 'Time', 'fillnova' ), 'icon' => 'clock', 'group' => 'advanced', 'defaults' => array( 'label' => __( 'Time', 'fillnova' ) ) ), 'file' => array( 'label' => __( 'File Upload', 'fillnova' ), 'icon' => 'upload', 'group' => 'advanced', 'defaults' => array( 'label' => __( 'Upload a file', 'fillnova' ), 'accept' => 'jpg,jpeg,png,pdf,doc,docx', 'max_size' => 5, 'max_files' => 1 ) ), 'hidden' => array( 'label' => __( 'Hidden Field', 'fillnova' ), 'icon' => 'hidden', 'group' => 'advanced', 'defaults' => array( 'label' => __( 'Hidden', 'fillnova' ) ) ), // Layout. 'heading' => array( 'label' => __( 'Heading', 'fillnova' ), 'icon' => 'heading', 'group' => 'layout', 'input' => false, 'defaults' => array( 'label' => __( 'Section title', 'fillnova' ), 'level' => 'h3' ) ), 'html' => array( 'label' => __( 'Text / HTML', 'fillnova' ), 'icon' => 'editor-code', 'group' => 'layout', 'input' => false, 'defaults' => array( 'label' => __( 'Content', 'fillnova' ), 'content' => '

' . __( 'Add some helpful text here.', 'fillnova' ) . '

' ) ), 'divider' => array( 'label' => __( 'Divider', 'fillnova' ), 'icon' => 'minus', 'group' => 'layout', 'input' => false, 'defaults' => array( 'label' => __( 'Divider', 'fillnova' ) ) ), 'pagebreak' => array( 'label' => __( 'Page Break', 'fillnova' ), 'icon' => 'admin-page', 'group' => 'layout', 'input' => false, 'defaults' => array( 'label' => __( 'Next step', 'fillnova' ) ) ), ); /** * Register Pro / custom field types. * * Pro types registered while Pro is inactive should set 'pro' => true so the builder shows them locked. */ $types = apply_filters( 'fillnova_field_types', $types ); foreach ( $types as $k => $t ) { $types[ $k ] = wp_parse_args( $t, array( 'input' => true, 'pro' => false, 'defaults' => array(), ) ); } if ( ! fillnova_is_pro() ) { // Free gets a small core of fields; everything else stays visible but locked (Pro). $free = fillnova_free_field_types(); foreach ( $types as $k => $t ) { if ( ! in_array( $k, $free, true ) ) { $types[ $k ]['pro'] = true; $types[ $k ]['locked'] = true; $types[ $k ]['group'] = 'pro'; } } $teasers = array( 'repeater' => array( 'label' => __( 'Repeater', 'fillnova' ), 'icon' => 'controls-repeat' ), 'signature' => array( 'label' => __( 'Signature', 'fillnova' ), 'icon' => 'edit' ), 'calculation' => array( 'label' => __( 'Calculation', 'fillnova' ), 'icon' => 'calculator' ), 'stripe' => array( 'label' => __( 'Card Payment (Stripe)', 'fillnova' ), 'icon' => 'cart' ), 'paypal' => array( 'label' => __( 'PayPal Payment', 'fillnova' ), 'icon' => 'money-alt' ), 'nps' => array( 'label' => __( 'NPS Score', 'fillnova' ), 'icon' => 'chart-bar' ), 'image_choice' => array( 'label' => __( 'Image Choice', 'fillnova' ), 'icon' => 'format-image' ), ); foreach ( $teasers as $k => $t ) { if ( ! isset( $types[ $k ] ) ) { $types[ $k ] = array_merge( $t, array( 'group' => 'pro', 'pro' => true, 'locked' => true, 'input' => true, 'defaults' => array(), ) ); } } } self::$types = $types; return $types; } /** * Reset cache (used when Pro registers late). */ public static function flush() { self::$types = null; } /** * Whether a usable field type exists (locked teasers are not usable). * * @param string $type Type. * @return bool */ public static function exists( $type ) { $types = self::types(); return isset( $types[ $type ] ) && empty( $types[ $type ]['locked'] ); } /** * Known type, including Pro types while Pro is inactive (kept so forms are never damaged). * * @param string $type Type. * @return bool */ public static function known( $type ) { $types = self::types(); return isset( $types[ $type ] ); } /** * Is the type a locked Pro teaser? * * @param string $type Type. * @return bool */ public static function locked( $type ) { $types = self::types(); return ! empty( $types[ $type ]['locked'] ); } /** * Whether a field collects input. * * @param array $field Field. * @return bool */ public static function is_input( $field ) { $types = self::types(); return isset( $types[ $field['type'] ] ) && ! empty( $types[ $field['type'] ]['input'] ) && empty( $types[ $field['type'] ]['locked'] ); } /** * Groups for the builder palette. * * @return array */ public static function groups() { return apply_filters( 'fillnova_field_groups', array( 'basic' => __( 'Basic', 'fillnova' ), 'choice' => __( 'Choices', 'fillnova' ), 'advanced' => __( 'Advanced', 'fillnova' ), 'layout' => __( 'Layout', 'fillnova' ), 'pro' => __( 'Pro', 'fillnova' ), ) ); } /** * Sanitize a submitted value for a field. * * @param array $field Field. * @param mixed $raw Raw value. * @return mixed */ public static function sanitize_value( $field, $raw ) { $type = $field['type']; switch ( $type ) { case 'email': return sanitize_email( self::str( $raw ) ); case 'url': return esc_url_raw( self::str( $raw ) ); case 'textarea': return sanitize_textarea_field( self::str( $raw ) ); case 'number': case 'range': return '' === $raw || null === $raw ? '' : (string) ( 0 + (float) $raw ); case 'rating': return '' === $raw || null === $raw ? '' : (string) absint( $raw ); case 'checkbox': $vals = self::str_list( $raw ); return array_values( array_intersect( $vals, self::option_values( $field ) ) ); case 'select': if ( ! empty( $field['multiple'] ) ) { $vals = self::str_list( $raw ); return array_values( array_intersect( $vals, self::option_values( $field ) ) ); } // Fall through for single select. case 'radio': $val = sanitize_text_field( self::str( $raw ) ); return in_array( $val, self::option_values( $field ), true ) ? $val : ''; case 'yesno': $val = sanitize_text_field( self::str( $raw ) ); return in_array( $val, array( 'Yes', 'No' ), true ) ? $val : ''; case 'consent': return ! empty( $raw ) ? 'Yes' : ''; case 'name': case 'address': $out = array(); foreach ( self::subfields( $field ) as $sub ) { $out[ $sub ] = is_array( $raw ) && isset( $raw[ $sub ] ) ? sanitize_text_field( self::str( $raw[ $sub ] ) ) : ''; } return $out; case 'date': $val = sanitize_text_field( self::str( $raw ) ); if ( ! preg_match( '/^(\d{4})-(\d{2})-(\d{2})$/', $val, $m ) || ! checkdate( (int) $m[2], (int) $m[3], (int) $m[1] ) ) { return ''; } return $val; case 'time': $val = sanitize_text_field( self::str( $raw ) ); return preg_match( '/^([01]\d|2[0-3]):[0-5]\d$/', $val ) ? $val : ''; default: $val = is_array( $raw ) ? self::str_list( $raw ) : sanitize_text_field( self::str( $raw ) ); return apply_filters( 'fillnova_sanitize_value_' . $type, $val, $field, $raw ); } } /** * Scalar to string (arrays become empty strings – never trust input shape). * * @param mixed $v Value. * @return string */ public static function str( $v ) { return is_scalar( $v ) ? (string) $v : ''; } /** * List of sanitized strings (nested arrays dropped). * * @param mixed $v Value. * @return string[] */ public static function str_list( $v ) { $out = array(); foreach ( (array) $v as $item ) { if ( is_scalar( $item ) ) { $out[] = sanitize_text_field( (string) $item ); } } return $out; } /** * Validate a sanitized value. Returns error string or empty string. * * @param array $field Field. * @param mixed $value Value. * @param mixed $raw Raw value. * @return string */ public static function validate( $field, $value, $raw ) { $required = ! empty( $field['required'] ); $empty = self::is_empty( $value ); $label = isset( $field['label'] ) ? $field['label'] : ''; $custom = ! empty( $field['error_message'] ) ? $field['error_message'] : ''; // Catch values that sanitization emptied out (e.g. an invalid email). $messages = apply_filters( 'fillnova_invalid_value_messages', array( 'email' => fillnova_message( 'email' ), 'url' => fillnova_message( 'url' ), 'date' => fillnova_message( 'date' ), 'time' => fillnova_message( 'time' ), 'radio' => fillnova_message( 'choice' ), 'select' => fillnova_message( 'choice' ), 'checkbox' => fillnova_message( 'choice' ), 'yesno' => fillnova_message( 'choice' ), 'rating' => fillnova_message( 'rating' ), 'nps' => __( 'Please choose a score from 0 to 10.', 'fillnova' ), 'image_choice' => fillnova_message( 'choice' ), ) ); if ( $empty && ! self::is_empty( $raw ) && isset( $messages[ $field['type'] ] ) ) { return $messages[ $field['type'] ]; } if ( $required && $empty ) { if ( $custom ) { return $custom; } return 'consent' === $field['type'] ? fillnova_message( 'consent', array( 'label' => $label ) ) : fillnova_message( 'required', array( 'label' => $label ) ); } if ( $empty ) { return ''; } switch ( $field['type'] ) { case 'email': if ( ! is_email( $value ) ) { return fillnova_message( 'email', array( 'label' => $label ) ); } break; case 'url': if ( ! filter_var( $value, FILTER_VALIDATE_URL ) || ! preg_match( '#^https?://#i', $value ) ) { return fillnova_message( 'url', array( 'label' => $label ) ); } break; case 'number': case 'range': if ( ! is_numeric( $raw ) ) { return fillnova_message( 'number', array( 'label' => $label ) ); } if ( isset( $field['min'] ) && '' !== $field['min'] && (float) $value < (float) $field['min'] ) { return fillnova_message( 'min', array( 'label' => $label, 'limit' => $field['min'] ) ); } if ( isset( $field['max'] ) && '' !== $field['max'] && (float) $value > (float) $field['max'] ) { return fillnova_message( 'max', array( 'label' => $label, 'limit' => $field['max'] ) ); } break; case 'phone': if ( ! preg_match( '/^[0-9\s\-\+\(\)\.]{5,25}$/', $value ) ) { return fillnova_message( 'phone', array( 'label' => $label ) ); } break; case 'rating': $stars = ! empty( $field['stars'] ) ? (int) $field['stars'] : 5; if ( (int) $value < 1 || (int) $value > $stars ) { return fillnova_message( 'rating', array( 'label' => $label ) ); } break; case 'name': if ( $required && '' === trim( implode( '', (array) $value ) ) ) { return $custom ? $custom : fillnova_message( 'required', array( 'label' => $label ) ); } break; } if ( in_array( $field['type'], array( 'text', 'textarea' ), true ) ) { $len = function_exists( 'mb_strlen' ) ? mb_strlen( $value ) : strlen( $value ); if ( ! empty( $field['minlength'] ) && $len < (int) $field['minlength'] ) { return fillnova_message( 'minlength', array( 'label' => $label, 'limit' => (int) $field['minlength'] ) ); } if ( ! empty( $field['maxlength'] ) && $len > (int) $field['maxlength'] ) { return fillnova_message( 'maxlength', array( 'label' => $label, 'limit' => (int) $field['maxlength'] ) ); } } return (string) apply_filters( 'fillnova_validate_field', '', $field, $value, $raw ); } /** * Is the value empty? * * @param mixed $value Value. * @return bool */ public static function is_empty( $value ) { if ( is_array( $value ) ) { return '' === trim( fillnova_value_to_string( $value ) ); } return '' === trim( (string) $value ); } /** * Option values of a choice field. * * @param array $field Field. * @return string[] */ public static function option_values( $field ) { $vals = array(); foreach ( (array) fillnova_arr( $field, 'options', array() ) as $o ) { $vals[] = (string) ( isset( $o['value'] ) ? $o['value'] : $o['label'] ); } return $vals; } /** * Sub-fields of name / address. * * @param array $field Field. * @return string[] */ public static function subfields( $field ) { $default = 'name' === $field['type'] ? 'first,last' : 'line1,line2,city,state,zip,country'; $list = ! empty( $field['subfields'] ) ? $field['subfields'] : $default; $allowed = 'name' === $field['type'] ? array( 'prefix', 'first', 'middle', 'last' ) : array( 'line1', 'line2', 'city', 'state', 'zip', 'country' ); return array_values( array_intersect( array_map( 'trim', explode( ',', $list ) ), $allowed ) ); } /** * Sub-field label. * * @param string $sub Sub key. * @return string */ public static function subfield_label( $sub ) { $labels = array( 'prefix' => __( 'Prefix', 'fillnova' ), 'first' => __( 'First name', 'fillnova' ), 'middle' => __( 'Middle name', 'fillnova' ), 'last' => __( 'Last name', 'fillnova' ), 'line1' => __( 'Street address', 'fillnova' ), 'line2' => __( 'Apartment, suite, etc.', 'fillnova' ), 'city' => __( 'City', 'fillnova' ), 'state' => __( 'State / Region', 'fillnova' ), 'zip' => __( 'ZIP / Postal code', 'fillnova' ), 'country' => __( 'Country', 'fillnova' ), ); return isset( $labels[ $sub ] ) ? $labels[ $sub ] : $sub; } /** * Flatten fields (expanding nothing – repeater children are handled by Pro). * * @param array $fields Fields. * @return array id => field */ public static function index( $fields ) { $out = array(); foreach ( $fields as $f ) { $out[ $f['id'] ] = $f; } return $out; } }