ColumnRequest.php
1.87 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
<?php
namespace AC\Controller;
use AC;
use AC\Column;
use AC\Column\Placeholder;
use AC\ListScreen;
use AC\Request;
use AC\View;
abstract class ColumnRequest {
private $list_screen_factory;
public function __construct( AC\ListScreenFactory $list_screen_factory ) {
$this->list_screen_factory = $list_screen_factory;
}
abstract protected function get_column( Request $request, ListScreen $list_screen ): ?Column;
public function request( Request $request ): void {
$list_key = (string) $request->get( 'list_screen' );
if ( ! $this->list_screen_factory->can_create( $list_key ) ) {
exit;
}
$list_screen = $this->list_screen_factory->create( $list_key );
$column = $this->get_column( $request, $list_screen );
if ( ! $column ) {
wp_send_json_error( [
'type' => 'message',
'error' => sprintf( __( 'Please visit the %s screen once to load all available columns', 'codepress-admin-columns' ), ac_helper()->html->link( $list_screen->get_screen_link(), $list_screen->get_label() ) ),
] );
}
$current_original_columns = (array) json_decode( $request->get( 'current_original_columns', '' ), true );
// Not cloneable message
if ( in_array( $column->get_type(), $current_original_columns, true ) ) {
wp_send_json_error( [
'type' => 'message',
'error' => sprintf(
__( '%s column is already present and can not be duplicated.', 'codepress-admin-columns' ),
'<strong>' . $column->get_label() . '</strong>' ),
] );
}
// Placeholder message
if ( $column instanceof Placeholder ) {
wp_send_json_error( [
'type' => 'message',
'error' => $column->get_message(),
] );
}
wp_send_json_success( $this->render_column( $column ) );
}
private function render_column( Column $column ): string {
$view = new View( [
'column' => $column,
] );
$view->set_template( 'admin/edit-column' );
return $view->render();
}
}