Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: field set text save empty values #1608

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion inc/fields/fieldset-text.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static function html( $meta, $field ) {
}

foreach ( $field['options'] as $key => $label ) {
$value = isset( $meta[ $key ] ) ? $meta[ $key ] : '';
$value = $meta[ $key ] ?? '';
$field['attributes']['name'] = $field['field_name'] . "[{$key}]";
$html[] = sprintf( $tpl, $label, parent::html( $value, $field ) );
}
Expand Down Expand Up @@ -106,4 +106,16 @@ public static function format_single_value( $field, $value, $args, $post_id ) {
$output .= '</tr>';
return $output;
}

/**
* Since we're using an array of text fields,
* we need to check if all of them are empty.
*
* Otherwise, there is no way to know if the field is empty or not.
*/
public static function value( $new, $old, $post_id, $field ) {
$all_empty = empty( array_filter( (array) $new ) );

return $all_empty ? null : $new;
}
}