PluginProbe
Polylang / 2.1
Polylang v2.1
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 +134 -90 3.02.1 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,36 @@
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
29 28 }
30 29
31 30 /**
32 - * Setups filters and terms
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 - // Translation of menus based on chosen locations
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
@@ -51,122 +52,130 @@
51 52 $this->create_nav_menu_locations();
52 53 }
53 54
54 55 /**
55 - * Language switcher metabox
56 + * language switcher metabox
56 57 * 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/
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 - $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1;
66 - ?>
64 + $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1; ?>
65 +
67 66 <div id="posttype-lang-switch" class="posttypediv">
68 67 <div id="tabs-panel-lang-switch" class="tabs-panel tabs-panel-active">
69 - <ul id="lang-switch-checklist" class="categorychecklist form-no-clear">
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">
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
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">
76 + </li>
77 + </ul>
78 + </div>
79 + <p class="button-controls">
80 + <span class="add-to-menu">
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">
82 + <span class="spinner"></span>
83 + </span>
84 + </p>
85 + </div><?php
88 86 }
89 87
90 88 /**
91 - * Prepares javascript to modify the language switcher menu item
89 + * prepares javascript to modify the language switcher menu item
92 90 *
93 91 * @since 1.1
94 - *
95 - * @return void
96 92 */
97 93 public function admin_enqueue_scripts() {
98 94 $screen = get_current_screen();
99 - if ( empty( $screen ) || 'nav-menus' !== $screen->base ) {
95 + if ( 'nav-menus' != $screen->base ) {
100 96 return;
101 97 }
102 98
103 99 $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 );
100 + wp_enqueue_script( 'pll_nav_menu', plugins_url( '/js/nav-menu' . $suffix . '.js', POLYLANG_FILE ), array( 'jquery' ), POLYLANG_VERSION );
105 101
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 - );
102 + $data['strings'] = PLL_Switcher::get_switcher_options( 'menu', 'string' ); // the strings for the options
103 + $data['title'] = __( 'Language switcher', 'polylang' ); // the title
111 104
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 - );
105 + // get all language switcher menu items
106 + $items = get_posts( array(
107 + 'numberposts' => -1,
108 + 'nopaging' => true,
109 + 'post_type' => 'nav_menu_item',
110 + 'fields' => 'ids',
111 + 'meta_key' => '_pll_menu_item',
112 + ) );
122 113
123 - // The options values for the language switcher
114 + // the options values for the language switcher
115 + $data['val'] = array();
124 116 foreach ( $items as $item ) {
125 117 $data['val'][ $item ] = get_post_meta( $item, '_pll_menu_item', true );
126 118 }
127 119
128 - // Send all these data to javascript
120 + // send all these data to javascript
129 121 wp_localize_script( 'pll_nav_menu', 'pll_data', $data );
130 122 }
131 123
132 124 /**
133 - * Save our menu item options
125 + * save our menu item options
134 126 *
135 127 * @since 1.1
136 128 *
137 129 * @param int $menu_id not used
138 130 * @param int $menu_item_db_id
139 - * @return void
140 131 */
141 132 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
133 + if ( empty( $_POST['menu-item-url'][ $menu_item_db_id ] ) || '#pll_switcher' != $_POST['menu-item-url'][ $menu_item_db_id ] ) {
143 134 return;
144 135 }
145 136
146 - // Security check as 'wp_update_nav_menu_item' can be called from outside WP admin
137 + // security check
138 + // as 'wp_update_nav_menu_item' can be called from outside WP admin
147 139 if ( current_user_can( 'edit_theme_options' ) ) {
148 140 check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );
149 141
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
142 + $options = array( 'hide_if_no_translation' => 0, 'hide_current' => 0,'force_home' => 0 ,'show_flags' => 0 ,'show_names' => 1, 'dropdown' => 0 ); // default values
143 + // our jQuery form has not been displayed
152 144 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
145 + if ( ! get_post_meta( $menu_item_db_id, '_pll_menu_item', true ) ) { // our options were never saved
154 146 update_post_meta( $menu_item_db_id, '_pll_menu_item', $options );
155 147 }
156 148 }
157 149 else {
158 - foreach ( array_keys( $options ) as $opt ) {
150 + foreach ( $options as $opt => $v ) {
159 151 $options[ $opt ] = empty( $_POST[ 'menu-item-' . $opt ][ $menu_item_db_id ] ) ? 0 : 1;
160 152 }
161 - update_post_meta( $menu_item_db_id, '_pll_menu_item', $options ); // Allow us to easily identify our nav menu item
153 + update_post_meta( $menu_item_db_id, '_pll_menu_item', $options ); // allow us to easily identify our nav menu item
162 154 }
163 155 }
164 156 }
165 157
166 158 /**
167 - * Assign menu languages and translations based on ( temporary ) locations
159 + * translates the language switcher menu items title in case the user switches the admin language
168 160 *
161 + * @since 1.1.1
162 + *
163 + * @param array $items
164 + * @return array modified $items
165 + */
166 + public function translate_switcher_title( $items ) {
167 + foreach ( $items as $item ) {
168 + if ( '#pll_switcher' == $item->url ) {
169 + $item->post_title = __( 'Language switcher', 'polylang' );
170 + }
171 + }
172 + return $items;
173 + }
174 +
175 + /**
176 + * assign menu languages and translations based on ( temporary ) locations
177 + *
169 178 * @since 1.8
170 179 *
171 180 * @param array $locations nav menu locations
172 181 * @return array
@@ -171,14 +180,16 @@
171 180 * @param array $locations nav menu locations
172 181 * @return array
173 182 */
174 183 public function update_nav_menu_locations( $locations ) {
175 - // Extract language and menu from locations
184 + $default = $this->options['default_lang'];
185 +
186 + // extract language and menu from locations
176 187 foreach ( $locations as $loc => $menu ) {
177 188 $infos = $this->explode_location( $loc );
178 189 $this->options['nav_menus'][ $this->theme ][ $infos['location'] ][ $infos['lang'] ] = $menu;
179 190 if ( $this->options['default_lang'] != $infos['lang'] ) {
180 - unset( $locations[ $loc ] ); // Remove temporary locations before database update
191 + unset( $locations[ $loc ] ); // remove temporary locations before database update
181 192 }
182 193 }
183 194
184 195 update_option( 'polylang', $this->options );
@@ -185,33 +196,33 @@
185 196 return $locations;
186 197 }
187 198
188 199 /**
189 - * Assign menu languages and translations based on ( temporary ) locations.
200 + * assign menu languages and translations based on ( temporary ) locations
190 201 *
191 202 * @since 1.1
192 203 *
193 - * @param mixed $mods Theme mods.
194 - * @return mixed
204 + * @param array $mods theme mods
205 + * @return unmodified $mods
195 206 */
196 207 public function pre_update_option_theme_mods( $mods ) {
197 208 if ( current_user_can( 'edit_theme_options' ) && isset( $mods['nav_menu_locations'] ) ) {
198 209
199 210 // Manage Locations tab in Appearance -> Menus
200 - if ( isset( $_GET['action'] ) && 'locations' === $_GET['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification
211 + if ( isset( $_GET['action'] ) && 'locations' == $_GET['action'] ) {
201 212 check_admin_referer( 'save-menu-locations' );
202 213 $this->options['nav_menus'][ $this->theme ] = array();
203 214 }
204 215
205 216 // 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'] ) {
217 + // add the test of $_POST['update-nav-menu-nonce'] to avoid conflict with Vantage theme
218 + elseif ( isset( $_POST['action'], $_POST['update-nav-menu-nonce'] ) && 'update' == $_POST['action'] ) {
208 219 check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );
209 220 $this->options['nav_menus'][ $this->theme ] = array();
210 221 }
211 222
212 - // Customizer
213 - // Don't reset locations in this case.
223 + // customizer
224 + // don't reset locations in this case.
214 225 // see http://wordpress.org/support/topic/menus-doesnt-show-and-not-saved-in-theme-settings-multilingual-site
215 226 elseif ( isset( $_POST['action'] ) && 'customize_save' == $_POST['action'] ) {
216 227 check_ajax_referer( 'save-customize_' . $GLOBALS['wp_customize']->get_stylesheet(), 'nonce' );
217 228 }
@@ -216,9 +227,9 @@
216 227 check_ajax_referer( 'save-customize_' . $GLOBALS['wp_customize']->get_stylesheet(), 'nonce' );
217 228 }
218 229
219 230 else {
220 - return $mods; // No modification for nav menu locations
231 + return $mods; // no modification for nav menu locations
221 232 }
222 233
223 234 $mods['nav_menu_locations'] = $this->update_nav_menu_locations( $mods['nav_menu_locations'] );
224 235 }
@@ -225,9 +236,9 @@
225 236 return $mods;
226 237 }
227 238
228 239 /**
229 - * Fills temporary menu locations based on menus translations
240 + * fills temporary menu locations based on menus translations
230 241 *
231 242 * @since 1.2
232 243 *
233 244 * @param bool|array $menus
@@ -233,17 +244,10 @@
233 244 * @param bool|array $menus
234 245 * @return bool|array modified list of menu locations
235 246 */
236 247 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 248 if ( is_array( $menus ) ) {
245 - foreach ( array_keys( $menus ) as $loc ) {
249 + foreach ( $menus as $loc => $menu ) {
246 250 foreach ( $this->model->get_languages_list() as $lang ) {
247 251 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 252 $menus[ $this->combine_location( $loc, $lang ) ] = $this->options['nav_menus'][ $this->theme ][ $loc ][ $lang->slug ];
249 253 }
@@ -254,16 +258,15 @@
254 258 return $menus;
255 259 }
256 260
257 261 /**
258 - * Removes the nav menu term_id from the locations stored in Polylang options when a nav menu is deleted
262 + * removes the nav menu term_id from the locations stored in Polylang options when a nav menu is deleted
259 263 *
260 264 * @since 1.7.3
261 265 *
262 - * @param int $term_id nav menu id
263 - * @return void
266 + * @param int nav menu id
264 267 */
265 - public function delete_nav_menu( $term_id ) {
268 + function delete_nav_menu( $term_id ) {
266 269 if ( isset( $this->options['nav_menus'] ) ) {
267 270 foreach ( $this->options['nav_menus'] as $theme => $locations ) {
268 271 foreach ( $locations as $loc => $languages ) {
269 272 foreach ( $languages as $lang => $menu_id ) {
@@ -274,7 +277,48 @@
274 277 }
275 278 }
276 279
277 280 update_option( 'polylang', $this->options );
281 + }
282 + }
283 +
284 + /**
285 + * filters the option nav_menu_options for auto added pages to menu
286 + *
287 + * @since 0.9.4
288 + *
289 + * @param array $options
290 + * @return array Modified options
291 + */
292 + public function nav_menu_options( $options ) {
293 + $options['auto_add'] = array_intersect( $options['auto_add'], $this->auto_add_menus );
294 + return $options;
295 + }
296 +
297 + /**
298 + * filters _wp_auto_add_pages_to_menu by language
299 + *
300 + * @since 0.9.4
301 + *
302 + * @param string $new_status Transition to this post status.
303 + * @param string $old_status Previous post status.
304 + * @param object $post Post data.
305 + */
306 + public function auto_add_pages_to_menu( $new_status, $old_status, $post ) {
307 + if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type || ! empty( $post->post_parent ) || ! ( $lang = $this->model->post->get_language( $post->ID ) ) ) {
308 + return;
309 + }
310 +
311 + if ( ! empty( $this->options['nav_menus'][ $this->theme ] ) ) {
312 + $this->auto_add_menus = array();
313 +
314 + // get all the menus in the page language
315 + foreach ( $this->options['nav_menus'][ $this->theme ] as $loc ) {
316 + if ( ! empty( $loc[ $lang->slug ] ) ) {
317 + $this->auto_add_menus[] = $loc[ $lang->slug ];
318 + }
319 + }
320 +
321 + add_filter( 'option_nav_menu_options', array( $this, 'nav_menu_options' ) );
278 322 }
279 323 }
280 324 }