$val ) {
if ( is_array( $val ) ) {
$val = wp_json_encode( $val );
}
$data_attr .= ' data-' . $key . '=\'' . $val . '\'';
}
}
// Check for readonly inputs.
if ( ! empty( $args['readonly' ] ) ) {
$data_attr .= 'readonly';
}
// Determine what field type to output.
switch ( $option ) {
// Text input.
case 'text':
$output = sprintf(
'',
! empty( $args['type'] ) ? esc_attr( $args['type'] ) : 'text',
$input_id,
$field_name,
esc_attr( $value ),
$placeholder,
$input_class,
$data_attr
);
break;
// Textarea.
case 'textarea':
$output = sprintf(
'',
$input_id,
$field_name,
! empty( $args['rows'] ) ? (int) $args['rows'] : '3',
$placeholder,
$input_class,
$data_attr,
esc_textarea( $value )
);
break;
// TinyMCE.
case 'tinymce':
$id = str_replace( '-', '_', $input_id );
$args['tinymce']['textarea_name'] = $field_name;
$args['tinymce']['teeny'] = true;
$args['tinymce'] = wp_parse_args( $args['tinymce'], array(
'media_buttons' => false,
'teeny' => true,
) );
ob_start();
wp_editor( $value, $id, $args['tinymce'] );
$output = ob_get_clean();
break;
// Checkbox.
case 'checkbox':
$output = sprintf(
'',
$input_id,
$field_name,
$input_class,
checked( '1', $value, false ),
$data_attr
);
$output .= sprintf(
'';
break;
// Radio.
case 'radio':
$options = $args['options'];
$radio_counter = 1;
$output = '';
foreach ( $options as $key => $item ) {
if ( empty( $item['label'] ) ) {
continue;
}
$item_value = ! empty( $item['value'] ) ? $item['value'] : $key;
$output .= '';
if ( ! empty( $item['pre_label'] ) ) {
$output .= '';
$radio_counter ++;
}
if ( ! empty( $output ) ) {
$output = '
' . $output . '
';
}
break;
// Select.
case 'select':
if ( empty( $args['options'] ) && empty( $args['field_map'] ) ) {
return '';
}
if ( ! empty( $args['field_map'] ) ) {
$options = array();
$available_fields = wpforms_get_form_fields( $form_data, $args['field_map'] );
if ( ! empty( $available_fields ) ) {
foreach ( $available_fields as $id => $available_field ) {
$options[ $id ] = ! empty( $available_field['label'] )
? esc_attr( $available_field['label'] )
: sprintf( /* translators: %d - field ID. */
esc_html__( 'Field #%d', 'wpforms-lite' ),
absint( $id )
);
}
}
$input_class .= ' wpforms-field-map-select';
$data_attr .= ' data-field-map-allowed="' . implode( ' ', $args['field_map'] ) . '"';
if ( ! empty( $placeholder ) ) {
$data_attr .= ' data-field-map-placeholder="' . esc_attr( $placeholder ) . '"';
}
} else {
$options = $args['options'];
}
$output = sprintf(
'';
break;
}
// Put the pieces together.
$field_open = sprintf(
'';
$output = $field_open . $field_label . $output . $field_close;
// Wash our hands.
if ( $echo ) {
echo $output;
} else {
return $output;
}
}
/**
* Get settings block state, whether it's opened or closed.
*
* @since 1.4.8
*
* @param int $form_id
* @param int $block_id
* @param string $block_type
*
* @return string
*/
function wpforms_builder_settings_block_get_state( $form_id, $block_id, $block_type ) {
$form_id = absint( $form_id );
$block_id = absint( $block_id );
$block_type = sanitize_key( $block_type );
$state = 'opened';
$all_states = get_user_meta( get_current_user_id(), 'wpforms_builder_settings_collapsable_block_states', true );
if ( empty( $all_states ) ) {
return $state;
}
if (
is_array( $all_states ) &&
! empty( $all_states[ $form_id ][ $block_type ][ $block_id ] ) &&
'closed' === $all_states[ $form_id ][ $block_type ][ $block_id ]
) {
$state = 'closed';
}
// Backward compatibility for notifications.
if ( 'notification' === $block_type && 'closed' !== $state ) {
$notification_states = get_user_meta( get_current_user_id(), 'wpforms_builder_notification_states', true );
}
if (
! empty( $notification_states[ $form_id ][ $block_id ] ) &&
'closed' === $notification_states[ $form_id ][ $block_id ]
) {
$state = 'closed';
}
if ( 'notification' === $block_type ) {
// Backward compatibility for notifications.
return apply_filters( 'wpforms_builder_notification_get_state', $state, $form_id, $block_id );
}
return apply_filters( 'wpforms_builder_settings_block_get_state', $state, $form_id, $block_id, $block_type );
}
/**
* Get the list of allowed tags, used in pair with wp_kses() function.
* This allows getting rid of all potentially harmful HTML tags and attributes.
*
* @since 1.5.9
*
* @return array Allowed Tags.
*/
function wpforms_builder_preview_get_allowed_tags() {
static $allowed_tags;
if ( ! empty( $allowed_tags ) ) {
return $allowed_tags;
}
$atts = [ 'align', 'class', 'type', 'id', 'for', 'style', 'src', 'rel', 'href', 'target', 'value', 'width', 'height' ];
$tags = [ 'label', 'iframe', 'style', 'button', 'strong', 'small', 'table', 'span', 'abbr', 'code', 'pre', 'div', 'img', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ol', 'ul', 'li', 'em', 'hr', 'br', 'th', 'tr', 'td', 'p', 'a', 'b', 'i' ];
$allowed_atts = array_fill_keys( $atts, [] );
$allowed_tags = array_fill_keys( $tags, $allowed_atts );
return $allowed_tags;
}
/**
* Output builder panel fields group wrapper.
*
* @since 1.6.6
*
* @param string $inner Inner HTML to wrap.
* @param array $args Array of arguments.
* @param bool $echo Flag to display.
*
* @return string
*/
function wpforms_panel_fields_group( $inner, $args = [], $echo = true ) {
$output = '';
if ( ! $echo ) {
return $output;
}
echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}