| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
* Leaflet_Map_Plugin_Option |
| 5 |
* |
| 6 |
* store values; render widgets |
| 7 |
* @param array $details array of option details |
| 8 |
**/ |
| 9 |
|
| 10 |
// Exit if accessed directly |
| 11 |
if ( !defined( 'ABSPATH' ) ) exit; |
| 12 |
|
| 13 |
class Leaflet_Map_Plugin_Option { |
| 14 |
/** |
| 15 |
* @var $default default value |
| 16 |
* either string or int/boolean? |
| 17 |
*/ |
| 18 |
public $default = ''; |
| 19 |
/** |
| 20 |
* @var $type input type |
| 21 |
* ex: ('text', 'select', 'checkbox') |
| 22 |
*/ |
| 23 |
public $type; |
| 24 |
/** |
| 25 |
* @var $options optional |
| 26 |
* used for select; maybe checkbox/radio |
| 27 |
*/ |
| 28 |
public $options = array(); |
| 29 |
public $helptext = ''; |
| 30 |
|
| 31 |
function __construct ($details = array()) { |
| 32 |
if (!$details) { |
| 33 |
// just an empty db entry (for now) |
| 34 |
// nothing to store, nothing to render |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
$option_filter = array( |
| 39 |
'display_name' => FILTER_SANITIZE_STRING, |
| 40 |
'default' => null, |
| 41 |
'type' => FILTER_SANITIZE_STRING, |
| 42 |
'options' => array( |
| 43 |
'filter' => FILTER_SANITIZE_STRING, |
| 44 |
'flags' => FILTER_FORCE_ARRAY |
| 45 |
), |
| 46 |
'helptext' => FILTER_SANITIZE_STRING |
| 47 |
); |
| 48 |
|
| 49 |
// get matching keys only |
| 50 |
$details = array_intersect_key($details, $option_filter); |
| 51 |
|
| 52 |
// apply filter |
| 53 |
$details = filter_var_array($details, $option_filter); |
| 54 |
|
| 55 |
foreach ($details as $key => $value) { |
| 56 |
$this->$key = $value; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Renders a widget |
| 62 |
* @param value required chosen value |
| 63 |
* @return HTML |
| 64 |
*/ |
| 65 |
function widget ($name, $value) { |
| 66 |
switch ($this->type) { |
| 67 |
case 'text': |
| 68 |
?> |
| 69 |
<input |
| 70 |
class="full-width" |
| 71 |
name="<?php echo $name; ?>" |
| 72 |
type="text" |
| 73 |
id="<?php echo $name; ?>" |
| 74 |
value="<?php echo htmlspecialchars( $value ); ?>" |
| 75 |
/> |
| 76 |
<?php |
| 77 |
break; |
| 78 |
|
| 79 |
case 'textarea': |
| 80 |
?> |
| 81 |
|
| 82 |
<textarea |
| 83 |
id="<?php echo $name; ?>" |
| 84 |
class="full-width" |
| 85 |
name="<?php echo $name; ?>"><?php echo htmlspecialchars( $value ); ?></textarea> |
| 86 |
|
| 87 |
<?php |
| 88 |
break; |
| 89 |
|
| 90 |
case 'checkbox': |
| 91 |
?> |
| 92 |
|
| 93 |
<input |
| 94 |
class="checkbox" |
| 95 |
name="<?php echo $name; ?>" |
| 96 |
type="checkbox" |
| 97 |
id="<?php echo $name; ?>" |
| 98 |
<?php if ($value) echo ' checked="checked"' ?> |
| 99 |
/> |
| 100 |
|
| 101 |
<?php |
| 102 |
break; |
| 103 |
|
| 104 |
case 'select': |
| 105 |
?> |
| 106 |
<select id="<?php echo $name; ?>" |
| 107 |
name="<?php echo $name; ?>" |
| 108 |
class="full-width"> |
| 109 |
<?php |
| 110 |
foreach ($this->options as $o => $n) { |
| 111 |
?> |
| 112 |
<option value="<?php echo $o; ?>"<?php if ($value == $o) echo ' selected' ?>> |
| 113 |
<?php echo $n; ?> |
| 114 |
</option> |
| 115 |
<?php |
| 116 |
} |
| 117 |
?> |
| 118 |
</select> |
| 119 |
<?php |
| 120 |
break; |
| 121 |
default: |
| 122 |
?> |
| 123 |
<div>No option type chosen for <?php echo $name; ?> with value <?php echo htmlspecialchars($value); ?></div> |
| 124 |
<?php |
| 125 |
break; |
| 126 |
} |
| 127 |
} |
| 128 |
} |