/** * Hooks the WP admin_bar_menu action * * @param object $wp_admin_bar The WP Admin Bar, passed by reference * @return void **/ public function admin_bar_menu($wp_admin_bar) { $links = bbl_get_switcher_links('bbl-admin-bar'); $current_lang = bbl_get_current_lang(); // Remove the current language unset($links[$current_lang->code]); $parent_id = "bbl-admin-bar-{$current_lang->url_prefix}"; $wp_admin_bar->add_menu(array('children' => array(), 'href' => '#', 'id' => $parent_id, 'meta' => array('class' => "bbl_lang_{$current_lang->code} bbl_lang"), 'title' => $current_lang->display_name, 'parent' => false)); foreach ($links as &$link) { $link['parent'] = $parent_id; $wp_admin_bar->add_menu($link); } }
/** * Translates the menu's post items before the menu gets built * * @param WP_Query &$wp_query * * @return void */ public function wp_get_nav_menu_items($items, $menu, $args) { if (is_admin()) { return $items; } foreach ($items as $element) { if ('nav_menu_item' !== $element->post_type) { continue; } if ($element->title == '%bbl_language_title%') { foreach (bbl_get_switcher_links() as $key => $switch) { if (bbl_get_current_lang_code() == $key) { continue; } $element->title = $switch["lang"]->display_name; $element->url = $switch["href"]; } continue; } if (bbl_get_current_lang_code() === bbl_get_default_lang_code()) { continue; } if ('post_type' !== $element->type) { continue; } $post = bbl_get_post_in_lang($element->object_id, bbl_get_current_lang_code()); $element->title = $post->post_title; $element->url = get_permalink($post->ID); } return $items; }
function widget($args, $instance) { $args = array_merge(array('show_as' => 'dropdown'), $args); echo wp_kses_post($args['before_widget']); echo wp_kses_post($args['before_title'] . __('Languages', 'babble') . $args['after_title']); $list = bbl_get_switcher_links(); switch ($instance['show_as']) { case 'dropdown': echo '<select onchange="document.location.href=this.options[this.selectedIndex].value;">'; foreach ($list as $item) { if ($item['active']) { $selected = 'selected="selected" '; } else { $selected = ''; } if (in_array('bbl-add', $item['classes'])) { /* We're logged in, there's no translation of this page as yet, but the user has the ability to create one; so here's a link to allow him/her to do so */ echo '<option ' . esc_attr($selected) . 'class="' . esc_attr($item['class']) . '" value="' . esc_url($item['href']) . '">' . esc_html($item['lang']->display_name) . ' [' . esc_html__('Add') . ']</option>'; } elseif ($item['href']) { /* Means there is a translation of this page into the language in question */ echo '<option ' . esc_attr($selected) . 'class="' . esc_attr($item['class']) . '" value="' . esc_url($item['href']) . '">' . esc_html($item['lang']->display_name) . '</option>'; } elseif ('on' === $instance['show_if_unavailable']) { /* We're on the front end, but there is no translation into this language as yet; the user has said 'Show if unavailable' in the widget management screen. Applies a no-translation class to the <li>, and a bbl-no-translation class to the <div> in case you want to 'grey it out' somehow */ echo '<option disabled class="' . esc_attr($item['class']) . '" value="">' . esc_html($item['lang']->display_name) . '</option>'; } } echo '</select>'; break; case 'list': echo '<ul class="languages_list">'; foreach ($list as $item) { if (in_array('bbl-add', $item['classes'])) { /* We're logged in, there's no translation of this page as yet, but the user has the ability to create one; so here's a link to allow him/her to do so */ echo '<li><a href="' . esc_url($item['href']) . '" class="' . esc_attr($item['class']) . '">' . esc_html($item['lang']->display_name) . ' [' . esc_html__('Add', 'babble') . ']</a></li>'; } elseif ($item['href']) { /* Means there is a translation of this page into the language in question */ echo '<li><a href="' . esc_url($item['href']) . '" class="' . esc_attr($item['class']) . '">' . esc_html($item['lang']->display_name) . '</a></li>'; } elseif ('on' === $instance['show_if_unavailable']) { /* We're on the front end, but there is no translation into this language as yet; the user has said 'Show if unavailable' in the widget management screen. Applies a no-translation class to the <li>, and a bbl-no-translation class to the <div> in case you want to 'grey it out' somehow */ echo '<li class="no-translation"><div class="bbl-no-translation">' . esc_html($item['lang']->display_name) . '</div></li>'; } } echo '</ul>'; break; } echo wp_kses_post($args['after_widget']); }