|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Manipulates input data for Radio field values. |
| 4 | + * |
| 5 | + * @package WPGraphQL\GF\Data\FieldValueInput |
| 6 | + * @since @todo |
| 7 | + */ |
| 8 | + |
| 9 | +declare( strict_types = 1 ); |
| 10 | + |
| 11 | +namespace WPGraphQL\GF\Data\FieldValueInput; |
| 12 | + |
| 13 | +use GFCommon; |
| 14 | + |
| 15 | +/** |
| 16 | + * Class - RadioValueInput |
| 17 | + */ |
| 18 | +class RadioValueInput extends AbstractFieldValueInput { |
| 19 | + /** |
| 20 | + * {@inheritDoc} |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $args; |
| 25 | + |
| 26 | + /** |
| 27 | + * {@inheritDoc} |
| 28 | + * |
| 29 | + * @var array<int|string,?string> |
| 30 | + */ |
| 31 | + public $value; |
| 32 | + |
| 33 | + /** |
| 34 | + * {@inheritDoc} |
| 35 | + */ |
| 36 | + protected function get_field_name(): string { |
| 37 | + return 'value'; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * {@inheritDoc} |
| 42 | + * |
| 43 | + * @return array<int|string,?string> |
| 44 | + */ |
| 45 | + protected function prepare_value() { |
| 46 | + $value = $this->args; |
| 47 | + |
| 48 | + // Handle values with price. |
| 49 | + if ( ! empty( $this->field->enablePrice ) && false === strpos( $value, '|' ) ) { |
| 50 | + $value_key = ! empty( $this->field->enablePrice ) || ! empty( $this->field->enableChoiceValue ) ? 'value' : 'text'; |
| 51 | + $choice_key = array_search( $value, array_column( $this->field->choices, $value_key ), true ); |
| 52 | + $choice = $this->field->choices[ $choice_key ]; |
| 53 | + $price = rgempty( 'price', $choice ) ? 0 : GFCommon::to_number( rgar( $choice, 'price' ) ); |
| 54 | + $value = $value . '|' . $price; |
| 55 | + } |
| 56 | + |
| 57 | + if ( empty( $this->field->enableOtherChoice ) ) { |
| 58 | + return [ |
| 59 | + $this->field->id => $value, |
| 60 | + ]; |
| 61 | + } |
| 62 | + |
| 63 | + $allowed_values = wp_list_pluck( $this->field->choices, 'value' ); |
| 64 | + |
| 65 | + if ( ! in_array( $value, $allowed_values, true ) ) { |
| 66 | + $_POST[ $this->field->id . '_other' ] = $value; |
| 67 | + $_POST[ $this->field->id ] = 'gf_other_choice'; |
| 68 | + return [ |
| 69 | + $this->field->id => 'gf_other_choice', |
| 70 | + $this->field->id . '_other' => $value, |
| 71 | + ]; |
| 72 | + } |
| 73 | + |
| 74 | + return [ |
| 75 | + $this->field->id => $value, |
| 76 | + ]; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * {@inheritDoc} |
| 81 | + */ |
| 82 | + public function add_value_to_submission( array &$field_values ): void { |
| 83 | + $field_values += $this->value; |
| 84 | + } |
| 85 | +} |
0 commit comments