| 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 string|int |
| 13 |
* |
| 14 |
* @since 2.03.05 |
| 15 |
*/ |
| 16 |
protected $option_key; |
| 17 |
|
| 18 |
/** |
| 19 |
* @var string|array |
| 20 |
* |
| 21 |
* @since 2.03.05 |
| 22 |
*/ |
| 23 |
protected $option; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var string |
| 27 |
* @since 2.03.05 |
| 28 |
*/ |
| 29 |
protected $saved_value = ''; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var string |
| 33 |
* @since 2.03.05 |
| 34 |
*/ |
| 35 |
protected $option_label = ''; |
| 36 |
|
| 37 |
public function __construct( $option_key, $option, $args = array() ) { |
| 38 |
$this->option_key = $option_key; |
| 39 |
$this->option = $option; |
| 40 |
$this->set_option_label(); |
| 41 |
$this->set_saved_value(); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Set the option label |
| 46 |
* |
| 47 |
* @since 2.03.05 |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
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 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Set the saved value |
| 61 |
* |
| 62 |
* @since 2.03.05 |
| 63 |
* |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
protected function set_saved_value() { |
| 67 |
$this->saved_value = $this->option_label; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Print a single option |
| 72 |
* |
| 73 |
* @since 2.03.05 |
| 74 |
* |
| 75 |
* @param string $selected_value |
| 76 |
* @param int $truncate |
| 77 |
* |
| 78 |
* @return void |
| 79 |
*/ |
| 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>'; |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|