Encoder
1 month ago
EncoderFactory
1 month ago
Repository
1 month ago
Table
1 month ago
Encoder.php
1 month ago
EncoderFactory.php
1 month ago
KeyValue.php
1 month ago
Option.php
1 month ago
OptionData.php
1 month ago
OptionDataFactory.php
1 month ago
OptionFactory.php
1 month ago
SiteOption.php
1 month ago
SiteOptionFactory.php
1 month ago
Table.php
1 month ago
Timestamp.php
1 month ago
Transaction.php
1 month ago
UserData.php
1 month ago
UserMeta.php
1 month ago
UserOption.php
1 month ago
Table.php
46 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Storage; |
| 6 | |
| 7 | use LogicException; |
| 8 | |
| 9 | abstract class Table |
| 10 | { |
| 11 | |
| 12 | public function exists(): bool |
| 13 | { |
| 14 | global $wpdb; |
| 15 | |
| 16 | $query = $wpdb->prepare('SHOW TABLES LIKE %s', $wpdb->esc_like($this->get_name())); |
| 17 | |
| 18 | return $wpdb->get_var($query) === $this->get_name(); |
| 19 | } |
| 20 | |
| 21 | public function update(): void |
| 22 | { |
| 23 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 24 | |
| 25 | dbDelta($this->get_schema()); |
| 26 | } |
| 27 | |
| 28 | public function create(): bool |
| 29 | { |
| 30 | global $wpdb; |
| 31 | |
| 32 | if ($this->exists()) { |
| 33 | throw new LogicException(sprintf('Table %s does already exist', $this->get_name())); |
| 34 | } |
| 35 | |
| 36 | return $wpdb->query($this->get_schema()) === true; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Return full name including WordPress database prefix |
| 41 | */ |
| 42 | abstract public function get_name(): string; |
| 43 | |
| 44 | abstract public function get_schema(): string; |
| 45 | |
| 46 | } |