| 1 |
<?php |
| 2 |
/** |
| 3 |
* SureDonation Database Tables Register Class. |
| 4 |
* |
| 5 |
* @package SureDonation |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace SureDonation\Inc\Database; |
| 9 |
|
| 10 |
use SureDonation\Inc\Database\Tables\Donations; |
| 11 |
use SureDonation\Inc\Database\Tables\Donors; |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
defined( 'ABSPATH' ) || exit; |
| 15 |
|
| 16 |
/** |
| 17 |
* SureDonation Database Tables Register Class |
| 18 |
* |
| 19 |
* @since 0.0.1 |
| 20 |
*/ |
| 21 |
class Register { |
| 22 |
/** |
| 23 |
* Init database registration. |
| 24 |
* |
| 25 |
* @return void |
| 26 |
* @since 0.0.1 |
| 27 |
*/ |
| 28 |
public static function init() { |
| 29 |
/* |
| 30 |
* ### Here, order is important. ### |
| 31 |
* 1. Start the DB upgrade which also manages the internal versioning of each tables. |
| 32 |
* 2. Init create method, which create the table if the table does not exists. |
| 33 |
* 3. Finally, stop the DB upgrade and update the current version in option table. |
| 34 |
*/ |
| 35 |
foreach ( static::get_db_tables() as $instance ) { |
| 36 |
$instance->start_db_upgrade(); |
| 37 |
|
| 38 |
if ( $instance->is_db_upgradable() ) { |
| 39 |
// Only execute below methods if DB is upgradable. |
| 40 |
$instance->create( $instance->get_columns_definition() ); |
| 41 |
$instance->maybe_add_new_columns( $instance->get_new_columns_definition() ); |
| 42 |
// One-time data migrations (backfills) after the columns exist. |
| 43 |
$instance->run_data_migrations(); |
| 44 |
} |
| 45 |
|
| 46 |
// Stop the upgrade process of current table and move to next. |
| 47 |
$instance->stop_db_upgrade(); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Returns an array of instances/objects of our custom tables. |
| 53 |
* |
| 54 |
* @return array<string,\SureDonation\Inc\Database\Base> |
| 55 |
* @since 0.0.1 |
| 56 |
*/ |
| 57 |
public static function get_db_tables() { |
| 58 |
return [ |
| 59 |
'donations' => Donations::get_instance(), |
| 60 |
'donors' => Donors::get_instance(), |
| 61 |
]; |
| 62 |
} |
| 63 |
} |
| 64 |
|