Skip to content
This repository has been archived by the owner on Oct 31, 2022. It is now read-only.

Commit

Permalink
Added migration script for settings when upgrading
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredcobb committed Jan 7, 2018
1 parent c34704d commit 8d46270
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions includes/class-ccb-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ private function define_hooks() {
// Internationalization.
add_action( 'plugins_loaded', [ $this, 'load_plugin_textdomain' ] );

// Check the plugin / database version and run any required upgrades.
add_action( 'plugins_loaded', [ $this, 'check_version' ] );

// Plugin settings, menus, options.
add_filter( 'plugin_action_links_' . CCB_CORE_BASENAME, [ $this, 'add_settings_link' ] );

Expand Down Expand Up @@ -135,6 +138,28 @@ public function load_plugin_textdomain() {

}

/**
* Check the current plugin version and kick off any applicable upgrades.
*
* @return void
*/
public function check_version() {
$current_version = get_option( 'ccb_core_version' );

// We are currently up to date.
if ( version_compare( $current_version, CCB_CORE_VERSION, '>=' ) ) {
return;
}

// Upgrade to version 1.0.0.
if ( version_compare( $current_version, '1.0.0', '<' ) ) {
$this->upgrade_to_1_0_0();
}

// Update the DB version.
update_option( 'ccb_core_version', CCB_CORE_VERSION );
}

/**
* Create a helpful settings link on the plugin page
*
Expand Down Expand Up @@ -342,5 +367,49 @@ public function enqueue_scripts( $hook ) {

}

/**
* Converts any legacy options to the new format
*
* @return void
*/
private function upgrade_to_1_0_0() {
$current_options = CCB_Core_Helpers::instance()->get_options();
$updated_options = [];
$options_hash = [
'subdomain' => 'subdomain',
'credentials' => 'credentials',
'groups-enabled' => 'groups_enabled',
'groups-name' => 'groups_name',
'groups-slug' => 'groups_slug',
'groups-import-images' => 'groups_import_images',
'groups-advanced' => 'groups_advanced',
'groups-exclude-from-search' => 'groups_exclude_from_search',
'groups-publicly-queryable' => 'groups_publicly_queryable',
'groups-show-ui' => 'groups_show_ui',
'groups-show-in-nav-menus' => 'groups_show_in_nav_menus',
'calendar-enabled' => 'calendar_enabled',
'calendar-name' => 'calendar_name',
'calendar-slug' => 'calendar_slug',
'calendar-advanced' => 'calendar_advanced',
'calendar-date-range-type' => 'calendar_date_range_type',
'calendar-relative-weeks-past' => 'calendar_relative_weeks_past',
'calendar-relative-weeks-future' => 'calendar_relative_weeks_future',
'calendar-specific-start' => 'calendar_specific_start',
'calendar-specific-end' => 'calendar_specific_end',
'calendar-exclude-from-search' => 'calendar_exclude_from_search',
'calendar-publicly-queryable' => 'calendar_publicly_queryable',
'calendar-show-ui' => 'calendar_show_ui',
'calendar-show-in-nav-menus' => 'calendar_show_in_nav_menus',
];

if ( ! empty( $current_options ) ) {
foreach ( $options_hash as $old => $new ) {
if ( isset( $current_options[ $old ] ) ) {
$updated_options[ $new ] = $current_options[ $old ];
}
}
update_option( 'ccb_core_settings', $updated_options );
}
}

}

0 comments on commit 8d46270

Please sign in to comment.