SerializedArray.php
1.73 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
65
66
67
68
69
70
71
72
73
74
<?php
namespace ACP\Settings\Column;
use AC;
use AC\View;
class SerializedArray extends AC\Settings\Column implements AC\Settings\FormatValue {
/**
* @var string
*/
private $array_keys;
protected function set_name() {
$this->name = 'array_keys';
}
protected function define_options() {
return [ 'array_keys' ];
}
public function create_view() {
$setting = $this->create_element( 'text' )
->set_attribute( 'data-label', 'update' )
->set_attribute( 'placeholder', sprintf( '%s: %s', __( 'example', 'codepress-admin-columns' ), 'sizes.medium.file' ) );
$instructions = ( new View() )->set_template( 'tooltip/serialized' );
return new View( [
'setting' => $setting,
'label' => __( 'Array Keys', 'codepress-admin-columns' ),
'instructions' => $instructions->render(),
] );
}
public function set_array_keys( $keys ) {
$this->array_keys = $keys;
}
public function get_array_keys() {
return $this->array_keys;
}
public function get_keys() {
return array_filter( array_map( 'trim', explode( '.', $this->array_keys ) ) );
}
private function get_expanded_level() {
return (int) apply_filters( 'acp/column/settings/serialized/expanded_level', 1, $this->column );
}
public function format( $value, $original_value ) {
if ( ! is_array( $value ) ) {
return $value;
}
$value = ac_helper()->array->get_nested_value(
$value,
$this->get_keys()
);
if ( ac_helper()->array->is_associative( $value ) ) {
return sprintf(
'<div data-component="ac-json" data-json="%s" data-level="%s" ></div>',
esc_attr( json_encode( $value ) ),
$this->get_expanded_level()
);
}
return ac_helper()->array->implode_recursive( __( ', ' ), $value );
}
}