| @@ -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,26 +99,22 @@ | ||
| 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 | - $items = get_posts( | |
| 108 | - array( | |
| 109 | - 'numberposts' => -1, | |
| 110 | - 'nopaging' => true, | |
| 111 | - 'post_type' => 'nav_menu_item', | |
| 112 | - 'fields' => 'ids', | |
| 113 | - 'meta_key' => '_pll_menu_item', | |
| 114 | - ) | |
| 115 | - ); | |
| 107 | + $items = get_posts( array( | |
| 108 | + 'numberposts' => -1, | |
| 109 | + 'nopaging' => true, | |
| 110 | + 'post_type' => 'nav_menu_item', | |
| 111 | + 'fields' => 'ids', | |
| 112 | + 'meta_key' => '_pll_menu_item', | |
| 113 | + ) ); | |
| 116 | 114 | |
| 117 | 115 | // The options values for the language switcher |
| 116 | + $data['val'] = array(); | |
| 118 | 117 | foreach ( $items as $item ) { |
| 119 | 118 | $data['val'][ $item ] = get_post_meta( $item, '_pll_menu_item', true ); |
| 120 | 119 | } |
| 121 | 120 | |
| @@ -131,9 +130,9 @@ | ||
| 131 | 130 | * @param int $menu_id not used |
| 132 | 131 | * @param int $menu_item_db_id |
| 133 | 132 | */ |
| 134 | 133 | 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 | |
| 134 | + if ( empty( $_POST['menu-item-url'][ $menu_item_db_id ] ) || '#pll_switcher' != $_POST['menu-item-url'][ $menu_item_db_id ] ) { | |
| 136 | 135 | return; |
| 137 | 136 | } |
| 138 | 137 | |
| 139 | 138 | // Security check as 'wp_update_nav_menu_item' can be called from outside WP admin |
| @@ -147,9 +146,9 @@ | ||
| 147 | 146 | update_post_meta( $menu_item_db_id, '_pll_menu_item', $options ); |
| 148 | 147 | } |
| 149 | 148 | } |
| 150 | 149 | else { |
| 151 | - foreach ( array_keys( $options ) as $opt ) { | |
| 150 | + foreach ( $options as $opt => $v ) { | |
| 152 | 151 | $options[ $opt ] = empty( $_POST[ 'menu-item-' . $opt ][ $menu_item_db_id ] ) ? 0 : 1; |
| 153 | 152 | } |
| 154 | 153 | update_post_meta( $menu_item_db_id, '_pll_menu_item', $options ); // Allow us to easily identify our nav menu item |
| 155 | 154 | } |
| @@ -156,8 +155,25 @@ | ||
| 156 | 155 | } |
| 157 | 156 | } |
| 158 | 157 | |
| 159 | 158 | /** |
| 159 | + * Translates the language switcher menu items title in case the user switches the admin language | |
| 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 | + /** | |
| 160 | 176 | * Assign menu languages and translations based on ( temporary ) locations |
| 161 | 177 | * |
| 162 | 178 | * @since 1.8 |
| 163 | 179 | * |
| @@ -164,8 +180,10 @@ | ||
| 164 | 180 | * @param array $locations nav menu locations |
| 165 | 181 | * @return array |
| 166 | 182 | */ |
| 167 | 183 | public function update_nav_menu_locations( $locations ) { |
| 184 | + $default = $this->options['default_lang']; | |
| 185 | + | |
| 168 | 186 | // Extract language and menu from locations |
| 169 | 187 | foreach ( $locations as $loc => $menu ) { |
| 170 | 188 | $infos = $this->explode_location( $loc ); |
| 171 | 189 | $this->options['nav_menus'][ $this->theme ][ $infos['location'] ][ $infos['lang'] ] = $menu; |
| @@ -189,9 +207,9 @@ | ||
| 189 | 207 | public function pre_update_option_theme_mods( $mods ) { |
| 190 | 208 | if ( current_user_can( 'edit_theme_options' ) && isset( $mods['nav_menu_locations'] ) ) { |
| 191 | 209 | |
| 192 | 210 | // Manage Locations tab in Appearance -> Menus |
| 193 | - if ( isset( $_GET['action'] ) && 'locations' === $_GET['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 211 | + if ( isset( $_GET['action'] ) && 'locations' == $_GET['action'] ) { | |
| 194 | 212 | check_admin_referer( 'save-menu-locations' ); |
| 195 | 213 | $this->options['nav_menus'][ $this->theme ] = array(); |
| 196 | 214 | } |
| 197 | 215 | |
| @@ -196,9 +214,9 @@ | ||
| 196 | 214 | } |
| 197 | 215 | |
| 198 | 216 | // Edit Menus tab in Appearance -> Menus |
| 199 | 217 | // 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'] ) { | |
| 218 | + elseif ( isset( $_POST['action'], $_POST['update-nav-menu-nonce'] ) && 'update' == $_POST['action'] ) { | |
| 201 | 219 | check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' ); |
| 202 | 220 | $this->options['nav_menus'][ $this->theme ] = array(); |
| 203 | 221 | } |
| 204 | 222 | |
| @@ -234,9 +252,9 @@ | ||
| 234 | 252 | $menus = is_array( $menus ) ? array_merge( $locations, $menus ) : $locations; |
| 235 | 253 | } |
| 236 | 254 | |
| 237 | 255 | if ( is_array( $menus ) ) { |
| 238 | - foreach ( array_keys( $menus ) as $loc ) { | |
| 256 | + foreach ( $menus as $loc => $menu ) { | |
| 239 | 257 | foreach ( $this->model->get_languages_list() as $lang ) { |
| 240 | 258 | 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 | 259 | $menus[ $this->combine_location( $loc, $lang ) ] = $this->options['nav_menus'][ $this->theme ][ $loc ][ $lang->slug ]; |
| 242 | 260 | } |
| @@ -253,9 +271,9 @@ | ||
| 253 | 271 | * @since 1.7.3 |
| 254 | 272 | * |
| 255 | 273 | * @param int $term_id nav menu id |
| 256 | 274 | */ |
| 257 | - public function delete_nav_menu( $term_id ) { | |
| 275 | + function delete_nav_menu( $term_id ) { | |
| 258 | 276 | if ( isset( $this->options['nav_menus'] ) ) { |
| 259 | 277 | foreach ( $this->options['nav_menus'] as $theme => $locations ) { |
| 260 | 278 | foreach ( $locations as $loc => $languages ) { |
| 261 | 279 | foreach ( $languages as $lang => $menu_id ) { |
| @@ -266,7 +284,51 @@ | ||
| 266 | 284 | } |
| 267 | 285 | } |
| 268 | 286 | |
| 269 | 287 | update_option( 'polylang', $this->options ); |
| 288 | + } | |
| 289 | + } | |
| 290 | + | |
| 291 | + /** | |
| 292 | + * Filters the option nav_menu_options for auto added pages to menu | |
| 293 | + * | |
| 294 | + * @since 0.9.4 | |
| 295 | + * | |
| 296 | + * @param array $options | |
| 297 | + * @return array Modified options | |
| 298 | + */ | |
| 299 | + public function nav_menu_options( $options ) { | |
| 300 | + $options['auto_add'] = array_intersect( $options['auto_add'], $this->auto_add_menus ); | |
| 301 | + return $options; | |
| 302 | + } | |
| 303 | + | |
| 304 | + /** | |
| 305 | + * Filters _wp_auto_add_pages_to_menu by language | |
| 306 | + * | |
| 307 | + * @since 0.9.4 | |
| 308 | + * | |
| 309 | + * @param string $new_status Transition to this post status. | |
| 310 | + * @param string $old_status Previous post status. | |
| 311 | + * @param object $post Post data. | |
| 312 | + */ | |
| 313 | + public function auto_add_pages_to_menu( $new_status, $old_status, $post ) { | |
| 314 | + if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type || ! empty( $post->post_parent ) ) { | |
| 315 | + return; | |
| 316 | + } | |
| 317 | + | |
| 318 | + if ( ! empty( $this->options['nav_menus'][ $this->theme ] ) ) { | |
| 319 | + $this->auto_add_menus = array(); | |
| 320 | + | |
| 321 | + $lang = $this->model->post->get_language( $post->ID ); | |
| 322 | + $lang = empty( $lang ) ? $this->options['default_lang'] : $lang->slug; // If the page has no language yet, the default language will be assigned | |
| 323 | + | |
| 324 | + // Get all the menus in the page language | |
| 325 | + foreach ( $this->options['nav_menus'][ $this->theme ] as $loc ) { | |
| 326 | + if ( ! empty( $loc[ $lang ] ) ) { | |
| 327 | + $this->auto_add_menus[] = $loc[ $lang ]; | |
| 328 | + } | |
| 329 | + } | |
| 330 | + | |
| 331 | + add_filter( 'option_nav_menu_options', array( $this, 'nav_menu_options' ) ); | |
| 270 | 332 | } |
| 271 | 333 | } |
| 272 | 334 | } |