PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 7.0.19
Admin Columns v7.0.19
7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / ListScreenRepository / Database.php
codepress-admin-columns / classes / ListScreenRepository Last commit date
Filter 1 month ago Rule 1 month ago Sort 1 month ago Storage 1 month ago Base.php 1 month ago Database.php 1 month ago Filter.php 1 month ago Rule.php 1 month ago Rules.php 1 month ago Sort.php 1 month ago Storage.php 1 month ago Types.php 1 month ago
Database.php
253 lines
1 <?php
2
3 declare(strict_types=1);
4
5 namespace AC\ListScreenRepository;
6
7 use AC\ColumnFactories\Aggregate;
8 use AC\ColumnIterator;
9 use AC\ColumnIterator\ProxyColumnIterator;
10 use AC\ColumnRepository\EncodedData;
11 use AC\Exception\FailedToSaveListScreen;
12 use AC\ListScreen;
13 use AC\ListScreenCollection;
14 use AC\ListScreenRepositoryWritable;
15 use AC\Setting\ConfigCollection;
16 use AC\Storage\EncoderFactory;
17 use AC\Storage\Repository\OriginalColumnsRepository;
18 use AC\TableScreen;
19 use AC\TableScreenFactory;
20 use AC\Type\ListScreenId;
21 use AC\Type\ListScreenStatus;
22 use AC\Type\TableId;
23 use DateTime;
24
25 class Database extends Base implements ListScreenRepositoryWritable
26 {
27
28 private const TABLE = 'admin_columns';
29
30 private TableScreenFactory $table_screen_factory;
31
32 private EncoderFactory $encoder_factory;
33
34 private Aggregate $column_factory;
35
36 private OriginalColumnsRepository $original_columns_repository;
37
38 public function __construct(
39 TableScreenFactory $table_screen_factory,
40 EncoderFactory $encoder_factory,
41 Aggregate $column_factory,
42 OriginalColumnsRepository $original_columns_repository
43 ) {
44 $this->table_screen_factory = $table_screen_factory;
45 $this->encoder_factory = $encoder_factory;
46 $this->column_factory = $column_factory;
47 $this->original_columns_repository = $original_columns_repository;
48 }
49
50 public function find(ListScreenId $id): ?ListScreen
51 {
52 global $wpdb;
53
54 $sql = $wpdb->prepare(
55 '
56 SELECT *
57 FROM ' . $wpdb->prefix . self::TABLE . '
58 WHERE list_id = %s
59 ',
60 (string)$id
61 );
62
63 $row = $wpdb->get_row($sql);
64
65 if ( ! $row) {
66 return null;
67 }
68
69 return $this->create_list_screen($row);
70 }
71
72 public function find_all(?Sort $sort = null): ListScreenCollection
73 {
74 global $wpdb;
75
76 $sql = '
77 SELECT *
78 FROM ' . $wpdb->prefix . self::TABLE . '
79 ';
80
81 return $this->sort(
82 $this->create_list_screens($wpdb->get_results($sql)),
83 $sort
84 );
85 }
86
87 public function find_all_by_table_id(
88 TableId $table_id,
89 ?Sort $sort = null,
90 ?ListScreenStatus $status = null
91 ): ListScreenCollection {
92 global $wpdb;
93
94 $sql = $wpdb->prepare(
95 '
96 SELECT *
97 FROM ' . $wpdb->prefix . self::TABLE . '
98 WHERE list_key = %s
99 ',
100 (string)$table_id,
101 );
102
103 if ($status) {
104 $is_empty = '' === (string)$status;
105
106 $sql .= $is_empty
107 ? " AND ( status = '' OR STATUS IS NULL )"
108 : $wpdb->prepare(' AND status = %s', (string)$status);
109 }
110
111 return $this->sort(
112 $this->create_list_screens($wpdb->get_results($sql)),
113 $sort
114 );
115 }
116
117 /**
118 * @throws FailedToSaveListScreen
119 */
120 public function save(ListScreen $list_screen): void
121 {
122 global $wpdb;
123
124 $list_screen_dto = $this->encoder_factory
125 ->create()
126 ->set_list_screen($list_screen)
127 ->encode()['list_screen'];
128
129 $settings = $this->save_preferences($list_screen_dto);
130 $date = DateTime::createFromFormat('U', (string)$list_screen_dto['updated']);
131
132 $args = [
133 'list_id' => $list_screen_dto['id'],
134 'list_key' => $list_screen_dto['type'],
135 'title' => $list_screen_dto['title'],
136 'columns' => $list_screen_dto['columns'] ? serialize($list_screen_dto['columns']) : null,
137 'settings' => $settings ? serialize($settings) : null,
138 'date_modified' => $date->format('Y-m-d H:i:s'),
139 'status' => (string)$list_screen_dto['status'],
140 ];
141
142 $table = $wpdb->prefix . self::TABLE;
143 $exists = $this->exists($list_screen->get_id());
144
145 if ($exists) {
146 $wpdb->update(
147 $table,
148 $args,
149 [
150 'list_id' => (string)$list_screen->get_id(),
151 ],
152 array_fill(0, 7, '%s')
153 );
154 } else {
155 $args['date_created'] = $args['date_modified'];
156
157 $wpdb->insert(
158 $table,
159 $args,
160 array_fill(0, 8, '%s')
161 );
162 }
163
164 if ($wpdb->last_error) {
165 throw new FailedToSaveListScreen('Failed to save list screen: ' . $wpdb->last_error);
166 }
167 }
168
169 public function delete(ListScreen $list_screen): void
170 {
171 global $wpdb;
172
173 $wpdb->delete(
174 $wpdb->prefix . self::TABLE,
175 [
176 'list_id' => (string)$list_screen->get_id(),
177 ],
178 [
179 '%s',
180 ]
181 );
182 }
183
184 /**
185 * Template method to add and remove preferences before save
186 */
187 protected function save_preferences(array $list_screen_dto): array
188 {
189 return $list_screen_dto['settings'];
190 }
191
192 /**
193 * Template method to add and remove preferences before retrieval
194 */
195 protected function get_preferences(object $data): array
196 {
197 return $data->settings
198 ? unserialize($data->settings, ['allowed_classes' => false])
199 : [];
200 }
201
202 protected function create_list_screen(object $data): ?ListScreen
203 {
204 $table_id = new TableId($data->list_key);
205
206 if ( ! $this->table_screen_factory->can_create($table_id)) {
207 return null;
208 }
209
210 $table_screen = $this->table_screen_factory->create($table_id);
211
212 return new ListScreen(
213 new ListScreenId($data->list_id),
214 $data->title,
215 $table_screen,
216 $this->create_column_iterator($table_screen, $data),
217 $this->get_preferences($data),
218 new ListScreenStatus($data->status),
219 new DateTime($data->date_modified)
220 );
221 }
222
223 private function create_column_iterator(TableScreen $table_screen, object $data): ColumnIterator
224 {
225 return new ProxyColumnIterator(
226 new EncodedData(
227 $this->column_factory->create($table_screen),
228 $this->create_configs($data),
229 $this->original_columns_repository,
230 $table_screen
231 )
232 );
233 }
234
235 private function create_configs(object $data): ConfigCollection
236 {
237 $columns = $data->columns
238 ? unserialize($data->columns, ['allowed_classes' => false])
239 : [];
240
241 return ConfigCollection::create_from_array($columns);
242 }
243
244 private function create_list_screens(array $rows): ListScreenCollection
245 {
246 $list_screens = array_filter(
247 array_map([$this, 'create_list_screen'], $rows)
248 );
249
250 return new ListScreenCollection($list_screens);
251 }
252
253 }