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