/**
 * Function to install all location manager related data and options.
 *
 * @since 1.0.0
 * @package GeoDirectory_Location_Manager
 *
 * @global object $wpdb WordPress Database object.
 * @global string $plugin_prefix Geodirectory plugin table prefix.
 */
function geodir_location_activation_script()
{
    global $wpdb, $plugin_prefix;
    /**
     * Include any functions needed for upgrades.
     *
     * @since 1.3.6
     */
    require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    $is_set_default_location = geodir_get_default_location();
    $wpdb->hide_errors();
    // rename tables if we need to
    if ($wpdb->query("SHOW TABLES LIKE 'geodir_post_locations'") > 0) {
        $wpdb->query("RENAME TABLE geodir_post_locations TO " . $wpdb->prefix . "geodir_post_locations");
    }
    if ($wpdb->query("SHOW TABLES LIKE 'geodir_post_neighbourhood'") > 0) {
        $wpdb->query("RENAME TABLE geodir_post_neighbourhood TO " . $wpdb->prefix . "geodir_post_neighbourhood");
    }
    if ($wpdb->query("SHOW TABLES LIKE 'geodir_countries'") > 0) {
        $wpdb->query("RENAME TABLE geodir_countries TO " . $wpdb->prefix . "geodir_countries");
    }
    if ($wpdb->query("SHOW TABLES LIKE 'geodir_location_seo'") > 0) {
        $wpdb->query("RENAME TABLE geodir_location_seo TO " . $wpdb->prefix . "geodir_location_seo");
    }
    $collate = '';
    if ($wpdb->has_cap('collation')) {
        if (!empty($wpdb->charset)) {
            $collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
        }
        if (!empty($wpdb->collate)) {
            $collate .= " COLLATE {$wpdb->collate}";
        }
    }
    if ($wpdb->get_var("SHOW TABLES LIKE '" . POST_LOCATION_TABLE . "'") != POST_LOCATION_TABLE) {
        $location_table = "CREATE TABLE IF NOT EXISTS " . POST_LOCATION_TABLE . " (\n\t\t\t\t\t`location_id` int(11) NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t`country` varchar(254) NOT NULL,\n\t\t\t\t\t`region` varchar(254) NOT NULL,\n\t\t\t\t\t`city` varchar(254) NOT NULL, \n\t\t\t\t\t`country_slug` varchar(254) NOT NULL,\n\t\t\t\t\t`region_slug` varchar(254) NOT NULL,\n\t\t\t\t\t`city_slug` varchar(254) NOT NULL,\n\t\t\t\t\t`city_latitude` varchar(254) NOT NULL,\n\t\t\t\t\t`city_longitude` varchar(254) NOT NULL,\n\t\t\t\t\t`is_default` ENUM( '0', '1' ) NOT NULL DEFAULT '0',\n\t\t\t\t\t`city_meta` VARCHAR( 254 ) NOT NULL,\n\t\t\t\t\t`city_desc` TEXT NOT NULL,\n\t\t\t\t\tPRIMARY KEY (`location_id`)) {$collate} ";
        if ($wpdb->query("SHOW TABLES LIKE 'geodir_post_locations'") > 0) {
            $wpdb->query("RENAME TABLE geodir_post_locations TO " . $wpdb->prefix . "geodir_post_locations");
        } else {
            dbDelta($location_table);
        }
    }
    $location_result = (array) geodir_get_default_location();
    // this function is there in core plugin location_functions.php file.
    $post_types = geodir_get_posttypes();
    // Fuction in core geodirectory plugin
    $location_info = geodir_add_new_location_via_adon($location_result);
    geodir_location_set_default($location_info->location_id);
    if (!empty($post_types)) {
        foreach ($post_types as $post_type) {
            $table = $plugin_prefix . $post_type . '_detail';
            $wpdb->query($wpdb->prepare("UPDATE " . $table . " SET post_location_id=%d WHERE post_location_id=0", array($location_info->location_id)));
        }
    }
    if ($wpdb->get_var("SHOW TABLES LIKE '" . POST_NEIGHBOURHOOD_TABLE . "'") != POST_NEIGHBOURHOOD_TABLE) {
        $neighbourhood_table = "CREATE TABLE IF NOT EXISTS " . POST_NEIGHBOURHOOD_TABLE . " (\n\t\t\t\t\t`hood_id` int(11) NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t`hood_location_id` int(11) NOT NULL,\n\t\t\t\t\t`hood_name` varchar(254) NOT NULL,\n\t\t\t\t\t`hood_latitude` varchar(254) NOT NULL,\n\t\t\t\t\t`hood_longitude` varchar(254) NOT NULL,\n\t\t\t\t\t`hood_slug` varchar(254) NOT NULL,\n\t\t\t\t\tPRIMARY KEY (`hood_id`)) {$collate} ";
        if ($wpdb->query("SHOW TABLES LIKE 'geodir_post_neighbourhood'") > 0) {
            $wpdb->query("RENAME TABLE geodir_post_neighbourhood TO " . $wpdb->prefix . "geodir_post_neighbourhood");
        } else {
            dbDelta($neighbourhood_table);
        }
    }
    $address_extra_info = $wpdb->get_results("select id, extra_fields from " . GEODIR_CUSTOM_FIELDS_TABLE . " where field_type = 'address'");
    if (!empty($address_extra_info)) {
        foreach ($address_extra_info as $extra) {
            $fields = array();
            if ($extra->extra_fields != '') {
                $fields = unserialize($extra->extra_fields);
                if (!isset($fields['show_city'])) {
                    $fields['show_city'] = 1;
                }
                if (!isset($fields['city_lable'])) {
                    $fields['city_lable'] = __('City', GEODIRLOCATION_TEXTDOMAIN);
                }
                if (!isset($fields['show_region'])) {
                    $fields['show_region'] = 1;
                }
                if (!isset($fields['region_lable'])) {
                    $fields['region_lable'] = __('Region', GEODIRLOCATION_TEXTDOMAIN);
                }
                if (!isset($fields['show_country'])) {
                    $fields['show_country'] = 1;
                }
                if (!isset($fields['country_lable'])) {
                    $fields['country_lable'] = __('Country', GEODIRLOCATION_TEXTDOMAIN);
                }
                $wpdb->query($wpdb->prepare("UPDATE " . GEODIR_CUSTOM_FIELDS_TABLE . " SET extra_fields=%s WHERE id=%d", array(serialize($fields), $extra->id)));
            }
        }
    }
    //$post_types = geodir_get_posttypes();
    if (!empty($post_types)) {
        foreach ($post_types as $post_type) {
            $detail_table = $plugin_prefix . $post_type . '_detail';
            $meta_field_add = "VARCHAR( 30 ) NULL";
            geodir_add_column_if_not_exist($detail_table, "post_neighbourhood", $meta_field_add);
        }
    }
    // location seo table
    if ($wpdb->get_var("SHOW TABLES LIKE '" . LOCATION_SEO_TABLE . "'") != LOCATION_SEO_TABLE) {
        $location_table = "CREATE TABLE IF NOT EXISTS " . LOCATION_SEO_TABLE . " (\n\t\t\t\t\t`seo_id` int(11) NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t  `location_type` varchar(255) NOT NULL,\n\t\t\t\t\t  `country_slug` varchar(254) NOT NULL,\n\t\t\t\t\t  `region_slug` varchar(254) NOT NULL,\n\t\t\t\t\t  `city_slug` varchar(254) NOT NULL,\n\t\t\t\t\t  `seo_title` varchar(254) NOT NULL,\n\t\t\t\t\t  `seo_desc` text NOT NULL,\n\t\t\t\t\t  `date_created` datetime NOT NULL,\n\t\t\t\t\t  `date_updated` datetime NOT NULL,\n\t\t\t\t\t  PRIMARY KEY (`seo_id`)\n\t\t\t\t\t) {$collate} ";
        if ($wpdb->query("SHOW TABLES LIKE 'geodir_location_seo'") > 0) {
            $wpdb->query("RENAME TABLE geodir_location_seo TO " . $wpdb->prefix . "geodir_location_seo");
        } else {
            dbDelta($location_table);
        }
    }
    // location term count table
    if ($wpdb->get_var("SHOW TABLES LIKE '" . GEODIR_TERM_META . "'") != GEODIR_TERM_META) {
        $term_meta_table = "CREATE TABLE IF NOT EXISTS " . GEODIR_TERM_META . " (\n\t\t\t\t\t\tid int NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t\tlocation_type varchar( 100 ) NULL DEFAULT NULL,\n\t\t\t\t\t\tlocation_name varchar( 100 ) NULL DEFAULT NULL,\n\t\t\t\t\t\tterm_count varchar( 5000 ) NULL DEFAULT NULL,\n\t\t\t\t\t\treview_count varchar( 5000 ) NULL DEFAULT NULL,\n\t\t\t\t\t\tPRIMARY KEY  (id)\n\t\t\t\t\t\t) {$collate} ";
        dbDelta($term_meta_table);
    }
}
/**
 * Handles set default location request.
 *
 * @since 1.0.0
 * @package GeoDirectory_Location_Manager
 *
 * @global object $wpdb WordPress Database object.
 */
function geodir_set_default()
{
    global $wpdb;
    if (isset($_REQUEST['_wpnonce']) && isset($_REQUEST['id']) && current_user_can('manage_options')) {
        $locationid = $_REQUEST['id'];
        if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'location_action_' . $_REQUEST['id'])) {
            return;
        }
        geodir_location_set_default($locationid);
        $msg = MSG_LOCATION_SET_DEFAULT;
        $msg = urlencode($msg);
        $location = admin_url() . "admin.php?page=geodirectory&tab=managelocation_fields&subtab=geodir_location_manager&location_success=" . $msg;
        wp_redirect($location);
        exit;
    } else {
        wp_redirect(home_url() . '/?geodir_signup=true');
        exit;
    }
}