';
return $html;
}
/**
* Splits the fields into pages at each page break.
*
* @param array $fields Fields in order.
* @return array One list of fields per page.
*/
public static function pages( array $fields ) {
$pages = array( array() );
foreach ( $fields as $field ) {
if ( ! Registry::get( $field['type'] ) ) {
// Belongs to an extension that is not active.
continue;
}
if ( 'page_break' === $field['type'] ) {
$pages[] = array();
} else {
$pages[ count( $pages ) - 1 ][] = $field;
}
}
// A break at the very start or two in a row would leave an empty page.
return array_values( array_filter( $pages ) ) ? array_values( array_filter( $pages ) ) : array( array() );
}
/**
* What the browser script needs to know about the form. Notifications, webhooks and anything
* else private stay on the server.
*
* @param array $form Form.
* @param bool $preview Whether this is the builder's preview.
* @return array
*/
public static function client_config( array $form, $preview = false ) {
$fields = array();
$keep = array( 'id', 'key', 'type', 'required', 'conditions', 'formula', 'decimals', 'prefix', 'min', 'max', 'minlength', 'maxlength', 'max_size_mb', 'max_files', 'allowed_types' );
$dynamic = array();
foreach ( $form['schema']['fields'] as $field ) {
if ( Registry::is_input( $field['type'] ) ) {
$fields[] = array_intersect_key( $field, array_flip( $keep ) );
// A default that depends on the address bar or the page is worked out again in the
// browser, which knows both even on a cached page or another website.
$template = DynamicValues::browser_template( $field['default'] ?? '' );
if ( '' !== $template ) {
$dynamic[ $field['key'] ] = $template;
}
}
}
$settings = $form['schema']['settings'];
return array(
'id' => (int) $form['id'],
'preview' => (bool) $preview,
// Present only for a form that uses reCAPTCHA; nothing is loaded from Google otherwise.
'recaptcha' => ! $preview && Recaptcha::is_active( $form ) ? Recaptcha::client_config() : null,
'endpoint' => esc_url_raw( rest_url( 'vuloform/v1/public/forms/' . (int) $form['id'] . '/submit' ) ),
'token' => esc_url_raw( rest_url( 'vuloform/v1/public/forms/' . (int) $form['id'] . '/token' ) ),
'progress' => $settings['progress'],
'dynamic' => $dynamic,
'messages' => array_merge(
$settings['messages'],
array(
'email' => __( 'Enter a valid email address.', 'vuloform' ),
'url' => __( 'Enter a valid web address, starting with https://.', 'vuloform' ),
'number' => __( 'Enter a number.', 'vuloform' ),
'file' => __( 'That file is too large or not an allowed type.', 'vuloform' ),
'network' => __( 'Something went wrong. Please check your connection and try again.', 'vuloform' ),
'sending' => __( 'Sending…', 'vuloform' ),
'preview' => __( 'This is a preview. Nothing was sent.', 'vuloform' ),
/* translators: 1: current step, 2: number of steps. */
'step' => __( 'Step %1$s of %2$s', 'vuloform' ),
)
),
'fields' => $fields,
);
}
/**
* @param int $count Number of pages.
* @param string $type steps or bar.
* @return string
*/
private static function progress( $count, $type ) {
/* translators: 1: current step, 2: number of steps. */
$label = sprintf( __( 'Step %1$s of %2$s', 'vuloform' ), 1, $count );
$html = sprintf( '