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();
    }
}
Пример #2
0
function adinj_activate_hook()
{
    $stored_options = adinj_options();
    $pending_options = adinj_default_options();
    if (empty($stored_options)) {
        // Save defaults to DB below.
        if (!is_plugin_active('wp-super-cache/wp-cache.php')) {
            $pending_options['ad_insertion_mode'] = 'direct_dynamic';
        }
    } else {
        // Upgrade options if necessary. Use default as a baseline,
        // and then overwrite default with the saved ones.
        foreach ($pending_options as $key => $value) {
            if (array_key_exists($key, $stored_options)) {
                $pending_options[$key] = $stored_options[$key];
            }
        }
    }
    $random_file2 = ADINJ_AD_PATH . '/' . ADINJ_AD_RANDOM_FILE;
    $top_file2 = ADINJ_AD_PATH . '/' . ADINJ_AD_TOP_FILE;
    $bottom_file2 = ADINJ_AD_PATH . '/' . ADINJ_AD_BOTTOM_FILE;
    if ($pending_options['ad_insertion_mode'] == 'mfunc') {
        // Restore data after automatic upgrade
        // TODO could remove this code further down the line when everyone
        // has moved to the new ad store location
        if (!@file_exists($random_file2) && !empty($pending_options['ad_code_random_1'])) {
            write_ad_to_file($pending_options['ad_code_random_1'], $random_file2);
        }
        if (!@file_exists($top_file2) && !empty($pending_options['ad_code_top_1'])) {
            write_ad_to_file($pending_options['ad_code_top_1'], $top_file2);
        }
        if (!@file_exists($bottom_file2) && !empty($pending_options['ad_code_bottom_1'])) {
            write_ad_to_file($pending_options['ad_code_bottom_1'], $bottom_file2);
        }
        if (!@file_exists(ADINJ_CONFIG_FILE)) {
            adinj_write_config_file();
        }
    }
    adinj_update_options($pending_options);
}