PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 4.7.7
Admin Columns v4.7.7
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 2 years ago Rule 2 years ago Sort 2 years ago Storage 2 years ago Database.php 2 years ago Filter.php 2 years ago ListScreenRepositoryTrait.php 2 years ago Rule.php 2 years ago Rules.php 2 years ago Sort.php 2 years ago Storage.php 2 years ago Types.php 2 years ago
Database.php
192 lines
1 <?php
2
3 declare(strict_types=1);
4
5 namespace AC\ListScreenRepository;
6
7 use AC\Exception\MissingListScreenIdException;
8 use AC\ListScreen;
9 use AC\ListScreenCollection;
10 use AC\ListScreenFactory;
11 use AC\ListScreenRepositoryWritable;
12 use AC\Type\ListScreenId;
13 use LogicException;
14
15 class Database implements ListScreenRepositoryWritable
16 {
17
18 use ListScreenRepositoryTrait;
19
20 private const TABLE = 'admin_columns';
21
22 private $list_screen_factory;
23
24 public function __construct(ListScreenFactory $list_screen_factory)
25 {
26 $this->list_screen_factory = $list_screen_factory;
27 }
28
29 protected function find_from_source(ListScreenId $id): ?ListScreen
30 {
31 global $wpdb;
32
33 $sql = $wpdb->prepare(
34 '
35 SELECT *
36 FROM ' . $wpdb->prefix . self::TABLE . '
37 WHERE list_id = %s
38 ',
39 (string)$id
40 );
41
42 $row = $wpdb->get_row($sql);
43
44 if ( ! $row) {
45 return null;
46 }
47
48 return $this->create_list_screen($row);
49 }
50
51 protected function find_all_from_source(): ListScreenCollection
52 {
53 global $wpdb;
54
55 $sql = '
56 SELECT *
57 FROM ' . $wpdb->prefix . self::TABLE . '
58 ';
59
60 return $this->create_list_screens(
61 $wpdb->get_results($sql)
62 );
63 }
64
65 protected function find_all_by_key_from_source(string $key): ListScreenCollection
66 {
67 global $wpdb;
68
69 $sql = $wpdb->prepare(
70 '
71 SELECT *
72 FROM ' . $wpdb->prefix . self::TABLE . '
73 WHERE list_key = %s
74 ',
75 $key
76 );
77
78 return $this->create_list_screens(
79 $wpdb->get_results($sql)
80 );
81 }
82
83 public function save(ListScreen $list_screen): void
84 {
85 global $wpdb;
86
87 if ( ! $list_screen->has_id()) {
88 throw MissingListScreenIdException::from_saving_list_screen();
89 }
90
91 $list_screen->set_preferences($this->save_preferences($list_screen));
92
93 $args = [
94 'list_id' => (string)$list_screen->get_id(),
95 'list_key' => $list_screen->get_key(),
96 'title' => $list_screen->get_title(),
97 'columns' => $list_screen->get_settings() ? serialize($list_screen->get_settings()) : null,
98 'settings' => $list_screen->get_preferences() ? serialize($list_screen->get_preferences()) : null,
99 'date_modified' => $list_screen->get_updated()->format('Y-m-d H:i:s'),
100 ];
101
102 $table = $wpdb->prefix . self::TABLE;
103 $exists = $this->exists($list_screen->get_id());
104
105 if ($exists) {
106 $wpdb->update(
107 $table,
108 $args,
109 [
110 'list_id' => (string)$list_screen->get_id(),
111 ],
112 array_fill(0, 6, '%s')
113 );
114 } else {
115 $args['date_created'] = $args['date_modified'];
116
117 $wpdb->insert(
118 $table,
119 $args,
120 array_fill(0, 7, '%s')
121 );
122 }
123 }
124
125 public function delete(ListScreen $list_screen): void
126 {
127 global $wpdb;
128
129 if ( ! $list_screen->has_id()) {
130 throw new LogicException('Cannot delete a ListScreen without an identity.');
131 }
132
133 $wpdb->delete(
134 $wpdb->prefix . self::TABLE,
135 [
136 'list_id' => (string)$list_screen->get_id(),
137 ],
138 [
139 '%s',
140 ]
141 );
142 }
143
144 /**
145 * Template method to add and remove preferences before save
146 */
147 protected function save_preferences(ListScreen $list_screen): array
148 {
149 return $list_screen->get_preferences();
150 }
151
152 /**
153 * Template method to add and remove preferences before retrieval
154 */
155 protected function get_preferences(ListScreen $list_screen): array
156 {
157 return $list_screen->get_preferences();
158 }
159
160 private function create_list_screen(object $data): ?ListScreen
161 {
162 if ( ! $this->list_screen_factory->can_create($data->list_key)) {
163 return null;
164 }
165
166 $list_screen = $this->list_screen_factory->create(
167 $data->list_key,
168 [
169 'title' => $data->title,
170 'list_id' => $data->list_id,
171 'date' => $data->date_modified,
172 'preferences' => $data->settings ? unserialize($data->settings, ['allowed_classes' => false]) : [],
173 'columns' => $data->columns ? unserialize($data->columns, ['allowed_classes' => false]) : [],
174 'group' => null,
175 ]
176 );
177
178 $list_screen->set_preferences($this->get_preferences($list_screen));
179
180 return $list_screen;
181 }
182
183 private function create_list_screens(array $rows): ListScreenCollection
184 {
185 $list_screens = array_filter(
186 array_map([$this, 'create_list_screen'], $rows)
187 );
188
189 return new ListScreenCollection($list_screens);
190 }
191
192 }