Database.php
4.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
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
<?php
namespace AC\ListScreenRepository;
use AC\Exception\MissingListScreenIdException;
use AC\ListScreen;
use AC\ListScreenCollection;
use AC\ListScreenRepositoryWritable;
use AC\ListScreenTypes;
use AC\Type\ListScreenId;
use DateTime;
use LogicException;
final class Database implements ListScreenRepositoryWritable {
const TABLE = 'admin_columns';
/**
* @var ListScreenTypes
*/
private $list_screen_types;
public function __construct( ListScreenTypes $list_screen_types ) {
$this->list_screen_types = $list_screen_types;
}
/**
* @param array $args
*
* @return array
*/
private function find_all_from_database( array $args = [] ) {
global $wpdb;
$args = array_merge( [
self::KEY => null,
], $args );
$sql = '
SELECT *
FROM ' . $wpdb->prefix . self::TABLE . '
WHERE 1=1
';
$where = [];
if ( $args[ self::KEY ] ) {
$where[] = $wpdb->prepare( 'AND list_key = %s', $args[ self::KEY ] );
}
$sql .= implode( "\n", $where );
return $wpdb->get_results( $sql );
}
/**
* @param array $args
*
* @return ListScreenCollection
*/
public function find_all( array $args = [] ) {
$list_screens = new ListScreenCollection();
foreach ( $this->find_all_from_database( $args ) as $list_data ) {
$list_screen = $this->create_list_screen( $list_data );
if ( $list_screen instanceof ListScreen ) {
$list_screens->add( $list_screen );
}
}
return $list_screens;
}
/**
* @param ListScreenId $id
*
* @return object|null
*/
private function find_from_database( ListScreenId $id ) {
global $wpdb;
$sql = '
SELECT *
FROM ' . $wpdb->prefix . self::TABLE . '
WHERE list_id = %s
LIMIT 1;
';
$data = $wpdb->get_row( $wpdb->prepare( $sql, $id->get_id() ) );
if ( ! isset( $data->list_id ) ) {
return null;
}
return $data;
}
/**
* @param ListScreenId $id
*
* @return ListScreen|null
*/
public function find( ListScreenId $id ) {
$data = $this->find_from_database( $id );
if ( ! $data ) {
return null;
}
return $this->create_list_screen( $data );
}
/**
* @param ListScreenId $list_screen_id
*
* @return bool
*/
public function exists( ListScreenId $list_screen_id ) {
return null !== $this->find_from_database( $list_screen_id );
}
/**
* @param ListScreen $list_screen
*
* @return void
*/
public function save( ListScreen $list_screen ) {
global $wpdb;
if ( ! $list_screen->has_id() ) {
throw MissingListScreenIdException::from_saving_list_screen();
}
$args = [
'list_id' => $list_screen->get_layout_id(),
'list_key' => $list_screen->get_key(),
'title' => $list_screen->get_title(),
'columns' => $list_screen->get_settings() ? serialize( $list_screen->get_settings() ) : null,
'settings' => $list_screen->get_preferences() ? serialize( $list_screen->get_preferences() ) : null,
'date_modified' => $list_screen->get_updated()->format( 'Y-m-d H:i:s' ),
];
$table = $wpdb->prefix . self::TABLE;
$stored = $this->find_from_database( $list_screen->get_id() );
if ( $stored ) {
$wpdb->update(
$table,
$args,
[
'id' => $stored->id,
],
array_fill( 0, 6, '%s' ),
[
'%d',
]
);
} else {
$args['date_created'] = $args['date_modified'];
$wpdb->insert(
$table,
$args,
array_fill( 0, 7, '%s' )
);
}
}
public function delete( ListScreen $list_screen ) {
global $wpdb;
if ( ! $list_screen->has_id() ) {
throw new LogicException( 'Cannot delete a ListScreen without an identity.' );
}
/**
* Fires before a column setup is removed from the database
* Primarily used when columns are deleted through the Admin Columns settings screen
*
* @param ListScreen $list_screen
*
* @deprecated 4.0
* @since 3.0.8
*/
do_action( 'ac/columns_delete', $list_screen );
$wpdb->delete(
$wpdb->prefix . self::TABLE,
[
'list_id' => $list_screen->get_id()->get_id(),
],
[
'%s',
]
);
}
/**
* @param $data
*
* @return ListScreen
*/
private function create_list_screen( $data ) {
$list_screen = $this->list_screen_types->get_list_screen_by_key( $data->list_key );
if ( $list_screen ) {
$list_screen->set_title( $data->title )
->set_layout_id( $data->list_id )
->set_updated( DateTime::createFromFormat( 'Y-m-d H:i:s', $data->date_modified ) );
if ( $data->settings ) {
$list_screen->set_preferences( unserialize( $data->settings, [ 'allowed_classes' => false ] ) ?: [] );
}
if ( $data->columns ) {
$list_screen->set_settings( unserialize( $data->columns, [ 'allowed_classes' => false ] ) ?: [] );
}
}
return $list_screen;
}
}