Bookable
1 year ago
Booking
1 year ago
Cache
1 year ago
Coupon
1 year ago
CustomField
1 year ago
Gallery
1 year ago
Location
1 year ago
Notification
1 year ago
Payment
1 year ago
Tax
1 year ago
User
1 year ago
AbstractDatabaseTable.php
1 year ago
AbstractDatabaseTable.php
92 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © TMS-Plugins. All rights reserved. |
| 5 | * @licence See LICENCE.md for license details. |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaBooking\Infrastructure\WP\InstallActions\DB; |
| 9 | |
| 10 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 11 | |
| 12 | /** |
| 13 | * Class AbstractDatabaseTable |
| 14 | * |
| 15 | * @package AmeliaBooking\Infrastructure\WP\InstallActions\DB |
| 16 | */ |
| 17 | class AbstractDatabaseTable |
| 18 | { |
| 19 | public const TABLE = ''; |
| 20 | |
| 21 | /** |
| 22 | * @return string |
| 23 | * @throws InvalidArgumentException |
| 24 | */ |
| 25 | public static function getTableName() |
| 26 | { |
| 27 | if (!static::TABLE) { |
| 28 | throw new InvalidArgumentException('Table name is not provided'); |
| 29 | } |
| 30 | |
| 31 | global $wpdb; |
| 32 | return $wpdb->prefix . 'amelia_' . static::TABLE; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @return string |
| 37 | * @throws InvalidArgumentException |
| 38 | */ |
| 39 | public static function buildTable() |
| 40 | { |
| 41 | return ''; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Create new table in the database |
| 46 | */ |
| 47 | public static function init() |
| 48 | { |
| 49 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 50 | dbDelta(static::buildTable()); |
| 51 | |
| 52 | global $wpdb; |
| 53 | |
| 54 | foreach (static::alterTable() as $command) { |
| 55 | $wpdb->query($command); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Delete table table from the database |
| 61 | * |
| 62 | * @throws InvalidArgumentException |
| 63 | */ |
| 64 | public static function delete() |
| 65 | { |
| 66 | global $wpdb; |
| 67 | |
| 68 | $table = self::getTableName(); |
| 69 | |
| 70 | $sql = "DROP TABLE IF EXISTS {$table};"; |
| 71 | $wpdb->query($sql); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @return boolean |
| 76 | */ |
| 77 | public static function isValidTablePrefix() |
| 78 | { |
| 79 | global $wpdb; |
| 80 | |
| 81 | return strlen($wpdb->prefix) <= 16; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @return array |
| 86 | */ |
| 87 | public static function alterTable() |
| 88 | { |
| 89 | return []; |
| 90 | } |
| 91 | } |
| 92 |