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