Example #1
0
    /**
     * Displays a language switcher
     * or returns the raw elements to build a custom language switcher
     *
     * List of parameters accepted in $args:
     *
     * dropdown               => the list is displayed as dropdown if set, defaults to 0
     * echo                   => echoes the list if set to 1, defaults to 1
     * hide_if_empty          => hides languages with no posts ( or pages ) if set to 1, defaults to 1
     * show_flags             => displays flags if set to 1, defaults to 0
     * show_names             => show language names if set to 1, defaults to 1
     * display_names_as       => wether to display the language name or its slug, valid options are 'slug' and 'name', defaults to name
     * force_home             => will always link to home in translated language if set to 1, defaults to 0
     * hide_if_no_translation => hide the link if there is no translation if set to 1, defaults to 0
     * hide_current           => hide the current language if set to 1, defaults to 0
     * post_id                => returns links to translations of post defined by post_id if set, defaults not set
     * raw                    => return a raw array instead of html markup if set to 1, defaults to 0
     *
     * @since 0.1
     *
     * @param object $links instance of PLL_Frontend_Links
     * @param array  $args
     * @return string|array either the html markup of the switcher or the raw elements to build a custom language switcher
     */
    public function the_languages($links, $args = '')
    {
        $defaults = array('dropdown' => 0, 'echo' => 1, 'hide_if_empty' => 1, 'menu' => 0, 'show_flags' => 0, 'show_names' => 1, 'display_names_as' => 'name', 'force_home' => 0, 'hide_if_no_translation' => 0, 'hide_current' => 0, 'post_id' => null, 'raw' => 0);
        $args = wp_parse_args($args, $defaults);
        /**
         * Filter the arguments of the 'pll_the_languages' template tag
         *
         * @since 1.5
         *
         * @param array $args
         */
        $args = apply_filters('pll_the_languages_args', $args);
        // Prevents showing empty options in dropdown
        if ($args['dropdown']) {
            $args['show_names'] = 1;
        }
        $elements = $this->get_elements($links, $args);
        if ($args['raw']) {
            return $elements;
        }
        if ($args['dropdown']) {
            $args['name'] = 'lang_choice_' . $args['dropdown'];
            $walker = new PLL_Walker_Dropdown();
            $args['selected'] = $links->curlang->slug;
        } else {
            $walker = new PLL_Walker_List();
        }
        /**
         * Filter the whole html markup returned by the 'pll_the_languages' template tag
         *
         * @since 0.8
         *
         * @param string $html html returned/outputed by the template tag
         * @param array  $args arguments passed to the template tag
         */
        $out = apply_filters('pll_the_languages', $walker->walk($elements, $args), $args);
        // Javascript to switch the language when using a dropdown list
        if ($args['dropdown']) {
            foreach ($links->model->get_languages_list() as $language) {
                $url = $links->get_translation_url($language);
                $urls[$language->slug] = $args['force_home'] || empty($url) ? $links->get_home_url($language) : $url;
            }
            // Accept only few valid characters for the urls_x variable name ( as the widget id includes '-' which is invalid )
            $out .= sprintf('
				<script type="text/javascript">
					//<![CDATA[
					var %1$s = %2$s;
					document.getElementById( "%3$s" ).onchange = function() {
						location.href = %1$s[this.value];
					}
					//]]>
				</script>', 'urls_' . preg_replace('#[^a-zA-Z0-9]#', '', $args['dropdown']), json_encode($urls), esc_js($args['name']));
        }
        if ($args['echo']) {
            echo $out;
        }
        return $out;
    }
Example #2
0
 public function the_languages($links, $args = '')
 {
     $defaults = array('dropdown' => 0, 'echo' => 1, 'hide_if_empty' => 1, 'menu' => 0, 'show_flags' => 0, 'show_names' => 1, 'display_names_as' => 'name', 'force_home' => 0, 'hide_if_no_translation' => 0, 'hide_current' => 0, 'post_id' => null, 'raw' => 0);
     $args = wp_parse_args($args, $defaults);
     $args = apply_filters('pll_the_languages_args', $args);
     $elements = $this->get_elements($links, $args);
     if ($args['raw']) {
         return $elements;
     }
     if ($args['dropdown']) {
         $walker = new PLL_Walker_Dropdown();
         $args['selected'] = pll_current_language();
     } else {
         $walker = new PLL_Walker_List();
     }
     $out = apply_filters('pll_the_languages', $walker->walk($elements, $args), $args);
     if ($args['echo']) {
         echo $out;
     }
     return $out;
 }