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 +154 -79 2.82.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,18 +16,26 @@
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 41 */
@@ -36,13 +41,17 @@
36 41 */
37 42 public function admin_init() {
38 43 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
39 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' ) );
40 46
41 - // Translation of menus based on chosen locations
47 + // translation of menus based on chosen locations
42 48 add_filter( 'pre_update_option_theme_mods_' . $this->theme, array( $this, 'pre_update_option_theme_mods' ) );
43 49 add_action( 'delete_nav_menu', array( $this, 'delete_nav_menu' ) );
44 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 +
45 54 // FIXME is it possible to choose the order ( after theme locations in WP3.5 and older ) ?
46 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
47 56 add_meta_box( 'pll_lang_switch_box', __( 'Language switcher', 'polylang' ), array( $this, 'lang_switch' ), 'nav-menus', 'side', 'high' );
48 57
@@ -49,43 +58,42 @@
49 58 $this->create_nav_menu_locations();
50 59 }
51 60
52 61 /**
53 - * Language switcher metabox
62 + * language switcher metabox
54 63 * The checkbox and all hidden fields are important
55 - * 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/
56 65 *
57 66 * @since 1.1
58 67 */
59 68 public function lang_switch() {
60 69 global $_nav_menu_placeholder, $nav_menu_selected_id;
61 - $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1;
62 - ?>
70 + $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1; ?>
71 +
63 72 <div id="posttype-lang-switch" class="posttypediv">
64 73 <div id="tabs-panel-lang-switch" class="tabs-panel tabs-panel-active">
65 - <ul id="lang-switch-checklist" class="categorychecklist form-no-clear">
74 + <ul id ="lang-switch-checklist" class="categorychecklist form-no-clear">
66 75 <li>
67 76 <label class="menu-item-title">
68 - <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' ); ?>
69 78 </label>
70 - <input type="hidden" class="menu-item-type" name="menu-item[<?php echo (int) $_nav_menu_placeholder; ?>][menu-item-type]" value="custom">
71 - <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' ); ?>">
72 - <input type="hidden" class="menu-item-url" name="menu-item[<?php echo (int) $_nav_menu_placeholder; ?>][menu-item-url]" value="#pll_switcher">
73 - </li>
74 - </ul>
75 - </div>
76 - <p class="button-controls">
77 - <span class="add-to-menu">
78 - <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">
79 - <span class="spinner"></span>
80 - </span>
81 - </p>
82 - </div>
83 - <?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
84 92 }
85 93
86 94 /**
87 - * Prepares javascript to modify the language switcher menu item
95 + * prepares javascript to modify the language switcher menu item
88 96 *
89 97 * @since 1.1
90 98 */
91 99 public function admin_enqueue_scripts() {
@@ -94,38 +102,34 @@
94 102 return;
95 103 }
96 104
97 105 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
98 - wp_enqueue_script( 'pll_nav_menu', plugins_url( '/js/nav-menu' . $suffix . '.js', POLYLANG_FILE ), array( 'jquery' ), POLYLANG_VERSION );
106 + wp_enqueue_script( 'pll_nav_menu', POLYLANG_URL .'/js/nav-menu' . $suffix . '.js', array( 'jquery' ), POLYLANG_VERSION );
99 107
100 - $data = array(
101 - 'strings' => PLL_Switcher::get_switcher_options( 'menu', 'string' ), // The strings for the options
102 - 'title' => __( 'Languages', 'polylang' ), // The title
103 - 'val' => array(),
104 - );
108 + $data['strings'] = PLL_Switcher::get_switcher_options( 'menu', 'string' ); // the strings for the options
109 + $data['title'] = __( 'Language switcher', 'polylang' ); // the title
105 110
106 - // Get all language switcher menu items
107 - $items = get_posts(
108 - array(
109 - 'numberposts' => -1,
110 - 'nopaging' => true,
111 - 'post_type' => 'nav_menu_item',
112 - 'fields' => 'ids',
113 - 'meta_key' => '_pll_menu_item',
114 - )
115 - );
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 + ) );
116 119
117 - // The options values for the language switcher
120 + // the options values for the language switcher
121 + $data['val'] = array();
118 122 foreach ( $items as $item ) {
119 123 $data['val'][ $item ] = get_post_meta( $item, '_pll_menu_item', true );
120 124 }
121 125
122 - // Send all these data to javascript
126 + // send all these data to javascript
123 127 wp_localize_script( 'pll_nav_menu', 'pll_data', $data );
124 128 }
125 129
126 130 /**
127 - * Save our menu item options
131 + * save our menu item options
128 132 *
129 133 * @since 1.1
130 134 *
131 135 * @param int $menu_id not used
@@ -131,35 +135,53 @@
131 135 * @param int $menu_id not used
132 136 * @param int $menu_item_db_id
133 137 */
134 138 public function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0 ) {
135 - 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 ] ) {
136 140 return;
137 141 }
138 142
139 - // 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
140 145 if ( current_user_can( 'edit_theme_options' ) ) {
141 146 check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );
142 147
143 - $options = array( 'hide_if_no_translation' => 0, 'hide_current' => 0, 'force_home' => 0, 'show_flags' => 0, 'show_names' => 1, 'dropdown' => 0 ); // Default values
144 - // 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
145 150 if ( empty( $_POST['menu-item-pll-detect'][ $menu_item_db_id ] ) ) {
146 - 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
147 152 update_post_meta( $menu_item_db_id, '_pll_menu_item', $options );
148 153 }
149 154 }
150 155 else {
151 - foreach ( array_keys( $options ) as $opt ) {
156 + foreach ( $options as $opt => $v ) {
152 157 $options[ $opt ] = empty( $_POST[ 'menu-item-' . $opt ][ $menu_item_db_id ] ) ? 0 : 1;
153 158 }
154 - 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
155 160 }
156 161 }
157 162 }
158 163
159 164 /**
160 - * 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
161 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 + *
162 184 * @since 1.8
163 185 *
164 186 * @param array $locations nav menu locations
165 187 * @return array
@@ -164,14 +186,16 @@
164 186 * @param array $locations nav menu locations
165 187 * @return array
166 188 */
167 189 public function update_nav_menu_locations( $locations ) {
168 - // Extract language and menu from locations
190 + $default = $this->options['default_lang'];
191 +
192 + // extract language and menu from locations
169 193 foreach ( $locations as $loc => $menu ) {
170 194 $infos = $this->explode_location( $loc );
171 195 $this->options['nav_menus'][ $this->theme ][ $infos['location'] ][ $infos['lang'] ] = $menu;
172 196 if ( $this->options['default_lang'] != $infos['lang'] ) {
173 - unset( $locations[ $loc ] ); // Remove temporary locations before database update
197 + unset( $locations[ $loc ] ); // remove temporary locations before database update
174 198 }
175 199 }
176 200
177 201 update_option( 'polylang', $this->options );
@@ -178,9 +202,9 @@
178 202 return $locations;
179 203 }
180 204
181 205 /**
182 - * Assign menu languages and translations based on ( temporary ) locations
206 + * assign menu languages and translations based on ( temporary ) locations
183 207 *
184 208 * @since 1.1
185 209 *
186 210 * @param array $mods theme mods
@@ -189,22 +213,22 @@
189 213 public function pre_update_option_theme_mods( $mods ) {
190 214 if ( current_user_can( 'edit_theme_options' ) && isset( $mods['nav_menu_locations'] ) ) {
191 215
192 216 // Manage Locations tab in Appearance -> Menus
193 - if ( isset( $_GET['action'] ) && 'locations' === $_GET['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification
217 + if ( isset( $_GET['action'] ) && 'locations' == $_GET['action'] ) {
194 218 check_admin_referer( 'save-menu-locations' );
195 219 $this->options['nav_menus'][ $this->theme ] = array();
196 220 }
197 221
198 222 // Edit Menus tab in Appearance -> Menus
199 - // Add the test of $_POST['update-nav-menu-nonce'] to avoid conflict with Vantage theme
200 - 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'] ) {
201 225 check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );
202 226 $this->options['nav_menus'][ $this->theme ] = array();
203 227 }
204 228
205 - // Customizer
206 - // Don't reset locations in this case.
229 + // customizer
230 + // don't reset locations in this case.
207 231 // see http://wordpress.org/support/topic/menus-doesnt-show-and-not-saved-in-theme-settings-multilingual-site
208 232 elseif ( isset( $_POST['action'] ) && 'customize_save' == $_POST['action'] ) {
209 233 check_ajax_referer( 'save-customize_' . $GLOBALS['wp_customize']->get_stylesheet(), 'nonce' );
210 234 }
@@ -209,9 +233,9 @@
209 233 check_ajax_referer( 'save-customize_' . $GLOBALS['wp_customize']->get_stylesheet(), 'nonce' );
210 234 }
211 235
212 236 else {
213 - return $mods; // No modification for nav menu locations
237 + return $mods; // no modification for nav menu locations
214 238 }
215 239
216 240 $mods['nav_menu_locations'] = $this->update_nav_menu_locations( $mods['nav_menu_locations'] );
217 241 }
@@ -218,9 +242,9 @@
218 242 return $mods;
219 243 }
220 244
221 245 /**
222 - * Fills temporary menu locations based on menus translations
246 + * fills temporary menu locations based on menus translations
223 247 *
224 248 * @since 1.2
225 249 *
226 250 * @param bool|array $menus
@@ -226,17 +250,10 @@
226 250 * @param bool|array $menus
227 251 * @return bool|array modified list of menu locations
228 252 */
229 253 public function theme_mod_nav_menu_locations( $menus ) {
230 - // Prefill locations with 0 value in case a location does not exist in $menus
231 - $locations = get_registered_nav_menus();
232 - if ( is_array( $locations ) ) {
233 - $locations = array_fill_keys( array_keys( $locations ), 0 );
234 - $menus = is_array( $menus ) ? array_merge( $locations, $menus ) : $locations;
235 - }
236 -
237 254 if ( is_array( $menus ) ) {
238 - foreach ( array_keys( $menus ) as $loc ) {
255 + foreach ( $menus as $loc => $menu ) {
239 256 foreach ( $this->model->get_languages_list() as $lang ) {
240 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' ) ) {
241 258 $menus[ $this->combine_location( $loc, $lang ) ] = $this->options['nav_menus'][ $this->theme ][ $loc ][ $lang->slug ];
242 259 }
@@ -247,15 +264,15 @@
247 264 return $menus;
248 265 }
249 266
250 267 /**
251 - * 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
252 269 *
253 270 * @since 1.7.3
254 271 *
255 - * @param int $term_id nav menu id
272 + * @param int nav menu id
256 273 */
257 - public function delete_nav_menu( $term_id ) {
274 + function delete_nav_menu( $term_id ) {
258 275 if ( isset( $this->options['nav_menus'] ) ) {
259 276 foreach ( $this->options['nav_menus'] as $theme => $locations ) {
260 277 foreach ( $locations as $loc => $languages ) {
261 278 foreach ( $languages as $lang => $menu_id ) {
@@ -267,6 +284,64 @@
267 284 }
268 285
269 286 update_option( 'polylang', $this->options );
270 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;
271 346 }
272 347 }