component-bases
1 year ago
filter-hooks
3 years ago
class-fwp-debug.php
1 year ago
class-fwp-html-element.php
3 years ago
class-fwp-html-fields.php
3 years ago
class-fwp-html-fields.php
291 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class FWP_HTML_Fields_1x0x0 |
| 4 | * |
| 5 | * @package FWP |
| 6 | * @category WordPress Library |
| 7 | * @version 1.0.0 |
| 8 | |
| 9 | * @link https://www.webfactoryltd.com/ |
| 10 | */ |
| 11 | class FWP_HTML_Fields_1x0x0 |
| 12 | { |
| 13 | |
| 14 | /** |
| 15 | * @var array |
| 16 | */ |
| 17 | private $values = array(); |
| 18 | |
| 19 | /** |
| 20 | * @var string |
| 21 | */ |
| 22 | private $field_id_format = '%s'; |
| 23 | |
| 24 | /** |
| 25 | * @var string |
| 26 | */ |
| 27 | private $field_name_format = '%s'; |
| 28 | |
| 29 | /** |
| 30 | * Constructor |
| 31 | */ |
| 32 | public function __construct( array $values = array(), $field_id_format = null, $field_name_format = null ) |
| 33 | { |
| 34 | $this->values = $values; |
| 35 | $this->field_id_format = $field_id_format; |
| 36 | $this->field_name_format = $field_name_format; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Get form field id |
| 41 | * @param string $key |
| 42 | * @return string |
| 43 | */ |
| 44 | public function get_field_id( $key ) |
| 45 | { |
| 46 | return sprintf( $this->field_id_format, $key ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Get form field name |
| 51 | * @param string $key |
| 52 | * @return string |
| 53 | */ |
| 54 | public function get_field_name( $key ) |
| 55 | { |
| 56 | return sprintf( $this->field_name_format, $key ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get value |
| 61 | * @param string $key |
| 62 | * @param mixed $default_value Optional |
| 63 | * @return mixed|null |
| 64 | */ |
| 65 | public function get_value( $key, $default_value = null ) |
| 66 | { |
| 67 | $values = $this->values; |
| 68 | |
| 69 | if ( !isset( $values[ $key ] ) ) { |
| 70 | return $default_value; |
| 71 | } |
| 72 | |
| 73 | return $values[ $key ]; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Show html label |
| 78 | * @param string $key |
| 79 | * @param string $label_text |
| 80 | * @param array $atts Optional |
| 81 | */ |
| 82 | public function label( $key, $label_text, array $atts = array() ) |
| 83 | { |
| 84 | WPEL_Plugin::wp_kses_wf('<label for="' . esc_attr($this->get_field_id( $key )) . '" |
| 85 | ' . $this->get_html_atts( $atts ) . ' |
| 86 | >' . esc_attr($label_text) . ' |
| 87 | </label>'); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Show text input field |
| 92 | * @param string $key |
| 93 | * @param array $atts Optional |
| 94 | */ |
| 95 | public function text( $key, array $atts = array() ) |
| 96 | { |
| 97 | WPEL_Plugin::wp_kses_wf('<input type="text" |
| 98 | id="' . esc_attr($this->get_field_id( $key )). '" |
| 99 | name="' . esc_attr($this->get_field_name( $key )) . '" |
| 100 | value="' . esc_attr( $this->get_value( $key ) ) . '" |
| 101 | ' . $this->get_html_atts( $atts ) . ' |
| 102 | >'); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Show text input field |
| 107 | * @param string $key |
| 108 | * @param array $atts Optional |
| 109 | */ |
| 110 | public function text_area( $key, array $atts = array() ) |
| 111 | { |
| 112 | WPEL_Plugin::wp_kses_wf('<textarea id="' . esc_attr($this->get_field_id( $key )) . '" |
| 113 | name="' . esc_attr($this->get_field_name( $key )) . '" |
| 114 | ' . $this->get_html_atts( $atts ) . ' |
| 115 | >'. esc_textarea( $this->get_value( $key ) ) .'</textarea>'); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Show a check field |
| 120 | * @param string $key |
| 121 | * @param mixed $checked_value |
| 122 | * @param mixed $unchecked_value |
| 123 | * @param array $atts Optional |
| 124 | */ |
| 125 | public function check( $key, $checked_value = '1', $unchecked_value = '', array $atts = array() ) |
| 126 | { |
| 127 | // workaround for also posting a value when checkbox is unchecked |
| 128 | if ( null !== $unchecked_value ) { |
| 129 | WPEL_Plugin::wp_kses_wf('<input type="hidden" |
| 130 | name="' . $this->get_field_name( $key ) . '" |
| 131 | value="' . esc_attr( $unchecked_value ) . '" |
| 132 | >'); |
| 133 | } |
| 134 | |
| 135 | WPEL_Plugin::wp_kses_wf('<span class="checkbox-container"><input type="checkbox" |
| 136 | id="' . $this->get_field_id( $key ) . '" |
| 137 | name="' . $this->get_field_name( $key ) . '" |
| 138 | value="' . esc_attr( $checked_value ) . '" |
| 139 | ' . $this->get_checked_attr( $key, $checked_value ) . ' |
| 140 | ' . $this->get_html_atts( $atts ) . ' |
| 141 | ><span class="checkmark"></span></span>'); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Show a check field with label |
| 146 | * @param string $key |
| 147 | * @param string $label_text |
| 148 | * @param mixed $checked_value |
| 149 | * @param mixed $unchecked_value |
| 150 | * @param array $atts Optional |
| 151 | */ |
| 152 | public function check_with_label( $key, $label_text, $checked_value, $unchecked_value = null, array $atts = array() ) |
| 153 | { |
| 154 | echo '<label>'; |
| 155 | $this->check( $key, $checked_value, $unchecked_value, $atts ); |
| 156 | WPEL_Plugin::wp_kses_wf($label_text); |
| 157 | echo '</label>'; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Show a radio field |
| 162 | * @param string $key |
| 163 | * @param mixed $checked_value |
| 164 | * @param array $atts Optional |
| 165 | */ |
| 166 | public function radio( $key, $checked_value, array $atts = array() ) |
| 167 | { |
| 168 | $id = $this->get_field_id( $key ) . '-' . sanitize_key( $checked_value ); |
| 169 | |
| 170 | WPEL_Plugin::wp_kses_wf('<span class="radio-container"><input type="radio" |
| 171 | id="' . $id . '" |
| 172 | name="' . $this->get_field_name( $key ) . '" |
| 173 | value="' . esc_attr( $checked_value ) . '" |
| 174 | ' . $this->get_checked_attr( $key, $checked_value ) . ' |
| 175 | ' . $this->get_html_atts( $atts ) . ' |
| 176 | ><span class="radio"></span></span>'); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Show a check field with label |
| 181 | * @param string $key |
| 182 | * @param string $label_text |
| 183 | * @param mixed $checked_value |
| 184 | * @param array $atts Optional |
| 185 | */ |
| 186 | public function radio_with_label( $key, $label_text, $checked_value, array $atts = array() ) |
| 187 | { |
| 188 | echo '<label>'; |
| 189 | $this->radio( $key, $checked_value, $atts ); |
| 190 | WPEL_Plugin::wp_kses_wf($label_text); |
| 191 | echo '</label>'; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Show select field with or without options |
| 196 | * @param string $key |
| 197 | * @param mixed $checked_value |
| 198 | * @param array $options |
| 199 | * @param array $atts Optional |
| 200 | */ |
| 201 | public function select( $key, array $options = array(), array $atts = array() ) |
| 202 | { |
| 203 | $value = $this->get_value( $key ); |
| 204 | |
| 205 | WPEL_Plugin::wp_kses_wf('<select id="' . $this->get_field_id( $key ) . '" |
| 206 | name="' . $this->get_field_name( $key ) . '" |
| 207 | ' . $this->get_html_atts( $atts ) . ' |
| 208 | >'); |
| 209 | |
| 210 | foreach ( $options as $option_value => $option_text ) { |
| 211 | $this->select_option( $option_text, $option_value, ( $value == $option_value ) ); |
| 212 | } |
| 213 | |
| 214 | echo '</select>'; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Show a select option |
| 219 | * @param string $text |
| 220 | * @param string $value |
| 221 | * @param boolean $selected |
| 222 | */ |
| 223 | public function select_option( $text, $value, $selected = false ) |
| 224 | { |
| 225 | WPEL_Plugin::wp_kses_wf('<option value="' . esc_attr( $value ) . '"' . ( $selected ? ' selected' : '' ) . '> |
| 226 | ' . $text . ' |
| 227 | </option>'); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * @param array $atts |
| 232 | * @return string |
| 233 | */ |
| 234 | private function get_html_atts( array $atts ) |
| 235 | { |
| 236 | $html_atts = ''; |
| 237 | |
| 238 | foreach ( $atts as $key => $value ) { |
| 239 | if ( null === $value ) { |
| 240 | $html_atts .= ' '. $key; |
| 241 | } else { |
| 242 | $html_atts .= ' '. $key .'="'. esc_attr( $value ) .'"'; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | return $html_atts; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Get the checked attribute |
| 251 | * @param string $key |
| 252 | * @param mixed $checked_value |
| 253 | * @return string |
| 254 | */ |
| 255 | private function get_checked_attr( $key, $checked_value ) |
| 256 | { |
| 257 | return ( $this->get_value( $key ) == $checked_value ) ? ' checked' : ''; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Show text input field |
| 262 | * @param string $key |
| 263 | * @param array $atts Optional |
| 264 | */ |
| 265 | public function number( $key, array $atts = array() ) |
| 266 | { |
| 267 | WPEL_Plugin::wp_kses_wf('<input type="number" |
| 268 | id="' . $this->get_field_id( $key ) . '" |
| 269 | name="' . $this->get_field_name( $key ) . '" |
| 270 | value="' . esc_attr( $this->get_value( $key ) ) . '" |
| 271 | ' . $this->get_html_atts( $atts ) . ' |
| 272 | > ' . $atts['unit']); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Show text input field |
| 277 | * @param string $key |
| 278 | * @param array $atts Optional |
| 279 | */ |
| 280 | public function color( $key, array $atts = array() ) |
| 281 | { |
| 282 | WPEL_Plugin::wp_kses_wf('<input type="text" class="wpel-colorpicker" |
| 283 | id="' . $this->get_field_id( $key ) . '" |
| 284 | name="' . $this->get_field_name( $key ) . '" |
| 285 | value="' . esc_attr( $this->get_value( $key ) ) . '" |
| 286 | ' . $this->get_html_atts( $atts ) . ' |
| 287 | >'); |
| 288 | } |
| 289 | |
| 290 | } |
| 291 |