PluginProbe
Polylang / 2.5
Polylang v2.5
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 +82 -18 2.92.5 View file →
@@ -1,8 +1,5 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 4 * Manages custom menus translations as well as the language switcher menu item on admin side
8 5 *
@@ -19,8 +16,10 @@
19 16 */
20 17 public function __construct( &$polylang ) {
21 18 parent::__construct( $polylang );
22 19
20 + $this->theme = get_option( 'stylesheet' );
21 +
23 22 // Populates nav menus locations
24 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
@@ -36,13 +35,17 @@
36 35 */
37 36 public function admin_init() {
38 37 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
39 38 add_action( 'wp_update_nav_menu_item', array( $this, 'wp_update_nav_menu_item' ), 10, 2 );
39 + add_filter( 'wp_get_nav_menu_items', array( $this, 'translate_switcher_title' ) );
40 40
41 41 // Translation of menus based on chosen locations
42 42 add_filter( 'pre_update_option_theme_mods_' . $this->theme, array( $this, 'pre_update_option_theme_mods' ) );
43 43 add_action( 'delete_nav_menu', array( $this, 'delete_nav_menu' ) );
44 44
45 + // Filter _wp_auto_add_pages_to_menu by language
46 + add_action( 'transition_post_status', array( $this, 'auto_add_pages_to_menu' ), 5, 3 ); // before _wp_auto_add_pages_to_menu
47 +
45 48 // FIXME is it possible to choose the order ( after theme locations in WP3.5 and older ) ?
46 49 // 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 50 add_meta_box( 'pll_lang_switch_box', __( 'Language switcher', 'polylang' ), array( $this, 'lang_switch' ), 'nav-menus', 'side', 'high' );
48 51
@@ -64,19 +67,19 @@
64 67 <div id="tabs-panel-lang-switch" class="tabs-panel tabs-panel-active">
65 68 <ul id="lang-switch-checklist" class="categorychecklist form-no-clear">
66 69 <li>
67 70 <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' ); ?>
71 + <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 72 </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 + <input type="hidden" class="menu-item-type" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-type]" value="custom">
74 + <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' ); ?>">
75 + <input type="hidden" class="menu-item-url" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-url]" value="#pll_switcher">
73 76 </li>
74 77 </ul>
75 78 </div>
76 79 <p class="button-controls">
77 80 <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">
81 + <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">
79 82 <span class="spinner"></span>
80 83 </span>
81 84 </p>
82 85 </div>
@@ -96,13 +99,10 @@
96 99
97 100 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
98 101 wp_enqueue_script( 'pll_nav_menu', plugins_url( '/js/nav-menu' . $suffix . '.js', POLYLANG_FILE ), array( 'jquery' ), POLYLANG_VERSION );
99 102
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 - );
103 + $data['strings'] = PLL_Switcher::get_switcher_options( 'menu', 'string' ); // The strings for the options
104 + $data['title'] = __( 'Language switcher', 'polylang' ); // The title
105 105
106 106 // Get all language switcher menu items
107 107 $items = get_posts(
108 108 array(
@@ -114,8 +114,9 @@
114 114 )
115 115 );
116 116
117 117 // The options values for the language switcher
118 + $data['val'] = array();
118 119 foreach ( $items as $item ) {
119 120 $data['val'][ $item ] = get_post_meta( $item, '_pll_menu_item', true );
120 121 }
121 122
@@ -131,9 +132,9 @@
131 132 * @param int $menu_id not used
132 133 * @param int $menu_item_db_id
133 134 */
134 135 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
136 + if ( empty( $_POST['menu-item-url'][ $menu_item_db_id ] ) || '#pll_switcher' != $_POST['menu-item-url'][ $menu_item_db_id ] ) {
136 137 return;
137 138 }
138 139
139 140 // Security check as 'wp_update_nav_menu_item' can be called from outside WP admin
@@ -147,9 +148,9 @@
147 148 update_post_meta( $menu_item_db_id, '_pll_menu_item', $options );
148 149 }
149 150 }
150 151 else {
151 - foreach ( array_keys( $options ) as $opt ) {
152 + foreach ( $options as $opt => $v ) {
152 153 $options[ $opt ] = empty( $_POST[ 'menu-item-' . $opt ][ $menu_item_db_id ] ) ? 0 : 1;
153 154 }
154 155 update_post_meta( $menu_item_db_id, '_pll_menu_item', $options ); // Allow us to easily identify our nav menu item
155 156 }
@@ -156,8 +157,25 @@
156 157 }
157 158 }
158 159
159 160 /**
161 + * Translates the language switcher menu items title in case the user switches the admin language
162 + *
163 + * @since 1.1.1
164 + *
165 + * @param array $items
166 + * @return array modified $items
167 + */
168 + public function translate_switcher_title( $items ) {
169 + foreach ( $items as $item ) {
170 + if ( '#pll_switcher' == $item->url ) {
171 + $item->post_title = __( 'Language switcher', 'polylang' );
172 + }
173 + }
174 + return $items;
175 + }
176 +
177 + /**
160 178 * Assign menu languages and translations based on ( temporary ) locations
161 179 *
162 180 * @since 1.8
163 181 *
@@ -164,8 +182,10 @@
164 182 * @param array $locations nav menu locations
165 183 * @return array
166 184 */
167 185 public function update_nav_menu_locations( $locations ) {
186 + $default = $this->options['default_lang'];
187 +
168 188 // Extract language and menu from locations
169 189 foreach ( $locations as $loc => $menu ) {
170 190 $infos = $this->explode_location( $loc );
171 191 $this->options['nav_menus'][ $this->theme ][ $infos['location'] ][ $infos['lang'] ] = $menu;
@@ -189,9 +209,9 @@
189 209 public function pre_update_option_theme_mods( $mods ) {
190 210 if ( current_user_can( 'edit_theme_options' ) && isset( $mods['nav_menu_locations'] ) ) {
191 211
192 212 // Manage Locations tab in Appearance -> Menus
193 - if ( isset( $_GET['action'] ) && 'locations' === $_GET['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification
213 + if ( isset( $_GET['action'] ) && 'locations' == $_GET['action'] ) {
194 214 check_admin_referer( 'save-menu-locations' );
195 215 $this->options['nav_menus'][ $this->theme ] = array();
196 216 }
197 217
@@ -196,9 +216,9 @@
196 216 }
197 217
198 218 // Edit Menus tab in Appearance -> Menus
199 219 // 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'] ) {
220 + elseif ( isset( $_POST['action'], $_POST['update-nav-menu-nonce'] ) && 'update' == $_POST['action'] ) {
201 221 check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );
202 222 $this->options['nav_menus'][ $this->theme ] = array();
203 223 }
204 224
@@ -234,9 +254,9 @@
234 254 $menus = is_array( $menus ) ? array_merge( $locations, $menus ) : $locations;
235 255 }
236 256
237 257 if ( is_array( $menus ) ) {
238 - foreach ( array_keys( $menus ) as $loc ) {
258 + foreach ( $menus as $loc => $menu ) {
239 259 foreach ( $this->model->get_languages_list() as $lang ) {
240 260 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 261 $menus[ $this->combine_location( $loc, $lang ) ] = $this->options['nav_menus'][ $this->theme ][ $loc ][ $lang->slug ];
242 262 }
@@ -266,7 +286,51 @@
266 286 }
267 287 }
268 288
269 289 update_option( 'polylang', $this->options );
290 + }
291 + }
292 +
293 + /**
294 + * Filters the option nav_menu_options for auto added pages to menu
295 + *
296 + * @since 0.9.4
297 + *
298 + * @param array $options
299 + * @return array Modified options
300 + */
301 + public function nav_menu_options( $options ) {
302 + $options['auto_add'] = array_intersect( $options['auto_add'], $this->auto_add_menus );
303 + return $options;
304 + }
305 +
306 + /**
307 + * Filters _wp_auto_add_pages_to_menu by language
308 + *
309 + * @since 0.9.4
310 + *
311 + * @param string $new_status Transition to this post status.
312 + * @param string $old_status Previous post status.
313 + * @param object $post Post data.
314 + */
315 + public function auto_add_pages_to_menu( $new_status, $old_status, $post ) {
316 + if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type || ! empty( $post->post_parent ) ) {
317 + return;
318 + }
319 +
320 + if ( ! empty( $this->options['nav_menus'][ $this->theme ] ) ) {
321 + $this->auto_add_menus = array();
322 +
323 + $lang = $this->model->post->get_language( $post->ID );
324 + $lang = empty( $lang ) ? $this->options['default_lang'] : $lang->slug; // If the page has no language yet, the default language will be assigned
325 +
326 + // Get all the menus in the page language
327 + foreach ( $this->options['nav_menus'][ $this->theme ] as $loc ) {
328 + if ( ! empty( $loc[ $lang ] ) ) {
329 + $this->auto_add_menus[] = $loc[ $lang ];
330 + }
331 + }
332 +
333 + add_filter( 'option_nav_menu_options', array( $this, 'nav_menu_options' ) );
270 334 }
271 335 }
272 336 }