1: <?php
2:
3: 4: 5: 6:
7: class Papi_Property_Checkbox extends Papi_Property {
8:
9: 10: 11: 12: 13:
14: public $convert_type = 'array';
15:
16: 17: 18: 19: 20:
21: public $default_value = [];
22:
23: 24: 25: 26: 27: 28: 29: 30: 31: 32:
33: public function format_value( $value, $slug, $post_id ) {
34: if ( is_string( $value ) && ! papi_is_empty( $value ) ) {
35: return [papi_cast_string_value( $value )];
36: }
37:
38: if ( ! is_array( $value ) ) {
39: return $this->default_value;
40: }
41:
42: return array_map( 'papi_cast_string_value', $value );
43: }
44:
45: 46: 47: 48: 49:
50: public function get_default_settings() {
51: return [
52: 'items' => [],
53: 'selected' => []
54: ];
55: }
56:
57: 58: 59:
60: public function html() {
61: $settings = $this->get_settings();
62: $value = papi_cast_string_value( $this->get_value() );
63:
64:
65:
66: if ( ! papi_is_empty( $value ) ) {
67: $settings->selected = $value;
68: }
69:
70: $settings->selected = papi_to_array( $settings->selected );
71:
72: foreach ( $settings->items as $key => $value ) {
73: $key = is_numeric( $key ) ? $value : $key;
74:
75: papi_render_html_tag( 'label', [
76: 'class' => 'light',
77: 'for' => $this->html_id( $key ),
78:
79: papi_html_tag( 'input', [
80: 'checked' => in_array( $value, $settings->selected ) ? 'checked' : null,
81: 'id' => $this->html_id( $key ),
82: 'name' => $this->html_name() . '[]',
83: 'type' => 'checkbox',
84: 'value' => $value
85: ] ),
86:
87: papi_convert_to_string( $key )
88: ] );
89:
90: papi_render_html_tag( 'br' );
91: }
92: }
93:
94: 95: 96: 97: 98: 99: 100: 101: 102:
103: public function import_value( $value, $slug, $post_id ) {
104: if ( is_string( $value ) && ! papi_is_empty( $value ) ) {
105: return [$value];
106: }
107:
108: if ( ! is_array( $value ) ) {
109: return;
110: }
111:
112: return $value;
113: }
114: }
115: