<?php

// Load the default BuddyPress AJAX functions
require_once(BP_PLUGIN_DIR . '/bp-themes/bp-default/_inc/ajax.php');

if (bp_is_active('dedications')) : 
    define("BP_DEFAULT_COMPONENT","dedications");
endif;

$countries_array = array(
    'AF' => 'Afghanistan',
    'AX' => 'Aland Islands',
    'AL' => 'Albania',
    'DZ' => 'Algeria',
    'AS' => 'American Samoa',
    'AD' => 'Andorra',
    'AO' => 'Angola',
    'AI' => 'Anguilla',
    'AQ' => 'Antarctica',
    'AG' => 'Antigua and Barbuda',
    'AR' => 'Argentina',
    'AM' => 'Armenia',
    'AW' => 'Aruba',
    'AU' => 'Australia',
    'AT' => 'Austria',
    'AZ' => 'Azerbaijan',
    'BS' => 'Bahamas',
    'BH' => 'Bahrain',
    'BD' => 'Bangladesh',
    'BB' => 'Barbados',
    'BY' => 'Belarus',
    'BE' => 'Belgium',
    'BZ' => 'Belize',
    'BJ' => 'Benin',
    'BM' => 'Bermuda',
    'BT' => 'Bhutan',
    'BO' => 'Bolivia',
    'BA' => 'Bosnia and Herzegovina',
    'BW' => 'Botswana',
    'BV' => 'Bouvet Island',
    'BR' => 'Brazil',
    'IO' => 'British Indian Ocean Territory',
    'BN' => 'Brunei Darussalam',
    'BG' => 'Bulgaria',
    'BF' => 'Burkina Faso',
    'BI' => 'Burundi',
    'KH' => 'Cambodia',
    'CM' => 'Cameroon',
    'CA' => 'Canada',
    'CV' => 'Cape Verde',
    'KY' => 'Cayman Islands',
    'CF' => 'Central African Republic',
    'TD' => 'Chad',
    'CL' => 'Chile',
    'CN' => 'China',
    'CX' => 'Christmas Island',
    'CC' => 'Cocos (Keeling) Islands',
    'CO' => 'Colombia',
    'KM' => 'Comoros',
    'CG' => 'Congo',
    'CD' => 'Congo, The Democratic Republic of The',
    'CK' => 'Cook Islands',
    'CR' => 'Costa Rica',
    'CI' => "Cote D'ivoire",
    'HR' => 'Croatia',
    'CU' => 'Cuba',
    'CY' => 'Cyprus',
    'CZ' => 'Czech Republic',
    'DK' => 'Denmark',
    'DJ' => 'Djibouti',
    'DM' => 'Dominica',
    'DO' => 'Dominican Republic',
    'EC' => 'Ecuador',
    'EG' => 'Egypt',
    'SV' => 'El Salvador',
    'GQ' => 'Equatorial Guinea',
    'ER' => 'Eritrea',
    'EE' => 'Estonia',
    'ET' => 'Ethiopia',
    'FK' => 'Falkland Islands (Malvinas)',
    'FO' => 'Faroe Islands',
    'FJ' => 'Fiji',
    'FI' => 'Finland',
    'FR' => 'France',
    'GF' => 'French Guiana',
    'PF' => 'French Polynesia',
    'TF' => 'French Southern Territories',
    'GA' => 'Gabon',
    'GM' => 'Gambia',
    'GE' => 'Georgia',
    'DE' => 'Germany',
    'GH' => 'Ghana',
    'GI' => 'Gibraltar',
    'GR' => 'Greece',
    'GL' => 'Greenland',
    'GD' => 'Grenada',
    'GP' => 'Guadeloupe',
    'GU' => 'Guam',
    'GT' => 'Guatemala',
    'GG' => 'Guernsey',
    'GN' => 'Guinea',
    'GW' => 'Guinea-bissau',
    'GY' => 'Guyana',
    'HT' => 'Haiti',
    'HM' => 'Heard Island and Mcdonald Islands',
    'VA' => 'Holy See (Vatican City State)',
    'HN' => 'Honduras',
    'HK' => 'Hong Kong',
    'HU' => 'Hungary',
    'IS' => 'Iceland',
    'IN' => 'India',
    'ID' => 'Indonesia',
    'IR' => 'Iran, Islamic Republic of',
    'IQ' => 'Iraq',
    'IE' => 'Ireland',
    'IM' => 'Isle of Man',
    'IL' => 'Israel',
    'IT' => 'Italy',
    'JM' => 'Jamaica',
    'JP' => 'Japan',
    'JE' => 'Jersey',
    'JO' => 'Jordan',
    'KZ' => 'Kazakhstan',
    'KE' => 'Kenya',
    'KI' => 'Kiribati',
    'KP' => "Korea, Democratic People's Republic of",
    'KR' => 'Korea, Republic of',
    'KW' => 'Kuwait',
    'KG' => 'Kyrgyzstan',
    'LA' => "Lao People's Democratic Republic",
    'LV' => 'Latvia',
    'LB' => 'Lebanon',
    'LS' => 'Lesotho',
    'LR' => 'Liberia',
    'LY' => 'Libyan Arab Jamahiriya',
    'LI' => 'Liechtenstein',
    'LT' => 'Lithuania',
    'LU' => 'Luxembourg',
    'MO' => 'Macao',
    'MK' => 'Macedonia, The Former Yugoslav Republic of',
    'MG' => 'Madagascar',
    'MW' => 'Malawi',
    'MY' => 'Malaysia',
    'MV' => 'Maldives',
    'ML' => 'Mali',
    'MT' => 'Malta',
    'MH' => 'Marshall Islands',
    'MQ' => 'Martinique',
    'MR' => 'Mauritania',
    'MU' => 'Mauritius',
    'YT' => 'Mayotte',
    'MX' => 'Mexico',
    'FM' => 'Micronesia, Federated States of',
    'MD' => 'Moldova, Republic of',
    'MC' => 'Monaco',
    'MN' => 'Mongolia',
    'ME' => 'Montenegro',
    'MS' => 'Montserrat',
    'MA' => 'Morocco',
    'MZ' => 'Mozambique',
    'MM' => 'Myanmar',
    'NA' => 'Namibia',
    'NR' => 'Nauru',
    'NP' => 'Nepal',
    'NL' => 'Netherlands',
    'AN' => 'Netherlands Antilles',
    'NC' => 'New Caledonia',
    'NZ' => 'New Zealand',
    'NI' => 'Nicaragua',
    'NE' => 'Niger',
    'NG' => 'Nigeria',
    'NU' => 'Niue',
    'NF' => 'Norfolk Island',
    'MP' => 'Northern Mariana Islands',
    'NO' => 'Norway',
    'OM' => 'Oman',
    'PK' => 'Pakistan',
    'PW' => 'Palau',
    'PS' => 'Palestinian Territory, Occupied',
    'PA' => 'Panama',
    'PG' => 'Papua New Guinea',
    'PY' => 'Paraguay',
    'PE' => 'Peru',
    'PH' => 'Philippines',
    'PN' => 'Pitcairn',
    'PL' => 'Poland',
    'PT' => 'Portugal',
    'PR' => 'Puerto Rico',
    'QA' => 'Qatar',
    'RE' => 'Reunion',
    'RO' => 'Romania',
    'RU' => 'Russian Federation',
    'RW' => 'Rwanda',
    'SH' => 'Saint Helena',
    'KN' => 'Saint Kitts and Nevis',
    'LC' => 'Saint Lucia',
    'PM' => 'Saint Pierre and Miquelon',
    'VC' => 'Saint Vincent and The Grenadines',
    'WS' => 'Samoa',
    'SM' => 'San Marino',
    'ST' => 'Sao Tome and Principe',
    'SA' => 'Saudi Arabia',
    'SN' => 'Senegal',
    'RS' => 'Serbia',
    'SC' => 'Seychelles',
    'SL' => 'Sierra Leone',
    'SG' => 'Singapore',
    'SK' => 'Slovakia',
    'SI' => 'Slovenia',
    'SB' => 'Solomon Islands',
    'SO' => 'Somalia',
    'ZA' => 'South Africa',
    'GS' => 'South Georgia and The South Sandwich Islands',
    'ES' => 'Spain',
    'LK' => 'Sri Lanka',
    'SD' => 'Sudan',
    'SR' => 'Suriname',
    'SJ' => 'Svalbard and Jan Mayen',
    'SZ' => 'Swaziland',
    'SE' => 'Sweden',
    'CH' => 'Switzerland',
    'SY' => 'Syrian Arab Republic',
    'TW' => 'Taiwan, Province of China',
    'TJ' => 'Tajikistan',
    'TZ' => 'Tanzania, United Republic of',
    'TH' => 'Thailand',
    'TL' => 'Timor-leste',
    'TG' => 'Togo',
    'TK' => 'Tokelau',
    'TO' => 'Tonga',
    'TT' => 'Trinidad and Tobago',
    'TN' => 'Tunisia',
    'TR' => 'Turkey',
    'TM' => 'Turkmenistan',
    'TC' => 'Turks and Caicos Islands',
    'TV' => 'Tuvalu',
    'UG' => 'Uganda',
    'UA' => 'Ukraine',
    'AE' => 'United Arab Emirates',
    'GB' => 'United Kingdom',
    'US' => 'United States',
    'UM' => 'United States Minor Outlying Islands',
    'UY' => 'Uruguay',
    'UZ' => 'Uzbekistan',
    'VU' => 'Vanuatu',
    'VE' => 'Venezuela',
    'VN' => 'Viet Nam',
    'VG' => 'Virgin Islands, British',
    'VI' => 'Virgin Islands, U.S.',
    'WF' => 'Wallis and Futuna',
    'EH' => 'Western Sahara',
    'YE' => 'Yemen',
    'ZM' => 'Zambia',
    'ZW' => 'Zimbabwe',
);


// Add class if BuddyPress active
if (function_exists('bp_is_active')) {
    add_filter('body_class', 'gp_bp_active_class');

    function gp_bp_active_class($classes) {
        $classes[] = 'bp-active';
        return $classes;
    }

}


// Add classes on BuddyPress pages
if (function_exists('bp_is_active')) {
    add_filter('body_class', 'gp_bp_class');

    function gp_bp_class($classes) {
        if (!bp_is_blog_page() OR (function_exists('is_bbpress') && is_bbpress())) {
            if (is_rtl()) {
                $classes[] = 'bp-wrapper custom-background rtl';
            } else {
                $classes[] = 'bp-wrapper custom-background';
            }
        }
        return $classes;
    }

}

// No ver mostrar de buddypress excepto para el tablero
if (!is_admin() ) define( 'BP_DISABLE_ADMIN_BAR', true );


// Add a class when admin bar is enabled for logged out users
if (!bp_get_option('hide-loggedout-adminbar')) {
    add_filter('body_class', 'gp_bp_adminbar_class');

    function gp_bp_adminbar_class($classes) {
        $classes[] = 'bp-adminbar';
        return $classes;
    }

}


// Add a class for bbPress on topic tag pages
if (function_exists('bbp_is_topic_tag') OR function_exists('bbp_is_topic_tag_edit')) {
    add_filter('body_class', 'gp_bp_bbpress_class');

    function gp_bp_bbpress_class($classes) {
        if (bbp_is_topic_tag() OR bbp_is_topic_tag_edit()) {
            $classes[] = 'bbPress';
        }
        return $classes;
    }

}

// Custom BuddyPress admin bar links
function gp_bp_login_redirect() {
    require(ghostpool_inc . 'options.php');
    global $bp;
    remove_action('bp_adminbar_menus', 'bp_adminbar_login_menu', 2);
    if (is_user_logged_in()) {
        if (is_singular()) {
            edit_post_link(__('Edit', 'gp_lang'), '<li class="no-arrow">', '</li>');
        }
    } else {
        if ($theme_login_url) {
            $url = $theme_login_url;
        } else {
            $url = wp_login_url();
        }
        echo '<li class="bp-login no-arrow"><a href="' . $url . '">' . __('Login', 'gp_lang') . '</a></li>';
        if (bp_get_signup_allowed())
            echo '<li class="bp-signup no-arrow"><a href="' . bp_get_signup_page(false) . '">' . __('Register', 'gp_lang') . '</a></li>';
    }
}

add_action('bp_adminbar_menus', 'gp_bp_login_redirect', 1);


// Include BuddyPress JS and CSS functions
if (!is_admin()) {
    if (!function_exists('gp_bp_enqueue_defaults_init')) {

        function gp_bp_enqueue_defaults_init() {
            wp_enqueue_style('buddypress', BP_THEME_URL . '/lib/buddypress/bp.css', false, '0.1', 'screen');
            wp_enqueue_script('dtheme-ajax-js', BP_PLUGIN_URL . '/bp-themes/bp-default/_inc/global.js', array('jquery'));
        }

    }
    add_action('init', 'gp_bp_enqueue_defaults_init', 1);
}


// Avatar dimensions
if (!defined('BP_AVATAR_THUMB_WIDTH'))
    define('BP_AVATAR_THUMB_WIDTH', 50); //change this with your desired thumb width

if (!defined('BP_AVATAR_THUMB_HEIGHT'))
    define('BP_AVATAR_THUMB_HEIGHT', 50); //change this with your desired thumb height

if (!defined('BP_AVATAR_FULL_WIDTH'))
    define('BP_AVATAR_FULL_WIDTH', 150); //change this with your desired full size,weel I changed it to 260 <img src="http://buddydev.com/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley"> 

if (!defined('BP_AVATAR_FULL_HEIGHT'))
    define('BP_AVATAR_FULL_HEIGHT', 150); //change this to default height for full avatar


    
// Modify the activity output
if (!function_exists('gp_bp_activity_action')) {

    function gp_bp_activity_action() {
        $content = bp_get_activity_action();
        $content = str_replace('&middot;', '', $content); // no more dots between content
        $content = str_replace(': <span', '<span', $content); // get rid of the ":" before date
        $content = str_replace('started the forum topic', __('Started topic:', 'gp_lang'), $content);
        $content = str_replace('posted on the forum topic', __('Posted on:', 'gp_lang'), $content);
        $content = str_replace('</a> created', '</a> ' . __('Created', 'gp_lang'), $content); // capitalization fix
        $content = str_replace('</a> posted', '</a> ' . __('Posted', 'gp_lang'), $content); // capitalization fix
        $content = str_replace('</a> started', '</a> ' . __('Started', 'gp_lang'), $content); // capitalization fix
        $content = str_replace('in the group', __('in', 'gp_lang'), $content);
        echo $content;
    }

}


// Add words that we need to use in JS to the end of the page so they can be translated and still used
if (!function_exists('bp_dtheme_enqueue_scripts')) {

    function bp_dtheme_enqueue_scripts() {
        $params = array(
            'my_favs' => __('My Favorites', 'buddypress'),
            'accepted' => __('Accepted', 'buddypress'),
            'rejected' => __('Rejected', 'buddypress'),
            'show_all_comments' => __('Show all comments for this thread', 'buddypress'),
            'show_all' => __('Show all', 'buddypress'),
            'comments' => __('comments', 'buddypress'),
            'close' => __('Close', 'buddypress'),
            'view' => __('View', 'buddypress'),
            'mark_as_fav' => __('Favorite', 'buddypress'),
            'remove_fav' => __('Remove Favorite', 'buddypress')
        );
        wp_localize_script('dtheme-ajax-js', 'BP_DTheme', $params);
    }

    add_action('wp_enqueue_scripts', 'bp_dtheme_enqueue_scripts');
}


// Member Buttons
if (bp_is_active('friends'))
    add_action('bp_member_header_actions', 'bp_add_friend_button');

if (bp_is_active('activity'))
    add_action('bp_member_header_actions', 'bp_send_public_message_button');

if (bp_is_active('messages'))
    add_action('bp_member_header_actions', 'bp_send_private_message_button');


// Group Buttons
if (bp_is_active('groups')) {
    add_action('bp_group_header_actions', 'bp_group_join_button');
    add_action('bp_group_header_actions', 'bp_group_new_topic_button');
    add_action('bp_directory_groups_actions', 'bp_group_join_button');
}


// Blog Buttons
if (bp_is_active('blogs'))
    add_action('bp_directory_blogs_actions', 'bp_blogs_visit_blog_button');




add_filter('bp_get_the_profile_field_value', 'gp_bp_get_the_profile_field_value', 3);

function gp_bp_get_the_profile_field_value($value, $type = '', $id = '') {
    global $field;
    global $countries_array;	

    if (substr_count(strtolower($field->name), 'country')) {
        $country = $value;
        $code = array_search($country, $countries_array);
        if ($code) {
            $value = gp_get_flag_img($code, 32);
        }
    }
    return $value;
}

function remove_xprofile_links() {
    remove_filter('bp_get_the_profile_field_value', 'xprofile_filter_link_profile_data', 9, 2);
}

add_action('bp_init', 'remove_xprofile_links');





function check_country_field() {
    global $wpdb; 
    global $countries_array;	
    
    $country_field = ''; 

    $sql = " SELECT name FROM  wp_bp_xprofile_fields WHERE name = 'Country' "; 
    $country_field = $wpdb->get_var( $sql );
    if( !$country_field ) {
        $wpdb->insert( 
                'wp_bp_xprofile_fields', 
                array( 
                    'group_id'=>1,
                    'parent_id'=>0,
                    'type'=>'selectbox',
                    'name'=>'Country',
                    'description'=>'',
                    'is_required'=>1,
                    'is_default_option'=>0,
                    'field_order'=>2,
                    'option_order'=>0,
                    'order_by'=>'asc',
                    'can_delete'=>0
                )
        );
        $parent_id = $wpdb->insert_id;
        
        foreach ($countries_array as $country) {
            $wpdb->insert( 
                'wp_bp_xprofile_fields', 
                array( 
                    'group_id'=>1,
                    'parent_id'=>$parent_id,
                    'type'=>'option',
                    'name'=>$country,
                    'description'=>'',
                    'is_required'=>0,
                    'is_default_option'=> 0,
                    'field_order'=>0,
                    'option_order'=>0,
                    'order_by'=>'',
                    'can_delete'=>0
                )
            );            
        }
    }
}
add_action('bp_init', 'check_country_field');

?>