PluginProbe
Polylang / 2.0.12
Polylang v2.0.12
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 3.0.3 All 233 releases
← All changes | admin/admin-nav-menu.php +157 -90 3.02.0.12 View file →
@@ -1,11 +1,8 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 - * Manages custom menus translations as well as the language switcher menu item on admin side
4 + * manages custom menus translations as well as the language switcher menu item on admin side
8 5 *
9 6 * @since 1.2
10 7 */
11 8 class PLL_Admin_Nav_Menu extends PLL_Nav_Menu {
@@ -10,9 +7,9 @@
10 7 */
11 8 class PLL_Admin_Nav_Menu extends PLL_Nav_Menu {
12 9
13 10 /**
14 - * Constructor: setups filters and actions
11 + * constructor: setups filters and actions
15 12 *
16 13 * @since 1.2
17 14 *
18 15 * @param object $polylang
@@ -19,32 +16,42 @@
19 16 */
20 17 public function __construct( &$polylang ) {
21 18 parent::__construct( $polylang );
22 19
23 - // Populates nav menus locations
24 - // Since WP 4.4, must be done before customize_register is fired
20 + $this->theme = get_option( 'stylesheet' );
21 +
22 + // populates nav menus locations
23 + // since WP 4.4, must be done before customize_register is fired
25 24 add_filter( 'theme_mod_nav_menu_locations', array( $this, 'theme_mod_nav_menu_locations' ), 20 );
26 25
27 - // Integration in the WP menu interface
26 + // integration in the WP menu interface
28 27 add_action( 'admin_init', array( $this, 'admin_init' ) ); // after Polylang upgrade
28 +
29 + // protection against #24802
30 + // backward compatibility with WP < 4.1
31 + if ( version_compare( $GLOBALS['wp_version'], '4.1', '<' ) ) {
32 + add_filter( 'pre_insert_term', array( $this, 'pre_insert_term' ), 10, 2 );
33 + }
29 34 }
30 35
31 36 /**
32 - * Setups filters and terms
37 + * setups filters and terms
33 38 * adds the language switcher metabox and create new nav menu locations
34 39 *
35 40 * @since 1.1
36 - *
37 - * @return void
38 41 */
39 42 public function admin_init() {
40 43 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
41 44 add_action( 'wp_update_nav_menu_item', array( $this, 'wp_update_nav_menu_item' ), 10, 2 );
45 + add_filter( 'wp_get_nav_menu_items', array( $this, 'translate_switcher_title' ) );
42 46
43 - // Translation of menus based on chosen locations
47 + // translation of menus based on chosen locations
44 48 add_filter( 'pre_update_option_theme_mods_' . $this->theme, array( $this, 'pre_update_option_theme_mods' ) );
45 49 add_action( 'delete_nav_menu', array( $this, 'delete_nav_menu' ) );
46 50
51 + // filter _wp_auto_add_pages_to_menu by language
52 + add_action( 'transition_post_status', array( $this, 'auto_add_pages_to_menu' ), 5, 3 ); // before _wp_auto_add_pages_to_menu
53 +
47 54 // FIXME is it possible to choose the order ( after theme locations in WP3.5 and older ) ?
48 55 // FIXME not displayed if Polylang is activated before the first time the user goes to nav menus http://core.trac.wordpress.org/ticket/16828
49 56 add_meta_box( 'pll_lang_switch_box', __( 'Language switcher', 'polylang' ), array( $this, 'lang_switch' ), 'nav-menus', 'side', 'high' );
50 57
@@ -51,122 +58,130 @@
51 58 $this->create_nav_menu_locations();
52 59 }
53 60
54 61 /**
55 - * Language switcher metabox
62 + * language switcher metabox
56 63 * The checkbox and all hidden fields are important
57 - * Thanks to John Morris for his very interesting post http://www.johnmorrisonline.com/how-to-add-a-fully-functional-custom-meta-box-to-wordpress-navigation-menus/
64 + * thanks to John Morris for his very interesting post http://www.johnmorrisonline.com/how-to-add-a-fully-functional-custom-meta-box-to-wordpress-navigation-menus/
58 65 *
59 66 * @since 1.1
60 - *
61 - * @return void
62 67 */
63 68 public function lang_switch() {
64 69 global $_nav_menu_placeholder, $nav_menu_selected_id;
65 - $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1;
66 - ?>
70 + $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1; ?>
71 +
67 72 <div id="posttype-lang-switch" class="posttypediv">
68 73 <div id="tabs-panel-lang-switch" class="tabs-panel tabs-panel-active">
69 - <ul id="lang-switch-checklist" class="categorychecklist form-no-clear">
74 + <ul id ="lang-switch-checklist" class="categorychecklist form-no-clear">
70 75 <li>
71 76 <label class="menu-item-title">
72 - <input type="checkbox" class="menu-item-checkbox" name="menu-item[<?php echo (int) $_nav_menu_placeholder; ?>][menu-item-object-id]" value="-1"> <?php esc_html_e( 'Languages', 'polylang' ); ?>
77 + <input type="checkbox" class="menu-item-checkbox" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-object-id]" value="-1"> <?php esc_html_e( 'Language switcher', 'polylang' ); ?>
73 78 </label>
74 - <input type="hidden" class="menu-item-type" name="menu-item[<?php echo (int) $_nav_menu_placeholder; ?>][menu-item-type]" value="custom">
75 - <input type="hidden" class="menu-item-title" name="menu-item[<?php echo (int) $_nav_menu_placeholder; ?>][menu-item-title]" value="<?php esc_html_e( 'Languages', 'polylang' ); ?>">
76 - <input type="hidden" class="menu-item-url" name="menu-item[<?php echo (int) $_nav_menu_placeholder; ?>][menu-item-url]" value="#pll_switcher">
77 - </li>
78 - </ul>
79 - </div>
80 - <p class="button-controls">
81 - <span class="add-to-menu">
82 - <input type="submit" <?php disabled( $nav_menu_selected_id, 0 ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu', 'polylang' ); ?>" name="add-post-type-menu-item" id="submit-posttype-lang-switch">
83 - <span class="spinner"></span>
84 - </span>
85 - </p>
86 - </div>
87 - <?php
79 + <input type="hidden" class="menu-item-type" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-type]" value="custom">
80 + <input type="hidden" class="menu-item-title" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-title]" value="<?php esc_html_e( 'Language switcher', 'polylang' ); ?>">
81 + <input type="hidden" class="menu-item-url" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-url]" value="#pll_switcher">
82 + </li>
83 + </ul>
84 + </div>
85 + <p class="button-controls">
86 + <span class="add-to-menu">
87 + <input type="submit" <?php disabled( $nav_menu_selected_id, 0 ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-post-type-menu-item" id="submit-posttype-lang-switch">
88 + <span class="spinner"></span>
89 + </span>
90 + </p>
91 + </div><?php
88 92 }
89 93
90 94 /**
91 - * Prepares javascript to modify the language switcher menu item
95 + * prepares javascript to modify the language switcher menu item
92 96 *
93 97 * @since 1.1
94 - *
95 - * @return void
96 98 */
97 99 public function admin_enqueue_scripts() {
98 100 $screen = get_current_screen();
99 - if ( empty( $screen ) || 'nav-menus' !== $screen->base ) {
101 + if ( 'nav-menus' != $screen->base ) {
100 102 return;
101 103 }
102 104
103 105 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
104 - wp_enqueue_script( 'pll_nav_menu', plugins_url( '/js/build/nav-menu' . $suffix . '.js', POLYLANG_BASENAME ), array( 'jquery' ), POLYLANG_VERSION );
106 + wp_enqueue_script( 'pll_nav_menu', POLYLANG_URL .'/js/nav-menu' . $suffix . '.js', array( 'jquery' ), POLYLANG_VERSION );
105 107
106 - $data = array(
107 - 'strings' => PLL_Switcher::get_switcher_options( 'menu', 'string' ), // The strings for the options
108 - 'title' => __( 'Languages', 'polylang' ), // The title
109 - 'val' => array(),
110 - );
108 + $data['strings'] = PLL_Switcher::get_switcher_options( 'menu', 'string' ); // the strings for the options
109 + $data['title'] = __( 'Language switcher', 'polylang' ); // the title
111 110
112 - // Get all language switcher menu items
113 - $items = get_posts(
114 - array(
115 - 'numberposts' => -1,
116 - 'nopaging' => true,
117 - 'post_type' => 'nav_menu_item',
118 - 'fields' => 'ids',
119 - 'meta_key' => '_pll_menu_item',
120 - )
121 - );
111 + // get all language switcher menu items
112 + $items = get_posts( array(
113 + 'numberposts' => -1,
114 + 'nopaging' => true,
115 + 'post_type' => 'nav_menu_item',
116 + 'fields' => 'ids',
117 + 'meta_key' => '_pll_menu_item',
118 + ) );
122 119
123 - // The options values for the language switcher
120 + // the options values for the language switcher
121 + $data['val'] = array();
124 122 foreach ( $items as $item ) {
125 123 $data['val'][ $item ] = get_post_meta( $item, '_pll_menu_item', true );
126 124 }
127 125
128 - // Send all these data to javascript
126 + // send all these data to javascript
129 127 wp_localize_script( 'pll_nav_menu', 'pll_data', $data );
130 128 }
131 129
132 130 /**
133 - * Save our menu item options
131 + * save our menu item options
134 132 *
135 133 * @since 1.1
136 134 *
137 135 * @param int $menu_id not used
138 136 * @param int $menu_item_db_id
139 - * @return void
140 137 */
141 138 public function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0 ) {
142 - if ( empty( $_POST['menu-item-url'][ $menu_item_db_id ] ) || '#pll_switcher' !== $_POST['menu-item-url'][ $menu_item_db_id ] ) { // phpcs:ignore WordPress.Security.NonceVerification
139 + if ( empty( $_POST['menu-item-url'][ $menu_item_db_id ] ) || '#pll_switcher' != $_POST['menu-item-url'][ $menu_item_db_id ] ) {
143 140 return;
144 141 }
145 142
146 - // Security check as 'wp_update_nav_menu_item' can be called from outside WP admin
143 + // security check
144 + // as 'wp_update_nav_menu_item' can be called from outside WP admin
147 145 if ( current_user_can( 'edit_theme_options' ) ) {
148 146 check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );
149 147
150 - $options = array( 'hide_if_no_translation' => 0, 'hide_current' => 0, 'force_home' => 0, 'show_flags' => 0, 'show_names' => 1, 'dropdown' => 0 ); // Default values
151 - // Our jQuery form has not been displayed
148 + $options = array( 'hide_if_no_translation' => 0, 'hide_current' => 0,'force_home' => 0 ,'show_flags' => 0 ,'show_names' => 1, 'dropdown' => 0 ); // default values
149 + // our jQuery form has not been displayed
152 150 if ( empty( $_POST['menu-item-pll-detect'][ $menu_item_db_id ] ) ) {
153 - if ( ! get_post_meta( $menu_item_db_id, '_pll_menu_item', true ) ) { // Our options were never saved
151 + if ( ! get_post_meta( $menu_item_db_id, '_pll_menu_item', true ) ) { // our options were never saved
154 152 update_post_meta( $menu_item_db_id, '_pll_menu_item', $options );
155 153 }
156 154 }
157 155 else {
158 - foreach ( array_keys( $options ) as $opt ) {
156 + foreach ( $options as $opt => $v ) {
159 157 $options[ $opt ] = empty( $_POST[ 'menu-item-' . $opt ][ $menu_item_db_id ] ) ? 0 : 1;
160 158 }
161 - update_post_meta( $menu_item_db_id, '_pll_menu_item', $options ); // Allow us to easily identify our nav menu item
159 + update_post_meta( $menu_item_db_id, '_pll_menu_item', $options ); // allow us to easily identify our nav menu item
162 160 }
163 161 }
164 162 }
165 163
166 164 /**
167 - * Assign menu languages and translations based on ( temporary ) locations
165 + * translates the language switcher menu items title in case the user switches the admin language
168 166 *
167 + * @since 1.1.1
168 + *
169 + * @param array $items
170 + * @return array modified $items
171 + */
172 + public function translate_switcher_title( $items ) {
173 + foreach ( $items as $item ) {
174 + if ( '#pll_switcher' == $item->url ) {
175 + $item->post_title = __( 'Language switcher', 'polylang' );
176 + }
177 + }
178 + return $items;
179 + }
180 +
181 + /**
182 + * assign menu languages and translations based on ( temporary ) locations
183 + *
169 184 * @since 1.8
170 185 *
171 186 * @param array $locations nav menu locations
172 187 * @return array
@@ -171,14 +186,16 @@
171 186 * @param array $locations nav menu locations
172 187 * @return array
173 188 */
174 189 public function update_nav_menu_locations( $locations ) {
175 - // Extract language and menu from locations
190 + $default = $this->options['default_lang'];
191 +
192 + // extract language and menu from locations
176 193 foreach ( $locations as $loc => $menu ) {
177 194 $infos = $this->explode_location( $loc );
178 195 $this->options['nav_menus'][ $this->theme ][ $infos['location'] ][ $infos['lang'] ] = $menu;
179 196 if ( $this->options['default_lang'] != $infos['lang'] ) {
180 - unset( $locations[ $loc ] ); // Remove temporary locations before database update
197 + unset( $locations[ $loc ] ); // remove temporary locations before database update
181 198 }
182 199 }
183 200
184 201 update_option( 'polylang', $this->options );
@@ -185,33 +202,33 @@
185 202 return $locations;
186 203 }
187 204
188 205 /**
189 - * Assign menu languages and translations based on ( temporary ) locations.
206 + * assign menu languages and translations based on ( temporary ) locations
190 207 *
191 208 * @since 1.1
192 209 *
193 - * @param mixed $mods Theme mods.
194 - * @return mixed
210 + * @param array $mods theme mods
211 + * @return unmodified $mods
195 212 */
196 213 public function pre_update_option_theme_mods( $mods ) {
197 214 if ( current_user_can( 'edit_theme_options' ) && isset( $mods['nav_menu_locations'] ) ) {
198 215
199 216 // Manage Locations tab in Appearance -> Menus
200 - if ( isset( $_GET['action'] ) && 'locations' === $_GET['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification
217 + if ( isset( $_GET['action'] ) && 'locations' == $_GET['action'] ) {
201 218 check_admin_referer( 'save-menu-locations' );
202 219 $this->options['nav_menus'][ $this->theme ] = array();
203 220 }
204 221
205 222 // Edit Menus tab in Appearance -> Menus
206 - // Add the test of $_POST['update-nav-menu-nonce'] to avoid conflict with Vantage theme
207 - elseif ( isset( $_POST['action'], $_POST['update-nav-menu-nonce'] ) && 'update' === $_POST['action'] ) {
223 + // add the test of $_POST['update-nav-menu-nonce'] to avoid conflict with Vantage theme
224 + elseif ( isset( $_POST['action'], $_POST['update-nav-menu-nonce'] ) && 'update' == $_POST['action'] ) {
208 225 check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );
209 226 $this->options['nav_menus'][ $this->theme ] = array();
210 227 }
211 228
212 - // Customizer
213 - // Don't reset locations in this case.
229 + // customizer
230 + // don't reset locations in this case.
214 231 // see http://wordpress.org/support/topic/menus-doesnt-show-and-not-saved-in-theme-settings-multilingual-site
215 232 elseif ( isset( $_POST['action'] ) && 'customize_save' == $_POST['action'] ) {
216 233 check_ajax_referer( 'save-customize_' . $GLOBALS['wp_customize']->get_stylesheet(), 'nonce' );
217 234 }
@@ -216,9 +233,9 @@
216 233 check_ajax_referer( 'save-customize_' . $GLOBALS['wp_customize']->get_stylesheet(), 'nonce' );
217 234 }
218 235
219 236 else {
220 - return $mods; // No modification for nav menu locations
237 + return $mods; // no modification for nav menu locations
221 238 }
222 239
223 240 $mods['nav_menu_locations'] = $this->update_nav_menu_locations( $mods['nav_menu_locations'] );
224 241 }
@@ -225,9 +242,9 @@
225 242 return $mods;
226 243 }
227 244
228 245 /**
229 - * Fills temporary menu locations based on menus translations
246 + * fills temporary menu locations based on menus translations
230 247 *
231 248 * @since 1.2
232 249 *
233 250 * @param bool|array $menus
@@ -233,17 +250,10 @@
233 250 * @param bool|array $menus
234 251 * @return bool|array modified list of menu locations
235 252 */
236 253 public function theme_mod_nav_menu_locations( $menus ) {
237 - // Prefill locations with 0 value in case a location does not exist in $menus
238 - $locations = get_registered_nav_menus();
239 - if ( is_array( $locations ) ) {
240 - $locations = array_fill_keys( array_keys( $locations ), 0 );
241 - $menus = is_array( $menus ) ? array_merge( $locations, $menus ) : $locations;
242 - }
243 -
244 254 if ( is_array( $menus ) ) {
245 - foreach ( array_keys( $menus ) as $loc ) {
255 + foreach ( $menus as $loc => $menu ) {
246 256 foreach ( $this->model->get_languages_list() as $lang ) {
247 257 if ( ! empty( $this->options['nav_menus'][ $this->theme ][ $loc ][ $lang->slug ] ) && term_exists( $this->options['nav_menus'][ $this->theme ][ $loc ][ $lang->slug ], 'nav_menu' ) ) {
248 258 $menus[ $this->combine_location( $loc, $lang ) ] = $this->options['nav_menus'][ $this->theme ][ $loc ][ $lang->slug ];
249 259 }
@@ -254,16 +264,15 @@
254 264 return $menus;
255 265 }
256 266
257 267 /**
258 - * Removes the nav menu term_id from the locations stored in Polylang options when a nav menu is deleted
268 + * removes the nav menu term_id from the locations stored in Polylang options when a nav menu is deleted
259 269 *
260 270 * @since 1.7.3
261 271 *
262 - * @param int $term_id nav menu id
263 - * @return void
272 + * @param int nav menu id
264 273 */
265 - public function delete_nav_menu( $term_id ) {
274 + function delete_nav_menu( $term_id ) {
266 275 if ( isset( $this->options['nav_menus'] ) ) {
267 276 foreach ( $this->options['nav_menus'] as $theme => $locations ) {
268 277 foreach ( $locations as $loc => $languages ) {
269 278 foreach ( $languages as $lang => $menu_id ) {
@@ -275,6 +284,64 @@
275 284 }
276 285
277 286 update_option( 'polylang', $this->options );
278 287 }
288 + }
289 +
290 + /**
291 + * filters the option nav_menu_options for auto added pages to menu
292 + *
293 + * @since 0.9.4
294 + *
295 + * @param array $options
296 + * @return array Modified options
297 + */
298 + public function nav_menu_options( $options ) {
299 + $options['auto_add'] = array_intersect( $options['auto_add'], array( $this->auto_add_menus ) );
300 + return $options;
301 + }
302 +
303 + /**
304 + * filters _wp_auto_add_pages_to_menu by language
305 + *
306 + * @since 0.9.4
307 + *
308 + * @param string $new_status Transition to this post status.
309 + * @param string $old_status Previous post status.
310 + * @param object $post Post data.
311 + */
312 + public function auto_add_pages_to_menu( $new_status, $old_status, $post ) {
313 + if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type || ! empty( $post->post_parent ) || ! ( $lang = $this->model->post->get_language( $post->ID ) ) ) {
314 + return;
315 + }
316 +
317 + if ( ! empty( $this->options['nav_menus'][ $this->theme ] ) ) {
318 + // get all the menus in the page language
319 + foreach ( $this->options['nav_menus'][ $this->theme ] as $loc ) {
320 + if ( ! empty( $loc[ $lang->slug ] ) ) {
321 + $menus[] = $loc[ $lang->slug ];
322 + }
323 + }
324 +
325 + if ( ! empty( $menus ) ) {
326 + $this->auto_add_menus = implode( ',', $menus );
327 + add_filter( 'option_nav_menu_options', array( $this, 'nav_menu_options' ) );
328 + }
329 + }
330 + }
331 +
332 + /**
333 + * prevents sharing a menu term with a language term by renaming the nav menu before its creation
334 + * to avoid http://core.trac.wordpress.org/ticket/24802
335 + * and http://wordpress.org/support/topic/all-connection-between-elements-lost
336 + * backward compatibility with WP < 4.1
337 + *
338 + * @since 1.1.3
339 + *
340 + * @param string $name term name
341 + * @param string $taxonomy
342 + * @return string modified ( nav menu ) term name if necessary
343 + */
344 + function pre_insert_term( $name, $taxonomy ) {
345 + return ( 'nav_menu' == $taxonomy && in_array( $name, $this->model->get_languages_list( array( 'fields' => 'name' ) ) ) ) ? $name .= '-menu' : $name;
279 346 }
280 347 }