| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hostinger\Reach\Admin\Database; |
| 4 |
|
| 5 |
use wpdb; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
die; |
| 9 |
} |
| 10 |
|
| 11 |
abstract class DatabaseTable implements DatabaseInterface { |
| 12 |
private wpdb $wpdb; |
| 13 |
private string $prefix = 'hostinger_reach_'; |
| 14 |
protected string $table_name; |
| 15 |
|
| 16 |
public function __construct( wpdb $wpdb ) { |
| 17 |
$this->wpdb = $wpdb; |
| 18 |
} |
| 19 |
|
| 20 |
public function create(): void { |
| 21 |
$charset_collate = $this->wpdb->get_charset_collate(); |
| 22 |
$schema = implode( ",\n", $this->schema() ); |
| 23 |
$table = $this->table_name(); |
| 24 |
|
| 25 |
$sql = "CREATE TABLE $table (\n$schema\n) $charset_collate;"; |
| 26 |
|
| 27 |
$this->execute_db_delta( $sql ); |
| 28 |
} |
| 29 |
|
| 30 |
public function table_name(): string { |
| 31 |
return $this->wpdb->prefix . $this->prefix . $this->table_name; |
| 32 |
} |
| 33 |
|
| 34 |
abstract public function schema(): array; |
| 35 |
|
| 36 |
public function execute_db_delta( string $sql ): void { |
| 37 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 38 |
dbDelta( $sql ); |
| 39 |
} |
| 40 |
} |
| 41 |
|