diff --git a/includes/fields/class-acf-field-radio.php b/includes/fields/class-acf-field-radio.php index 28d33385..b3d2c5e3 100644 --- a/includes/fields/class-acf-field-radio.php +++ b/includes/fields/class-acf-field-radio.php @@ -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 @@ -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; } @@ -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 diff --git a/tests/php/includes/fields/test-class-acf-field-radio.php b/tests/php/includes/fields/test-class-acf-field-radio.php index 06b73945..48808d69 100644 --- a/tests/php/includes/fields/test-class-acf-field-radio.php +++ b/tests/php/includes/fields/test-class-acf-field-radio.php @@ -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. */