widget_preview = false;
$this->color_schemes = $this->get_default_color_schemes();
$this->widget_css_defaults = $this->color_schemes[ 'White' ];
$this->footer_css_defaults = $this->color_schemes[ 'White' ];
add_action( 'plugins_loaded', array( $this, 'init' ) );
}
function init() {
global $sitepress_settings;
$this->settings = $sitepress_settings;
if ( ! empty( $this->settings[ 'icl_lang_sel_footer' ] ) ) {
add_action( 'wp_head', array( $this, 'language_selector_footer_style' ), 19 );
add_action( 'wp_footer', array( $this, 'language_selector_footer' ), 19 );
}
if ( is_admin() ) {
add_action( 'icl_language_switcher_options', array( $this, 'admin' ), 1 );
} else if ( ! empty( $this->settings[ 'icl_post_availability' ] ) ) {
if ( function_exists( 'icl_register_string' ) ) {
icl_register_string( 'WPML', 'Text for alternative languages for posts', $this->settings[ 'icl_post_availability_text' ] );
}
add_filter( 'the_content', array( $this, 'post_availability' ), 100 );
}
// the language selector widget
add_action( 'widgets_init', array( $this, 'language_selector_widget_init' ) );
if ( is_admin() && isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] == ICL_PLUGIN_FOLDER . '/menu/languages.php' ) {
add_action( 'admin_head', 'icl_lang_sel_nav_css', 1, 1, true );
add_action( 'admin_head', array( $this, 'custom_language_switcher_style' ) );
}
if ( ! is_admin() ) {
add_action( 'wp_head', array( $this, 'custom_language_switcher_style' ) );
}
add_filter( 'wp_nav_menu_items', array( $this, 'wp_nav_menu_items_filter' ), 10, 2 );
add_filter( 'wp_page_menu', array( $this, 'wp_page_menu_filter' ), 10, 2 );
}
function language_selector_widget_init() {
register_widget( 'ICL_Language_Switcher' );
add_action( 'template_redirect', 'icl_lang_sel_nav_ob_start', 0 );
add_action( 'wp_head', 'icl_lang_sel_nav_ob_end' );
}
private function must_filter_menus() {
global $sitepress_settings;
return ! empty( $sitepress_settings[ 'display_ls_in_menu' ] ) && ( ! function_exists( 'wpml_home_url_ls_hide_check' ) || ! wpml_home_url_ls_hide_check() );
}
function set_widget() {
global $sitepress, $sitepress_settings;
if ( isset( $_POST[ 'icl_widget_update' ] ) ) {
$sitepress_settings[ 'icl_widget_title_show' ] = ( isset( $_POST[ 'icl_widget_title_show' ] ) ) ? 1 : 0;
$sitepress->save_settings( $sitepress_settings );
}
echo '';
echo '
';
}
function post_availability( $content ) {
$out = '';
if ( is_singular() ) {
$languages = icl_get_languages( 'skip_missing=true' );
if ( 1 < count( $languages ) ) {
//$out .= $this->settings['post_available_before'] ? $this->settings['post_available_before'] : '';
$langs = array();
foreach ( $languages as $l ) {
if ( ! $l[ 'active' ] ) {
$langs[ ] = '' . $l[ 'translated_name' ] . '';
}
}
$out .= join( ', ', $langs );
//$out .= $this->settings['post_available_after'] ? $this->settings['post_available_after'] : '';
$str = function_exists( 'icl_t' ) ? icl_t( 'WPML', 'Text for alternative languages for posts', $this->settings[ 'icl_post_availability_text' ] ) : $this->settings[ 'icl_post_availability_text' ];
$out = '
' . sprintf( $str, $out ) . '
';
}
}
$out = apply_filters( 'icl_post_alternative_languages', $out );
if ( $this->settings[ 'icl_post_availability_position' ] == 'above' ) {
$content = $out . $content;
} else {
$content = $content . $out;
}
return $content;
}
function language_selector_footer_style() {
$add = false;
foreach ( $this->footer_css_defaults as $key => $d ) {
if ( isset( $this->settings[ 'icl_lang_sel_footer_config' ][ $key ] ) && $this->settings[ 'icl_lang_sel_footer_config' ][ $key ] != $d ) {
$add = true;
break;
}
}
if ( $add ) {
echo "\n\n";
}
}
static function get_language_selector_footer() {
global $sitepress;
$language_selector_footer = '';
$languages = array();
if ( ! function_exists( 'wpml_home_url_ls_hide_check' ) || ! wpml_home_url_ls_hide_check() ) {
$languages = $sitepress->get_ls_languages();
}
if ( ! empty( $languages ) ) {
$language_selector_footer = '
';
}
return $language_selector_footer;
}
function language_selector_footer() {
echo self::get_language_selector_footer();
}
function admin() {
global $sitepress;
foreach ( $this->color_schemes as $color_scheme_name => $color_scheme_data ) {
foreach ( $this->widget_css_defaults as $scheme_attribute => $scheme_attribute_value ) {
?>
load_language_selector_css() ) {
$this->render_admin_language_preview();
} else {
$this->render_admin_language_blocked_preview();
}
?>
load_language_selector_css() ) {
?>
color_schemes as $color_scheme_name => $color_scheme_data ): ?>
footer_css_defaults as $scheme_attribute => $scheme_attribute_value ): ?>
settings[ 'icl_additional_css' ] ) ) {
$icl_additional_css = trim( $this->settings[ 'icl_additional_css' ] );
} else {
$icl_additional_css = '';
}
?>
settings[ 'icl_lang_sel_type' ] ) || $this->settings[ 'icl_lang_sel_type' ] == 'dropdown' ) {
echo ' style="display:none;"';
} ?> class="lang_sel_list_settings[ 'icl_lang_sel_orientation' ] ?>">
get_current_language() ? 'sel"' : 'other"';
$li_items .= $sitepress->render_ls_li_item( $lang, false, true, $language_selected );
}
echo $li_items;
?>
widget_css_defaults as $key => $d ) {
if ( isset( $this->settings[ 'icl_lang_sel_config' ][ $key ] ) && $this->settings[ 'icl_lang_sel_config' ][ $key ] != $d ) {
$add = true;
break;
}
}
if ( $add ) {
$list = ( $this->settings[ 'icl_lang_sel_type' ] == 'list' ) ? true : false;
echo "\n\n";
}
if ( isset( $this->settings[ 'icl_additional_css' ] ) && ! empty( $this->settings[ 'icl_additional_css' ] ) ) {
echo "\r\n";
}
}
function wp_page_menu_filter( $items, $args ) {
if($this->must_filter_menus()) {
$obj_args = new stdClass();
foreach ( $args as $key => $value ) {
$obj_args->$key = $value;
}
$items = str_replace( "", "", $items );
$items = apply_filters( 'wp_nav_menu_items', $items, $obj_args );
$items .= "";
}
return $items;
}
/**
* Filter on the 'wp_nav_menu_items' hook, that potentially adds a language switcher to the item of some menus.
*
* @param string $items
* @param object $args
*
* @return string
*/
function wp_nav_menu_items_filter( $items, $args ) {
if ( $this->must_filter_menus() && $this->menu_has_ls( $args ) ) {
$items .= $this->get_menu_ls_html( $args );
}
return $items;
}
/**
* Returns the HTML string of the language switcher for a given menu.
*
* @param object $args
*
* @return string
*/
private function get_menu_ls_html( $args ) {
global $sitepress, $wpml_post_translations, $wpml_term_translations;
$current_language = $sitepress->get_current_language();
$languages_helper = new WPML_Languages( $wpml_term_translations, $sitepress, $wpml_post_translations );
$languages = $sitepress->get_ls_languages();
$items = '';
$items .= '';
}
$sub_items = $sub_items && $menu_is_vertical ? '' : $sub_items;
$sub_items = $menu_is_vertical ? $sub_items . '' : '' . $sub_items;
return $sub_items;
}
public function language_display( $native_name, $translated_name = false, $show_native_name = false, $show_translate_name = false, $include_html = true ) {
$result = '';
if ( ! $show_native_name ) {
$native_name = '';
}
if ( ! $show_translate_name ) {
$translated_name = '';
}
if ( $native_name && $translated_name ) {
if ( $native_name != $translated_name ) {
if ( $show_native_name ) {
if($include_html) {
$result .= '';
}
$result .= '%1$s';
if($include_html) {
$result .= '';
}
if($show_translate_name) {
$result .= ' ';
if($include_html) {
$result .= '';
}
$result .= $show_native_name
? '(%2$s)'
: '%2$s';
if($include_html) {
$result .= '';
}
}
}elseif($show_translate_name) {
if($include_html) {
$result .= '';
}
$result .= '%2$s';
if($include_html) {
$result .= '';
}
}
} else {
if($include_html) {
$result .= '';
}
$result .= '%1$s';
if($include_html) {
$result .= '';
}
}
} elseif ( $native_name ) {
$result = '%1$s';
} elseif ( $translated_name ) {
$result = '%2$s';
}
return sprintf($result, $native_name, $translated_name);
}
private function maybe_render_flag( $language, $rendered_name ) {
global $sitepress;
$html = $rendered_name;
if ( $sitepress->get_setting( 'icl_lso_flags' ) ) {
$alt_title_lang = $rendered_name ? esc_attr( $rendered_name ) : esc_attr( $language[ 'native_name' ] );
$html = '
' . $html;
}
return $html;
}
function set_current_language_color_selector_control_arguments( $setting_base_key, $input_base_id, $state = '', $in_footer = false ) {
$input_id = $input_base_id . ( $state ? '-' . $state : '' );
$setting_key = $setting_base_key . ( $state ? '-' . $state : '' );
$input_name_group = 'icl_lang_sel_config';
if ( $in_footer ) {
$input_name_group = 'icl_lang_sel_footer_config';
}
$this->current_language_color_selector_item[ 'input_name_group' ] = $input_name_group;
$this->current_language_color_selector_item[ 'input_name_id' ] = $input_id;
$this->current_language_color_selector_item[ 'default' ] = $this->footer_css_defaults[ $setting_key ];
if ( isset( $this->settings[ $input_name_group ][ $setting_key ] ) ) {
$this->current_language_color_selector_item[ 'value' ] = $this->settings[ $input_name_group ][ $setting_key ];
}
return $this->current_language_color_selector_item;
}
function current_language_color_selector_control() {
$wpml_color_picker = new WPML_Color_Picker( $this->current_language_color_selector_item );
echo $wpml_color_picker->get_current_language_color_selector_control();
}
protected function render_admin_language_blocked_preview() {
?>
| |
|
|
|
set_current_language_color_selector_control_arguments( 'font-current', 'font-current', 'normal' );
$this->current_language_color_selector_control();
?>
|
set_current_language_color_selector_control_arguments( 'font-current', 'font-current', 'hover' );
$this->current_language_color_selector_control();
?>
|
|
set_current_language_color_selector_control_arguments( 'background-current', 'background-current', 'normal' );
$this->current_language_color_selector_control();
?>
|
set_current_language_color_selector_control_arguments( 'background-current', 'background-current', 'hover' );
$this->current_language_color_selector_control();
?>
|
|
set_current_language_color_selector_control_arguments( 'font-other', 'font-other', 'normal' );
$this->current_language_color_selector_control();
?>
|
set_current_language_color_selector_control_arguments( 'font-other', 'font-other', 'hover' );
$this->current_language_color_selector_control();
?>
|
|
set_current_language_color_selector_control_arguments( 'background-other', 'background-other', 'normal' );
$this->current_language_color_selector_control();
?>
|
set_current_language_color_selector_control_arguments( 'background-other', 'background-other', 'hover' );
$this->current_language_color_selector_control();
?>
|
|
set_current_language_color_selector_control_arguments( 'background', 'background' );
$this->current_language_color_selector_control();
?>
|
|
|
set_current_language_color_selector_control_arguments( 'border', 'border' );
$this->current_language_color_selector_control();
?>
|
'#222222',
'font-current-hover' => '#000000',
'background-current-normal' => '#eeeeee',
'background-current-hover' => '#eeeeee',
'font-other-normal' => '#222222',
'font-other-hover' => '#000000',
'background-other-normal' => '#e5e5e5',
'background-other-hover' => '#eeeeee',
'border' => '#cdcdcd',
'background' => '#e5e5e5'
);
$white = array(
'font-current-normal' => '#444444',
'font-current-hover' => '#000000',
'background-current-normal' => '#ffffff',
'background-current-hover' => '#eeeeee',
'font-other-normal' => '#444444',
'font-other-hover' => '#000000',
'background-other-normal' => '#ffffff',
'background-other-hover' => '#eeeeee',
'border' => '#cdcdcd',
'background' => '#ffffff'
);
$blue = array(
'font-current-normal' => '#ffffff',
'font-current-hover' => '#000000',
'background-current-normal' => '#95bedd',
'background-current-hover' => '#95bedd',
'font-other-normal' => '#000000',
'font-other-hover' => '#ffffff',
'background-other-normal' => '#cbddeb',
'background-other-hover' => '#95bedd',
'border' => '#0099cc',
'background' => '#cbddeb'
);
return array(
'Gray' => $gray,
'White' => $white,
'Blue' => $blue
);
}
/**
* @return bool
*/
protected function load_language_selector_css() {
return ! defined( 'ICL_DONT_LOAD_LANGUAGE_SELECTOR_CSS' ) || ! ICL_DONT_LOAD_LANGUAGE_SELECTOR_CSS;
}
} // end class
// language switcher functions
function language_selector_widget( $args ) {
global $sitepress, $sitepress_settings;
extract( $args, EXTR_SKIP );
/** @var $before_widget string */
echo $before_widget;
if ( $sitepress_settings[ 'icl_widget_title_show' ] ) {
echo $args[ 'before_title' ];
_e( 'Languages', 'sitepress' );
echo $args[ 'after_title' ];
}
$sitepress->language_selector();
/** @var $after_widget string */
echo $after_widget;
}
function icl_lang_sel_nav_ob_start() {
if ( is_feed() ) {
return;
}
ob_start( 'icl_lang_sel_nav_prepend_css' );
}
function icl_lang_sel_nav_ob_end() {
$ob_handlers = ob_list_handlers();
$active_handler = array_pop( $ob_handlers );
if ( $active_handler == 'icl_lang_sel_nav_prepend_css' ) {
ob_end_flush();
}
}
function icl_lang_sel_nav_prepend_css( $buf ) {
if ( defined( 'ICL_DONT_LOAD_LANGUAGE_SELECTOR_CSS' ) && ICL_DONT_LOAD_LANGUAGE_SELECTOR_CSS ) {
return $buf;
}
return preg_replace( '##i', '' . PHP_EOL . PHP_EOL . icl_lang_sel_nav_css( false ), $buf );
}
function icl_lang_sel_nav_css( $show = true ) {
if ( defined( 'ICL_DONT_LOAD_LANGUAGE_SELECTOR_CSS' ) && ICL_DONT_LOAD_LANGUAGE_SELECTOR_CSS ) {
return '';
}
$link_tag = '';
if ( ! $show && ( ! isset( $_GET[ 'page' ] ) || $_GET[ 'page' ] != ICL_PLUGIN_FOLDER . '/menu/languages.php' ) ) {
return $link_tag;
} else {
echo $link_tag;
}
return $link_tag;
}
global $icl_language_switcher;
$icl_language_switcher = new SitePressLanguageSwitcher;