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