Example #1
0
function pll_the_languages($args = '')
{
    if (PLL_ADMIN) {
        return '';
    }
    $switcher = new PLL_Switcher();
    return $switcher->the_languages(PLL()->links, $args);
}
Example #2
0
/**
 * Template tag: displays the language switcher
 *
 * List of parameters accepted in $args:
 *
 * dropdown               => displays a dropdown if set to 1, defaults to 0
 * echo                   => echoes the the switcher if set to 1 ( default )
 * hide_if_empty          => hides languages with no posts ( or pages ) if set to 1 ( default )
 * show_flags             => shows flags if set to 1, defaults to 0
 * show_names             => shows languages names if set to 1 ( default )
 * display_names_as       => whether to display the language name or code. valid options are 'slug' and 'name'
 * force_home             => forces linking to the home page is set to 1, defaults to 0
 * hide_if_no_translation => hides the link if there is no translation if set to 1, defaults to 0
 * hide_current           => hides the current language if set to 1, defaults to 0
 * post_id                => if not null, link to translations of post defined by post_id, defaults to null
 * raw                    => set this to true to build your own custom language switcher, defaults to 0
 *
 * @since 0.5
 *
 * @param array $args optional
 * @return null|string|array null if displaying, array if raw is requested, string otherwise
 */
function pll_the_languages($args = '')
{
    if (PLL() instanceof PLL_Frontend) {
        $switcher = new PLL_Switcher();
        return $switcher->the_languages(PLL()->links, $args);
    }
    return '';
}
Example #3
0
function pll_the_languages($args = '')
{
    global $polylang;
    if ($polylang instanceof PLL_Frontend && !empty($polylang->links)) {
        $switcher = new PLL_Switcher();
        return $switcher->the_languages($polylang->links, $args);
    }
    return '';
}
 /**
  * Splits the one item of backend in several items on frontend
  * take care to menu_order as it is used later in wp_nav_menu
  *
  * @since 1.1.1
  *
  * @param array $items menu items
  * @return array modified items
  */
 public function wp_get_nav_menu_items($items)
 {
     if (doing_action('customize_register')) {
         // needed since WP 4.3, doing_action available since WP 3.9
         return $items;
     }
     // The customizer menus does not sort the items and we need them to be sorted before splitting the language switcher
     usort($items, array($this, 'usort_menu_items'));
     $new_items = array();
     $offset = 0;
     foreach ($items as $key => $item) {
         if ($options = get_post_meta($item->ID, '_pll_menu_item', true)) {
             $i = 0;
             $switcher = new PLL_Switcher();
             $args = array_merge(array('raw' => 1), $options);
             $the_languages = $switcher->the_languages(PLL()->links, $args);
             // parent item for dropdown
             if (!empty($options['dropdown'])) {
                 $item->title = $options['show_flags'] && $options['show_names'] ? $this->curlang->flag . ' ' . esc_html($this->curlang->name) : ($options['show_flags'] ? $this->curlang->flag : esc_html($this->curlang->name));
                 $item->url = '';
                 $item->classes = array('pll-parent-menu-item');
                 $new_items[] = $item;
                 $offset++;
             }
             foreach ($the_languages as $lang) {
                 $lang_item = clone $item;
                 $lang_item->ID = $lang_item->ID . '-' . $lang['slug'];
                 // A unique ID
                 $lang_item->title = $options['show_flags'] && $options['show_names'] ? $lang['flag'] . '<span style="margin-left:0.3em;">' . esc_html($lang['name']) . '</span>' : ($options['show_flags'] ? $lang['flag'] : esc_html($lang['name']));
                 $lang_item->url = $lang['url'];
                 $lang_item->lang = $lang['locale'];
                 // Save this for use in nav_menu_link_attributes
                 $lang_item->classes = $lang['classes'];
                 $lang_item->menu_order += $offset + $i++;
                 if (!empty($options['dropdown'])) {
                     $lang_item->menu_item_parent = $item->db_id;
                     $lang_item->db_id = 0;
                     // to avoid recursion
                 }
                 $new_items[] = $lang_item;
             }
             $offset += $i - 1;
         } else {
             $item->menu_order += $offset;
             $new_items[] = $item;
         }
     }
     return $new_items;
 }
Example #5
0
 public function wp_get_nav_menu_items($items)
 {
     if (doing_action('customize_register')) {
         // needed since WP 4.3, doing_action available since WP 3.9
         return $items;
     }
     // the customizer menus does not sort the items and we need them to be sorted before splitting the language switcher
     usort($items, array($this, 'usort_menu_items'));
     $new_items = array();
     $offset = 0;
     foreach ($items as $key => $item) {
         if ($options = get_post_meta($item->ID, '_pll_menu_item', true)) {
             $i = 0;
             $switcher = new PLL_Switcher();
             $args = array_merge(array('raw' => 1), $options);
             $the_languages = $switcher->the_languages(PLL()->links, $args);
             foreach ($the_languages as $lang) {
                 $lang_item = clone $item;
                 $lang_item->ID = $lang_item->ID . '-' . $lang['slug'];
                 // a unique ID
                 $lang_item->title = $options['show_flags'] && $options['show_names'] ? $lang['flag'] . '&nbsp;' . esc_html($lang['name']) : ($options['show_flags'] ? $lang['flag'] : esc_html($lang['name']));
                 $lang_item->url = $lang['url'];
                 $lang_item->lang = $lang['locale'];
                 // save this for use in nav_menu_link_attributes
                 $lang_item->classes = $lang['classes'];
                 $lang_item->menu_order += $offset + $i++;
                 $new_items[] = $lang_item;
             }
             $offset += $i - 1;
         } else {
             $item->menu_order += $offset;
             $new_items[] = $item;
         }
     }
     return $new_items;
 }
Example #6
0
 function form($instance)
 {
     // default values
     $instance = wp_parse_args((array) $instance, array_merge(array('title' => ''), PLL_Switcher::get_switcher_options('widget', 'default')));
     // title
     $title = sprintf('<p><label for="%1$s">%2$s</label><input class="widefat" id="%1$s" name="%3$s" type="text" value="%4$s" /></p>', $this->get_field_id('title'), __('Title:', 'polylang'), $this->get_field_name('title'), esc_attr($instance['title']));
     $fields = '';
     foreach (PLL_Switcher::get_switcher_options('widget') as $key => $str) {
         $fields .= sprintf('<div class = "%5$s" %6$s><input type="checkbox" class="checkbox" id="%1$s" name="%2$s" %3$s/> <label for="%1$s">%4$s</label></div>', $this->get_field_id($key), $this->get_field_name($key), $instance[$key] ? 'checked="checked"' : '', esc_html($str), 'dropdown' == $key ? '' : 'no-dropdown-' . $this->id, 'dropdown' == $key || empty($instance['dropdown']) ? '' : 'style="display:none;"');
     }
     echo $title . '<p>' . $fields . '</p>';
     // FIXME echoing script in form is not very clean
     // but it does not work if enqueued properly :
     // clicking save on a widget makes this code unreachable for the just saved widget (?!)
     $this->admin_print_script();
 }
Example #7
0
 public function admin_enqueue_scripts()
 {
     $screen = get_current_screen();
     if ('nav-menus' != $screen->base) {
         return;
     }
     $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
     wp_enqueue_script('pll_nav_menu', POLYLANG_URL . '/js/nav-menu' . $suffix . '.js', array('jquery'), POLYLANG_VERSION);
     // the strings for the options
     foreach (array_reverse(PLL_Switcher::get_switcher_options('menu', 'string')) as $str) {
         $data['strings'][] = $str;
     }
     $data['strings'][] = __('Language switcher', 'polylang');
     // the title
     // get all language switcher menu items
     $items = get_posts(array('numberposts' => -1, 'nopaging' => true, 'post_type' => 'nav_menu_item', 'fields' => 'ids', 'meta_key' => '_pll_menu_item'));
     // the options values for the language switcher
     $data['val'] = array();
     foreach ($items as $item) {
         $data['val'][$item] = get_post_meta($item, '_pll_menu_item', true);
     }
     // send all these data to javascript
     wp_localize_script('pll_nav_menu', 'pll_data', $data);
 }
 /**
  * @return string Menu HTML.
  */
 public function alternate_versions_menu()
 {
     global $polylang;
     $wpml_selector = $this->wpml_language_selector_html();
     if ($wpml_selector) {
         return $wpml_selector;
     }
     if (!class_exists('PLL_Switcher')) {
         return '';
     }
     $switcher = new PLL_Switcher();
     $languages = $switcher->the_languages($polylang->links, array('post_id' => $this->post->ID, 'echo' => false, 'hide_if_no_translation' => true, 'hide_current' => true));
     return empty($languages) ? '' : html('ul class="alternate-languages"', $languages);
 }