EditableDataFactory.php
2.58 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace ACP\Editing;
use AC\Column;
use ACP\Editing\Factory\BulkEditFactory;
use ACP\Editing\Factory\InlineEditFactory;
/**
* Get all data settings needed to load editing for the WordPress list table
*/
class EditableDataFactory {
/**
* @var InlineEditFactory
*/
private $inline_edit_factory;
/**
* @var BulkEditFactory
*/
private $bulk_edit_factory;
public function __construct( InlineEditFactory $inline_edit_factory, BulkEditFactory $bulk_edit_factory ) {
$this->inline_edit_factory = $inline_edit_factory;
$this->bulk_edit_factory = $bulk_edit_factory;
}
public function create() {
$data = [];
foreach ( $this->inline_edit_factory->create() as $column ) {
$column_data = $this->create_data_by_column( $column, Service::CONTEXT_SINGLE );
if ( $column_data ) {
$data[ $column->get_name() ]['type'] = $column->get_type();
$data[ $column->get_name() ]['inline_edit'] = $column_data;
}
}
foreach ( $this->bulk_edit_factory->create() as $column ) {
$column_data = $this->create_data_by_column( $column, Service::CONTEXT_BULK );
if ( $column_data ) {
$data[ $column->get_name() ]['type'] = $column->get_type();
$data[ $column->get_name() ]['bulk_edit'] = $column_data;
}
}
return $data;
}
/**
* @param Column $column
* @param string $context
*
* @return array|null
*/
private function create_data_by_column( Column $column, $context ) {
$service = ServiceFactory::create( $column );
$filter = new ApplyFilter\View( $column, $context, $service );
$view = $filter->apply_filters( $service->get_view( $context ) );
if ( ! $view instanceof View ) {
return null;
}
$data = $view->get_args();
$data = apply_filters_deprecated( 'acp/editing/view_settings', [ $data, $column ], '5.7', "acp/editing/view" );
$data = apply_filters_deprecated( 'acp/editing/view_settings/' . $column->get_type(), [ $data, $column ], '5.7', "acp/editing/view" );
if ( ! is_array( $data ) ) {
return null;
}
if ( isset( $data['options'] ) ) {
$data['options'] = $this->format_js( $data['options'] );
}
return $data;
}
/**
* @param array $list
*
* @return array
*/
private function format_js( $list ) {
$options = [];
if ( $list ) {
foreach ( $list as $index => $option ) {
if ( is_array( $option ) && isset( $option['options'] ) ) {
$option['options'] = $this->format_js( $option['options'] );
$options[] = $option;
} else if ( is_scalar( $option ) ) {
$options[] = [
'value' => $index,
'label' => html_entity_decode( $option ),
];
}
}
}
return $options;
}
}