PluginProbe
Polylang / 3.7.7
Polylang v3.7.7
3.8.10 3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 All 234 releases
← All changes | frontend/frontend-nav-menu.php +48 -32 2.9 → 3.7.7 View file →
@@ -8,8 +8,13 @@
8 8 *
9 9 * @since 1.2
10 10 */
11 11 class PLL_Frontend_Nav_Menu extends PLL_Nav_Menu {
12 + /**
13 + * Current language.
14 + *
15 + * @var PLL_Language|null|false
16 + */
12 17 public $curlang;
13 18
14 19 /**
15 20 * Constructor
@@ -15,9 +20,9 @@
15 20 * Constructor
16 21 *
17 22 * @since 1.2
18 23 *
19 - * @param object $polylang
24 + * @param object $polylang The Polylang object.
20 25 */
21 26 public function __construct( &$polylang ) {
22 27 parent::__construct( $polylang );
23 28
@@ -39,14 +44,14 @@
39 44 }
40 45 }
41 46
42 47 /**
43 - * Sort menu items by menu order
48 + * Sorts menu items by menu order.
44 49 *
45 50 * @since 1.7.9
46 51 *
47 - * @param object $a The first object to compare
48 - * @param object $b The second object to compare
52 + * @param stdClass $a The first object to compare.
53 + * @param stdClass $b The second object to compare.
49 54 * @return int -1 or 1 if $a is considered to be respectively less than or greater than $b.
50 55 */
51 56 protected function usort_menu_items( $a, $b ) {
52 57 return ( $a->menu_order < $b->menu_order ) ? -1 : 1;
@@ -75,17 +80,21 @@
75 80 return $title;
76 81 }
77 82
78 83 /**
79 - * Splits the one item of backend in several items on frontend
80 - * take care to menu_order as it is used later in wp_nav_menu
84 + * Splits the one language switcher menu item of backend in several menu items on frontend.
85 + * Takes care to menu_order as it is used later in wp_nav_menu().
81 86 *
82 87 * @since 1.1.1
83 88 *
84 - * @param array $items menu items
85 - * @return array modified items
89 + * @param stdClass[] $items Menu items.
90 + * @return stdClass[] Modified menu items.
86 91 */
87 92 public function wp_get_nav_menu_items( $items ) {
93 + if ( empty( $this->curlang ) ) {
94 + return $items;
95 + }
96 +
88 97 if ( doing_action( 'customize_register' ) ) { // needed since WP 4.3, doing_action available since WP 3.9
89 98 return $items;
90 99 }
91 100
@@ -92,32 +101,36 @@
92 101 // The customizer menus does not sort the items and we need them to be sorted before splitting the language switcher
93 102 usort( $items, array( $this, 'usort_menu_items' ) );
94 103
95 104 $new_items = array();
105 +
96 106 $offset = 0;
97 107
98 108 foreach ( $items as $item ) {
99 109 if ( $options = get_post_meta( $item->ID, '_pll_menu_item', true ) ) {
100 - $i = 0;
101 -
102 110 /** This filter is documented in include/switcher.php */
103 111 $options = apply_filters( 'pll_the_languages_args', $options ); // Honor the filter here for 'show_flags', 'show_names' and 'dropdown'.
104 112
105 113 $switcher = new PLL_Switcher();
106 114 $args = array_merge( array( 'raw' => 1 ), $options );
115 +
116 + /** @var array */
107 117 $the_languages = $switcher->the_languages( PLL()->links, $args );
108 118
109 119 // parent item for dropdown
110 120 if ( ! empty( $options['dropdown'] ) ) {
111 121 $name = isset( $options['display_names_as'] ) && 'slug' === $options['display_names_as'] ? $this->curlang->slug : $this->curlang->name;
112 - $item->title = $this->get_item_title( $this->curlang->get_display_flag(), $name, $options );
122 + $item->title = $this->get_item_title( $this->curlang->get_display_flag( empty( $options['show_names'] ) ? 'alt' : 'no-alt' ), $name, $options );
113 123 $item->attr_title = '';
114 124 $item->classes = array( 'pll-parent-menu-item' );
125 + $item->menu_order += $offset;
115 126 $new_items[] = $item;
116 - $offset++;
127 + ++$offset;
117 128 }
118 129
130 + $i = 0; // for incrementation of menu order only in case of dropdown
119 131 foreach ( $the_languages as $lang ) {
132 + ++$i;
120 133 $lang_item = clone $item;
121 134 $lang_item->ID = $lang_item->ID . '-' . $lang['slug']; // A unique ID
122 135 $lang_item->title = $this->get_item_title( $lang['flag'], $lang['name'], $options );
123 136 $lang_item->attr_title = '';
@@ -123,16 +136,19 @@
123 136 $lang_item->attr_title = '';
124 137 $lang_item->url = $lang['url'];
125 138 $lang_item->lang = $lang['locale']; // Save this for use in nav_menu_link_attributes
126 139 $lang_item->classes = $lang['classes'];
127 - $lang_item->menu_order += $offset + $i++;
128 140 if ( ! empty( $options['dropdown'] ) ) {
141 + $lang_item->menu_order = $item->menu_order + $i;
129 142 $lang_item->menu_item_parent = $item->db_id;
130 143 $lang_item->db_id = 0; // to avoid recursion
144 + } else {
145 + $lang_item->menu_order += $offset;
131 146 }
132 147 $new_items[] = $lang_item;
148 + ++$offset;
133 149 }
134 - $offset += $i - 1;
150 + --$offset;
135 151 } else {
136 152 $item->menu_order += $offset;
137 153 $new_items[] = $item;
138 154 }
@@ -140,31 +156,31 @@
140 156 return $new_items;
141 157 }
142 158
143 159 /**
144 - * Returns the ancestors of a menu item
160 + * Returns the ancestors of a menu item.
145 161 *
146 162 * @since 1.1.1
147 163 *
148 - * @param object $item
149 - * @return array ancestors ids
164 + * @param stdClass $item Menu item.
165 + * @return int[] Ancestors ids.
150 166 */
151 167 public function get_ancestors( $item ) {
152 168 $ids = array();
153 - $_anc_id = (int) $item->db_id;
154 - while ( ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) && ! in_array( $_anc_id, $ids ) ) {
155 - $ids[] = $_anc_id;
169 + $ancestor_id = (int) $item->db_id;
170 + while ( ( $ancestor_id = get_post_meta( $ancestor_id, '_menu_item_menu_item_parent', true ) ) && ! in_array( $ancestor_id, $ids ) ) {
171 + $ids[] = $ancestor_id;
156 172 }
157 173 return $ids;
158 174 }
159 175
160 176 /**
161 - * Removes current-menu and current-menu-ancestor classes to lang switcher when not on the home page
177 + * Removes current-menu and current-menu-ancestor classes to lang switcher when not on the home page.
162 178 *
163 179 * @since 1.1.1
164 180 *
165 - * @param array $items
166 - * @return array modified menu items
181 + * @param stdClass[] $items An array of menu items.
182 + * @return stdClass[]
167 183 */
168 184 public function wp_nav_menu_objects( $items ) {
169 185 $r_ids = $k_ids = array();
170 186
@@ -191,16 +207,16 @@
191 207 return $items;
192 208 }
193 209
194 210 /**
195 - * Adds hreflang attribute for the language switcher menu items
196 - * available since WP 3.6
211 + * Adds hreflang attribute for the language switcher menu items.
212 + * available since WP 3.6.
197 213 *
198 214 * @since 1.1
199 215 *
200 - * @param array $atts
201 - * @param object $item
202 - * @return array modified $atts
216 + * @param string[] $atts HTML attributes applied to the menu item's `<a>` element.
217 + * @param stdClass $item Menu item.
218 + * @return string[] Modified attributes.
203 219 */
204 220 public function nav_menu_link_attributes( $atts, $item ) {
205 221 if ( isset( $item->lang ) ) {
206 222 $atts['lang'] = $atts['hreflang'] = esc_attr( $item->lang );
@@ -234,9 +250,9 @@
234 250 $loc = substr( trim( $key, ']' ), 19 );
235 251 $infos = $this->explode_location( $loc );
236 252 if ( $infos['lang'] === $this->curlang->slug ) {
237 253 $menus[ $infos['location'] ] = (int) $value;
238 - } elseif ( $this->curlang->slug === $this->options['default_lang'] ) {
254 + } elseif ( $this->curlang->is_default ) {
239 255 $menus[ $loc ] = (int) $value;
240 256 }
241 257 }
242 258 }
@@ -245,14 +261,14 @@
245 261 return $menus;
246 262 }
247 263
248 264 /**
249 - * Attempt to translate the nav menu when it is hardcoded or when no location is defined in wp_nav_menu
265 + * Attempts to translate the nav menu when it is hardcoded or when no location is defined in wp_nav_menu().
250 266 *
251 267 * @since 1.7.10
252 268 *
253 - * @param array $args
254 - * @return array modified $args
269 + * @param array $args Array of `wp_nav_menu()` arguments.
270 + * @return array
255 271 */
256 272 public function wp_nav_menu_args( $args ) {
257 273 $theme = get_option( 'stylesheet' );
258 274