Skip to content
Open
Show file tree
Hide file tree
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
30 changes: 21 additions & 9 deletions includes/fields/class-acf-field-radio.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,6 @@ function update_field( $field ) {
* @type filter
* @since ACF 3.6
* @date 23/01/13
* @todo Fix bug where $field was found via json and has no ID
*
* @param $value - the value which will be saved in the database
* @param $post_id - the post_id of which the value will be saved
* @param $field - the field array holding all the field options
Expand All @@ -342,13 +340,23 @@ function update_value( $value, $post_id, $field ) {
// value isn't in choices yet
if ( ! isset( $field['choices'][ $value ] ) ) {

// get raw $field (may have been changed via repeater field)
// if field is local, it won't have an ID
$selector = $field['ID'] ? $field['ID'] : $field['key'];
$field = acf_get_field( $selector );
// get raw $field (may have been changed via repeater field).
$selector = '';
if ( ! empty( $field['ID'] ) ) {
$selector = $field['ID'];
} elseif ( ! empty( $field['key'] ) ) {
$selector = $field['key'];
}

// Bail early when the provided field has no identifier.
if ( ! $selector ) {
return $value;
}

$field = acf_get_field( $selector );

// bail early if no ID (JSON only)
if ( ! $field['ID'] ) {
// Bail early if field lookup fails or field is JSON-only.
if ( ! is_array( $field ) || empty( $field['ID'] ) ) {
return $value;
}

Expand All @@ -358,7 +366,11 @@ function update_value( $value, $post_id, $field ) {
// sanitize (remove tags)
$value = sanitize_text_field( $value );

// update $field
// Update $field.
if ( ! isset( $field['choices'] ) || ! is_array( $field['choices'] ) ) {
$field['choices'] = array();
}

$field['choices'][ $value ] = $value;

// save
Expand Down
29 changes: 29 additions & 0 deletions tests/php/includes/fields/test-class-acf-field-radio.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,35 @@ public function test_update_value_empty() {
$this->assertEquals( '', $result );
}

/**
* Test save_other_choice does not trigger warnings for JSON-only fields.
*/
public function test_update_value_save_other_choice_handles_json_field_without_id() {
$field = $this->get_field(
array(
'save_other_choice' => 1,
)
);

$result = $this->field_instance->update_value( 'custom_value', $this->post_id, $field );
$this->assertSame( 'custom_value', $result );
}

/**
* Test save_other_choice bails early when field key and ID are both missing.
*/
public function test_update_value_save_other_choice_handles_missing_field_identifier() {
$field = $this->get_field(
array(
'save_other_choice' => 1,
)
);
unset( $field['key'] );

$result = $this->field_instance->update_value( 'custom_value', $this->post_id, $field );
$this->assertSame( 'custom_value', $result );
}

/**
* Test get_rest_schema returns valid schema.
*/
Expand Down
Loading