__( 'Option 1', 'fieldwren' ), 'value' => 'option-1' ),
array( 'label' => __( 'Option 2', 'fieldwren' ), 'value' => 'option-2' ),
array( 'label' => __( 'Option 3', 'fieldwren' ), 'value' => 'option-3' ),
);
}
/** @return bool Whether the dynamic source control is offered. */
protected function supports_dynamic() {
return true;
}
protected function register_type_controls() {
if ( $this->supports_dynamic() ) {
$this->add_control(
'source',
array(
'label' => __( 'Options from', 'fieldwren' ),
'type' => Controls::SELECT,
'default' => 'manual',
'options' => array(
'manual' => __( 'Manual list', 'fieldwren' ),
'posts' => __( 'Posts', 'fieldwren' ),
'terms' => __( 'Taxonomy terms', 'fieldwren' ),
'users' => __( 'Users', 'fieldwren' ),
),
)
);
$types = array();
foreach ( get_post_types( array( 'public' => true ), 'objects' ) as $pt ) {
$types[ $pt->name ] = $pt->labels->singular_name;
}
$this->add_control( 'source_post_type', array( 'label' => __( 'Post type', 'fieldwren' ), 'type' => Controls::SELECT, 'default' => 'post', 'options' => $types, 'condition' => array( 'source' => 'posts' ) ) );
$tax = array();
foreach ( get_taxonomies( array( 'public' => true ), 'objects' ) as $t ) {
$tax[ $t->name ] = $t->labels->singular_name;
}
$this->add_control( 'source_taxonomy', array( 'label' => __( 'Taxonomy', 'fieldwren' ), 'type' => Controls::SELECT, 'default' => 'category', 'options' => $tax, 'condition' => array( 'source' => 'terms' ) ) );
$roles = array( '' => __( 'Any role', 'fieldwren' ) );
foreach ( wp_roles()->get_names() as $k => $n ) {
$roles[ $k ] = translate_user_role( $n );
}
$this->add_control( 'source_role', array( 'label' => __( 'Role', 'fieldwren' ), 'type' => Controls::SELECT, 'default' => '', 'options' => $roles, 'condition' => array( 'source' => 'users' ) ) );
$this->add_control(
'source_value',
array(
'label' => __( 'Option value', 'fieldwren' ),
'type' => Controls::SELECT,
'default' => 'id',
'options' => array(
'id' => 'ID',
'title' => __( 'Title / name', 'fieldwren' ),
'slug' => __( 'Slug / login', 'fieldwren' ),
),
'condition' => array( 'source!' => 'manual' ),
)
);
$this->add_control( 'source_limit', array( 'label' => __( 'Limit', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => 100, 'inline' => true, 'condition' => array( 'source!' => 'manual' ) ) );
}
$this->add_control(
'options',
array(
'label' => __( 'Options', 'fieldwren' ),
'type' => self::OPTIONS,
'default' => $this->default_options(),
'images' => $this->supports_images(),
'calc' => true,
'condition' => $this->supports_dynamic() ? array( 'source' => 'manual' ) : array(),
)
);
$this->add_control( 'randomize', array( 'label' => __( 'Shuffle options', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => '' ) );
$this->register_choice_controls();
}
/** @return bool */
protected function supports_images() {
return false;
}
/** Extra choice controls. */
protected function register_choice_controls() {}
/**
* Options as printed and validated: manual list or dynamic source.
*
* @param array $s Settings.
* @return array
*/
public function get_options( array $s ) {
$src = $s['source'] ?? 'manual';
if ( ! $this->supports_dynamic() || 'manual' === $src ) {
return self::normalize_options( $s['options'] ?? array() );
}
$limit = max( 1, min( 1000, (int) ( $s['source_limit'] ?? 100 ) ) );
$vk = $s['source_value'] ?? 'id';
$out = array();
if ( 'posts' === $src ) {
foreach ( get_posts( array( 'post_type' => $s['source_post_type'] ?? 'post', 'numberposts' => $limit, 'post_status' => 'publish', 'orderby' => 'title', 'order' => 'ASC' ) ) as $p ) {
$out[] = array( 'label' => get_the_title( $p ), 'value' => 'id' === $vk ? (string) $p->ID : ( 'slug' === $vk ? $p->post_name : get_the_title( $p ) ) );
}
} elseif ( 'terms' === $src ) {
$terms = get_terms( array( 'taxonomy' => $s['source_taxonomy'] ?? 'category', 'hide_empty' => false, 'number' => $limit ) );
foreach ( is_array( $terms ) ? $terms : array() as $t ) {
$out[] = array( 'label' => $t->name, 'value' => 'id' === $vk ? (string) $t->term_id : ( 'slug' === $vk ? $t->slug : $t->name ) );
}
} elseif ( 'users' === $src ) {
$args = array( 'number' => $limit, 'orderby' => 'display_name' );
if ( ! empty( $s['source_role'] ) ) {
$args['role'] = $s['source_role'];
}
foreach ( get_users( $args ) as $u ) {
$out[] = array( 'label' => $u->display_name, 'value' => 'id' === $vk ? (string) $u->ID : ( 'slug' === $vk ? $u->user_login : $u->display_name ) );
}
}
return self::normalize_options( $out );
}
/** @return bool Whether the field holds several values. */
protected function is_multiple( array $s ) {
return false;
}
protected function schema_extra( array $s ) {
return array(
'options' => $this->get_options( $s ),
'multiple' => $this->is_multiple( $s ),
'other' => 'yes' === ( $s['other'] ?? '' ),
'min_sel' => isset( $s['min_select'] ) && '' !== $s['min_select'] ? (int) $s['min_select'] : null,
'max_sel' => isset( $s['max_select'] ) && '' !== $s['max_select'] ? (int) $s['max_select'] : null,
);
}
public function sanitize( $raw, array $f ) {
if ( ! empty( $f['multiple'] ) ) {
$raw = is_array( $raw ) ? $raw : ( '' === (string) $raw ? array() : array( $raw ) );
return array_values( array_map( 'sanitize_text_field', array_map( 'strval', array_filter( $raw, 'is_scalar' ) ) ) );
}
return sanitize_text_field( is_array( $raw ) ? (string) reset( $raw ) : (string) $raw );
}
public function validate( $value, array $f ) {
$allowed = wp_list_pluck( $f['options'], 'value' );
$values = is_array( $value ) ? $value : ( '' === (string) $value ? array() : array( $value ) );
foreach ( $values as $v ) {
if ( ! in_array( (string) $v, $allowed, true ) ) {
return array( __( 'Please choose one of the listed options.', 'fieldwren' ) );
}
}
if ( null !== ( $f['min_sel'] ?? null ) && $values && count( $values ) < $f['min_sel'] ) {
/* translators: %d choices */
return array( sprintf( __( 'Please choose at least %d options.', 'fieldwren' ), $f['min_sel'] ) );
}
if ( ! empty( $f['max_sel'] ) && count( $values ) > $f['max_sel'] ) {
/* translators: %d choices */
return array( sprintf( __( 'Please choose no more than %d options.', 'fieldwren' ), $f['max_sel'] ) );
}
return array();
}
public function format( $value, array $f, $mode = 'html' ) {
$map = array();
foreach ( $f['options'] ?? array() as $o ) {
$map[ $o['value'] ] = $o['label'];
}
$values = is_array( $value ) ? $value : ( '' === (string) $value ? array() : array( $value ) );
$labels = array();
foreach ( $values as $v ) {
$labels[] = $map[ $v ] ?? $v;
}
$out = implode( ', ', $labels );
return 'html' === $mode ? esc_html( $out ) : $out;
}
public function numeric( $value, array $f ) {
$calc = array();
foreach ( $f['options'] ?? array() as $o ) {
$calc[ $o['value'] ] = '' !== $o['calc'] ? (float) $o['calc'] : ( is_numeric( $o['value'] ) ? (float) $o['value'] : 0 );
}
$sum = 0;
foreach ( is_array( $value ) ? $value : array( $value ) as $v ) {
$sum += $calc[ $v ] ?? ( is_numeric( $v ) ? (float) $v : 0 );
}
return $sum;
}
/** Options in display order (shuffled when asked, never in the editor). */
protected function display_options( array $s ) {
$opts = $this->get_options( $s );
if ( 'yes' === ( $s['randomize'] ?? '' ) && ! Renderer::is_editor() ) {
shuffle( $opts );
}
return apply_filters( 'fwren/field_options', $opts, $s, $this );
}
/** @return array Default selection: options flagged selected, or the default value. */
protected function selected_values( array $s ) {
$sel = array();
foreach ( $this->get_options( $s ) as $o ) {
if ( $o['selected'] ) {
$sel[] = $o['value'];
}
}
$d = $this->default_for_display( $s );
if ( '' !== $d ) {
$sel = array_map( 'trim', explode( ',', $d ) );
}
return $sel;
}
/** Option data attributes for calculations / inventory. */
protected static function opt_attrs( array $o ) {
$a = '';
if ( '' !== $o['calc'] ) {
$a .= ' data-calc="' . esc_attr( $o['calc'] ) . '"';
}
if ( null !== $o['stock'] ) {
$a .= ' data-stock="' . esc_attr( (string) $o['stock'] ) . '"';
}
return $a;
}
}
/** Dropdown. */
class Select_Field extends Choice_Field {
public function get_name() {
return 'select';
}
public function get_title() {
return __( 'Dropdown', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'arrow-down-alt2' );
}
public function get_keywords() {
return array( 'select', 'dropdown', 'multi select' );
}
protected function has_placeholder() {
return true;
}
protected function register_choice_controls() {
$this->add_control( 'multiple', array( 'label' => __( 'Allow several choices', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => '' ) );
$this->add_control( 'searchable', array( 'label' => __( 'Searchable', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => '' ) );
$this->add_control( 'max_select', array( 'label' => __( 'Maximum choices', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => '', 'inline' => true, 'condition' => array( 'multiple' => 'yes' ) ) );
}
protected function is_multiple( array $s ) {
return 'yes' === ( $s['multiple'] ?? '' );
}
protected function render_input( array $s, $key, $id ) {
$multi = $this->is_multiple( $s );
$sel = $this->selected_values( $s );
$extra = array(
'multiple' => $multi,
'data-fwren-search' => 'yes' === ( $s['searchable'] ?? '' ) ? '1' : false,
'data-max' => $multi && '' !== (string) ( $s['max_select'] ?? '' ) ? (int) $s['max_select'] : false,
'placeholder' => false,
);
echo '';
}
}
/** Radio buttons (single choice), also as button group or image choice. */
class Radio_Field extends Choice_Field {
public function get_name() {
return 'radio';
}
public function get_title() {
return __( 'Radio', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'marker' );
}
public function get_keywords() {
return array( 'radio', 'single choice', 'image choice', 'buttons' );
}
protected function input_type() {
return 'radio';
}
protected function supports_images() {
return true;
}
protected function register_choice_controls() {
$this->add_control(
'display',
array(
'label' => __( 'Display', 'fieldwren' ),
'type' => Controls::SELECT,
'default' => 'list',
'options' => array(
'list' => __( 'List', 'fieldwren' ),
'inline' => __( 'Inline', 'fieldwren' ),
'col-2' => __( 'Two columns', 'fieldwren' ),
'col-3' => __( 'Three columns', 'fieldwren' ),
'buttons' => __( 'Buttons', 'fieldwren' ),
'cards' => __( 'Cards', 'fieldwren' ),
'images' => __( 'Image choices', 'fieldwren' ),
),
)
);
$this->add_control( 'other', array( 'label' => __( 'Add "Other" with a text input', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => '' ) );
$this->add_control( 'other_label', array( 'label' => __( '"Other" label', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => __( 'Other', 'fieldwren' ), 'condition' => array( 'other' => 'yes' ) ) );
if ( 'checkbox' === $this->input_type() ) {
$this->add_control( 'min_select', array( 'label' => __( 'Minimum choices', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => '', 'inline' => true ) );
$this->add_control( 'max_select', array( 'label' => __( 'Maximum choices', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => '', 'inline' => true ) );
$this->add_control( 'select_all', array( 'label' => __( 'Add "Select all"', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => '' ) );
}
}
protected function is_multiple( array $s ) {
return 'checkbox' === $this->input_type();
}
public function validate( $value, array $f ) {
if ( ! empty( $f['other'] ) ) {
$allowed = wp_list_pluck( $f['options'], 'value' );
$values = is_array( $value ) ? $value : array( $value );
$f['options'] = array_merge( $f['options'], array_map( static function ( $v ) {
return array( 'value' => (string) $v, 'label' => (string) $v );
}, array_diff( $values, $allowed ) ) );
}
return parent::validate( $value, $f );
}
protected function render_label( array $s, $id, $tag = 'label' ) {
parent::render_label( $s, $id, 'div' );
}
protected function render_input( array $s, $key, $id ) {
$type = $this->input_type();
$display = (string) ( $s['display'] ?? 'list' );
$sel = $this->selected_values( $s );
$name = $key . ( 'checkbox' === $type ? '[]' : '' );
$req = 'yes' === ( $s['required'] ?? '' );
$data = '';
if ( 'checkbox' === $type ) {
$data .= '' !== (string) ( $s['max_select'] ?? '' ) ? ' data-max="' . (int) $s['max_select'] . '"' : '';
$data .= '' !== (string) ( $s['min_select'] ?? '' ) ? ' data-min="' . (int) $s['min_select'] . '"' : '';
}
echo '
';
}
}
/** Checkboxes (several choices). */
class Checkbox_Field extends Radio_Field {
public function get_name() {
return 'checkbox';
}
public function get_title() {
return __( 'Checkboxes', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'yes-alt' );
}
public function get_keywords() {
return array( 'checkbox', 'multiple choice', 'image choice' );
}
protected function input_type() {
return 'checkbox';
}
}
/** Country dropdown. */
class Country_Field extends Choice_Field {
public function get_name() {
return 'country';
}
public function get_title() {
return __( 'Country', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'admin-site-alt3' );
}
protected function has_placeholder() {
return true;
}
protected function supports_dynamic() {
return false;
}
protected function register_type_controls() {
$this->add_control( 'countries', array( 'label' => __( 'Only these countries', 'fieldwren' ), 'type' => Controls::SELECT2, 'default' => array(), 'options' => \FWREN\Countries::names(), 'description' => __( 'Empty = all countries.', 'fieldwren' ) ) );
$this->add_control( 'exclude', array( 'label' => __( 'Exclude countries', 'fieldwren' ), 'type' => Controls::SELECT2, 'default' => array(), 'options' => \FWREN\Countries::names() ) );
$this->add_control( 'priority', array( 'label' => __( 'Show first', 'fieldwren' ), 'type' => Controls::SELECT2, 'default' => array(), 'options' => \FWREN\Countries::names() ) );
$this->add_control( 'searchable', array( 'label' => __( 'Searchable', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => 'yes' ) );
}
public function get_options( array $s ) {
$names = \FWREN\Countries::names();
$only = array_filter( (array) ( $s['countries'] ?? array() ) );
$ex = array_filter( (array) ( $s['exclude'] ?? array() ) );
$pri = array_filter( (array) ( $s['priority'] ?? array() ) );
$out = array();
foreach ( $pri as $iso ) {
if ( isset( $names[ $iso ] ) ) {
$out[ $iso ] = array( 'label' => $names[ $iso ], 'value' => $iso );
}
}
foreach ( $names as $iso => $n ) {
if ( ( $only && ! in_array( $iso, $only, true ) ) || in_array( $iso, $ex, true ) || isset( $out[ $iso ] ) ) {
continue;
}
$out[ $iso ] = array( 'label' => $n, 'value' => $iso );
}
return self::normalize_options( array_values( $out ) );
}
protected function render_input( array $s, $key, $id ) {
$d = strtoupper( $this->default_for_display( $s ) );
echo '';
}
}
/** Star rating. */
class Rating_Field extends Field_Block {
public function get_name() {
return 'rating';
}
public function get_title() {
return __( 'Rating', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'star-filled' );
}
public function get_category() {
return 'fwren-choice';
}
protected function has_placeholder() {
return false;
}
protected function register_type_controls() {
$this->add_control( 'max', array( 'label' => __( 'Number of stars', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => 5, 'min' => 2, 'max' => 10, 'inline' => true ) );
$this->add_control( 'shape', array( 'label' => __( 'Icon', 'fieldwren' ), 'type' => Controls::SELECT, 'default' => 'star', 'options' => array( 'star' => '★', 'heart' => '♥', 'circle' => '●', 'thumb' => '👍' ) ) );
$this->add_control( 'hint_labels', array( 'label' => __( 'Hover labels (comma separated)', 'fieldwren' ), 'type' => Controls::TEXT, 'placeholder' => 'Poor, Fair, Good, Very good, Excellent' ) );
$this->add_control( 'active_color', array( 'label' => __( 'Active color', 'fieldwren' ), 'type' => Controls::COLOR, 'default' => '#f59e0b', 'selectors' => array( '{{WRAPPER}} .fwren-rating' => '--fwren-rating-on: {{VALUE}};' ) ) );
}
protected function schema_extra( array $s ) {
return array( 'max' => max( 2, (int) $s['max'] ) );
}
public function sanitize( $raw, array $f ) {
return '' === (string) $raw || is_array( $raw ) ? '' : (int) $raw;
}
public function validate( $value, array $f ) {
return '' !== $value && ( $value < 1 || $value > $f['max'] ) ? array( __( 'Please choose a rating.', 'fieldwren' ) ) : array();
}
public function format( $value, array $f, $mode = 'html' ) {
if ( '' === $value ) {
return '';
}
return 'html' === $mode ? '' . str_repeat( '★', (int) $value ) . str_repeat( '☆', max( 0, $f['max'] - (int) $value ) ) . ' (' . (int) $value . '/' . (int) $f['max'] . ')' : $value . '/' . $f['max'];
}
protected function render_label( array $s, $id, $tag = 'label' ) {
parent::render_label( $s, $id, 'div' );
}
protected function render_input( array $s, $key, $id ) {
$max = max( 2, (int) $s['max'] );
$icons = array( 'star' => '★', 'heart' => '♥', 'circle' => '●', 'thumb' => '👍' );
$icon = $icons[ $s['shape'] ] ?? '★';
$hints = array_map( 'trim', explode( ',', (string) $s['hint_labels'] ) );
$d = (int) $this->default_for_display( $s );
echo '';
for ( $i = $max; $i >= 1; $i-- ) {
$hint = $hints[ $i - 1 ] ?? (string) $i;
echo '';
echo '';
}
echo '
';
}
}
/** Net Promoter Score 0–10. */
class Nps_Field extends Field_Block {
public function get_name() {
return 'nps';
}
public function get_title() {
return __( 'Net Promoter Score', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'chart-bar' );
}
public function get_category() {
return 'fwren-choice';
}
protected function has_placeholder() {
return false;
}
protected function default_label() {
return __( 'How likely are you to recommend us to a friend or colleague?', 'fieldwren' );
}
protected function register_type_controls() {
$this->add_control( 'low_label', array( 'label' => __( 'Low label', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => __( 'Not likely', 'fieldwren' ) ) );
$this->add_control( 'high_label', array( 'label' => __( 'High label', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => __( 'Extremely likely', 'fieldwren' ) ) );
$this->add_control( 'start', array( 'label' => __( 'Start at', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => 0, 'inline' => true ) );
$this->add_control( 'end', array( 'label' => __( 'End at', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => 10, 'inline' => true ) );
}
protected function schema_extra( array $s ) {
return array( 'start' => (int) $s['start'], 'end' => (int) $s['end'] );
}
public function sanitize( $raw, array $f ) {
return '' === (string) $raw || is_array( $raw ) ? '' : (int) $raw;
}
public function validate( $value, array $f ) {
return '' !== $value && ( $value < $f['start'] || $value > $f['end'] ) ? array( __( 'Please choose a score.', 'fieldwren' ) ) : array();
}
protected function render_label( array $s, $id, $tag = 'label' ) {
parent::render_label( $s, $id, 'div' );
}
protected function render_input( array $s, $key, $id ) {
echo '';
}
}
/** Range slider. */
class Range_Field extends Field_Block {
public function get_name() {
return 'range';
}
public function get_title() {
return __( 'Range Slider', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'leftright' );
}
public function get_category() {
return 'fwren-choice';
}
protected function has_placeholder() {
return false;
}
protected function register_type_controls() {
$this->add_control( 'min', array( 'label' => __( 'Minimum', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => 0, 'inline' => true ) );
$this->add_control( 'max', array( 'label' => __( 'Maximum', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => 100, 'inline' => true ) );
$this->add_control( 'step', array( 'label' => __( 'Step', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => 1, 'inline' => true ) );
$this->add_control( 'prefix', array( 'label' => __( 'Prefix', 'fieldwren' ), 'type' => Controls::TEXT ) );
$this->add_control( 'suffix', array( 'label' => __( 'Suffix', 'fieldwren' ), 'type' => Controls::TEXT ) );
$this->add_control( 'accent', array( 'label' => __( 'Color', 'fieldwren' ), 'type' => Controls::COLOR, 'selectors' => array( '{{WRAPPER}} input[type=range]' => 'accent-color: {{VALUE}};' ) ) );
}
protected function schema_extra( array $s ) {
return array( 'min' => (float) $s['min'], 'max' => (float) $s['max'], 'prefix' => (string) $s['prefix'], 'suffix' => (string) $s['suffix'] );
}
public function sanitize( $raw, array $f ) {
return is_numeric( $raw ) ? $raw + 0 : '';
}
public function validate( $value, array $f ) {
return '' !== $value && ( $value < $f['min'] || $value > $f['max'] ) ? array( __( 'Please choose a value in range.', 'fieldwren' ) ) : array();
}
public function format( $value, array $f, $mode = 'html' ) {
$o = '' === $value ? '' : $f['prefix'] . $value . $f['suffix'];
return 'html' === $mode ? esc_html( $o ) : $o;
}
protected function render_input( array $s, $key, $id ) {
$d = $this->default_for_display( $s );
$d = is_numeric( $d ) ? $d : $s['min'];
echo '';
echo '';
echo '
';
}
}
/** Likert / checkable grid. */
class Grid_Field extends Field_Block {
public function get_name() {
return 'grid';
}
public function get_title() {
return __( 'Checkable Grid (Likert)', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'grid-view' );
}
public function get_category() {
return 'fwren-choice';
}
protected function has_placeholder() {
return false;
}
protected function has_default() {
return false;
}
protected function register_type_controls() {
$this->add_control( 'grid_type', array( 'label' => __( 'Type', 'fieldwren' ), 'type' => Controls::SELECT, 'default' => 'radio', 'options' => array( 'radio' => __( 'One per row', 'fieldwren' ), 'checkbox' => __( 'Several per row', 'fieldwren' ) ) ) );
$this->add_control( 'rows', array( 'label' => __( 'Rows (one per line)', 'fieldwren' ), 'type' => Controls::TEXTAREA, 'rows' => 4, 'default' => "Quality\nSpeed\nSupport" ) );
$this->add_control( 'columns', array( 'label' => __( 'Columns (one per line)', 'fieldwren' ), 'type' => Controls::TEXTAREA, 'rows' => 4, 'default' => "Poor\nAverage\nGood\nExcellent" ) );
}
private static function lines( $s ) {
return array_values( array_filter( array_map( 'trim', preg_split( '/\r?\n/', (string) $s ) ) ) );
}
protected function schema_extra( array $s ) {
return array( 'rows' => self::lines( $s['rows'] ), 'columns' => self::lines( $s['columns'] ), 'grid_type' => $s['grid_type'] );
}
public function sanitize( $raw, array $f ) {
$out = array();
foreach ( $f['rows'] as $i => $row ) {
$v = $raw[ $i ] ?? '';
$out[ $row ] = 'checkbox' === $f['grid_type'] ? array_values( array_intersect( (array) $v, $f['columns'] ) ) : ( in_array( (string) $v, $f['columns'], true ) ? (string) $v : '' );
}
return $out;
}
public function validate( $value, array $f ) {
if ( ! empty( $f['required'] ) ) {
foreach ( (array) $value as $v ) {
if ( $this->is_empty( $v ) ) {
return array( __( 'Please answer every row.', 'fieldwren' ) );
}
}
}
return array();
}
public function format( $value, array $f, $mode = 'html' ) {
$lines = array();
foreach ( (array) $value as $row => $v ) {
$lines[] = $row . ': ' . ( is_array( $v ) ? implode( ', ', $v ) : $v );
}
return 'html' === $mode ? implode( '
', array_map( 'esc_html', $lines ) ) : implode( "\n", $lines );
}
protected function render_label( array $s, $id, $tag = 'label' ) {
parent::render_label( $s, $id, 'div' );
}
protected function render_input( array $s, $key, $id ) {
$rows = self::lines( $s['rows'] );
$cols = self::lines( $s['columns'] );
$type = 'checkbox' === $s['grid_type'] ? 'checkbox' : 'radio';
echo '';
}
}
/** Ranking (drag to order). */
class Ranking_Field extends Choice_Field {
public function get_name() {
return 'ranking';
}
public function get_title() {
return __( 'Ranking', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'sort' );
}
protected function supports_dynamic() {
return false;
}
protected function is_multiple( array $s ) {
return true;
}
public function format( $value, array $f, $mode = 'html' ) {
$map = wp_list_pluck( $f['options'], 'label', 'value' );
$out = array();
foreach ( (array) $value as $i => $v ) {
$out[] = ( $i + 1 ) . '. ' . ( $map[ $v ] ?? $v );
}
return 'html' === $mode ? implode( '
', array_map( 'esc_html', $out ) ) : implode( "\n", $out );
}
protected function render_label( array $s, $id, $tag = 'label' ) {
parent::render_label( $s, $id, 'div' );
}
protected function render_input( array $s, $key, $id ) {
echo '';
foreach ( $this->display_options( $s ) as $o ) {
echo '- ⋮⋮' . esc_html( $o['label'] ) . '';
echo '';
echo '
';
}
echo '
';
}
}
/** Color picker. */
class Color_Field extends Field_Block {
public function get_name() {
return 'color';
}
public function get_title() {
return __( 'Color Picker', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'art' );
}
public function get_category() {
return 'fwren-advanced';
}
protected function has_placeholder() {
return false;
}
public function sanitize( $raw, array $f ) {
return (string) sanitize_hex_color( is_array( $raw ) ? '' : (string) $raw );
}
public function format( $value, array $f, $mode = 'html' ) {
return 'html' === $mode && $value ? ' ' . esc_html( $value ) : (string) $value;
}
protected function render_input( array $s, $key, $id ) {
$this->print_input( 'color', $s, $key, $id );
}
}