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