| @@ -1,131 +1,149 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | -/** | |
| 7 | - * Manages custom menus translations as well as the language switcher menu item on admin side | |
| 3 | +/* | |
| 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 | -class PLL_Admin_Nav_Menu extends PLL_Nav_Menu { | |
| 8 | +class PLL_Admin_Nav_Menu { | |
| 12 | 9 | |
| 13 | - /** | |
| 14 | - * Constructor: setups filters and actions | |
| 10 | + /* | |
| 11 | + * constructor: setups filters and actions | |
| 15 | 12 | * |
| 16 | 13 | * @since 1.2 |
| 17 | 14 | * |
| 18 | 15 | * @param object $polylang |
| 19 | 16 | */ |
| 20 | - public function __construct( &$polylang ) { | |
| 21 | - parent::__construct( $polylang ); | |
| 17 | + public function __construct(&$polylang) { | |
| 18 | + $this->model = &$polylang->model; | |
| 19 | + $this->options = &$polylang->options; | |
| 20 | + $this->theme = get_option( 'stylesheet' ); | |
| 22 | 21 | |
| 23 | - // Populates nav menus locations | |
| 24 | - // Since WP 4.4, must be done before customize_register is fired | |
| 25 | - add_filter( 'theme_mod_nav_menu_locations', array( $this, 'theme_mod_nav_menu_locations' ), 20 ); | |
| 22 | + // integration in the WP menu interface | |
| 23 | + add_action('admin_init', array(&$this, 'admin_init')); // after Polylang upgrade | |
| 26 | 24 | |
| 27 | - // Integration in the WP menu interface | |
| 28 | - add_action( 'admin_init', array( $this, 'admin_init' ) ); // after Polylang upgrade | |
| 25 | + // integration with WP customizer | |
| 26 | + add_action('customize_register', array(&$this, 'create_nav_menu_locations'), 5); | |
| 27 | + | |
| 28 | + // protection against #24802 | |
| 29 | + add_filter('pre_insert_term', array(&$this, 'pre_insert_term'), 10, 2); | |
| 29 | 30 | } |
| 30 | 31 | |
| 31 | - /** | |
| 32 | - * Setups filters and terms | |
| 32 | + /* | |
| 33 | + * setups filters and terms | |
| 33 | 34 | * adds the language switcher metabox and create new nav menu locations |
| 34 | 35 | * |
| 35 | 36 | * @since 1.1 |
| 36 | 37 | */ |
| 37 | - public function admin_init() { | |
| 38 | - add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); | |
| 39 | - add_action( 'wp_update_nav_menu_item', array( $this, 'wp_update_nav_menu_item' ), 10, 2 ); | |
| 38 | + public function admin_init(){ | |
| 39 | + add_action('admin_enqueue_scripts', array(&$this, 'admin_enqueue_scripts')); | |
| 40 | + add_action('wp_update_nav_menu_item', array(&$this, 'wp_update_nav_menu_item'), 10, 2); | |
| 41 | + add_filter('wp_get_nav_menu_items', array(&$this, 'translate_switcher_title')); | |
| 40 | 42 | |
| 41 | - // Translation of menus based on chosen locations | |
| 42 | - add_filter( 'pre_update_option_theme_mods_' . $this->theme, array( $this, 'pre_update_option_theme_mods' ) ); | |
| 43 | - add_action( 'delete_nav_menu', array( $this, 'delete_nav_menu' ) ); | |
| 43 | + // translation of menus based on chosen locations | |
| 44 | + add_filter('pre_update_option_theme_mods_' . $this->theme, array($this, 'update_nav_menu_locations')); | |
| 45 | + add_filter('theme_mod_nav_menu_locations', array($this, 'nav_menu_locations'), 20); | |
| 44 | 46 | |
| 45 | - // FIXME is it possible to choose the order ( after theme locations in WP3.5 and older ) ? | |
| 47 | + // filter _wp_auto_add_pages_to_menu by language | |
| 48 | + add_action('transition_post_status', array(&$this, 'auto_add_pages_to_menu'), 5, 3); // before _wp_auto_add_pages_to_menu | |
| 49 | + | |
| 50 | + // FIXME is it possible to choose the order (after theme locations in WP3.5 and older) ? | |
| 46 | 51 | // 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 | - add_meta_box( 'pll_lang_switch_box', __( 'Language switcher', 'polylang' ), array( $this, 'lang_switch' ), 'nav-menus', 'side', 'high' ); | |
| 52 | + add_meta_box('pll_lang_switch_box', __('Language switcher', 'polylang'), array( &$this, 'lang_switch' ), 'nav-menus', 'side', 'high'); | |
| 48 | 53 | |
| 49 | 54 | $this->create_nav_menu_locations(); |
| 50 | 55 | } |
| 51 | 56 | |
| 52 | - /** | |
| 53 | - * Language switcher metabox | |
| 57 | + /* | |
| 58 | + * create temporary nav menu locations (one per location and per language) for all non-default language | |
| 59 | + * | |
| 60 | + * @since 1.2 | |
| 61 | + */ | |
| 62 | + public function create_nav_menu_locations() { | |
| 63 | + global $_wp_registered_nav_menus; | |
| 64 | + | |
| 65 | + if (isset($_wp_registered_nav_menus)) { | |
| 66 | + foreach ($_wp_registered_nav_menus as $loc => $name) | |
| 67 | + foreach ($this->model->get_languages_list() as $lang) | |
| 68 | + $arr[$loc . (pll_default_language() == $lang->slug ? '' : '___' . $lang->slug)] = $name . ' ' . $lang->name; | |
| 69 | + | |
| 70 | + $_wp_registered_nav_menus = $arr; | |
| 71 | + } | |
| 72 | + } | |
| 73 | + | |
| 74 | + /* | |
| 75 | + * language switcher metabox | |
| 54 | 76 | * The checkbox and all hidden fields are important |
| 55 | - * 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/ | |
| 77 | + * 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/ | |
| 56 | 78 | * |
| 57 | 79 | * @since 1.1 |
| 58 | 80 | */ |
| 59 | 81 | public function lang_switch() { |
| 60 | 82 | global $_nav_menu_placeholder, $nav_menu_selected_id; |
| 61 | - $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1; | |
| 62 | - ?> | |
| 83 | + $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1;?> | |
| 84 | + | |
| 63 | 85 | <div id="posttype-lang-switch" class="posttypediv"> |
| 64 | 86 | <div id="tabs-panel-lang-switch" class="tabs-panel tabs-panel-active"> |
| 65 | - <ul id="lang-switch-checklist" class="categorychecklist form-no-clear"> | |
| 87 | + <ul id ="lang-switch-checklist" class="categorychecklist form-no-clear"> | |
| 66 | 88 | <li> |
| 67 | 89 | <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' ); ?> | |
| 90 | + <input type="checkbox" class="menu-item-checkbox" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-object-id]" value="-1"> <?php _e('Language switcher', 'polylang'); ?> | |
| 69 | 91 | </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 | - </li> | |
| 74 | - </ul> | |
| 75 | - </div> | |
| 76 | - <p class="button-controls"> | |
| 77 | - <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"> | |
| 79 | - <span class="spinner"></span> | |
| 80 | - </span> | |
| 81 | - </p> | |
| 82 | - </div> | |
| 83 | - <?php | |
| 92 | + <input type="hidden" class="menu-item-type" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-type]" value="custom"> | |
| 93 | + <input type="hidden" class="menu-item-title" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-title]" value="<?php _e('Language switcher', 'polylang'); ?>"> | |
| 94 | + <input type="hidden" class="menu-item-url" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-url]" value="#pll_switcher"> | |
| 95 | + </li> | |
| 96 | + </ul> | |
| 97 | + </div> | |
| 98 | + <p class="button-controls"> | |
| 99 | + <span class="add-to-menu"> | |
| 100 | + <input type="submit" <?php disabled( $nav_menu_selected_id, 0 ); ?> class="button-secondary submit-add-to-menu right" value="Add to Menu" name="add-post-type-menu-item" id="submit-posttype-lang-switch"> | |
| 101 | + <span class="spinner"></span> | |
| 102 | + </span> | |
| 103 | + </p> | |
| 104 | + </div><?php | |
| 84 | 105 | } |
| 85 | 106 | |
| 86 | - /** | |
| 87 | - * Prepares javascript to modify the language switcher menu item | |
| 107 | + /* | |
| 108 | + * prepares javascript to modify the language switcher menu item | |
| 88 | 109 | * |
| 89 | 110 | * @since 1.1 |
| 90 | 111 | */ |
| 91 | 112 | public function admin_enqueue_scripts() { |
| 92 | 113 | $screen = get_current_screen(); |
| 93 | - if ( 'nav-menus' != $screen->base ) { | |
| 114 | + if ('nav-menus' != $screen->base) | |
| 94 | 115 | return; |
| 95 | - } | |
| 96 | 116 | |
| 97 | - $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; | |
| 98 | - wp_enqueue_script( 'pll_nav_menu', plugins_url( '/js/nav-menu' . $suffix . '.js', POLYLANG_FILE ), array( 'jquery' ), POLYLANG_VERSION ); | |
| 117 | + $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; | |
| 118 | + wp_enqueue_script('pll_nav_menu', POLYLANG_URL .'/js/nav-menu'.$suffix.'.js', array('jquery'), POLYLANG_VERSION); | |
| 99 | 119 | |
| 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 | - ); | |
| 120 | + // the strings for the options | |
| 121 | + foreach (array_reverse(PLL_Switcher::get_switcher_options('menu', 'string')) as $str) | |
| 122 | + $data['strings'][] = $str; | |
| 105 | 123 | |
| 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 | - ); | |
| 124 | + $data['strings'][] = __('Language switcher', 'polylang'); // the title | |
| 116 | 125 | |
| 117 | - // The options values for the language switcher | |
| 118 | - foreach ( $items as $item ) { | |
| 119 | - $data['val'][ $item ] = get_post_meta( $item, '_pll_menu_item', true ); | |
| 120 | - } | |
| 126 | + // get all language switcher menu items | |
| 127 | + $items = get_posts(array( | |
| 128 | + 'numberposts' => -1, | |
| 129 | + 'nopaging' => true, | |
| 130 | + 'post_type' => 'nav_menu_item', | |
| 131 | + 'fields' => 'ids', | |
| 132 | + 'meta_key' => '_pll_menu_item' | |
| 133 | + )); | |
| 121 | 134 | |
| 122 | - // Send all these data to javascript | |
| 123 | - wp_localize_script( 'pll_nav_menu', 'pll_data', $data ); | |
| 135 | + // the options values for the language switcher | |
| 136 | + $data['val'] = array(); | |
| 137 | + foreach ($items as $item) | |
| 138 | + $data['val'][$item] = get_post_meta($item, '_pll_menu_item', true); | |
| 139 | + | |
| 140 | + // send all these data to javascript | |
| 141 | + wp_localize_script('pll_nav_menu', 'pll_data', $data); | |
| 124 | 142 | } |
| 125 | 143 | |
| 126 | - /** | |
| 127 | - * Save our menu item options | |
| 144 | + /* | |
| 145 | + * save our menu item options | |
| 128 | 146 | * |
| 129 | 147 | * @since 1.1 |
| 130 | 148 | * |
| 131 | 149 | * @param int $menu_id not used |
| @@ -131,56 +149,49 @@ | ||
| 131 | 149 | * @param int $menu_id not used |
| 132 | 150 | * @param int $menu_item_db_id |
| 133 | 151 | */ |
| 134 | 152 | 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 | |
| 153 | + if (empty($_POST['menu-item-url'][$menu_item_db_id]) || $_POST['menu-item-url'][$menu_item_db_id] != '#pll_switcher') | |
| 136 | 154 | return; |
| 137 | - } | |
| 138 | 155 | |
| 139 | - // Security check as 'wp_update_nav_menu_item' can be called from outside WP admin | |
| 140 | - if ( current_user_can( 'edit_theme_options' ) ) { | |
| 141 | - check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' ); | |
| 156 | + // security check | |
| 157 | + // as 'wp_update_nav_menu_item' can be called from outside WP admin | |
| 158 | + if ( ! current_user_can('edit_theme_options') ) | |
| 159 | + wp_die( __( 'Cheatin’ uh?' ) ); | |
| 142 | 160 | |
| 143 | - $options = array( 'hide_if_no_translation' => 0, 'hide_current' => 0, 'force_home' => 0, 'show_flags' => 0, 'show_names' => 1, 'dropdown' => 0 ); // Default values | |
| 144 | - // Our jQuery form has not been displayed | |
| 145 | - if ( empty( $_POST['menu-item-pll-detect'][ $menu_item_db_id ] ) ) { | |
| 146 | - if ( ! get_post_meta( $menu_item_db_id, '_pll_menu_item', true ) ) { // Our options were never saved | |
| 147 | - update_post_meta( $menu_item_db_id, '_pll_menu_item', $options ); | |
| 148 | - } | |
| 149 | - } | |
| 150 | - else { | |
| 151 | - foreach ( array_keys( $options ) as $opt ) { | |
| 152 | - $options[ $opt ] = empty( $_POST[ 'menu-item-' . $opt ][ $menu_item_db_id ] ) ? 0 : 1; | |
| 153 | - } | |
| 154 | - update_post_meta( $menu_item_db_id, '_pll_menu_item', $options ); // Allow us to easily identify our nav menu item | |
| 155 | - } | |
| 161 | + check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' ); | |
| 162 | + | |
| 163 | + $options = array('hide_current' => 0,'force_home' => 0 ,'show_flags' => 0 ,'show_names' => 1); // default values | |
| 164 | + // our jQuery form has not been displayed | |
| 165 | + if (empty($_POST['menu-item-pll-detect'][$menu_item_db_id])) { | |
| 166 | + if (!get_post_meta($menu_item_db_id, '_pll_menu_item', true)) // our options were never saved | |
| 167 | + update_post_meta($menu_item_db_id, '_pll_menu_item', $options); | |
| 156 | 168 | } |
| 169 | + else { | |
| 170 | + foreach ($options as $opt => $v) | |
| 171 | + $options[$opt] = empty($_POST['menu-item-'.$opt][$menu_item_db_id]) ? 0 : 1; | |
| 172 | + update_post_meta($menu_item_db_id, '_pll_menu_item', $options); // allow us to easily identify our nav menu item | |
| 173 | + } | |
| 174 | + | |
| 157 | 175 | } |
| 158 | 176 | |
| 159 | - /** | |
| 160 | - * Assign menu languages and translations based on ( temporary ) locations | |
| 177 | + /* | |
| 178 | + * translates the language switcher menu items title in case the user switches the admin language | |
| 161 | 179 | * |
| 162 | - * @since 1.8 | |
| 180 | + * @since 1.1.1 | |
| 163 | 181 | * |
| 164 | - * @param array $locations nav menu locations | |
| 165 | - * @return array | |
| 182 | + * @param array $items | |
| 183 | + * @return array modified $items | |
| 166 | 184 | */ |
| 167 | - public function update_nav_menu_locations( $locations ) { | |
| 168 | - // Extract language and menu from locations | |
| 169 | - foreach ( $locations as $loc => $menu ) { | |
| 170 | - $infos = $this->explode_location( $loc ); | |
| 171 | - $this->options['nav_menus'][ $this->theme ][ $infos['location'] ][ $infos['lang'] ] = $menu; | |
| 172 | - if ( $this->options['default_lang'] != $infos['lang'] ) { | |
| 173 | - unset( $locations[ $loc ] ); // Remove temporary locations before database update | |
| 174 | - } | |
| 175 | - } | |
| 176 | - | |
| 177 | - update_option( 'polylang', $this->options ); | |
| 178 | - return $locations; | |
| 185 | + public function translate_switcher_title($items) { | |
| 186 | + foreach ($items as $item) | |
| 187 | + if ('#pll_switcher' == $item->url) | |
| 188 | + $item->post_title = __('Language switcher', 'polylang'); | |
| 189 | + return $items; | |
| 179 | 190 | } |
| 180 | 191 | |
| 181 | - /** | |
| 182 | - * Assign menu languages and translations based on ( temporary ) locations | |
| 192 | + /* | |
| 193 | + * assign menu languages and translations based on (temporary) locations | |
| 183 | 194 | * |
| 184 | 195 | * @since 1.1 |
| 185 | 196 | * |
| 186 | 197 | * @param array $mods theme mods |
| @@ -185,42 +196,44 @@ | ||
| 185 | 196 | * |
| 186 | 197 | * @param array $mods theme mods |
| 187 | 198 | * @return unmodified $mods |
| 188 | 199 | */ |
| 189 | - public function pre_update_option_theme_mods( $mods ) { | |
| 190 | - if ( current_user_can( 'edit_theme_options' ) && isset( $mods['nav_menu_locations'] ) ) { | |
| 200 | + public function update_nav_menu_locations($mods) { | |
| 201 | + if (isset($mods['nav_menu_locations'])) { | |
| 191 | 202 | |
| 192 | - // Manage Locations tab in Appearance -> Menus | |
| 193 | - if ( isset( $_GET['action'] ) && 'locations' === $_GET['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 203 | + // security check | |
| 204 | + if ( ! current_user_can('edit_theme_options') ) | |
| 205 | + wp_die( __( 'Cheatin’ uh?' ) ); | |
| 206 | + | |
| 207 | + if (isset($_REQUEST['action']) && 'locations' == $_REQUEST['action']) | |
| 194 | 208 | check_admin_referer( 'save-menu-locations' ); |
| 195 | - $this->options['nav_menus'][ $this->theme ] = array(); | |
| 196 | - } | |
| 197 | 209 | |
| 198 | - // Edit Menus tab in Appearance -> Menus | |
| 199 | - // 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'] ) { | |
| 210 | + elseif (isset($_REQUEST['action']) && 'update' == $_REQUEST['action']) | |
| 201 | 211 | check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' ); |
| 202 | - $this->options['nav_menus'][ $this->theme ] = array(); | |
| 203 | - } | |
| 204 | 212 | |
| 205 | - // Customizer | |
| 206 | - // Don't reset locations in this case. | |
| 207 | - // see http://wordpress.org/support/topic/menus-doesnt-show-and-not-saved-in-theme-settings-multilingual-site | |
| 208 | - elseif ( isset( $_POST['action'] ) && 'customize_save' == $_POST['action'] ) { | |
| 209 | - check_ajax_referer( 'save-customize_' . $GLOBALS['wp_customize']->get_stylesheet(), 'nonce' ); | |
| 210 | - } | |
| 213 | + else | |
| 214 | + return; // not called from WP Admin | |
| 211 | 215 | |
| 212 | - else { | |
| 213 | - return $mods; // No modification for nav menu locations | |
| 216 | + $default = pll_default_language(); | |
| 217 | + $arr = array(); | |
| 218 | + | |
| 219 | + // extract language and menu from locations | |
| 220 | + foreach ($mods['nav_menu_locations'] as $loc => $menu) { | |
| 221 | + if ($pos = strpos($loc, '___')) { | |
| 222 | + $this->options['nav_menus'][$this->theme][substr($loc, 0, $pos)][substr($loc, $pos+3)] = $menu; | |
| 223 | + unset($mods['nav_menu_locations'][$loc]); // remove temporary locations before database update | |
| 224 | + } | |
| 225 | + else | |
| 226 | + $this->options['nav_menus'][$this->theme][$loc][$default] = $menu; | |
| 214 | 227 | } |
| 215 | 228 | |
| 216 | - $mods['nav_menu_locations'] = $this->update_nav_menu_locations( $mods['nav_menu_locations'] ); | |
| 229 | + update_option('polylang', $this->options); | |
| 217 | 230 | } |
| 218 | 231 | return $mods; |
| 219 | 232 | } |
| 220 | 233 | |
| 221 | - /** | |
| 222 | - * Fills temporary menu locations based on menus translations | |
| 234 | + /* | |
| 235 | + * fills temporary menu locations based on menus translations | |
| 223 | 236 | * |
| 224 | 237 | * @since 1.2 |
| 225 | 238 | * |
| 226 | 239 | * @param bool|array $menus |
| @@ -225,22 +238,14 @@ | ||
| 225 | 238 | * |
| 226 | 239 | * @param bool|array $menus |
| 227 | 240 | * @return bool|array modified list of menu locations |
| 228 | 241 | */ |
| 229 | - public function theme_mod_nav_menu_locations( $menus ) { | |
| 230 | - // Prefill locations with 0 value in case a location does not exist in $menus | |
| 231 | - $locations = get_registered_nav_menus(); | |
| 232 | - if ( is_array( $locations ) ) { | |
| 233 | - $locations = array_fill_keys( array_keys( $locations ), 0 ); | |
| 234 | - $menus = is_array( $menus ) ? array_merge( $locations, $menus ) : $locations; | |
| 235 | - } | |
| 236 | - | |
| 237 | - if ( is_array( $menus ) ) { | |
| 238 | - foreach ( array_keys( $menus ) as $loc ) { | |
| 239 | - foreach ( $this->model->get_languages_list() as $lang ) { | |
| 240 | - 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 | - $menus[ $this->combine_location( $loc, $lang ) ] = $this->options['nav_menus'][ $this->theme ][ $loc ][ $lang->slug ]; | |
| 242 | - } | |
| 242 | + public function nav_menu_locations($menus) { | |
| 243 | + if (is_array($menus)) { | |
| 244 | + foreach ($menus as $loc => $menu) { | |
| 245 | + foreach ($this->model->get_languages_list() as $lang) { | |
| 246 | + if (pll_default_language() != $lang->slug && !empty($this->options['nav_menus'][$this->theme][$loc][$lang->slug])) | |
| 247 | + $menus[$loc . '___' . $lang->slug] = $this->options['nav_menus'][$this->theme][$loc][$lang->slug]; | |
| 243 | 248 | } |
| 244 | 249 | } |
| 245 | 250 | } |
| 246 | 251 | |
| @@ -246,27 +251,39 @@ | ||
| 246 | 251 | |
| 247 | 252 | return $menus; |
| 248 | 253 | } |
| 249 | 254 | |
| 250 | - /** | |
| 251 | - * Removes the nav menu term_id from the locations stored in Polylang options when a nav menu is deleted | |
| 255 | + /* | |
| 256 | + * filters _wp_auto_add_pages_to_menu by language | |
| 252 | 257 | * |
| 253 | - * @since 1.7.3 | |
| 258 | + * @since 0.9.4 | |
| 254 | 259 | * |
| 255 | - * @param int $term_id nav menu id | |
| 260 | + * @param string $new_status Transition to this post status. | |
| 261 | + * @param string $old_status Previous post status. | |
| 262 | + * @param object $post Post data. | |
| 256 | 263 | */ |
| 257 | - public function delete_nav_menu( $term_id ) { | |
| 258 | - if ( isset( $this->options['nav_menus'] ) ) { | |
| 259 | - foreach ( $this->options['nav_menus'] as $theme => $locations ) { | |
| 260 | - foreach ( $locations as $loc => $languages ) { | |
| 261 | - foreach ( $languages as $lang => $menu_id ) { | |
| 262 | - if ( $menu_id === $term_id ) { | |
| 263 | - unset( $this->options['nav_menus'][ $theme ][ $loc ][ $lang ] ); | |
| 264 | - } | |
| 265 | - } | |
| 266 | - } | |
| 267 | - } | |
| 264 | + public function auto_add_pages_to_menu( $new_status, $old_status, $post ) { | |
| 265 | + if ('publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type || ! empty($post->post_parent) || !($lang = $this->model->get_post_language($post->ID))) | |
| 266 | + return; | |
| 268 | 267 | |
| 269 | - update_option( 'polylang', $this->options ); | |
| 270 | - } | |
| 268 | + // get all the menus in the post language | |
| 269 | + $menus = get_terms('nav_menu', array('lang' => $lang->slug, 'fields' => 'ids', 'hide_empty' => false)); | |
| 270 | + $menus = implode(',', $menus); | |
| 271 | + | |
| 272 | + add_filter('option_nav_menu_options', create_function('$a', "\$a['auto_add'] = array_intersect(\$a['auto_add'], array($menus)); return \$a;")); | |
| 273 | + } | |
| 274 | + | |
| 275 | + /* | |
| 276 | + * FIXME prevents sharing a menu term with a language term by renaming the nav menu before its creation | |
| 277 | + * to avoid http://core.trac.wordpress.org/ticket/24802 | |
| 278 | + * and http://wordpress.org/support/topic/all-connection-between-elements-lost | |
| 279 | + * | |
| 280 | + * @since 1.1.3 | |
| 281 | + * | |
| 282 | + * @param string $name term name | |
| 283 | + * @param string $taxonomy | |
| 284 | + * @return string modified (nav menu) term name if necessary | |
| 285 | + */ | |
| 286 | + function pre_insert_term($name, $taxonomy) { | |
| 287 | + return ('nav_menu' == $taxonomy && in_array($name, $this->model->get_languages_list(array('fields' => 'name')))) ? $name .= '-menu' : $name; | |
| 271 | 288 | } |
| 272 | 289 | } |