namespace WPForms\Forms\Fields\Addons\LikertScale;
use WPForms\Forms\Fields\Traits\ProField as ProFieldTrait;
class Field extends WPForms_Field {
* Primary class constructor.
// Define field type information.
$this->name = esc_html__( 'Likert Scale', 'wpforms-lite' );
$this->keywords = esc_html__( 'survey, rating scale', 'wpforms-lite' );
$this->type = 'likert_scale';
$this->icon = 'fa-ellipsis-h';
$this->addon_slug = 'surveys-polls';
$this->default_settings = [
1 => esc_html__( 'Item #1', 'wpforms-lite' ),
2 => esc_html__( 'Item #2', 'wpforms-lite' ),
3 => esc_html__( 'Item #3', 'wpforms-lite' ),
1 => esc_html__( 'Strongly Disagree', 'wpforms-lite' ),
2 => esc_html__( 'Disagree', 'wpforms-lite' ),
3 => esc_html__( 'Neutral', 'wpforms-lite' ),
4 => esc_html__( 'Agree', 'wpforms-lite' ),
5 => esc_html__( 'Strongly Agree', 'wpforms-lite' ),
protected function hooks() {}
* Field options panel inside the builder.
* @param array $field Field settings.
public function field_options( $field ) {
'after_title' => $this->get_field_options_notice(),
$this->field_option( 'label', $field );
$values = ! empty( $field['rows'] ) ? $field['rows'] : $this->default_settings['rows'];
$lbl = $this->field_element(
'value' => esc_html__( 'Rows', 'wpforms-lite' ),
'tooltip' => esc_html__( 'Add rows to the likert scale.', 'wpforms-lite' ),
'<ul id="wpforms-field-option-%1$d-rows-list" data-next-id="%2$s" class="choices-list wpforms-undo-redo-container %3$s" data-field-id="%1$d" data-field-type="%4$s" data-choice-type="%5$s">',
esc_attr( $field['id'] ),
max( array_keys( $values ) ) + 1,
! empty( $field['single_row'] ) ? 'wpforms-hidden' : '',
foreach ( $values as $key => $value ) {
$fld .= sprintf( '<li data-key="%d">', $key );
$fld .= '<span class="move"><i class="fa fa-grip-lines" aria-hidden="true"></i></span>';
$fld .= sprintf( '<input type="text" name="fields[%s][rows][%s]" value="%s" class="label">', esc_attr( $field['id'] ), $key, esc_attr( $value ) );
$fld .= '<a class="add" href="#" title="' . esc_attr__( 'Add likert scale row', 'wpforms-lite' ) . '"><i class="fa fa-plus-circle"></i></a>';
$fld .= '<a class="remove" href="# title="' . esc_attr__( 'Remove likert scale row', 'wpforms-lite' ) . '"><i class="fa fa-minus-circle"></i></a>';
'content' => $lbl . $fld,
'content' => $this->field_element(
'value' => isset( $field['single_row'] ) ? '1' : '0',
'desc' => esc_html__( 'Make this a single-row rating scale', 'wpforms-lite' ),
'tooltip' => esc_html__( 'Check this option to make this a single-row rating scale and remove the row choices.', 'wpforms-lite' ),
// Multiple row responses.
'slug' => 'multiple_responses',
'content' => $this->field_element(
'slug' => 'multiple_responses',
'value' => isset( $field['multiple_responses'] ) ? '1' : '0',
'desc' => esc_html__( 'Allow multiple responses per row', 'wpforms-lite' ),
'tooltip' => esc_html__( 'Check this option to allow multiple responses per row (uses checkboxes).', 'wpforms-lite' ),
$values = ! empty( $field['columns'] ) ? $field['columns'] : $this->default_settings['columns'];
$lbl = $this->field_element(
'value' => esc_html__( 'Columns', 'wpforms-lite' ),
'tooltip' => esc_html__( 'Add columns to the likert scale.', 'wpforms-lite' ),
'<ul id="wpforms-field-option-%1$d-columns-list" data-next-id="%2$s" class="choices-list wpforms-undo-redo-container" data-field-id="%1$d" data-field-type="%3$s" data-choice-type="%4$s">',
esc_attr( $field['id'] ),
max( array_keys( $values ) ) + 1,
foreach ( $values as $key => $value ) {
$fld .= sprintf( '<li data-key="%d">', $key );
$fld .= '<span class="move"><i class="fa fa-grip-lines" aria-hidden="true"></i></span>';
$fld .= sprintf( '<input type="text" name="fields[%s][columns][%s]" value="%s">', $field['id'], $key, esc_attr( $value ) );
$fld .= '<a class="add" href="#" title="' . esc_attr__( 'Add likert scale column', 'wpforms-lite' ) . '"><i class="fa fa-plus-circle"></i></a>';
$fld .= '<a class="remove" href="# title="' . esc_attr__( 'Remove likert scale column', 'wpforms-lite' ) . '"><i class="fa fa-minus-circle"></i></a>';
'content' => $lbl . $fld,
$this->field_option( 'description', $field );
$this->field_option( 'required', $field );
* Advanced field options.
$lbl = $this->field_element(
'value' => esc_html__( 'Style', 'wpforms-lite' ),
'tooltip' => esc_html__( 'Select the style for the likert scale.', 'wpforms-lite' ),
$fld = $this->field_element(
'value' => ! empty( $field['style'] ) ? esc_attr( $field['style'] ) : 'modern',
'modern' => esc_html__( 'Modern', 'wpforms-lite' ),
'classic' => esc_html__( 'Classic', 'wpforms-lite' ),
'content' => $lbl . $fld,
$this->field_option( 'size', $field );
$this->field_option( 'css', $field );
$this->field_option( 'label_hide', $field );
* Field preview inside the builder.
* @param array $field Field settings.
public function field_preview( $field ) { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh
$rows = ! empty( $field['rows'] ) ? $field['rows'] : $this->default_settings['rows'];
$columns = ! empty( $field['columns'] ) ? $field['columns'] : $this->default_settings['columns'];
$input_type = ! empty( $field['multiple_responses'] ) ? 'checkbox' : 'radio';
$style = ! empty( $field['style'] ) ? sanitize_html_class( $field['style'] ) : 'modern';
$single = ! empty( $field['single_row'] );
$width = $single ? round( 100 / count( $columns ), 4 ) : round( 80 / count( $columns ), 4 );
$this->field_preview_option(
'label_badge' => $this->get_field_preview_badge(),
<table class="<?php echo esc_attr( $style ); ?><?php echo $single ? ' single-row' : ''; ?>">
echo '<th style="width:20%;"></th>';
foreach ( $columns as $column ) {
'<th style="width:%d%%;">%s</th>',
esc_html( sanitize_text_field( $column ) )
foreach ( $rows as $row ) {
echo '<th>' . esc_html( sanitize_text_field( $row ) ) . '</th>';
* Column is needed for foreach syntax.
* @noinspection PhpUnusedLocalVariableInspection
foreach ( $columns as $column ) {
echo '<input type="' . esc_attr( $input_type ) . '" readonly>';
$this->field_preview_option( 'description', $field );
// Hide remaining elements.
$this->field_preview_option( 'hide-remaining', $field );
* Field display on the form front-end.
* @param array $field Field settings.
* @param array $deprecated Deprecated array.
* @param array $form_data Form data and settings.
public function field_display( $field, $deprecated, $form_data ) {