PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 4.6.1
Admin Columns v4.6.1
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 3 years ago Rule 3 years ago Sort 3 years ago Storage 3 years ago Database.php 3 years ago Filter.php 3 years ago ListScreenPermissionTrait.php 3 years ago Rule.php 3 years ago Rules.php 3 years ago Sort.php 3 years ago SourceAware.php 3 years ago Storage.php 3 years ago
Database.php
210 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\ListScreenRepositoryWritable;
11 use AC\ListScreenTypes;
12 use AC\Type\ListScreenId;
13 use DateTime;
14 use LogicException;
15 use stdClass;
16 use WP_User;
17
18 final class Database implements ListScreenRepositoryWritable {
19
20 use ListScreenPermissionTrait;
21
22 private const TABLE = 'admin_columns';
23
24 private $list_screen_types;
25
26 public function __construct( ListScreenTypes $list_screen_types ) {
27 $this->list_screen_types = $list_screen_types;
28 }
29
30 private function find_from_database( ListScreenId $id ): ?stdClass {
31 global $wpdb;
32 $sql = $wpdb->prepare( '
33 SELECT *
34 FROM ' . $wpdb->prefix . self::TABLE . '
35 WHERE list_id = %s
36 ',
37 (string) $id
38 );
39
40 $data = $wpdb->get_row( $sql );
41
42 return $data instanceof stdClass
43 ? $data
44 : null;
45 }
46
47 private function find_all_from_database(): array {
48 global $wpdb;
49
50 return $wpdb->get_results( '
51 SELECT *
52 FROM ' . $wpdb->prefix . self::TABLE . '
53 ' );
54 }
55
56 private function find_all_by_key_from_database( string $key ): array {
57 global $wpdb;
58 $sql = $wpdb->prepare( '
59 SELECT *
60 FROM ' . $wpdb->prefix . self::TABLE . '
61 WHERE list_key = %s
62 ',
63 $key
64 );
65
66 return $wpdb->get_results( $sql );
67 }
68
69 public function find_by_user( ListScreenId $id, WP_User $user ): ?ListScreen {
70 $list_screen = $this->find( $id );
71
72 return $list_screen && $this->user_can_view_list_screen( $list_screen, $user )
73 ? $list_screen
74 : null;
75 }
76
77 public function find_all_by_key( string $key, Sort $sort = null ): ListScreenCollection {
78 $list_screens = $this->create_list_screens(
79 $this->find_all_by_key_from_database( $key )
80 );
81
82 return $sort
83 ? $sort->sort( $list_screens )
84 : $list_screens;
85 }
86
87 public function find_all_by_user( string $key, WP_User $user, Sort $sort = null ): ListScreenCollection {
88 $list_screens = $this->find_all_by_key( $key, $sort );
89
90 return ( new Filter\User( $user ) )->filter( $list_screens );
91 }
92
93 public function find_all( Sort $sort = null ): ListScreenCollection {
94 $list_screens = $this->create_list_screens( $this->find_all_from_database() );
95
96 return $sort
97 ? $sort->sort( $list_screens )
98 : $list_screens;
99 }
100
101 private function create_list_screens( array $rows ): ListScreenCollection {
102 $list_screens = array_filter( array_map( [ $this, 'create_list_screen' ], $rows ) );
103
104 return new ListScreenCollection( $list_screens );
105 }
106
107 public function find( ListScreenId $id ): ?ListScreen {
108 $row = $this->find_from_database( $id );
109
110 return $row
111 ? $this->create_list_screen( $row )
112 : null;
113 }
114
115 public function exists( ListScreenId $id ): bool {
116 return null !== $this->find( $id );
117 }
118
119 public function save( ListScreen $list_screen ): void {
120 global $wpdb;
121
122 if ( ! $list_screen->has_id() ) {
123 throw MissingListScreenIdException::from_saving_list_screen();
124 }
125
126 $args = [
127 'list_id' => $list_screen->get_layout_id(),
128 'list_key' => $list_screen->get_key(),
129 'title' => $list_screen->get_title(),
130 'columns' => $list_screen->get_settings() ? serialize( $list_screen->get_settings() ) : null,
131 'settings' => $list_screen->get_preferences() ? serialize( $list_screen->get_preferences() ) : null,
132 'date_modified' => $list_screen->get_updated()->format( 'Y-m-d H:i:s' ),
133 ];
134
135 $table = $wpdb->prefix . self::TABLE;
136 $stored = $this->find_from_database( $list_screen->get_id() );
137
138 if ( $stored ) {
139 $wpdb->update(
140 $table,
141 $args,
142 [
143 'id' => $stored->id,
144 ],
145 array_fill( 0, 6, '%s' ),
146 [
147 '%d',
148 ]
149 );
150 } else {
151 $args['date_created'] = $args['date_modified'];
152
153 $wpdb->insert(
154 $table,
155 $args,
156 array_fill( 0, 7, '%s' )
157 );
158 }
159 }
160
161 public function delete( ListScreen $list_screen ): void {
162 global $wpdb;
163
164 if ( ! $list_screen->has_id() ) {
165 throw new LogicException( 'Cannot delete a ListScreen without an identity.' );
166 }
167
168 /**
169 * Fires before a column setup is removed from the database
170 * Primarily used when columns are deleted through the Admin Columns settings screen
171 *
172 * @param ListScreen $list_screen
173 *
174 * @deprecated 4.0
175 * @since 3.0.8
176 */
177 do_action( 'ac/columns_delete', $list_screen );
178
179 $wpdb->delete(
180 $wpdb->prefix . self::TABLE,
181 [
182 'list_id' => (string) $list_screen->get_id(),
183 ],
184 [
185 '%s',
186 ]
187 );
188 }
189
190 private function create_list_screen( object $data ): ?ListScreen {
191 $list_screen = $this->list_screen_types->get_list_screen_by_key( $data->list_key );
192
193 if ( $list_screen ) {
194 $list_screen->set_title( $data->title )
195 ->set_layout_id( $data->list_id )
196 ->set_updated( DateTime::createFromFormat( 'Y-m-d H:i:s', $data->date_modified ) );
197
198 if ( $data->settings ) {
199 $list_screen->set_preferences( unserialize( $data->settings, [ 'allowed_classes' => false ] ) ?: [] );
200 }
201
202 if ( $data->columns ) {
203 $list_screen->set_settings( unserialize( $data->columns, [ 'allowed_classes' => false ] ) ?: [] );
204 }
205 }
206
207 return $list_screen;
208 }
209
210 }