| @@ -8,9 +8,9 @@ | ||
| 8 | 8 | */ |
| 9 | 9 | class FrmFieldOption { |
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | - * @var string|int | |
| 12 | + * @var int|string | |
| 13 | 13 | * |
| 14 | 14 | * @since 2.03.05 |
| 15 | 15 | */ |
| 16 | 16 | protected $option_key; |
| @@ -15,9 +15,9 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | protected $option_key; |
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | - * @var string|array | |
| 19 | + * @var array|string | |
| 20 | 20 | * |
| 21 | 21 | * @since 2.03.05 |
| 22 | 22 | */ |
| 23 | 23 | protected $option; |
| @@ -23,8 +23,9 @@ | ||
| 23 | 23 | protected $option; |
| 24 | 24 | |
| 25 | 25 | /** |
| 26 | 26 | * @var string |
| 27 | + * | |
| 27 | 28 | * @since 2.03.05 |
| 28 | 29 | */ |
| 29 | 30 | protected $saved_value = ''; |
| 30 | 31 | |
| @@ -29,12 +30,20 @@ | ||
| 29 | 30 | protected $saved_value = ''; |
| 30 | 31 | |
| 31 | 32 | /** |
| 32 | 33 | * @var string |
| 34 | + * | |
| 33 | 35 | * @since 2.03.05 |
| 34 | 36 | */ |
| 35 | 37 | protected $option_label = ''; |
| 36 | 38 | |
| 39 | + /** | |
| 40 | + * @param int|string $option_key | |
| 41 | + * @param array|string $option | |
| 42 | + * @param array $args | |
| 43 | + * | |
| 44 | + * @return void | |
| 45 | + */ | |
| 37 | 46 | public function __construct( $option_key, $option, $args = array() ) { |
| 38 | 47 | $this->option_key = $option_key; |
| 39 | 48 | $this->option = $option; |
| 40 | 49 | $this->set_option_label(); |
| @@ -48,13 +57,9 @@ | ||
| 48 | 57 | * |
| 49 | 58 | * @return void |
| 50 | 59 | */ |
| 51 | 60 | private function set_option_label() { |
| 52 | - if ( is_array( $this->option ) ) { | |
| 53 | - $this->option_label = ( isset( $this->option['label'] ) ? $this->option['label'] : reset( $this->option ) ); | |
| 54 | - } else { | |
| 55 | - $this->option_label = $this->option; | |
| 56 | - } | |
| 61 | + $this->option_label = is_array( $this->option ) ? ( $this->option['label'] ?? reset( $this->option ) ) : $this->option; | |
| 57 | 62 | } |
| 58 | 63 | |
| 59 | 64 | /** |
| 60 | 65 | * Set the saved value |
| @@ -71,19 +76,28 @@ | ||
| 71 | 76 | * Print a single option |
| 72 | 77 | * |
| 73 | 78 | * @since 2.03.05 |
| 74 | 79 | * |
| 75 | - * @param string $selected_value | |
| 76 | - * @param int $truncate | |
| 80 | + * @param string $selected_value The value of the option to be selected. | |
| 81 | + * @param int $truncate Truncate the option label if true. | |
| 82 | + * @param bool $use_value_as_label Use the option value as the label if true. | |
| 77 | 83 | * |
| 78 | 84 | * @return void |
| 79 | 85 | */ |
| 80 | - public function print_single_option( $selected_value, $truncate ) { | |
| 81 | - if ( '' !== $this->saved_value ) { | |
| 82 | - echo '<option value="' . esc_attr( $this->saved_value ) . '"'; | |
| 83 | - selected( esc_attr( $selected_value ), esc_attr( $this->saved_value ) ); | |
| 84 | - // TODO: add hook that can add attributes to option text | |
| 85 | - echo '>'; | |
| 86 | - echo esc_html( FrmAppHelper::truncate( $this->option_label, $truncate ) ) . '</option>'; | |
| 86 | + public function print_single_option( $selected_value, $truncate, $use_value_as_label = false ) { | |
| 87 | + if ( '' === $this->saved_value && ! $use_value_as_label ) { | |
| 88 | + return; | |
| 87 | 89 | } |
| 90 | + | |
| 91 | + if ( $use_value_as_label && '' === trim( $this->option_label ) ) { | |
| 92 | + $label = '' !== $this->saved_value ? $this->saved_value : FrmAppHelper::get_no_label_text(); | |
| 93 | + } else { | |
| 94 | + $label = $this->option_label; | |
| 95 | + } | |
| 96 | + | |
| 97 | + echo '<option value="' . esc_attr( $this->saved_value ) . '"'; | |
| 98 | + selected( esc_attr( $selected_value ), esc_attr( $this->saved_value ) ); | |
| 99 | + // TODO: add hook that can add attributes to option text | |
| 100 | + echo '>'; | |
| 101 | + echo esc_html( FrmAppHelper::truncate( $label, $truncate ) ) . '</option>'; | |
| 88 | 102 | } |
| 89 | 103 | } |