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