1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
<?php
class Papi_Property_Dropdown extends Papi_Property {
public function format_value( $value, $slug, $post_id ) {
return papi_cast_string_value( $value );
}
public function get_default_settings() {
return [
'placeholder' => '',
'items' => [],
'selected' => [],
'select2' => true
];
}
protected function get_items() {
return papi_to_array( $this->get_setting( 'items', [] ) );
}
public function html() {
$settings = $this->get_settings();
$value = papi_cast_string_value( $this->get_value() );
$options_html = [];
if ( ! papi_is_empty( $value ) ) {
$settings->selected = $value;
}
$classes = 'papi-fullwidth';
if ( $settings->select2 ) {
$classes .= ' papi-component-select2';
}
if ( ! empty( $settings->placeholder ) ) {
$options_html[] = papi_html_tag( 'option', [
'value' => '',
$settings->placeholder
] );
}
foreach ( $this->get_items() as $key => $value ) {
$key = is_numeric( $key ) ? $value : $key;
if ( papi_is_empty( $key ) ) {
continue;
}
$options_html[] = papi_html_tag( 'option', [
'selected' => $value === $settings->selected ? 'selected' : null,
'value' => $value,
papi_convert_to_string( $key )
] );
}
papi_render_html_tag( 'select', [
'class' => $classes,
'data-allow-clear' => ! empty( $settings->placeholder ),
'data-placeholder' => $settings->placeholder,
'data-width' => '100%',
'id' => $this->html_id(),
'name' => $this->html_name(),
$options_html
] );
}
}