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