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