CheckboxList.php
1.67 KB
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
<?php
namespace ACA\MetaBox\Column;
use AC\Settings\Column\NumberOfItems;
use ACA\MetaBox\Column;
use ACA\MetaBox\Editing\StorageFactory;
use ACA\MetaBox\Search;
use ACP;
class CheckboxList extends Column
implements ACP\Search\Searchable, ACP\Editing\Editable, ACP\ConditionalFormat\Formattable {
use ACP\ConditionalFormat\ConditionalFormatTrait;
public function get_raw_value( $id ) {
$value = $this->get_meta_value( $id, $this->get_meta_key(), false );
return $value ?: false;
}
public function format_single_value( $value, $id = null ) {
if ( ! $value ) {
return $this->get_empty_char();
}
// MetaBox sometimes stores extra empty options, not need to show them
$value = array_filter( $value );
$values = [];
foreach ( $value as $key ) {
$values[] = $this->get_label_for_option( $key );
}
$setting_limit = $this->get_setting( 'number_of_items' );
return ac_helper()->html->more( $values, $setting_limit ? $setting_limit->get_value() : false );
}
protected function get_label_for_option( $key ) {
$options = $this->get_field_options();
return isset( $options[ $key ] ) ? $options[ $key ] : $key;
}
public function get_field_options() {
return $this->get_field_setting( 'options' );
}
protected function register_settings() {
$this->add_setting( new NumberOfItems( $this ) );
}
public function editing() {
return $this->is_clonable() ? false : new ACP\Editing\Service\Basic(
( new ACP\Editing\View\CheckboxList( $this->get_field_setting( 'options' ) ) )->set_clear_button( true ),
( new StorageFactory() )->create( $this, false )
);
}
public function search() {
return ( new Search\Factory\CheckboxList() )->create( $this );
}
}