function adinj_upgrade_widget_db($new_instance, $old_instance)
 {
     // Copy old values across to default
     $updated = $this->default_options();
     foreach ($updated as $key => $value) {
         if (array_key_exists($key, $new_instance)) {
             $updated[$key] = $new_instance[$key];
         }
     }
     // Upgrade any options as necessary
     $old_dbversion = adinj_db_version($old_instance);
     if ($old_dbversion == 1) {
         $updated['advert_1'] = $old_instance['advert'];
     }
     $updated['db_version'] = ADINJ_WIDGET_DB_VERSION;
     return $updated;
 }
function adinj_upgrade_db()
{
    $stored_options = adinj_options();
    $new_options = adinj_default_options();
    $stored_dbversion = adinj_db_version($stored_options);
    $new_dbversion = adinj_db_version($new_options);
    // 1. Copy existing options to new array. Use default as a baseline,
    // and then overwrite default with the saved ones.
    foreach ($new_options as $key => $value) {
        if (array_key_exists($key, $stored_options)) {
            $new_options[$key] = $stored_options[$key];
        }
    }
    // 2. Upgrade options if necessary.
    if ($stored_dbversion == 1) {
        // Copy this to new setting
        if (adinj_ticked('first_paragraph_ad')) {
            $new_options['start_from_paragraph'] = 1;
        }
        // This previously wan't configured correctly
        $new_options['block_ips'] = 'on';
    }
    if ($stored_dbversion < 4) {
        // Maintain previous behaviour for users who upgrade
        $new_options['content_length_unit'] = 'all';
    }
    if ($stored_dbversion < 5) {
        // Maintain previous behaviour for users who upgrade
        $new_options['exclude_search'] = 'on';
        $new_options['exclude_404'] = 'on';
        $new_options['widget_exclude_search'] = 'on';
        $new_options['widget_exclude_404'] = 'on';
    }
    if ($stored_dbversion < 6) {
        $new_options['home_max_num_random_ads_per_page'] = $stored_options['max_num_of_ads_home_page'];
    }
    if ($stored_dbversion < 9) {
        if ($stored_options['ad_insertion_mode'] == 'direct_static') {
            $new_options['ad_insertion_mode'] = 'direct';
            $new_options['block_ips'] = 'off';
            $new_options['sevisitors_only'] = 'off';
        }
        if ($stored_options['ad_insertion_mode'] == 'direct_dynamic') {
            $new_options['ad_insertion_mode'] = 'direct';
        }
    }
    if ($stored_dbversion < 11) {
        $new_options['rnd_allow_ads_on_last_paragraph'] = 'on';
    }
    if ($stored_dbversion < 15) {
        // values deliberately hard coded here to fix them on constants used for this version of db
        // if set to disabled
        if ($stored_options['top_ad_if_longer_than'] == 'd' || $stored_options['top_ad_if_longer_than'] == 'Disabled' || $stored_options['top_ad_if_longer_than'] == 'Rule Disabled') {
            $new_options['top_exclude_single'] = 'on';
            $new_options['top_exclude_page'] = 'on';
        }
        if ($stored_options['home_top_ad_if_longer_than'] == 'd' || $stored_options['home_top_ad_if_longer_than'] == 'Disabled' || $stored_option['home_top_ad_if_longer_than'] == 'Rule Disabled') {
            $new_options['top_exclude_home'] = 'on';
        }
        if ($stored_options['archive_top_ad_if_longer_than'] == 'd' || $stored_options['archive_top_ad_if_longer_than'] == 'Disabled' || $stored_options['archive_top_ad_if_longer_than'] == 'Rule Disabled') {
            $new_options['top_exclude_archive'] = 'on';
        }
        if ($stored_options['bottom_ad_if_longer_than'] == 'd' || $stored_options['bottom_ad_if_longer_than'] == 'Disabled' || $stored_options['bottom_ad_if_longer_than'] == 'Rule Disabled') {
            $new_options['bottom_exclude_single'] = 'on';
            $new_options['bottom_exclude_page'] = 'on';
        }
        if ($stored_options['home_bottom_ad_if_longer_than'] == 'd' || $stored_options['home_bottom_ad_if_longer_than'] == 'Disabled' || $stored_options['home_bottom_ad_if_longer_than'] == 'Rule Disabled') {
            $new_options['bottom_exclude_home'] = 'on';
        }
        if ($stored_options['archive_bottom_ad_if_longer_than'] == 'd' || $stored_options['archive_bottom_ad_if_longer_than'] == 'Disabled' || $stored_options['archive_bottom_ad_if_longer_than'] == 'Rule Disabled') {
            $new_options['bottom_exclude_archive'] = 'on';
        }
        // if set to always show
        if ($stored_options['top_ad_if_longer_than'] == 'a' || $stored_options['top_ad_if_longer_than'] == 'Always show') {
            $new_options['top_ad_if_longer_than'] = 'd';
        }
        if ($stored_options['home_top_ad_if_longer_than'] == 'a' || $stored_options['home_top_ad_if_longer_than'] == 'Always show') {
            $new_options['home_top_ad_if_longer_than'] = 'd';
        }
        if ($stored_options['archive_top_ad_if_longer_than'] == 'a' || $stored_options['archive_top_ad_if_longer_than'] == 'Always show') {
            $new_options['archive_top_ad_if_longer_than'] = 'd';
        }
        if ($stored_options['bottom_ad_if_longer_than'] == 'a' || $stored_options['bottom_ad_if_longer_than'] == 'Always show') {
            $new_options['bottom_ad_if_longer_than'] = 'd';
        }
        if ($stored_options['home_bottom_ad_if_longer_than'] == 'a' || $stored_options['home_bottom_ad_if_longer_than'] == 'Always show') {
            $new_options['home_bottom_ad_if_longer_than'] = 'd';
        }
        if ($stored_options['archive_bottom_ad_if_longer_than'] == 'a' || $stored_options['archive_bottom_ad_if_longer_than'] == 'Always show') {
            $new_options['archive_bottom_ad_if_longer_than'] = 'd';
        }
    }
    if ($stored_dbversion < 16) {
        $new_options['widgets_on_page_older_than'] = $stored_options['ads_on_page_older_than'];
    }
    if ($stored_dbversion < 18) {
        // todo test
        $new_options['random_ads_start_mode'] = $stored_options['random_ads_after_mode'];
        $new_options['random_ads_start_unit'] = $stored_options['random_ads_after_unit'];
        $new_options['random_ads_start_at'] = $stored_options['start_from_paragraph'];
        if ($new_options['random_ads_start_at'] == 'd') {
            $new_options['random_ads_start_mode'] = 'anywhere';
            $new_options['random_ads_start_at'] = '1';
        }
    }
    if ($stored_dbversion < 19) {
        if ($new_options['random_ads_start_at'] == 'd' || $new_options['random_ads_start_at'] == 'Rule Disabled' || $new_options['random_ads_start_at'] == 'Disabled') {
            $new_options['random_ads_start_mode'] = 'anywhere';
            $new_options['random_ads_start_at'] = '1';
        }
    }
    // 3. Bump up db version number.
    $new_options['db_version'] = $new_dbversion;
    // 4. Save upgraded options.
    adinj_update_options($new_options);
    // 5. Update config file.
    if ($new_options['ad_insertion_mode'] == 'mfunc') {
        // Update config file
        adinj_write_config_file();
    }
}
function adinj_upgrade_db_if_necessary()
{
    $cached_options = adinj_options();
    if (empty($cached_options)) {
        // 1st Install.
        require_once ADINJ_PATH . '/ad-injection-admin.php';
        adinj_install_db();
        return;
    }
    $cached_dbversion = adinj_db_version($cached_options);
    if (ADINJ_DB_VERSION != $cached_dbversion) {
        require_once ADINJ_PATH . '/ad-injection-admin.php';
        adinj_upgrade_db();
    }
}