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 +87 -31 3.02.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
@@ -32,19 +31,21 @@
32 31 * Setups filters and terms
33 32 * adds the language switcher metabox and create new nav menu locations
34 33 *
35 34 * @since 1.1
36 - *
37 - * @return void
38 35 */
39 36 public function admin_init() {
40 37 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
41 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' ) );
42 40
43 41 // Translation of menus based on chosen locations
44 42 add_filter( 'pre_update_option_theme_mods_' . $this->theme, array( $this, 'pre_update_option_theme_mods' ) );
45 43 add_action( 'delete_nav_menu', array( $this, 'delete_nav_menu' ) );
46 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 +
47 48 // FIXME is it possible to choose the order ( after theme locations in WP3.5 and older ) ?
48 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
49 50 add_meta_box( 'pll_lang_switch_box', __( 'Language switcher', 'polylang' ), array( $this, 'lang_switch' ), 'nav-menus', 'side', 'high' );
50 51
@@ -56,10 +57,8 @@
56 57 * The checkbox and all hidden fields are important
57 58 * 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 59 *
59 60 * @since 1.1
60 - *
61 - * @return void
62 61 */
63 62 public function lang_switch() {
64 63 global $_nav_menu_placeholder, $nav_menu_selected_id;
65 64 $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1;
@@ -68,19 +67,19 @@
68 67 <div id="tabs-panel-lang-switch" class="tabs-panel tabs-panel-active">
69 68 <ul id="lang-switch-checklist" class="categorychecklist form-no-clear">
70 69 <li>
71 70 <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' ); ?>
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' ); ?>
73 72 </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">
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">
77 76 </li>
78 77 </ul>
79 78 </div>
80 79 <p class="button-controls">
81 80 <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">
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">
83 82 <span class="spinner"></span>
84 83 </span>
85 84 </p>
86 85 </div>
@@ -90,25 +89,20 @@
90 89 /**
91 90 * Prepares javascript to modify the language switcher menu item
92 91 *
93 92 * @since 1.1
94 - *
95 - * @return void
96 93 */
97 94 public function admin_enqueue_scripts() {
98 95 $screen = get_current_screen();
99 - if ( empty( $screen ) || 'nav-menus' !== $screen->base ) {
96 + if ( 'nav-menus' != $screen->base ) {
100 97 return;
101 98 }
102 99
103 100 $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 );
101 + wp_enqueue_script( 'pll_nav_menu', plugins_url( '/js/nav-menu' . $suffix . '.js', POLYLANG_FILE ), array( 'jquery' ), POLYLANG_VERSION );
105 102
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 - );
103 + $data['strings'] = PLL_Switcher::get_switcher_options( 'menu', 'string' ); // The strings for the options
104 + $data['title'] = __( 'Language switcher', 'polylang' ); // The title
111 105
112 106 // Get all language switcher menu items
113 107 $items = get_posts(
114 108 array(
@@ -120,8 +114,9 @@
120 114 )
121 115 );
122 116
123 117 // The options values for the language switcher
118 + $data['val'] = array();
124 119 foreach ( $items as $item ) {
125 120 $data['val'][ $item ] = get_post_meta( $item, '_pll_menu_item', true );
126 121 }
127 122
@@ -135,12 +130,11 @@
135 130 * @since 1.1
136 131 *
137 132 * @param int $menu_id not used
138 133 * @param int $menu_item_db_id
139 - * @return void
140 134 */
141 135 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
136 + if ( empty( $_POST['menu-item-url'][ $menu_item_db_id ] ) || '#pll_switcher' != $_POST['menu-item-url'][ $menu_item_db_id ] ) {
143 137 return;
144 138 }
145 139
146 140 // Security check as 'wp_update_nav_menu_item' can be called from outside WP admin
@@ -154,9 +148,9 @@
154 148 update_post_meta( $menu_item_db_id, '_pll_menu_item', $options );
155 149 }
156 150 }
157 151 else {
158 - foreach ( array_keys( $options ) as $opt ) {
152 + foreach ( $options as $opt => $v ) {
159 153 $options[ $opt ] = empty( $_POST[ 'menu-item-' . $opt ][ $menu_item_db_id ] ) ? 0 : 1;
160 154 }
161 155 update_post_meta( $menu_item_db_id, '_pll_menu_item', $options ); // Allow us to easily identify our nav menu item
162 156 }
@@ -163,8 +157,25 @@
163 157 }
164 158 }
165 159
166 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 + /**
167 178 * Assign menu languages and translations based on ( temporary ) locations
168 179 *
169 180 * @since 1.8
170 181 *
@@ -171,8 +182,10 @@
171 182 * @param array $locations nav menu locations
172 183 * @return array
173 184 */
174 185 public function update_nav_menu_locations( $locations ) {
186 + $default = $this->options['default_lang'];
187 +
175 188 // Extract language and menu from locations
176 189 foreach ( $locations as $loc => $menu ) {
177 190 $infos = $this->explode_location( $loc );
178 191 $this->options['nav_menus'][ $this->theme ][ $infos['location'] ][ $infos['lang'] ] = $menu;
@@ -185,20 +198,20 @@
185 198 return $locations;
186 199 }
187 200
188 201 /**
189 - * Assign menu languages and translations based on ( temporary ) locations.
202 + * Assign menu languages and translations based on ( temporary ) locations
190 203 *
191 204 * @since 1.1
192 205 *
193 - * @param mixed $mods Theme mods.
194 - * @return mixed
206 + * @param array $mods theme mods
207 + * @return unmodified $mods
195 208 */
196 209 public function pre_update_option_theme_mods( $mods ) {
197 210 if ( current_user_can( 'edit_theme_options' ) && isset( $mods['nav_menu_locations'] ) ) {
198 211
199 212 // Manage Locations tab in Appearance -> Menus
200 - if ( isset( $_GET['action'] ) && 'locations' === $_GET['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification
213 + if ( isset( $_GET['action'] ) && 'locations' == $_GET['action'] ) {
201 214 check_admin_referer( 'save-menu-locations' );
202 215 $this->options['nav_menus'][ $this->theme ] = array();
203 216 }
204 217
@@ -203,9 +216,9 @@
203 216 }
204 217
205 218 // Edit Menus tab in Appearance -> Menus
206 219 // 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'] ) {
220 + elseif ( isset( $_POST['action'], $_POST['update-nav-menu-nonce'] ) && 'update' == $_POST['action'] ) {
208 221 check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );
209 222 $this->options['nav_menus'][ $this->theme ] = array();
210 223 }
211 224
@@ -241,9 +254,9 @@
241 254 $menus = is_array( $menus ) ? array_merge( $locations, $menus ) : $locations;
242 255 }
243 256
244 257 if ( is_array( $menus ) ) {
245 - foreach ( array_keys( $menus ) as $loc ) {
258 + foreach ( $menus as $loc => $menu ) {
246 259 foreach ( $this->model->get_languages_list() as $lang ) {
247 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' ) ) {
248 261 $menus[ $this->combine_location( $loc, $lang ) ] = $this->options['nav_menus'][ $this->theme ][ $loc ][ $lang->slug ];
249 262 }
@@ -259,9 +272,8 @@
259 272 *
260 273 * @since 1.7.3
261 274 *
262 275 * @param int $term_id nav menu id
263 - * @return void
264 276 */
265 277 public function delete_nav_menu( $term_id ) {
266 278 if ( isset( $this->options['nav_menus'] ) ) {
267 279 foreach ( $this->options['nav_menus'] as $theme => $locations ) {
@@ -274,7 +286,51 @@
274 286 }
275 287 }
276 288
277 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' ) );
278 334 }
279 335 }
280 336 }