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