Skip to content

Commit

Permalink
Add database versioning to Analytics Service Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
jrtashjian committed Jun 24, 2024
1 parent 4ab9f0e commit 1fdb58d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions includes/Analytics/AnalyticsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
* The AnalyticsServiceProvider class.
*/
class AnalyticsServiceProvider extends AbstractServiceProvider implements BootableServiceProviderInterface {
/**
* The database version.
*
* @var int
*/
const DB_VERSION = 1;

const EVENTS_TABLE = 'omniform_stats_events';
const VISITOR_TABLE = 'omniform_stats_visitors';

Expand Down Expand Up @@ -58,6 +65,8 @@ function () {
*/
public function boot(): void {
add_action( 'omniform_activate', array( $this, 'activate' ) );
add_action( 'admin_init', array( $this, 'update_database' ) );

add_action( 'delete_post', array( $this, 'on_delete_form' ), 10, 2 );
}

Expand Down Expand Up @@ -110,6 +119,21 @@ public function activate() {
}
}

/**
* Update the database if needed.
*/
public function update_database() {
$installed_version = get_option( 'omniform_analytics_db_version' );

// Check if an update is needed.
if ( false === $installed_version || version_compare( (int) $installed_version, self::DB_VERSION, '<' ) ) {
$this->activate();

// Update the installed version number.
update_option( 'omniform_analytics_db_version', self::DB_VERSION );
}
}

/**
* Fires immediately before a post is deleted from the database.
*
Expand Down

0 comments on commit 1fdb58d

Please sign in to comment.