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