<?php
/**
 * Created by Sublime text 2.
 * User: thanhhiep992
 * Date: 12/08/15
 * Time: 10:00 AM
 */

if(defined('ICL_LANGUAGE_CODE') || class_exists('Polylang')){
    if(!function_exists('sv_vc_language_selector'))
    {
        function sv_vc_language_selector($attr)
        {
            $html = $lang_sub = $lang_active = '';
            extract(shortcode_atts(array(
                'title'    => '',
                'name'      => 'no',
                'ul_class'  => ''
            ),$attr));
            if(defined('ICL_SITEPRESS_VERSION')){
                $wpml_lang = icl_get_languages('skip_missing=0&orderby=custom');
                foreach ($wpml_lang as $lang) {
                    if($lang['active']){
                        $l_class = 'active';
                        $lang_active .=     '<a href="'.esc_url($lang['country_flag_url']).'">';
                        if(!empty($title))      $lang_active .= '<span class="pre-label">'.$title.'</span> ';
                        $lang_active .=         '<img alt="flag" src="'.esc_url($lang['country_flag_url']).'">';
                        if($name == 'yes') $lang_active .=         $lang['native_name'];
                        $lang_active .=     '</a>';
                    }
                    else $l_class = '';
                    $lang_sub .=                '<li class="'.$l_class.'">
                                                    <a href="'.esc_url($lang['url']).'">';
                    $lang_sub .=                        '<img alt="flag" src="'.esc_url($lang['country_flag_url']).'">';
                    $lang_sub .=                        $lang['native_name'];
                    $lang_sub .=                    '</a>
                                                </li>';
                }
                $html .=    $lang_active;
                $html .=    '<ul class="'.esc_attr($ul_class).'">';
                $html .=        $lang_sub;
                $html .=    '</ul>';
            }
            else{
                if(class_exists('Polylang')){
                    global $polylang;
                    $languages = $polylang->model->get_languages_list();
                    $current_lang = pll_current_language();
                    foreach ($languages as $lang) {
                        if($lang->slug == $current_lang){
                            $l_class = 'active';
                            $lang_active .=     '<a href="'.esc_url($lang->home_url).'">';
                            if(!empty($title))      $lang_active .= '<span class="pre-label">'.$title.'</span> ';
                            $lang_active .=     '<img alt="flag" src="'.esc_url($lang->flag_url).'">';
                            $lang_active .=         $lang->name;
                            $lang_active .=     '</a>';
                        }
                        else $l_class = '';
                        $lang_sub .=                '<li class="'.$l_class.'">
                                                        <a href="'.esc_url($lang->home_url).'">';
                        $lang_sub .=                        '<img alt="flag" src="'.esc_url($lang->flag_url).'">';
                        $lang_sub .=                        $lang->name;
                        $lang_sub .=                    '</a>
                                                    </li>';
                    }
                    $html .=    $lang_active;
                    $html .=    '<ul class="'.esc_attr($ul_class).'">';
                    $html .=        $lang_sub;
                    $html .=    '</ul>';
                }
            }
            return $html;
        }
    }

    stp_reg_shortcode('sv_language_selector','sv_vc_language_selector');
}