admin_url( 'admin-ajax.php' ),
'i18n' => array(
'required' => fillnova_message( 'required', array( 'label' => '{label}' ) ),
'consent' => fillnova_message( 'consent', array( 'label' => '{label}' ) ),
'email' => fillnova_message( 'email', array( 'label' => '{label}' ) ),
'url' => fillnova_message( 'url', array( 'label' => '{label}' ) ),
'phone' => fillnova_message( 'phone', array( 'label' => '{label}' ) ),
'number' => fillnova_message( 'number', array( 'label' => '{label}' ) ),
'min' => fillnova_message( 'min', array( 'label' => '{label}', 'limit' => '{limit}' ) ),
'max' => fillnova_message( 'max', array( 'label' => '{label}', 'limit' => '{limit}' ) ),
'minlength' => fillnova_message( 'minlength', array( 'label' => '{label}', 'limit' => '{limit}' ) ),
'maxlength' => fillnova_message( 'maxlength', array( 'label' => '{label}', 'limit' => '{limit}' ) ),
'fileSize' => fillnova_message( 'file_size', array( 'label' => '{label}', 'file' => '' ) ),
'fileType' => fillnova_message( 'file_type', array( 'label' => '{label}', 'file' => '' ) ),
'sending' => __( 'Sending…', 'fillnova' ),
'error' => __( 'Something went wrong. Please try again.', 'fillnova' ),
'pressEnter' => __( 'press Enter ↵', 'fillnova' ),
/* translators: 1: current step number, 2: total number of steps */
'stepOf' => __( 'Step %1$s of %2$s', 'fillnova' ),
),
)
);
do_action( 'fillnova_enqueue_front' );
}
/**
* Render a form by ID.
*
* @param int $form_id Form ID.
* @param array $args Extra args (title => bool).
* @return string
*/
public static function render( $form_id, $args = array() ) {
$form = Fillnova_Forms::get( $form_id );
if ( ! $form || ( 'publish' !== $form['status'] && empty( $args['preview'] ) ) ) {
return current_user_can( 'manage_options' ) ? '
' . esc_html__( 'Fillnova: form not found or not published.', 'fillnova' ) . '
' : '';
}
$s = $form['settings'];
if ( ! empty( $s['require_login'] ) && ! is_user_logged_in() ) {
return '' . esc_html__( 'Please log in to fill in this form.', 'fillnova' ) . '
';
}
if ( ! empty( $s['entry_limit'] ) && Fillnova_Entries::count( $form['id'] ) >= (int) $s['entry_limit'] ) {
return '' . esc_html( $s['limit_message'] ) . '
';
}
self::enqueue();
Fillnova_Captcha::enqueue( $s['captcha'] );
++self::$count;
$uid = 'fillnova-' . $form['id'] . '-' . self::$count;
$steps = self::build_steps( $form['fields'], $s['mode'] );
$config = self::front_config( $form );
$style = sprintf(
'--fy-primary:%1$s;--fy-radius:%2$dpx;',
esc_attr( $s['primary_color'] ),
(int) $s['radius']
);
$classes = array(
'fillnova',
'fillnova-theme-' . $s['theme'],
'fillnova-mode-' . $s['mode'],
'fillnova-labels-' . $s['label_position'],
'fillnova-bg-' . $s['form_bg'],
'fillnova-size-' . $s['field_size'],
'fillnova-btnw-' . $s['button_width'],
);
if ( ! empty( $s['button_align'] ) && 'left' !== $s['button_align'] ) {
$classes[] = 'fillnova-align-' . $s['button_align'];
}
if ( ! empty( $s['button_size'] ) && 'auto' !== $s['button_size'] ) {
$classes[] = 'fillnova-btns-' . $s['button_size'];
}
/**
* Extra design: Pro adds CSS variables and classes here.
*/
$style = (string) apply_filters( 'fillnova_form_style', $style, $s, $form );
$classes = (array) apply_filters( 'fillnova_form_classes', $classes, $s, $form );
ob_start();
do_action( 'fillnova_before_form', $uid, $s, $form );
?>
array() );
if ( 'conversational' === $mode ) {
$pending = array();
foreach ( $fields as $f ) {
if ( 'pagebreak' === $f['type'] || 'divider' === $f['type'] ) {
continue;
}
if ( in_array( $f['type'], array( 'heading', 'html' ), true ) || 'hidden' === $f['type'] ) {
$pending[] = $f;
continue;
}
$steps[] = array( 'fields' => array_merge( $pending, array( $f ) ) );
$pending = array();
}
if ( $pending ) {
if ( $steps ) {
$steps[ count( $steps ) - 1 ]['fields'] = array_merge( $steps[ count( $steps ) - 1 ]['fields'], $pending );
} else {
$steps[] = array( 'fields' => $pending );
}
}
return $steps ? $steps : array( array( 'fields' => array() ) );
}
foreach ( $fields as $f ) {
if ( 'pagebreak' === $f['type'] && 'multistep' === $mode ) {
$current['break'] = isset( $f['label'] ) ? $f['label'] : '';
$steps[] = $current;
$current = array( 'fields' => array() );
continue;
}
if ( 'pagebreak' === $f['type'] ) {
continue;
}
$current['fields'][] = $f;
}
$steps[] = $current;
return array_values(
array_filter(
$steps,
function ( $s ) {
return ! empty( $s['fields'] );
}
)
);
}
/**
* Config for the front-end JS.
*
* @param array $form Form.
* @return array
*/
public static function front_config( $form ) {
$fields = array();
foreach ( $form['fields'] as $f ) {
$fields[] = self::field_config( $f );
}
$s = $form['settings'];
return apply_filters(
'fillnova_front_config',
array(
'id' => $form['id'],
'mode' => $s['mode'],
'successType' => $s['success_type'],
'fields' => $fields,
),
$form
);
}
/**
* Minimal config for a field.
*
* @param array $f Field.
* @return array
*/
public static function field_config( $f ) {
$keep = array( 'id', 'type', 'required', 'conditions', 'label', 'max_size', 'accept', 'max_files', 'min', 'max', 'mask', 'minlength', 'maxlength', 'error_message' );
$out = array_intersect_key( $f, array_flip( $keep ) );
if ( isset( $out['conditions'] ) && empty( $out['conditions']['enabled'] ) ) {
unset( $out['conditions'] );
}
return apply_filters( 'fillnova_field_front_config', $out, $f );
}
/**
* Render a field.
*
* @param array $field Field.
* @param array $form Form.
* @return string
*/
public static function render_field( $field, $form ) {
$type = $field['type'];
$id = $field['id'];
$dom_id = 'fy-' . $form['id'] . '-' . $id . '-' . self::$count;
$name = 'fillnova[' . $id . ']';
$label = isset( $field['label'] ) ? $field['label'] : '';
$req = ! empty( $field['required'] );
$width = ! empty( $field['width'] ) ? $field['width'] : 'full';
$classes = array( 'fillnova-field', 'fillnova-type-' . $type, 'fillnova-w-' . $width );
if ( ! empty( $field['css_class'] ) ) {
$classes[] = $field['css_class'];
}
if ( $req ) {
$classes[] = 'is-required';
}
$attrs = sprintf(
' class="%1$s" data-field-id="%2$s" data-type="%3$s"',
esc_attr( implode( ' ', $classes ) ),
esc_attr( $id ),
esc_attr( $type )
);
if ( ! empty( $field['conditions']['enabled'] ) ) {
$attrs .= ' data-conditional="1"';
}
if ( Fillnova_Fields::locked( $type ) ) {
return ''; // Pro field while Fillnova Pro is inactive.
}
// Layout elements.
if ( 'heading' === $type ) {
$tag = in_array( fillnova_arr( $field, 'level', 'h3' ), array( 'h2', 'h3', 'h4' ), true ) ? $field['level'] : 'h3';
$out = '<' . $tag . ' class="fillnova-heading">' . esc_html( $label ) . '' . $tag . '>';
if ( ! empty( $field['help'] ) ) {
$out .= '
' . esc_html( $field['help'] ) . '
';
}
return $out . '
';
}
if ( 'html' === $type ) {
return '' . wp_kses_post( fillnova_arr( $field, 'content', '' ) ) . '
';
}
if ( 'divider' === $type ) {
return '
';
}
if ( 'hidden' === $type ) {
$value = self::dynamic_default( fillnova_arr( $field, 'default', '' ) );
return '';
}
$custom = apply_filters( 'fillnova_render_field_' . $type, null, $field, $form, $dom_id, $name );
if ( null !== $custom ) {
$input = $custom;
} else {
$input = self::render_input( $field, $dom_id, $name );
}
$label_html = '';
$fieldset = in_array( $type, array( 'radio', 'checkbox', 'yesno', 'rating', 'name', 'address' ), true ) || apply_filters( 'fillnova_field_is_group', false, $type );
if ( 'consent' !== $type ) {
$label_class = 'fillnova-label' . ( ! empty( $field['hide_label'] ) ? ' screen-reader-text' : '' );
$req_html = $req ? ' *' : '';
$label_html = $fieldset
? ''
: '';
}
$help = ! empty( $field['help'] ) ? '' . esc_html( $field['help'] ) . '
' : '';
$err = '';
$open = $fieldset ? '' : '';
return '' . $open . $label_html . '
' . $input . '
' . $help . $err . $close . '
';
}
/**
* Render the input control of a field.
*
* @param array $f Field.
* @param string $dom_id DOM id.
* @param string $name Input name.
* @return string
*/
public static function render_input( $f, $dom_id, $name ) {
$type = $f['type'];
$req = ! empty( $f['required'] ) ? ' required aria-required="true"' : '';
$placeholder = ! empty( $f['placeholder'] ) ? ' placeholder="' . esc_attr( $f['placeholder'] ) . '"' : '';
$default = self::dynamic_default( fillnova_arr( $f, 'default', '' ) );
$describe = ' aria-describedby="' . esc_attr( $dom_id ) . '-error' . ( ! empty( $f['help'] ) ? ' ' . esc_attr( $dom_id ) . '-help' : '' ) . '"';
$common = ' id="' . esc_attr( $dom_id ) . '" name="' . esc_attr( $name ) . '"' . $req . $placeholder . $describe;
switch ( $type ) {
case 'text':
case 'email':
case 'url':
case 'phone':
case 'number':
$html_type = array(
'text' => 'text',
'email' => 'email',
'url' => 'url',
'phone' => 'tel',
'number' => 'number',
)[ $type ];
$extra = '';
foreach ( array( 'min', 'max', 'step' ) as $k ) {
if ( 'number' === $type && isset( $f[ $k ] ) && '' !== $f[ $k ] ) {
$extra .= ' ' . $k . '="' . esc_attr( $f[ $k ] ) . '"';
}
}
if ( ! empty( $f['maxlength'] ) && 'number' !== $type ) {
$extra .= ' maxlength="' . esc_attr( $f['maxlength'] ) . '"';
}
if ( ! empty( $f['mask'] ) ) {
$extra .= ' data-mask="' . esc_attr( $f['mask'] ) . '"';
}
$auto = array(
'email' => 'email',
'phone' => 'tel',
'url' => 'url',
);
if ( isset( $auto[ $type ] ) ) {
$extra .= ' autocomplete="' . $auto[ $type ] . '"';
}
$input = '';
if ( ! empty( $f['prefix'] ) || ! empty( $f['suffix'] ) ) {
$input = '' . ( ! empty( $f['prefix'] ) ? '' . esc_html( $f['prefix'] ) . '' : '' ) . $input . ( ! empty( $f['suffix'] ) ? '' . esc_html( $f['suffix'] ) . '' : '' ) . '
';
}
return $input;
case 'textarea':
$rows = ! empty( $f['rows'] ) ? (int) $f['rows'] : 5;
$extra = ! empty( $f['maxlength'] ) ? ' maxlength="' . esc_attr( $f['maxlength'] ) . '"' : '';
return '';
case 'select':
$multi = ! empty( $f['multiple'] );
$out = '';
case 'radio':
case 'checkbox':
$is_cb = 'checkbox' === $type;
$layout = fillnova_arr( $f, 'layout', 'stack' );
$out = '';
foreach ( (array) fillnova_arr( $f, 'options', array() ) as $i => $o ) {
$oid = $dom_id . '-' . $i;
$out .= '';
}
return $out . '
';
case 'yesno':
$out = '';
foreach ( array( 'Yes' => __( 'Yes', 'fillnova' ), 'No' => __( 'No', 'fillnova' ) ) as $val => $lab ) {
$oid = $dom_id . '-' . strtolower( $val );
$out .= '';
}
return $out . '
';
case 'rating':
$stars = ! empty( $f['stars'] ) ? min( 10, max( 3, (int) $f['stars'] ) ) : 5;
$out = '';
case 'range':
$min = isset( $f['min'] ) && '' !== $f['min'] ? $f['min'] : 0;
$max = isset( $f['max'] ) && '' !== $f['max'] ? $f['max'] : 100;
$step = isset( $f['step'] ) && '' !== $f['step'] ? $f['step'] : 1;
$val = '' !== $default ? $default : $min;
return '';
case 'consent':
$text = ! empty( $f['consent_text'] ) ? $f['consent_text'] : $f['label'];
return '';
case 'date':
$extra = '';
if ( isset( $f['min'] ) && '' !== $f['min'] ) {
$extra .= ' data-min-days="' . esc_attr( (int) $f['min'] ) . '"';
}
return '';
case 'time':
return '';
case 'file':
$accept = array_filter( array_map( 'trim', explode( ',', (string) fillnova_arr( $f, 'accept', '' ) ) ) );
$acc_str = $accept ? ' accept="' . esc_attr( '.' . implode( ',.', $accept ) ) . '"' : '';
$max = max( 1, (int) fillnova_arr( $f, 'max_files', 1 ) );
$multi = $max > 1 ? ' multiple' : '';
/* translators: 1: allowed types, 2: max size */
$hint = sprintf( __( 'Allowed: %1$s · Max %2$s MB', 'fillnova' ), $accept ? implode( ', ', $accept ) : __( 'any', 'fillnova' ), fillnova_arr( $f, 'max_size', 5 ) );
return '';
case 'name':
case 'address':
$subs = Fillnova_Fields::subfields( $f );
$out = '';
foreach ( $subs as $sub ) {
$sid = $dom_id . '-' . $sub;
$auto = array(
'first' => 'given-name',
'last' => 'family-name',
'middle' => 'additional-name',
'prefix' => 'honorific-prefix',
'line1' => 'address-line1',
'line2' => 'address-line2',
'city' => 'address-level2',
'state' => 'address-level1',
'zip' => 'postal-code',
'country' => 'country-name',
);
$sreq = ( $req && in_array( $sub, array( 'first', 'last', 'line1', 'city', 'zip', 'country' ), true ) ) ? ' required' : '';
$out .= '
';
}
return $out . '
';
}
return '';
}
/**
* Resolve dynamic default values like {user_email}, {get:param}.
*
* @param string $value Value.
* @return string
*/
public static function dynamic_default( $value ) {
if ( ! is_string( $value ) || false === strpos( $value, '{' ) ) {
return (string) $value;
}
$user = wp_get_current_user();
$map = array(
'{user_email}' => $user && $user->exists() ? $user->user_email : '',
'{user_name}' => $user && $user->exists() ? $user->display_name : '',
'{user_id}' => $user && $user->exists() ? (string) $user->ID : '',
'{page_title}' => get_the_title(),
'{page_url}' => get_permalink() ? get_permalink() : '',
'{date}' => wp_date( 'Y-m-d' ),
);
$value = strtr( $value, $map );
$value = preg_replace_callback(
'/\{get:([a-z0-9_\-]+)\}/i',
function ( $m ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
return isset( $_GET[ $m[1] ] ) ? sanitize_text_field( wp_unslash( $_GET[ $m[1] ] ) ) : '';
},
$value
);
return (string) $value;
}
}