/**
     * Echo the widget content
     *
     * @param array $args     Display arguments including before_title, after_title, before_widget, and after_widget.
     * @param array $instance The settings for the particular instance of the widget
     */
    public function widget($args, $instance)
    {
        if (!empty($instance['type'])) {
            $type = $instance['type'];
        } else {
            $type = 'flags';
        }
        $inside = '';
        $enabled_languages = WPGlobus::Config()->enabled_languages;
        switch ($type) {
            case 'list':
                $code = '<div class="list">{{inside}}</div>';
                break;
            case 'list_with_flags':
                $code = '<div class="list flags">{{inside}}</div>';
                break;
            case 'select':
            case 'select_with_code':
                $code = '<div class="select-styled"><select onchange="document.location.href = this.value;">{{inside}}</select></div>';
                break;
            case 'dropdown':
            case 'dropdown_with_flags':
                $sorted[] = WPGlobus::Config()->language;
                foreach ($enabled_languages as $language) {
                    if ($language != WPGlobus::Config()->language) {
                        $sorted[] = $language;
                    }
                }
                $enabled_languages = $sorted;
                $code = '<div class="dropdown-styled"> <ul>
					  <li>
						{{language}}
						<ul>
							{{inside}}
						</ul>
					  </li>
					</ul></div>';
                break;
            default:
                //	This is case 'flags'. Having is as default makes $code always set.
                $code = '<div class="flags-styled">{{inside}}</div>';
                break;
        }
        /**
         * Filter enabled languages.
         *
         * Returning array.
         *
         * @since 1.0.13
         *
         * @param array     $enabled_languages 			 An array with languages to show off in menu.
         * @param string    WPGlobus::Config()->language The current language.
         */
        $enabled_languages = apply_filters('wpglobus_extra_languages', $enabled_languages, WPGlobus::Config()->language);
        /**
         * Class for link in a and option tags. Used for adding hash.
         * @see class wpglobus-selector-link 
         * @since 1.2.0
         */
        $link_classes['selector_link'] = 'wpglobus-selector-link';
        /**
         * Class for flag box
         * @since 1.4.0
         */
        $flag_classes = array();
        echo $args['before_widget'];
        if (!empty($instance['title'])) {
            echo $args['before_title'] . $instance['title'] . $args['after_title'];
        }
        foreach ($enabled_languages as $language) {
            $selected = '';
            /**
             * Init current language class
             */
            $link_classes['current_language'] = '';
            /**
             * Init current language class for flag box
             */
            $flag_classes['current_language'] = '';
            if ($language == WPGlobus::Config()->language) {
                $selected = ' selected';
                switch ($type) {
                    case 'flags':
                        $flag_classes['current_language'] = 'wpglobus-current-language';
                        break;
                    case 'list':
                    case 'list_with_flags':
                    case 'dropdown':
                    case 'dropdown_with_flags':
                        $link_classes['current_language'] = 'wpglobus-current-language';
                        break;
                }
            }
            $url = WPGlobus_Utils::localize_current_url($language);
            $flag = WPGlobus::Config()->flags_url . WPGlobus::Config()->flag[$language];
            switch ($type) {
                case 'flags':
                    $inside .= '<span class="flag ' . implode(' ', $flag_classes) . '">';
                    $inside .= '<a href="' . $url . '" class="' . implode(' ', $link_classes) . '"><img src="' . $flag . '"/></a>';
                    $inside .= '</span>';
                    break;
                case 'list':
                case 'list_with_flags':
                    $inside .= '<a href="' . $url . '" class="' . implode(' ', $link_classes) . '">' . '<img src="' . $flag . '" alt=""/>' . ' ' . '<span class="name">' . WPGlobus::Config()->language_name[$language] . '</span>' . ' ' . '<span class="code">' . strtoupper($language) . '</span>' . '</a>';
                    break;
                case 'select':
                    $inside .= '<option class="' . implode(' ', $link_classes) . '" ' . $selected . ' value="' . $url . '">' . WPGlobus::Config()->language_name[$language] . '</option>';
                    break;
                case 'select_with_code':
                    $inside .= '<option class="' . implode(' ', $link_classes) . '" ' . $selected . ' value="' . $url . '">' . WPGlobus::Config()->language_name[$language] . '&nbsp;(' . strtoupper($language) . ')</option>';
                    break;
                case 'dropdown':
                    if ('' != $selected) {
                        $code = str_replace('{{language}}', '<a class="' . implode(' ', $link_classes) . '" href="' . $url . '">' . WPGlobus::Config()->language_name[$language] . '&nbsp;(' . strtoupper($language) . ')</a>', $code);
                    } else {
                        $inside .= '<li><a class="' . implode(' ', $link_classes) . '" href="' . $url . '">' . WPGlobus::Config()->language_name[$language] . '&nbsp;(' . strtoupper($language) . ')</a></li>';
                    }
                    break;
                case 'dropdown_with_flags':
                    if ('' != $selected) {
                        $code = str_replace('{{language}}', '<a class="' . implode(' ', $link_classes) . '" href="' . $url . '"><img src="' . $flag . '"/>&nbsp;&nbsp;' . WPGlobus::Config()->language_name[$language] . '</a>', $code);
                    } else {
                        $inside .= '<li><a class="' . implode(' ', $link_classes) . '" href="' . $url . '"><img src="' . $flag . '"/>&nbsp;&nbsp;' . WPGlobus::Config()->language_name[$language] . '</a></li>';
                    }
                    break;
            }
        }
        echo str_replace('{{inside}}', $inside, $code);
        echo $args['after_widget'];
    }
Beispiel #2
0
 /**
  * Add language switcher to navigation menu
  *
  * @param array  $sorted_menu_items
  * @param stdClass $args An object containing wp_nav_menu() arguments.
  *
  * @return array
  * @see wp_nav_menu()
  */
 public function on_add_item($sorted_menu_items, $args)
 {
     if (empty(WPGlobus::Config()->nav_menu)) {
         /**
          * User can use WPGlobus widget
          * @since 1.0.7
          */
         $disable_add_selector = true;
     } elseif ('all' === WPGlobus::Config()->nav_menu) {
         /**
          * Attach to every nav menu
          * @since 1.0.7
          */
         $disable_add_selector = false;
     } else {
         $items = array();
         foreach ($sorted_menu_items as $item) {
             $items[] = $item->ID;
         }
         $disable_add_selector = true;
         foreach ($this->menus as $key => $menu) {
             $diff = array_diff($items, $menu->menu_items);
             if (empty($diff) && WPGlobus::Config()->nav_menu === $menu->slug) {
                 $disable_add_selector = false;
                 break;
             }
         }
     }
     /**
      * Filter to add or not language selector to the menu.
      * Returning boolean.
      * @since 1.5.8
      *
      * @param bool 	$disable_add_selector 	Disable or not to add language selector to the menu.
      * @param stdClass 	$args 					An object containing wp_nav_menu() arguments.
      */
     $disable_add_selector = apply_filters('wpglobus_menu_add_selector', $disable_add_selector, $args);
     if ($disable_add_selector) {
         return $sorted_menu_items;
     }
     /**
      * List of all languages, except the main one.
      *
      * @var string[] $extra_languages
      */
     $extra_languages = array();
     foreach (WPGlobus::Config()->enabled_languages as $languages) {
         if ($languages !== WPGlobus::Config()->language) {
             $extra_languages[] = $languages;
         }
     }
     /**
      * Filter extra languages.
      * Returning array.
      * @since 1.0.13
      *
      * @param array $extra_languages An array of languages to show in the menu.
      * @param       string           WPGlobus::Config()->language The current language.
      */
     $extra_languages = apply_filters('wpglobus_extra_languages', $extra_languages, WPGlobus::Config()->language);
     // Main menu item classes.
     $menu_item_classes = array('', 'menu-item', 'menu-item-type-custom', 'menu-item-object-custom', 'menu_item_wpglobus_menu_switch', 'wpglobus-selector-link');
     // Submenu item classes.
     $submenu_item_classes = array('', 'menu-item', 'menu-item-type-custom', 'menu-item-object-custom', 'sub_menu_item_wpglobus_menu_switch', 'wpglobus-selector-link');
     if (apply_filters('wpglobus_dropdown_menu', true, WPGlobus::Config())) {
         $parent_item_ID = 9999999999;
         # 9 999 999 999
     } else {
         $parent_item_ID = 0;
     }
     $span_classes_lang = $this->_get_language_classes(WPGlobus::Config()->language);
     $current_url = WPGlobus_Utils::current_url();
     $item = new stdClass();
     $item->ID = 0 === $parent_item_ID ? 'wpglobus_menu_switch_' . WPGlobus::Config()->language : $parent_item_ID;
     $item->db_id = $item->ID;
     $item->object_id = $item->ID;
     $item->object = 'custom';
     $item->menu_item_parent = 0;
     $item->title = '<span class="' . implode(' ', $span_classes_lang) . '">' . $this->_get_flag_name(WPGlobus::Config()->language) . '</span>';
     // The top menu level points to the current URL. Useless? Maybe good for refresh.
     $item->url = $current_url;
     $item->classes = $menu_item_classes;
     $item->classes[] = 'wpglobus-current-language';
     $item->description = '';
     $item->language = WPGlobus::Config()->language;
     $wpglobus_menu_items[] = new WP_Post($item);
     foreach ($extra_languages as $language) {
         $span_classes_lang = $this->_get_language_classes($language);
         $item = new stdClass();
         $item->ID = 'wpglobus_menu_switch_' . $language;
         $item->db_id = $item->ID;
         $item->object_id = $item->ID;
         $item->object = 'custom';
         $item->menu_item_parent = $parent_item_ID;
         $item->title = '<span class="' . implode(' ', $span_classes_lang) . '">' . $this->_get_flag_name($language) . '</span>';
         // This points to the URL localized for the selected language.
         $item->url = WPGlobus_Utils::localize_current_url($language);
         $item->classes = 0 === $parent_item_ID ? $menu_item_classes : $submenu_item_classes;
         $item->description = '';
         $item->language = $language;
         $wpglobus_menu_items[] = new WP_Post($item);
     }
     $languages = $extra_languages;
     array_unshift($languages, WPGlobus::Config()->language);
     return array_merge($sorted_menu_items, apply_filters('wpglobus_menu_items', $wpglobus_menu_items, $languages));
 }
 /**
  * Build hreflang metas
  *
  * @since 1.1.1
  * @param WPGlobus_Config $config Alternative configuration (i.e. Unit Test mock object)
  * @return string[] Array of rel-alternate link tags
  */
 public static function hreflangs(WPGlobus_Config $config = null)
 {
     /**
      * Use the global configuration is alternative not passed
      */
     if (null === $config) {
         // @codeCoverageIgnoreStart
         $config = WPGlobus::Config();
     }
     // @codeCoverageIgnoreEnd
     $hreflangs = array();
     if (is_404()) {
         return $hreflangs;
     }
     foreach ($config->enabled_languages as $language) {
         $hreflangs[$language] = sprintf('<link rel="alternate" hreflang="%s" href="%s"/>', str_replace('_', '-', $config->locale[$language]), WPGlobus_Utils::localize_current_url($language, $config));
     }
     return $hreflangs;
 }
 /**
  * Add language switcher to navigation menu
  *
  * @param array  $sorted_menu_items
  * @param object $args An object containing wp_nav_menu() arguments.
  *
  * @return array
  * @see wp_nav_menu()
  */
 function on_add_item($sorted_menu_items, $args)
 {
     if (empty(WPGlobus::Config()->nav_menu)) {
         /**
          * User can use WPGlobus widget
          * @since 1.0.7
          */
         return $sorted_menu_items;
     } elseif ('all' == WPGlobus::Config()->nav_menu) {
         /**
          * Attach to every nav menu
          * @since 1.0.7
          */
     } else {
         $items = array();
         foreach ($sorted_menu_items as $item) {
             $items[] = $item->ID;
         }
         $return = true;
         foreach ($this->menus as $key => $menu) {
             $diff = array_diff($items, $menu->menu_items);
             if (empty($diff) && WPGlobus::Config()->nav_menu === $menu->slug) {
                 $return = false;
                 break;
             }
         }
         if ($return) {
             return $sorted_menu_items;
         }
     }
     $extra_languages = array();
     foreach (WPGlobus::Config()->enabled_languages as $languages) {
         if ($languages != WPGlobus::Config()->language) {
             $extra_languages[] = $languages;
         }
     }
     /**
      * Filter extra languages.
      * Returning array.
      * @since 1.0.13
      *
      * @param array $extra_languages An array with languages to show off in menu.
      * @param       string           WPGlobus::Config()->language The current language.
      */
     $extra_languages = apply_filters('wpglobus_extra_languages', $extra_languages, WPGlobus::Config()->language);
     /** main menu item classes */
     $menu_item_classes = array('', 'menu-item', 'menu_item_wpglobus_menu_switch', 'wpglobus-selector-link');
     /** submenu item classes */
     $submenu_item_classes = array('', 'sub_menu_item_wpglobus_menu_switch', 'wpglobus-selector-link');
     if (apply_filters('wpglobus_dropdown_menu', true, WPGlobus::Config())) {
         $parent_item_ID = 9999999999;
         # 9 999 999 999
     } else {
         $parent_item_ID = 0;
     }
     $span_classes_lang = $this->_get_language_classes(WPGlobus::Config()->language);
     $current_url = WPGlobus_Utils::current_url();
     $item = new stdClass();
     $item->ID = $parent_item_ID == 0 ? 'wpglobus_menu_switch_' . WPGlobus::Config()->language : $parent_item_ID;
     $item->db_id = $parent_item_ID == 0 ? 'wpglobus_menu_switch_' . WPGlobus::Config()->language : $parent_item_ID;
     $item->menu_item_parent = 0;
     $item->title = '<span class="' . implode(' ', $span_classes_lang) . '">' . $this->_get_flag_name(WPGlobus::Config()->language) . '</span>';
     // The top menu level points to the current URL. Useless? Maybe good for refresh.
     $item->url = $current_url;
     $item->classes = $menu_item_classes;
     $item->classes[] = 'wpglobus-current-language';
     $item->description = '';
     $item->language = WPGlobus::Config()->language;
     $wpglobus_menu_items[] = $item;
     foreach ($extra_languages as $language) {
         $span_classes_lang = $this->_get_language_classes($language);
         $item = new stdClass();
         $item->ID = 'wpglobus_menu_switch_' . $language;
         $item->db_id = 'wpglobus_menu_switch_' . $language;
         $item->menu_item_parent = $parent_item_ID;
         $item->title = '<span class="' . implode(' ', $span_classes_lang) . '">' . $this->_get_flag_name($language) . '</span>';
         // This points to the URL localized for the selected language
         //			$item->url         = WPGlobus_Utils::localize_url( $current_url, $language );
         $item->url = WPGlobus_Utils::localize_current_url($language);
         $item->classes = $parent_item_ID == 0 ? $menu_item_classes : $submenu_item_classes;
         $item->description = '';
         $item->language = $language;
         $wpglobus_menu_items[] = $item;
     }
     $languages = $extra_languages;
     array_unshift($languages, WPGlobus::Config()->language);
     return array_merge($sorted_menu_items, apply_filters('wpglobus_menu_items', $wpglobus_menu_items, $languages));
 }