add_control(
'mode',
array(
'label' => __( 'Type', 'fieldwren' ),
'type' => Controls::SELECT,
'default' => 'date',
'options' => array(
'date' => __( 'Date', 'fieldwren' ),
'time' => __( 'Time', 'fieldwren' ),
'datetime' => __( 'Date and time', 'fieldwren' ),
'month' => __( 'Month', 'fieldwren' ),
),
)
);
$this->add_control(
'display_format',
array(
'label' => __( 'Display format in entries', 'fieldwren' ),
'type' => Controls::TEXT,
'default' => '',
'placeholder' => get_option( 'date_format' ),
'description' => __( 'PHP date format. Empty = site format.', 'fieldwren' ),
)
);
}
protected function register_validation_controls() {
$this->add_control( 'min_date', array( 'label' => __( 'Earliest', 'fieldwren' ), 'type' => Controls::TEXT, 'placeholder' => 'today, +2 days, 2026-12-31', 'description' => __( 'Absolute (2026-12-31) or relative to today (today, +3 days, -1 year).', 'fieldwren' ) ) );
$this->add_control( 'max_date', array( 'label' => __( 'Latest', 'fieldwren' ), 'type' => Controls::TEXT, 'placeholder' => '+6 months' ) );
$this->add_control( 'no_weekends', array( 'label' => __( 'Disallow weekends', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => '' ) );
$this->add_control( 'blocked_dates', array( 'label' => __( 'Blocked dates', 'fieldwren' ), 'type' => Controls::TEXT, 'placeholder' => '2026-12-25, 2027-01-01' ) );
}
/** Resolve a min/max expression to Y-m-d. */
public static function resolve( $expr ) {
$expr = trim( (string) $expr );
if ( '' === $expr ) {
return '';
}
$ts = strtotime( $expr, current_time( 'timestamp' ) ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
return $ts ? gmdate( 'Y-m-d', $ts ) : '';
}
protected function schema_extra( array $s ) {
return array(
'mode' => $s['mode'],
'min' => self::resolve( $s['min_date'] ),
'max' => self::resolve( $s['max_date'] ),
'no_weekends' => 'yes' === $s['no_weekends'],
'blocked' => array_filter( array_map( 'trim', explode( ',', (string) $s['blocked_dates'] ) ) ),
'display_format' => (string) $s['display_format'],
);
}
public function sanitize( $raw, array $f ) {
return preg_replace( '/[^0-9T:\-\s]/', '', is_array( $raw ) ? '' : (string) $raw );
}
public function validate( $value, array $f ) {
if ( '' === $value ) {
return array();
}
if ( 'time' === $f['mode'] ) {
return preg_match( '/^\d{2}:\d{2}(:\d{2})?$/', $value ) ? array() : array( __( 'Please enter a valid time.', 'fieldwren' ) );
}
$ts = strtotime( 'month' === $f['mode'] ? $value . '-01' : $value );
if ( ! $ts ) {
return array( __( 'Please enter a valid date.', 'fieldwren' ) );
}
$day = gmdate( 'Y-m-d', $ts );
if ( $f['min'] && $day < $f['min'] ) {
return array( __( 'This date is too early.', 'fieldwren' ) );
}
if ( $f['max'] && $day > $f['max'] ) {
return array( __( 'This date is too late.', 'fieldwren' ) );
}
if ( ! empty( $f['no_weekends'] ) && in_array( (int) gmdate( 'N', $ts ), array( 6, 7 ), true ) ) {
return array( __( 'Please choose a weekday.', 'fieldwren' ) );
}
if ( in_array( $day, (array) $f['blocked'], true ) ) {
return array( __( 'This date is not available.', 'fieldwren' ) );
}
return array();
}
public function format( $value, array $f, $mode = 'html' ) {
if ( '' === (string) $value ) {
return '';
}
$fmt = $f['display_format'] ? $f['display_format'] : ( 'time' === $f['mode'] ? get_option( 'time_format' ) : ( 'datetime' === $f['mode'] ? get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) : get_option( 'date_format' ) ) );
$ts = strtotime( 'time' === $f['mode'] ? '1970-01-01 ' . $value : ( 'month' === $f['mode'] ? $value . '-01' : $value ) );
$out = $ts ? date_i18n( 'month' === $f['mode'] ? 'F Y' : $fmt, $ts ) : $value;
return 'html' === $mode ? esc_html( $out ) : $out;
}
protected function render_input( array $s, $key, $id ) {
$types = array( 'date' => 'date', 'time' => 'time', 'datetime' => 'datetime-local', 'month' => 'month' );
$extra = array();
$min = self::resolve( $s['min_date'] );
$max = self::resolve( $s['max_date'] );
if ( in_array( $s['mode'], array( 'date', 'datetime' ), true ) ) {
$extra['min'] = $min ? ( 'datetime' === $s['mode'] ? $min . 'T00:00' : $min ) : false;
$extra['max'] = $max ? ( 'datetime' === $s['mode'] ? $max . 'T23:59' : $max ) : false;
}
if ( 'yes' === $s['no_weekends'] ) {
$extra['data-no-weekends'] = '1';
}
if ( '' !== (string) $s['blocked_dates'] ) {
$extra['data-blocked'] = $s['blocked_dates'];
}
$this->print_input( $types[ $s['mode'] ] ?? 'date', $s, $key, $id, $extra );
}
}
/** File upload (drag & drop, several files, async upload). */
class File_Field extends Field_Block {
public function get_name() {
return 'file';
}
public function get_title() {
return __( 'File Upload', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'upload' );
}
public function get_category() {
return 'fwren-advanced';
}
protected function has_placeholder() {
return false;
}
protected function has_default() {
return false;
}
protected function has_validation() {
return true;
}
/** @return string Default allowed extensions. */
protected function default_types() {
return 'pdf,doc,docx,xls,xlsx,ppt,pptx,txt,csv,jpg,jpeg,png,gif,webp,zip';
}
protected function register_type_controls() {
$this->add_control( 'button_text', array( 'label' => __( 'Drop zone text', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => __( 'Drag files here or click to browse', 'fieldwren' ) ) );
}
protected function register_validation_controls() {
$this->add_control( 'allowed', array( 'label' => __( 'Allowed file types', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => $this->default_types(), 'description' => __( 'Comma separated extensions.', 'fieldwren' ) ) );
$this->add_control( 'max_size', array( 'label' => __( 'Maximum size per file (MB)', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => 5, 'inline' => true ) );
$this->add_control( 'max_files', array( 'label' => __( 'Maximum files', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => 1, 'inline' => true ) );
$this->add_control( 'media_library', array( 'label' => __( 'Add to the media library', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => '' ) );
}
protected function schema_extra( array $s ) {
return array(
'allowed' => Uploads::parse_types( $s['allowed'] ),
'max_size' => max( 0.01, (float) $s['max_size'] ),
'max_files' => max( 1, (int) $s['max_files'] ),
'media_library' => 'yes' === $s['media_library'],
'is_upload' => true,
);
}
public function sanitize( $raw, array $f ) {
$tokens = is_array( $raw ) ? $raw : ( '' === (string) $raw ? array() : array( $raw ) );
return array_values( array_filter( array_map( static function ( $t ) {
return preg_replace( '/[^a-zA-Z0-9_.\-]/', '', (string) $t );
}, $tokens ) ) );
}
public function validate( $value, array $f ) {
if ( count( (array) $value ) > $f['max_files'] ) {
/* translators: %d files */
return array( sprintf( __( 'Please upload no more than %d files.', 'fieldwren' ), $f['max_files'] ) );
}
foreach ( (array) $value as $token ) {
if ( ! Uploads::temp_exists( $token, $f ) ) {
return array( __( 'An uploaded file is missing. Please upload it again.', 'fieldwren' ) );
}
}
return array();
}
public function format( $value, array $f, $mode = 'html' ) {
$out = array();
foreach ( (array) $value as $file ) {
$url = is_array( $file ) ? ( $file['url'] ?? '' ) : '';
$name = is_array( $file ) ? ( $file['name'] ?? basename( $url ) ) : (string) $file;
$out[] = 'html' === $mode && $url ? '' . esc_html( $name ) . '' : ( 'html' === $mode ? esc_html( $name ) : ( $url ? Uploads::signed_url( $file ) : $name ) );
}
return implode( 'html' === $mode ? '
' : "\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 ) {
$types = Uploads::parse_types( $s['allowed'] );
$max = max( 1, (int) $s['max_files'] );
$accept = implode( ',', array_map( static function ( $e ) {
return '.' . $e;
}, $types ) );
echo '
';
}
}
/** Image upload (file upload limited to images, with previews). */
class Image_Field extends File_Field {
public function get_name() {
return 'image';
}
public function get_title() {
return __( 'Image Upload', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'format-image' );
}
protected function default_types() {
return 'jpg,jpeg,png,gif,webp';
}
public function get_field_type() {
return 'image';
}
}
/** Signature pad (saved as PNG). */
class Signature_Field extends Field_Block {
public function get_name() {
return 'signature';
}
public function get_title() {
return __( 'Signature', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'edit' );
}
public function get_category() {
return 'fwren-advanced';
}
protected function has_placeholder() {
return false;
}
protected function has_default() {
return false;
}
protected function register_type_controls() {
$this->add_control( 'pad_height', array( 'label' => __( 'Pad height (px)', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => 180, 'inline' => true ) );
$this->add_control( 'pen_color', array( 'label' => __( 'Pen color', 'fieldwren' ), 'type' => Controls::COLOR, 'default' => '#111827' ) );
$this->add_control( 'clear_text', array( 'label' => __( 'Clear button text', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => __( 'Clear', 'fieldwren' ) ) );
}
protected function schema_extra( array $s ) {
return array( 'is_upload' => true, 'signature' => true, 'allowed' => array( 'png' ), 'max_size' => 2, 'max_files' => 1 );
}
public function sanitize( $raw, array $f ) {
$raw = is_array( $raw ) ? '' : (string) $raw;
return 0 === strpos( $raw, 'data:image/png;base64,' ) && strlen( $raw ) < 2 * MB_IN_BYTES ? $raw : '';
}
public function format( $value, array $f, $mode = 'html' ) {
$file = is_array( $value ) ? ( $value[0] ?? $value ) : null;
if ( ! is_array( $file ) || empty( $file['url'] ) ) {
return '';
}
$url = Uploads::signed_url( $file );
return 'html' === $mode ? '
' : $url;
}
protected function render_label( array $s, $id, $tag = 'label' ) {
parent::render_label( $s, $id, 'div' );
}
protected function render_input( array $s, $key, $id ) {
echo '';
echo '';
echo '';
echo '
';
}
}
/** Terms & conditions / GDPR consent checkbox. */
class Consent_Field extends Field_Block {
public function get_name() {
return 'consent';
}
public function get_title() {
return __( 'Terms & GDPR Consent', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'shield' );
}
public function get_keywords() {
return array( 'terms', 'gdpr', 'privacy', 'agree', 'consent' );
}
public function get_category() {
return 'fwren-advanced';
}
public function key_prefix() {
return 'terms';
}
protected function has_placeholder() {
return false;
}
protected function has_default() {
return false;
}
protected function default_label() {
return '';
}
protected function register_type_controls() {
$this->add_control(
'consent_text',
array(
'label' => __( 'Consent text (HTML allowed)', 'fieldwren' ),
'type' => Controls::TEXTAREA,
'rows' => 3,
/* translators: %s privacy policy link */
'default' => sprintf( __( 'I agree to the %s and to being contacted about my request.', 'fieldwren' ), '' . __( 'Privacy Policy', 'fieldwren' ) . '' ),
)
);
$this->add_control( 'consent_type', array( 'label' => __( 'Kind', 'fieldwren' ), 'type' => Controls::SELECT, 'default' => 'terms', 'options' => array( 'terms' => __( 'Terms & conditions', 'fieldwren' ), 'gdpr' => __( 'GDPR agreement (always required)', 'fieldwren' ), 'marketing' => __( 'Marketing opt-in', 'fieldwren' ) ) ) );
$this->add_control( 'scroll_box', array( 'label' => __( 'Show full terms in a scroll box', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => '' ) );
$this->add_control( 'terms_body', array( 'label' => __( 'Terms text', 'fieldwren' ), 'type' => Controls::TEXTAREA, 'rows' => 6, 'condition' => array( 'scroll_box' => 'yes' ) ) );
}
protected function schema_extra( array $s ) {
return array(
'required' => 'gdpr' === $s['consent_type'] ? true : 'yes' === $s['required'],
'consent_type' => $s['consent_type'],
'label' => $s['label'] ? wp_strip_all_tags( $s['label'] ) : wp_strip_all_tags( $s['consent_text'] ),
);
}
public function sanitize( $raw, array $f ) {
return 'yes' === ( is_array( $raw ) ? '' : (string) $raw ) ? 'yes' : '';
}
public function format( $value, array $f, $mode = 'html' ) {
return 'yes' === $value ? __( 'Accepted', 'fieldwren' ) : __( 'Not accepted', 'fieldwren' );
}
protected function render_input( array $s, $key, $id ) {
$req = 'yes' === $s['required'] || 'gdpr' === $s['consent_type'];
if ( 'yes' === $s['scroll_box'] && '' !== trim( (string) $s['terms_body'] ) ) {
echo '' . wp_kses_post( wpautop( $s['terms_body'] ) ) . '
';
}
echo '';
}
}
/** Calculated value (formula over other fields). */
class Calculation_Field extends Field_Block {
public function get_name() {
return 'calculation';
}
public function get_title() {
return __( 'Calculation', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'performance' );
}
public function get_keywords() {
return array( 'formula', 'total', 'price', 'quote', 'estimate', 'math' );
}
public function get_category() {
return 'fwren-advanced';
}
protected function has_placeholder() {
return false;
}
protected function has_default() {
return false;
}
protected function register_type_controls() {
$this->add_control(
'formula',
array(
'label' => __( 'Formula', 'fieldwren' ),
'type' => Controls::TEXTAREA,
'rows' => 3,
'default' => '',
'placeholder' => '{inputs.quantity} * {inputs.plan} + 49',
'merge_tags' => true,
'description' => __( 'Use {inputs.key} for field values (choices use their calc value). Operators + - * / % ^ ( ) and functions round(), ceil(), floor(), abs(), min(), max(), sqrt(), if(cond, a, b). Comparisons > < >= <= == != work inside if().', 'fieldwren' ),
)
);
$this->add_control( 'decimals', array( 'label' => __( 'Decimals', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => 2, 'inline' => true ) );
$this->add_control( 'prefix', array( 'label' => __( 'Prefix', 'fieldwren' ), 'type' => Controls::TEXT, 'placeholder' => '$' ) );
$this->add_control( 'suffix', array( 'label' => __( 'Suffix', 'fieldwren' ), 'type' => Controls::TEXT ) );
$this->add_control( 'calc_display', array( 'label' => __( 'Display', 'fieldwren' ), 'type' => Controls::SELECT, 'default' => 'text', 'options' => array( 'text' => __( 'Large number', 'fieldwren' ), 'input' => __( 'Read-only input', 'fieldwren' ), 'hidden' => __( 'Hidden', 'fieldwren' ) ) ) );
}
protected function schema_extra( array $s ) {
return array( 'formula' => (string) $s['formula'], 'decimals' => max( 0, (int) $s['decimals'] ), 'prefix' => (string) $s['prefix'], 'suffix' => (string) $s['suffix'], 'calculated' => true );
}
public function sanitize( $raw, array $f ) {
return is_numeric( $raw ) ? $raw + 0 : '';
}
public function format( $value, array $f, $mode = 'html' ) {
if ( '' === $value || null === $value ) {
return '';
}
$o = $f['prefix'] . number_format_i18n( (float) $value, $f['decimals'] ) . $f['suffix'];
return 'html' === $mode ? esc_html( $o ) : $o;
}
protected function render_input( array $s, $key, $id ) {
$attrs = ' data-fwren-formula="' . esc_attr( $s['formula'] ) . '" data-decimals="' . (int) $s['decimals'] . '" data-prefix="' . esc_attr( $s['prefix'] ) . '" data-suffix="' . esc_attr( $s['suffix'] ) . '"';
if ( 'input' === $s['calc_display'] ) {
echo ''; // phpcs:ignore
return;
}
echo ''; // phpcs:ignore
if ( 'text' === $s['calc_display'] ) {
echo '' . esc_html( $s['prefix'] . number_format_i18n( 0, (int) $s['decimals'] ) . $s['suffix'] ) . '
';
}
}
protected function add_render_attributes() {
parent::add_render_attributes();
if ( 'hidden' === ( $this->settings['calc_display'] ?? '' ) && ! Renderer::is_editor() ) {
$this->add_render_attribute( '_wrapper', 'class', 'fwren-hidden-field' );
}
}
}
/** Address with optional Google Places autocomplete. */
class Address_Field extends Field_Block {
const PARTS = array( 'line1', 'line2', 'city', 'state', 'zip', 'country' );
public function get_name() {
return 'address';
}
public function get_title() {
return __( 'Address', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'location' );
}
public function get_category() {
return 'fwren-advanced';
}
protected function has_placeholder() {
return false;
}
protected function has_default() {
return false;
}
private static function labels() {
return array(
'line1' => __( 'Street address', 'fieldwren' ),
'line2' => __( 'Address line 2', 'fieldwren' ),
'city' => __( 'City', 'fieldwren' ),
'state' => __( 'State / Province', 'fieldwren' ),
'zip' => __( 'ZIP / Postal code', 'fieldwren' ),
'country' => __( 'Country', 'fieldwren' ),
);
}
protected function register_type_controls() {
foreach ( self::labels() as $p => $l ) {
$this->add_control( "show_{$p}", array( 'label' => sprintf( /* translators: %s part */ __( 'Show %s', 'fieldwren' ), $l ), 'type' => Controls::SWITCHER, 'default' => 'line2' === $p ? '' : 'yes' ) );
$this->add_control( "{$p}_label", array( 'label' => $l . ' – ' . __( 'label', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => $l, 'condition' => array( "show_{$p}" => 'yes' ) ) );
}
$this->add_control( 'autocomplete', array( 'label' => __( 'Google Places autocomplete', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => '', 'description' => __( 'Needs a Google Maps API key in Forms → Settings.', 'fieldwren' ) ) );
}
private function parts( array $s ) {
return array_values( array_filter( self::PARTS, static function ( $p ) use ( $s ) {
return 'yes' === ( $s[ "show_{$p}" ] ?? '' );
} ) );
}
protected function schema_extra( array $s ) {
return array( 'parts' => $this->parts( $s ) );
}
public function sanitize( $raw, array $f ) {
$out = array();
foreach ( $f['parts'] as $p ) {
$out[ $p ] = sanitize_text_field( (string) ( is_array( $raw ) ? ( $raw[ $p ] ?? '' ) : '' ) );
}
return $out;
}
public function validate( $value, array $f ) {
if ( ! empty( $f['required'] ) ) {
foreach ( array( 'line1', 'city', 'country' ) as $p ) {
if ( in_array( $p, $f['parts'], true ) && '' === ( $value[ $p ] ?? '' ) ) {
return array( __( 'Please complete the address.', 'fieldwren' ) );
}
}
}
return array();
}
public function format( $value, array $f, $mode = 'html' ) {
$v = (array) $value;
if ( ! empty( $v['country'] ) ) {
$names = \FWREN\Countries::names();
$v['country'] = $names[ $v['country'] ] ?? $v['country'];
}
$lines = array_filter( array( $v['line1'] ?? '', $v['line2'] ?? '', trim( implode( ' ', array_filter( array( $v['city'] ?? '', $v['state'] ?? '', $v['zip'] ?? '' ) ) ) ), $v['country'] ?? '' ) );
return 'html' === $mode ? implode( '
', array_map( 'esc_html', $lines ) ) : implode( ', ', $lines );
}
protected function render_label( array $s, $id, $tag = 'label' ) {
parent::render_label( $s, $id, 'div' );
}
protected function render_input( array $s, $key, $id ) {
$auto = array( 'line1' => 'address-line1', 'line2' => 'address-line2', 'city' => 'address-level2', 'state' => 'address-level1', 'zip' => 'postal-code', 'country' => 'country' );
echo '';
foreach ( $this->parts( $s ) as $p ) {
$pid = $id . '-' . $p;
$wide = in_array( $p, array( 'line1', 'line2' ), true ) ? ' fwren-subfield--wide' : '';
echo '
';
$req = 'yes' === $s['required'] && in_array( $p, array( 'line1', 'city', 'country' ), true ) ? ' required aria-required="true"' : '';
if ( 'country' === $p ) {
echo '';
} else {
echo ''; // phpcs:ignore
}
echo '
';
}
echo '
';
}
}
/** Repeater: rows of sub-fields (inner blocks). */
class Repeater_Field extends Field_Block {
public function get_name() {
return 'repeater';
}
public function get_title() {
return __( 'Repeater', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'list-view' );
}
public function get_keywords() {
return array( 'repeat', 'rows', 'group', 'table' );
}
public function get_category() {
return 'fwren-advanced';
}
protected function has_placeholder() {
return false;
}
protected function has_default() {
return false;
}
protected function has_input_style() {
return false;
}
public function get_inner_blocks() {
return array(
'allowed' => array( 'fwren/text', 'fwren/email', 'fwren/number', 'fwren/select', 'fwren/date', 'fwren/phone', 'fwren/url', 'fwren/textarea', 'fwren/radio', 'fwren/checkbox' ),
'template' => array(
array( 'fwren/text', array( 'label' => __( 'Item', 'fieldwren' ), 'name' => 'item', 'width' => '50' ) ),
array( 'fwren/number', array( 'label' => __( 'Quantity', 'fieldwren' ), 'name' => 'qty', 'width' => '50' ) ),
),
'orientation' => 'horizontal',
);
}
protected function register_type_controls() {
$this->add_control( 'min_rows', array( 'label' => __( 'Minimum rows', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => 1, 'inline' => true ) );
$this->add_control( 'max_rows', array( 'label' => __( 'Maximum rows', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => 10, 'inline' => true ) );
$this->add_control( 'add_text', array( 'label' => __( 'Add button text', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => __( '+ Add row', 'fieldwren' ) ) );
}
protected function schema_extra( array $s ) {
return array( 'min_rows' => max( 0, (int) $s['min_rows'] ), 'max_rows' => max( 1, (int) $s['max_rows'] ), 'repeater' => true, 'children' => array() );
}
public function sanitize( $raw, array $f ) {
$rows = array();
foreach ( array_slice( is_array( $raw ) ? array_values( $raw ) : array(), 0, $f['max_rows'] ) as $row ) {
if ( ! is_array( $row ) ) {
continue;
}
$clean = array();
foreach ( $f['children'] as $child ) {
$type = \FWREN\Fields::get( $child['type'] );
$clean[ $child['name'] ] = $type ? $type->sanitize( $row[ $child['name'] ] ?? '', $child ) : sanitize_text_field( (string) ( $row[ $child['name'] ] ?? '' ) );
}
if ( ! $this->is_empty( $clean ) ) {
$rows[] = $clean;
}
}
return $rows;
}
public function validate( $value, array $f ) {
if ( count( $value ) < $f['min_rows'] && ( ! empty( $f['required'] ) || $value ) ) {
/* translators: %d rows */
return array( sprintf( __( 'Please add at least %d rows.', 'fieldwren' ), $f['min_rows'] ) );
}
foreach ( $value as $row ) {
foreach ( $f['children'] as $child ) {
$type = \FWREN\Fields::get( $child['type'] );
$v = $row[ $child['name'] ] ?? '';
if ( ! empty( $child['required'] ) && $type && $type->is_empty( $v ) ) {
/* translators: %s field label */
return array( sprintf( __( '%s is required in every row.', 'fieldwren' ), $child['label'] ) );
}
if ( $type ) {
$e = $type->validate( $v, $child );
if ( $e ) {
return $e;
}
}
}
}
return array();
}
public function format( $value, array $f, $mode = 'html' ) {
if ( ! $value ) {
return '';
}
if ( 'html' !== $mode ) {
$lines = array();
foreach ( $value as $i => $row ) {
$lines[] = ( $i + 1 ) . '. ' . implode( ' | ', array_map( static function ( $v ) {
return is_array( $v ) ? implode( ', ', $v ) : $v;
}, $row ) );
}
return implode( "\n", $lines );
}
$h = '';
foreach ( $f['children'] as $c ) {
$h .= '| ' . esc_html( $c['label'] ) . ' | ';
}
$h .= '
';
foreach ( $value as $row ) {
$h .= '';
foreach ( $f['children'] as $c ) {
$t = \FWREN\Fields::get( $c['type'] );
$h .= '| ' . ( $t ? $t->format( $row[ $c['name'] ] ?? '', $c ) : esc_html( (string) ( $row[ $c['name'] ] ?? '' ) ) ) . ' | ';
}
$h .= '
';
}
return $h . '
';
}
protected function render_label( array $s, $id, $tag = 'label' ) {
parent::render_label( $s, $id, 'div' );
}
protected function render( $content = '' ) {
$s = $this->settings;
$key = $this->field_key( $s );
$id = $this->input_id( $key );
echo '';
$this->render_label( $s, $id );
if ( Renderer::is_editor() ) {
echo $content; // phpcs:ignore WordPress.Security.EscapeOutput
} else {
echo '
';
echo '
'; // phpcs:ignore
echo '
';
echo '
';
}
if ( '' !== trim( (string) $s['help'] ) ) {
echo '
' . wp_kses_post( $s['help'] ) . '
';
}
echo '
';
}
}