Columns.php
9.5 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
declare( strict_types=1 );
namespace AC\Admin\Page;
use AC\Admin;
use AC\Admin\Banner;
use AC\Admin\RenderableHead;
use AC\Admin\ScreenOption;
use AC\Admin\Section\Partial\Menu;
use AC\Asset\Assets;
use AC\Asset\Enqueueables;
use AC\Asset\Location;
use AC\Asset\Script;
use AC\Asset\Style;
use AC\Column;
use AC\DefaultColumnsRepository;
use AC\ListScreen;
use AC\Renderable;
use AC\Type\Url;
use AC\Type\Url\Documentation;
use AC\Type\Url\Site;
use AC\Type\Url\Tweet;
use AC\Type\Url\UtmTags;
use AC\View;
class Columns implements Enqueueables, Admin\ScreenOptions, Renderable, RenderableHead {
public const NAME = 'columns';
private $location;
private $list_screen;
private $default_columns_repository;
private $list_screen_uninitialized;
private $menu;
private $head;
private $is_acp_active;
public function __construct(
Location\Absolute $location,
ListScreen $list_screen,
DefaultColumnsRepository $default_columns_repository,
Admin\ListScreenUninitialized $list_screen_uninitialized,
Menu $menu,
Renderable $head,
bool $is_acp_active
) {
$this->location = $location;
$this->list_screen = $list_screen;
$this->default_columns_repository = $default_columns_repository;
$this->list_screen_uninitialized = $list_screen_uninitialized;
$this->menu = $menu;
$this->head = $head;
$this->is_acp_active = $is_acp_active;
}
public function get_list_screen(): ListScreen {
return $this->list_screen;
}
public function render_head() {
return $this->head;
}
public function get_assets() {
return new Assets( [
new Style( 'jquery-ui-lightness', $this->location->with_suffix( 'assets/ui-theme/jquery-ui-1.8.18.custom.css' ) ),
new Script( 'jquery-ui-slider' ),
new Admin\Asset\Columns(
'ac-admin-page-columns',
$this->location->with_suffix( 'assets/js/admin-page-columns.js' ),
$this->list_screen_uninitialized->find_all(),
$this->list_screen->get_key(),
$this->list_screen->has_id() ? $this->list_screen->get_id()->get_id() : ''
),
new Style( 'ac-admin-page-columns-css', $this->location->with_suffix( 'assets/css/admin-page-columns.css' ) ),
new Style( 'ac-select2' ),
new Script( 'ac-select2' ),
] );
}
private function get_column_id(): ScreenOption\ColumnId {
return new ScreenOption\ColumnId( new Admin\Preference\ScreenOptions() );
}
private function get_column_type(): ScreenOption\ColumnType {
return new ScreenOption\ColumnType( new Admin\Preference\ScreenOptions() );
}
private function get_list_screen_id(): ScreenOption\ListScreenId {
return new ScreenOption\ListScreenId( new Admin\Preference\ScreenOptions() );
}
private function get_list_screen_type(): ScreenOption\ListScreenType {
return new ScreenOption\ListScreenType( new Admin\Preference\ScreenOptions() );
}
public function get_screen_options(): array {
return [
$this->get_column_id(),
$this->get_column_type(),
$this->get_list_screen_id(),
$this->get_list_screen_type(),
];
}
private function get_tweet_url(): Url {
return new Tweet(
__( "I'm using Admin Columns for WordPress!", 'codepress-admin-columns' ),
new Url\WordpressPluginRepo(),
Tweet::TWITTER_HANDLE,
'admincolumns'
);
}
public function render() {
if ( ! $this->default_columns_repository->exists( $this->list_screen->get_key() ) ) {
$modal = new View( [
'message' => 'Loading columns',
] );
$modal->set_template( 'admin/loading-message' );
return $this->menu->render( $this->list_screen->get_key(), $this->list_screen->get_screen_link(), true ) . $modal->render();
}
$classes = [];
if ( $this->list_screen->get_settings() ) {
$classes[] = 'stored';
}
if ( $this->get_list_screen_id()->is_active() ) {
$classes[] = 'show-list-screen-id';
}
if ( $this->get_list_screen_type()->is_active() ) {
$classes[] = 'show-list-screen-type';
}
ob_start();
?>
<h1 class="screen-reader-text"><?= __( 'Columns', 'codepress-admin-columns' ); ?></h1>
<div class="ac-admin <?= esc_attr( implode( ' ', $classes ) ); ?>" data-type="<?= esc_attr( $this->list_screen->get_key() ); ?>">
<div class="ac-admin__header">
<?= $this->menu->render( $this->list_screen->get_key(), $this->list_screen->get_screen_link() ) ?>
<?php do_action( 'ac/settings/after_title', $this->list_screen ); ?>
</div>
<div class="ac-admin__wrap">
<div class="ac-admin__sidebar">
<?php if ( ! $this->list_screen->is_read_only() ) : ?>
<?php
$label_main = __( 'Store settings', 'codepress-admin-columns' );
$label_second = sprintf( '<span class="clear contenttype">%s</span>', esc_html( $this->list_screen->get_label() ) );
if ( 18 > strlen( $label_main ) && ( $truncated_label = $this->get_truncated_side_label( $this->list_screen->get_label(), $label_main ) ) ) {
$label_second = sprintf( '<span class="right contenttype">%s</span>', esc_html( $truncated_label ) );
}
$delete_confirmation_message = false;
if ( apply_filters( 'ac/delete_confirmation', true ) ) {
$delete_confirmation_message = sprintf( __( "Warning! The %s columns data will be deleted. This cannot be undone. 'OK' to delete, 'Cancel' to stop", 'codepress-admin-columns' ), "'" . $this->list_screen->get_title() . "'" );
}
$actions = new View( [
'label_main' => $label_main,
'label_second' => $label_second,
'list_screen_key' => $this->list_screen->get_key(),
'list_screen_id' => $this->list_screen->get_layout_id(),
'delete_confirmation_message' => $delete_confirmation_message,
] );
echo $actions->set_template( 'admin/edit-actions' );
endif; ?>
<?php do_action( 'ac/settings/sidebox', $this->list_screen ); ?>
<?php if ( apply_filters( 'ac/show_banner', true ) ) : ?>
<?= new Banner() ?>
<?php
$view = new View( [
'documentation_url' => ( new UtmTags( new Documentation(), 'feedback-docs-button' ) )->get_url(),
'upgrade_url' => ( new UtmTags( new Site( Site::PAGE_ABOUT_PRO ), 'feedback-purchase-button' ) )->get_url(),
'tweet_url' => $this->get_tweet_url()->get_url(),
'review_url' => ( new Url\WordpressPluginReview() )->get_url(),
] );
echo $view->set_template( 'admin/side-feedback' );
?>
<?php endif; ?>
<?php
$view = new View( [
'documentation_url' => ( new UtmTags( new Documentation(), 'support' ) )->get_url(),
] );
echo $view->set_template( 'admin/side-support' );
?>
</div>
<div class="ac-admin__main">
<?php do_action( 'ac/settings/notice', $this->list_screen ); ?>
<div id="listscreen_settings" data-form="listscreen" class="<?= $this->list_screen->is_read_only() ? '-disabled' : ''; ?>">
<?php
$classes = [];
if ( $this->list_screen->is_read_only() ) {
$classes[] = 'disabled';
}
if ( $this->get_column_id()->is_active() ) {
$classes[] = 'show-column-id';
}
if ( $this->get_column_type()->is_active() ) {
$classes[] = 'show-column-type';
}
$columns = new View( [
'class' => implode( ' ', $classes ),
'list_screen' => $this->list_screen->get_key(),
'list_screen_id' => $this->list_screen->get_layout_id(),
'title' => $this->list_screen->get_title(),
'columns' => $this->list_screen->get_columns(),
'show_actions' => ! $this->list_screen->is_read_only(),
'show_clear_all' => apply_filters( 'ac/enable_clear_columns_button', false ),
] );
do_action( 'ac/settings/before_columns', $this->list_screen );
echo $columns->set_template( 'admin/edit-columns' );
do_action( 'ac/settings/after_columns', $this->list_screen );
if ( ! $this->is_acp_active ) {
echo ( new View() )->set_template( 'admin/list-screen-settings-mockup' )->render();
}
?>
</div>
</div>
</div>
<div id="add-new-column-template">
<?= $this->render_column_template( $this->list_screen ) ?>
</div>
</div>
<div class="clear"></div>
<?php
$modal = new View( [
'upgrade_url' => ( new UtmTags( new Site( Site::PAGE_ABOUT_PRO ), 'upgrade' ) )->get_url(),
] );
echo $modal->set_template( 'admin/modal-pro' );
return ob_get_clean();
}
private function get_column_template_by_group( array $column_types, string $group = '' ): ?Column {
if ( ! $group ) {
return array_shift( $column_types ) ?: null;
}
$columns = [];
foreach ( $column_types as $column_type ) {
if ( $group === $column_type->get_group() ) {
$columns[ $column_type->get_label() ] = $column_type;
}
}
$column_keys = array_keys( $columns );
array_multisort( $column_keys, SORT_NATURAL, $columns );
$column = array_shift( $columns );
if ( ! $column ) {
return null;
}
return $column;
}
private function render_column_template( ListScreen $list_screen ): string {
$column = $this->get_column_template_by_group( $list_screen->get_column_types(), 'custom' );
if ( ! $column ) {
$column = $this->get_column_template_by_group( $list_screen->get_column_types() );
}
$view = new View( [
'column' => $column,
] );
return $view->set_template( 'admin/edit-column' )->render();
}
/**
* @param string $label
* @param string $main_label
*
* @return string
*/
private function get_truncated_side_label( $label, $main_label = '' ) {
if ( 34 < ( strlen( $label ) + ( strlen( $main_label ) * 1.1 ) ) ) {
$label = substr( $label, 0, absint( 34 - ( strlen( $main_label ) * 1.1 ) ) ) . '...';
}
return $label;
}
}