Assets.php
3.63 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php declare( strict_types=1 );
namespace ACP\ConditionalFormat\Service;
use AC;
use AC\Asset\Location;
use AC\Column;
use AC\ColumnRepository;
use AC\ListScreen;
use AC\Registerable;
use AC\Type\ListScreenId;
use AC\Type\UserId;
use ACP\ConditionalFormat;
use ACP\ConditionalFormat\ColumnRepository\FilterByConditionalFormat;
use ACP\ConditionalFormat\Operators;
use ACP\ConditionalFormat\RuleCollection;
use ACP\ConditionalFormat\RulesRepositoryFactory;
use ACP\ConditionalFormat\Settings\ListScreen\HideOnScreenFactory;
final class Assets implements Registerable {
/**
* @var Location\Absolute
*/
private $location;
/**
* @var Operators
*/
private $operators;
/**
* @var RulesRepositoryFactory
*/
private $rules_repository_factory;
/**
* @var HideOnScreenFactory
*/
private $hide_on_screen_factory;
public function __construct(
Location\Absolute $location,
Operators $operators,
RulesRepositoryFactory $rules_repository_factory,
HideOnScreenFactory $hide_on_screen_factory
) {
$this->location = $location;
$this->operators = $operators;
$this->rules_repository_factory = $rules_repository_factory;
$this->hide_on_screen_factory = $hide_on_screen_factory;
}
private function is_enabled( ListScreen $list_screen ): bool {
return ! $this->hide_on_screen_factory->create()->is_hidden( $list_screen );
}
private function get_column_labels( array $columns ): array {
$data = [];
foreach ( $columns as $column ) {
$data[ $column->get_name() ] = [
'label' => $this->get_column_label( $column ),
'operators' => [],
];
}
return $data;
}
private function get_column_label( Column $column ): string {
$label = $this->sanitize_column_label( $column->get_custom_label() );
if ( ! $label ) {
$label = $this->sanitize_column_label( $column->get_label() );
if ( ! $label ) {
$label = $column->get_type();
}
}
return $label;
}
/**
* Allows plain text and dashicons
*/
private function sanitize_column_label( string $label ): string {
if ( false === strpos( $label, 'dashicons' ) ) {
$label = strip_tags( $label );
}
return trim( $label );
}
public function register(): void {
add_action( 'ac/admin_scripts', function ( $page ) {
if ( ! $page instanceof AC\Admin\Page\Columns ) {
return;
}
$assets = [
new AC\Asset\Style( 'acp-cf-settings', $this->location->with_suffix( 'assets/conditional-format/css/settings.css' ) ),
];
foreach ( $assets as $asset ) {
$asset->enqueue();
}
} );
add_action( 'ac/table_scripts', function ( ListScreen $list_screen ) {
if ( ! $this->is_enabled( $list_screen ) || ! $list_screen->has_id() ) {
return;
}
$columns = ( new ColumnRepository( $list_screen ) )->find_all( [
'filter' => new FilterByConditionalFormat(),
] );
$assets = [
new ConditionalFormat\Asset\Table(
$this->location->with_suffix( 'assets/conditional-format/js/table.js' ),
$this->operators,
$this->get_rules( $list_screen->get_id(), array_keys( $columns ) ),
$this->get_column_labels( $columns )
),
new AC\Asset\Style( 'acp-cf-table', $this->location->with_suffix( 'assets/conditional-format/css/table.css' ) ),
];
foreach ( $assets as $asset ) {
$asset->enqueue();
}
} );
}
private function get_rules( ListScreenId $list_id, array $column_names ): RuleCollection {
$filtered = new RuleCollection();
$rules = $this->rules_repository_factory
->create( $list_id )
->find( new UserId( get_current_user_id() ) );
foreach ( $rules as $rule ) {
if ( in_array( $rule->get_column_name(), $column_names, true ) ) {
$filtered->add( $rule );
}
}
return $filtered;
}
}