/home/techb158/balacoffee.com/wp-content/plugins/wpforms/pro/includes/fields
Edit: /home/techb158/balacoffee.com/wp-content/plugins/wpforms/pro/includes/fields/class-rating.php (15767B)
'#e27730',
'modern' => '#066aab',
];
/**
* Primary class constructor.
*
* @since 1.4.4
*/
public function init() {
// Define field type information.
$this->name = esc_html__( 'Rating', 'wpforms' );
$this->type = 'rating';
$this->icon = 'fa-star';
$this->order = 200;
$this->group = 'fancy';
// Define additional field properties.
add_filter( 'wpforms_field_properties_rating', [ $this, 'field_properties' ], 5, 3 );
// Customize value format for HTML emails.
add_filter( 'wpforms_html_field_value', [ $this, 'html_email_value' ], 10, 4 );
}
/**
* Define additional field properties.
*
* @since 1.4.4
*
* @param array $properties Field properties.
* @param array $field Field settings.
* @param array $form_data Form data and settings.
*
* @return array
*/
public function field_properties( $properties, $field, $form_data ) {
// Primary input: set screen reader text class.
$properties['inputs']['primary']['class'][] = 'wpforms-screen-reader-element';
// Rating scale.
$properties['inputs']['primary']['rating']['scale'] = ! empty( $field['scale'] ) ? esc_attr( $field['scale'] ) : 5;
// Rating icon color.
$properties['inputs']['primary']['rating']['color'] = ! empty( $field['icon_color'] ) ? esc_attr( $field['icon_color'] ) : self::DEFAULT_ICON_COLOR;
// Rating icons size.
$icon_size = ! empty( $field['icon_size'] ) ? $field['icon_size'] : 'medium'; // Default size.
$properties['inputs']['primary']['rating']['size'] = $this->get_icon_size_css( $icon_size );
// Rating icon SVG image.
$properties['inputs']['primary']['rating']['svg'] = '
';
if ( ! empty( $field['icon'] ) && 'heart' === $field['icon'] ) {
$properties['inputs']['primary']['rating']['svg'] = '
';
} elseif ( ! empty( $field['icon'] ) && 'thumb' === $field['icon'] ) {
$properties['inputs']['primary']['rating']['svg'] = '
';
} elseif ( ! empty( $field['icon'] ) && 'smiley' === $field['icon'] ) {
$properties['inputs']['primary']['rating']['svg'] = '
';
}
return $properties;
}
/**
* Customize format for HTML email notifications and entry details.
*
* @since 1.4.4
*
* @param string $val The value.
* @param array $field Field.
* @param array $form_data Form data settings.
* @param string $context Context usage.
*
* @return string
*/
public function html_email_value( $val, $field, $form_data = [], $context = '' ) {
if ( ! empty( $field['value'] ) && 'rating' === $field['type'] && apply_filters( 'wpforms_rating_field_emoji', true ) ) {
// Determine emoji to use.
switch ( $field['icon'] ) {
case 'star':
$emoji = '⭐';
break;
case 'heart':
$emoji = '❤️';
break;
case 'thumb':
$emoji = '👍';
break;
default:
$emoji = '🙂';
break;
}
if ( 'entry-table' === $context ) {
// For the entry list table, lighten the scale display.
return sprintf(
'%s
(%d/%d)',
str_repeat( $emoji, absint( $field['value'] ) ),
absint( $field['value'] ),
absint( $field['scale'] )
);
}
return sprintf(
'%s (%d/%d)',
str_repeat( $emoji, absint( $field['value'] ) ),
absint( $field['value'] ),
absint( $field['scale'] )
);
}
return $val;
}
/**
* Field options panel inside the builder.
*
* @since 1.4.4
*
* @param array $field Field settings.
*/
public function field_options( $field ) {
/*
* Basic field options.
*/
// Options open markup.
$this->field_option(
'basic-options',
$field,
[
'markup' => 'open',
]
);
// Label.
$this->field_option( 'label', $field );
// Description.
$this->field_option( 'description', $field );
// Scale.
$lbl = $this->field_element(
'label',
$field,
[
'slug' => 'scale',
'value' => esc_html__( 'Scale', 'wpforms' ),
'tooltip' => esc_html__( 'Select rating scale', 'wpforms' ),
],
false
);
$fld = $this->field_element(
'select',
$field,
[
'slug' => 'scale',
'value' => ! empty( $field['scale'] ) ? esc_attr( $field['scale'] ) : '5',
'options' => [
'2' => '2',
'3' => '3',
'4' => '4',
'5' => '5',
'6' => '6',
'7' => '7',
'8' => '8',
'9' => '9',
'10' => '10',
],
],
false
);
$this->field_element(
'row',
$field,
[
'slug' => 'scale',
'content' => $lbl . $fld,
]
);
// Required toggle.
$this->field_option( 'required', $field );
// Options close markup.
$this->field_option(
'basic-options',
$field,
[
'markup' => 'close',
]
);
/*
* Advanced field options.
*/
// Options open markup.
$this->field_option(
'advanced-options',
$field,
[
'markup' => 'open',
]
);
// Icon.
$lbl = $this->field_element(
'label',
$field,
[
'slug' => 'icon',
'value' => esc_html__( 'Icon', 'wpforms' ),
'tooltip' => esc_html__( 'Select icon to display', 'wpforms' ),
],
false
);
$fld = $this->field_element(
'select',
$field,
[
'slug' => 'icon',
'value' => ! empty( $field['icon'] ) ? esc_attr( $field['icon'] ) : 'star',
'options' => [
'star' => esc_html__( 'Star', 'wpforms' ),
'heart' => esc_html__( 'Heart', 'wpforms' ),
'thumb' => esc_html__( 'Thumb', 'wpforms' ),
'smiley' => esc_html__( 'Smiley Face', 'wpforms' ),
],
],
false
);
$this->field_element(
'row',
$field,
[
'slug' => 'icon',
'content' => $lbl . $fld,
]
);
// Icon size.
$lbl = $this->field_element(
'label',
$field,
[
'slug' => 'icon_size',
'value' => esc_html__( 'Icon Size', 'wpforms' ),
'tooltip' => esc_html__( 'Select the size of the rating icon', 'wpforms' ),
],
false
);
$fld = $this->field_element(
'select',
$field,
[
'slug' => 'icon_size',
'value' => ! empty( $field['icon_size'] ) ? esc_attr( $field['icon_size'] ) : 'medium',
'options' => [
'small' => esc_html__( 'Small', 'wpforms' ),
'medium' => esc_html__( 'Medium', 'wpforms' ),
'large' => esc_html__( 'Large', 'wpforms' ),
],
],
false
);
$this->field_element(
'row',
$field,
[
'slug' => 'icon_size',
'content' => $lbl . $fld,
]
);
// Icon color picker.
$lbl = $this->field_element(
'label',
$field,
[
'slug' => 'icon_color',
'value' => esc_html__( 'Icon Color', 'wpforms' ),
'tooltip' => esc_html__( 'Select the color for the rating icon', 'wpforms' ),
],
false
);
$icon_color = isset( $field['icon_color'] ) ? wpforms_sanitize_hex_color( $field['icon_color'] ) : '';
$icon_color = empty( $icon_color ) ? $this->get_default_icon_color() : $icon_color;
$fld = $this->field_element(
'color',
$field,
[
'slug' => 'icon_color',
'value' => $icon_color,
'data' => [
'fallback-color' => $icon_color,
],
],
false
);
$this->field_element(
'row',
$field,
[
'slug' => 'icon_color',
'content' => $lbl . $fld,
'class' => 'color-picker-row',
]
);
// Custom CSS classes.
$this->field_option( 'css', $field );
// Hide label.
$this->field_option( 'label_hide', $field );
// Options close markup.
$this->field_option(
'advanced-options',
$field,
[
'markup' => 'close',
]
);
}
/**
* Field preview inside the builder.
*
* @since 1.4.4
*
* @param array $field Field settings.
*/
public function field_preview( $field ) {
// Define data.
$scale = ! empty( $field['scale'] ) ? esc_attr( $field['scale'] ) : 5;
$icon = ! empty( $field['icon'] ) ? esc_attr( $field['icon'] ) : 'star';
$icon_size = ! empty( $field['icon_size'] ) ? esc_attr( $field['icon_size'] ) : 'medium';
$icon_color = ! empty( $field['icon_color'] ) ? esc_attr( $field['icon_color'] ) : $this->get_default_icon_color();
$icon_class = '';
$icon_size_css = '';
// Set icon class.
switch ( $icon ) {
case 'star':
$icon_class = 'fa-star';
break;
case 'heart':
$icon_class = 'fa-heart';
break;
case 'thumb':
$icon_class = 'fa-thumbs-up';
break;
case 'smiley':
$icon_class = 'fa-smile-o';
break;
}
// Set icon size.
$icon_size_css = $this->get_icon_size_css( $icon_size );
// Label.
$this->field_preview_option( 'label', $field );
// Primary input.
for ( $i = 1; $i <= 10; $i++ ) {
printf(
'
',
$icon_class,
$icon_size,
$icon_color,
$i <= $scale ? 'inline-block' : 'none',
$icon_size_css
);
}
// Description.
$this->field_preview_option( 'description', $field );
}
/**
* Get icon size CSS value in pixels.
*
* @since 1.8.1
*
* @param string $icon_size Icon size value.
*/
private function get_icon_size_css( $icon_size ) {
$render_engine = wpforms_get_render_engine();
$icon_sizes = [
'classic' => [
'small' => '18',
'medium' => '28',
'large' => '38',
],
'modern' => [
'small' => '16',
'medium' => '24',
'large' => '38',
],
];
$default = $render_engine === 'modern' ? '24' : '28';
return ! empty( $icon_sizes[ $render_engine ][ $icon_size ] )
? $icon_sizes[ $render_engine ][ $icon_size ]
: $default;
}
/**
* Field display on the form front-end.
*
* @since 1.4.4
*
* @param array $field Field settings.
* @param array $deprecated Deprecated, don't use.
* @param array $form_data Form data and settings.
*/
public function field_display( $field, $deprecated, $form_data ) {
// Define data.
$primary = $field['properties']['inputs']['primary'];
$rating = $primary['rating'];
$svg = $rating['svg'];
$scale = ! empty( $rating['scale'] ) ? absint( $rating['scale'] ) : 5;
// Apply our customizations to the SVG.
$svg = str_replace( 'width=""', 'width="' . absint( $rating['size'] ) . '"', $svg );
$svg = str_replace( 'height=""', 'height="' . absint( $rating['size'] ) . '"', $svg );
$svg = str_replace( 'style=""', 'style="height:' . absint( $rating['size'] ) . 'px;width:' . absint( $rating['size'] ) . 'px;"', $svg );
$svg = str_replace( 'fill=""', 'fill="currentColor" color="' . wpforms_sanitize_hex_color( $rating['color'] ) . '"', $svg );
echo '
';
// Generate each rating icon/element.
for ( $i = 1; $i <= $scale; $i++ ) {
printf(
'';
}
echo '
';
}
/**
* @inheritdoc
*/
protected function get_field_populated_single_property_value( $raw_value, $input, $properties, $field ) {
if ( ! is_string( $raw_value ) ) {
return $properties;
}
$properties['inputs'][ $input ]['rating']['default'] = (int) $raw_value;
return $properties;
}
/**
* Format field.
*
* @since 1.4.4
*
* @param int $field_id Field ID.
* @param array $field_submit Submitted field value.
* @param array $form_data Form data and settings.
*/
public function format( $field_id, $field_submit, $form_data ) {
// Define data.
$name = ! empty( $form_data['fields'][ $field_id ]['label'] ) ? $form_data['fields'][ $field_id ]['label'] : '';
$value = ! empty( $field_submit ) ? absint( $field_submit ) : '';
$scale = absint( $form_data['fields'][ $field_id ]['scale'] );
if ( $value > $scale ) {
$value = '';
}
// Set final field details.
wpforms()->process->fields[ $field_id ] = [
'name' => sanitize_text_field( $name ),
'value' => sanitize_text_field( $value ),
'id' => absint( $field_id ),
'type' => $this->type,
'scale' => sanitize_text_field( $scale ),
'icon' => sanitize_text_field( $form_data['fields'][ $field_id ]['icon'] ),
];
}
/**
* Get default icon color.
*
* @since 1.8.1
*
* @return string
*/
public function get_default_icon_color() {
$render_engine = wpforms_get_render_engine();
return array_key_exists( $render_engine, self::DEFAULT_ICON_COLOR ) ? self::DEFAULT_ICON_COLOR[ $render_engine ] : self::DEFAULT_ICON_COLOR['modern'];
}
}
new WPForms_Rating_Text();