| @@ -2,8 +2,13 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * @package Polylang |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | +use WP_Syntex\Polylang\Switcher\Switcher; | |
| 7 | +use WP_Syntex\Polylang\Switcher\Element\Nav; | |
| 8 | +use WP_Syntex\Polylang\Switcher\Fields\Menu as Fields; | |
| 9 | +use WP_Syntex\Polylang\Switcher\Settings\Menu as Settings; | |
| 10 | + | |
| 6 | 11 | /** |
| 7 | 12 | * Manages custom menus translations as well as the language switcher menu item on frontend |
| 8 | 13 | * |
| 9 | 14 | * @since 1.2 |
| @@ -16,8 +21,13 @@ | ||
| 16 | 21 | */ |
| 17 | 22 | public $curlang; |
| 18 | 23 | |
| 19 | 24 | /** |
| 25 | + * @var PLL_Links|null | |
| 26 | + */ | |
| 27 | + private ?PLL_Links $links; | |
| 28 | + | |
| 29 | + /** | |
| 20 | 30 | * Constructor |
| 21 | 31 | * |
| 22 | 32 | * @since 1.2 |
| 23 | 33 | * |
| @@ -26,8 +36,9 @@ | ||
| 26 | 36 | public function __construct( &$polylang ) { |
| 27 | 37 | parent::__construct( $polylang ); |
| 28 | 38 | |
| 29 | 39 | $this->curlang = &$polylang->curlang; |
| 40 | + $this->links = &$polylang->links; | |
| 30 | 41 | |
| 31 | 42 | // Split the language switcher menu item in several language menu items |
| 32 | 43 | add_filter( 'wp_get_nav_menu_items', array( $this, 'wp_get_nav_menu_items' ), 20 ); // after the customizer menus |
| 33 | 44 | add_filter( 'wp_nav_menu_objects', array( $this, 'wp_nav_menu_objects' ) ); |
| @@ -57,31 +68,8 @@ | ||
| 57 | 68 | return ( $a->menu_order < $b->menu_order ) ? -1 : 1; |
| 58 | 69 | } |
| 59 | 70 | |
| 60 | 71 | /** |
| 61 | - * Format a language switcher menu item title based on options | |
| 62 | - * | |
| 63 | - * @since 2.2.6 | |
| 64 | - * | |
| 65 | - * @param string $flag Formatted flag | |
| 66 | - * @param string $name Language name | |
| 67 | - * @param array $options Language switcher options | |
| 68 | - * @return string Formatted menu item title | |
| 69 | - */ | |
| 70 | - protected function get_item_title( $flag, $name, $options ) { | |
| 71 | - if ( $options['show_flags'] ) { | |
| 72 | - if ( $options['show_names'] ) { | |
| 73 | - $title = sprintf( '%1$s<span style="margin-%2$s:0.3em;">%3$s</span>', $flag, is_rtl() ? 'right' : 'left', esc_html( $name ) ); | |
| 74 | - } else { | |
| 75 | - $title = $flag; | |
| 76 | - } | |
| 77 | - } else { | |
| 78 | - $title = esc_html( $name ); | |
| 79 | - } | |
| 80 | - return $title; | |
| 81 | - } | |
| 82 | - | |
| 83 | - /** | |
| 84 | 72 | * Splits the one language switcher menu item of backend in several menu items on frontend. |
| 85 | 73 | * Takes care to menu_order as it is used later in wp_nav_menu(). |
| 86 | 74 | * |
| 87 | 75 | * @since 1.1.1 |
| @@ -89,9 +77,9 @@ | ||
| 89 | 77 | * @param stdClass[] $items Menu items. |
| 90 | 78 | * @return stdClass[] Modified menu items. |
| 91 | 79 | */ |
| 92 | 80 | public function wp_get_nav_menu_items( $items ) { |
| 93 | - if ( empty( $this->curlang ) ) { | |
| 81 | + if ( empty( $this->curlang ) || empty( $this->links ) ) { | |
| 94 | 82 | return $items; |
| 95 | 83 | } |
| 96 | 84 | |
| 97 | 85 | if ( doing_action( 'customize_register' ) ) { // needed since WP 4.3, doing_action available since WP 3.9 |
| @@ -101,59 +89,73 @@ | ||
| 101 | 89 | // The customizer menus does not sort the items and we need them to be sorted before splitting the language switcher |
| 102 | 90 | usort( $items, array( $this, 'usort_menu_items' ) ); |
| 103 | 91 | |
| 104 | 92 | $new_items = array(); |
| 93 | + $offset = 0; | |
| 105 | 94 | |
| 106 | - $offset = 0; | |
| 95 | + foreach ( $items as $item ) { | |
| 96 | + $options = get_post_meta( $item->ID, '_pll_menu_item', true ); | |
| 107 | 97 | |
| 108 | - foreach ( $items as $item ) { | |
| 109 | - if ( $options = get_post_meta( $item->ID, '_pll_menu_item', true ) ) { | |
| 110 | - /** This filter is documented in src/switcher.php */ | |
| 111 | - $options = apply_filters( 'pll_the_languages_args', $options ); // Honor the filter here for 'show_flags', 'show_names' and 'dropdown'. | |
| 98 | + if ( empty( $options ) || ! is_array( $options ) ) { | |
| 99 | + $item->menu_order += $offset; | |
| 100 | + $new_items[] = $item; | |
| 101 | + continue; | |
| 102 | + } | |
| 112 | 103 | |
| 113 | - $switcher = new PLL_Switcher(); | |
| 114 | - $args = array_merge( array( 'raw' => 1 ), $options ); | |
| 104 | + $settings = new Settings( Fields::remove_legacy_settings( $options ) ); | |
| 105 | + $elements = ( new Switcher( $settings, $this->links ) )->get_elements(); | |
| 115 | 106 | |
| 116 | - /** @var array */ | |
| 117 | - $the_languages = $switcher->the_languages( PLL()->links, $args ); | |
| 107 | + if ( empty( $elements ) ) { | |
| 108 | + continue; | |
| 109 | + } | |
| 118 | 110 | |
| 119 | - // parent item for dropdown | |
| 120 | - if ( ! empty( $options['dropdown'] ) ) { | |
| 121 | - $name = isset( $options['display_names_as'] ) && 'slug' === $options['display_names_as'] ? $this->curlang->slug : $this->curlang->name; | |
| 122 | - $item->title = $this->get_item_title( $this->curlang->get_display_flag( empty( $options['show_names'] ) ? 'alt' : 'no-alt' ), $name, $options ); | |
| 123 | - $item->attr_title = ''; | |
| 124 | - $item->classes = array( 'pll-parent-menu-item' ); | |
| 125 | - $item->menu_order += $offset; | |
| 126 | - $new_items[] = $item; | |
| 127 | - ++$offset; | |
| 111 | + if ( 'dropdown' === $settings->layout ) { | |
| 112 | + // Parent item for dropdown. | |
| 113 | + $element = new Nav( $this->curlang, $settings, $this->links ); | |
| 114 | + | |
| 115 | + $item->title = $element->get_label(); | |
| 116 | + $item->attr_title = ''; | |
| 117 | + $item->url = $element->url; | |
| 118 | + $item->classes = array_merge( $item->classes, $element->item_classes, array( 'pll-parent-menu-item' ) ); | |
| 119 | + $item->menu_order += $offset; | |
| 120 | + | |
| 121 | + if ( $settings->show_flags ) { | |
| 122 | + // Since it is added to the parent `<li>`, no need to add it to the child elements too. | |
| 123 | + $item->classes[] = 'pll-aspect-ratio-' . str_replace( ':', '', $settings->flag_aspect_ratio ); | |
| 128 | 124 | } |
| 129 | 125 | |
| 130 | - $i = 0; // for incrementation of menu order only in case of dropdown | |
| 131 | - foreach ( $the_languages as $lang ) { | |
| 132 | - ++$i; | |
| 133 | - $lang_item = clone $item; | |
| 134 | - $lang_item->ID = $lang_item->ID . '-' . $lang['slug']; // A unique ID | |
| 135 | - $lang_item->title = $this->get_item_title( $lang['flag'], $lang['name'], $options ); | |
| 136 | - $lang_item->attr_title = ''; | |
| 137 | - $lang_item->url = $lang['url']; | |
| 138 | - $lang_item->lang = $lang['locale']; // Save this for use in nav_menu_link_attributes | |
| 139 | - $lang_item->classes = $lang['classes']; | |
| 140 | - if ( ! empty( $options['dropdown'] ) ) { | |
| 141 | - $lang_item->menu_order = $item->menu_order + $i; | |
| 142 | - $lang_item->menu_item_parent = $item->db_id; | |
| 143 | - $lang_item->db_id = 0; // to avoid recursion | |
| 144 | - } else { | |
| 145 | - $lang_item->menu_order += $offset; | |
| 146 | - } | |
| 147 | - $new_items[] = $lang_item; | |
| 148 | - ++$offset; | |
| 126 | + $new_items[] = $item; | |
| 127 | + ++$offset; | |
| 128 | + } | |
| 129 | + | |
| 130 | + $i = 0; // For incrementation of menu order only in case of dropdown. | |
| 131 | + foreach ( $elements as $element ) { | |
| 132 | + ++$i; | |
| 133 | + $lang_item = clone $item; | |
| 134 | + | |
| 135 | + $lang_item->ID = "{$lang_item->ID}-{$element->slug}"; // A unique ID. | |
| 136 | + $lang_item->title = $element->get_label(); | |
| 137 | + $lang_item->attr_title = ''; | |
| 138 | + $lang_item->url = $element->url; | |
| 139 | + $lang_item->lang = $element->locale; // Save this for use in nav_menu_link_attributes. | |
| 140 | + $lang_item->classes = $element->item_classes; | |
| 141 | + | |
| 142 | + if ( 'dropdown' === $settings->layout ) { | |
| 143 | + $lang_item->menu_order = $item->menu_order + $i; | |
| 144 | + $lang_item->menu_item_parent = $item->db_id; | |
| 145 | + $lang_item->db_id = 0; // To avoid recursion. | |
| 146 | + } else { | |
| 147 | + $lang_item->classes = array_diff( $lang_item->classes, array( 'lang-item-first' ) ); // Doesn't mean anything here. | |
| 148 | + $lang_item->classes[] = 'pll-aspect-ratio-' . str_replace( ':', '', $settings->flag_aspect_ratio ); | |
| 149 | + $lang_item->menu_order += $offset; | |
| 149 | 150 | } |
| 150 | - --$offset; | |
| 151 | - } else { | |
| 152 | - $item->menu_order += $offset; | |
| 153 | - $new_items[] = $item; | |
| 151 | + | |
| 152 | + $new_items[] = $lang_item; | |
| 153 | + ++$offset; | |
| 154 | 154 | } |
| 155 | + --$offset; | |
| 155 | 156 | } |
| 157 | + | |
| 156 | 158 | return $new_items; |
| 157 | 159 | } |
| 158 | 160 | |
| 159 | 161 | /** |