| @@ -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 | * |
| @@ -32,10 +29,8 @@ | ||
| 32 | 29 | * Setups filters and terms |
| 33 | 30 | * adds the language switcher metabox and create new nav menu locations |
| 34 | 31 | * |
| 35 | 32 | * @since 1.1 |
| 36 | - * | |
| 37 | - * @return void | |
| 38 | 33 | */ |
| 39 | 34 | public function admin_init() { |
| 40 | 35 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 41 | 36 | add_action( 'wp_update_nav_menu_item', array( $this, 'wp_update_nav_menu_item' ), 10, 2 ); |
| @@ -56,10 +51,8 @@ | ||
| 56 | 51 | * The checkbox and all hidden fields are important |
| 57 | 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 | 53 | * |
| 59 | 54 | * @since 1.1 |
| 60 | - * | |
| 61 | - * @return void | |
| 62 | 55 | */ |
| 63 | 56 | public function lang_switch() { |
| 64 | 57 | global $_nav_menu_placeholder, $nav_menu_selected_id; |
| 65 | 58 | $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1; |
| @@ -90,25 +83,20 @@ | ||
| 90 | 83 | /** |
| 91 | 84 | * Prepares javascript to modify the language switcher menu item |
| 92 | 85 | * |
| 93 | 86 | * @since 1.1 |
| 94 | - * | |
| 95 | - * @return void | |
| 96 | 87 | */ |
| 97 | 88 | public function admin_enqueue_scripts() { |
| 98 | 89 | $screen = get_current_screen(); |
| 99 | - if ( empty( $screen ) || 'nav-menus' !== $screen->base ) { | |
| 90 | + if ( 'nav-menus' != $screen->base ) { | |
| 100 | 91 | return; |
| 101 | 92 | } |
| 102 | 93 | |
| 103 | 94 | $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 ); | |
| 95 | + wp_enqueue_script( 'pll_nav_menu', plugins_url( '/js/nav-menu' . $suffix . '.js', POLYLANG_FILE ), array( 'jquery' ), POLYLANG_VERSION ); | |
| 105 | 96 | |
| 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 | - ); | |
| 97 | + $data['strings'] = PLL_Switcher::get_switcher_options( 'menu', 'string' ); // The strings for the options | |
| 98 | + $data['title'] = __( 'Languages', 'polylang' ); // The title | |
| 111 | 99 | |
| 112 | 100 | // Get all language switcher menu items |
| 113 | 101 | $items = get_posts( |
| 114 | 102 | array( |
| @@ -120,8 +108,9 @@ | ||
| 120 | 108 | ) |
| 121 | 109 | ); |
| 122 | 110 | |
| 123 | 111 | // The options values for the language switcher |
| 112 | + $data['val'] = array(); | |
| 124 | 113 | foreach ( $items as $item ) { |
| 125 | 114 | $data['val'][ $item ] = get_post_meta( $item, '_pll_menu_item', true ); |
| 126 | 115 | } |
| 127 | 116 | |
| @@ -135,9 +124,8 @@ | ||
| 135 | 124 | * @since 1.1 |
| 136 | 125 | * |
| 137 | 126 | * @param int $menu_id not used |
| 138 | 127 | * @param int $menu_item_db_id |
| 139 | - * @return void | |
| 140 | 128 | */ |
| 141 | 129 | public function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0 ) { |
| 142 | 130 | 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 |
| 143 | 131 | return; |
| @@ -154,9 +142,9 @@ | ||
| 154 | 142 | update_post_meta( $menu_item_db_id, '_pll_menu_item', $options ); |
| 155 | 143 | } |
| 156 | 144 | } |
| 157 | 145 | else { |
| 158 | - foreach ( array_keys( $options ) as $opt ) { | |
| 146 | + foreach ( $options as $opt => $v ) { | |
| 159 | 147 | $options[ $opt ] = empty( $_POST[ 'menu-item-' . $opt ][ $menu_item_db_id ] ) ? 0 : 1; |
| 160 | 148 | } |
| 161 | 149 | update_post_meta( $menu_item_db_id, '_pll_menu_item', $options ); // Allow us to easily identify our nav menu item |
| 162 | 150 | } |
| @@ -171,8 +159,10 @@ | ||
| 171 | 159 | * @param array $locations nav menu locations |
| 172 | 160 | * @return array |
| 173 | 161 | */ |
| 174 | 162 | public function update_nav_menu_locations( $locations ) { |
| 163 | + $default = $this->options['default_lang']; | |
| 164 | + | |
| 175 | 165 | // Extract language and menu from locations |
| 176 | 166 | foreach ( $locations as $loc => $menu ) { |
| 177 | 167 | $infos = $this->explode_location( $loc ); |
| 178 | 168 | $this->options['nav_menus'][ $this->theme ][ $infos['location'] ][ $infos['lang'] ] = $menu; |
| @@ -185,14 +175,14 @@ | ||
| 185 | 175 | return $locations; |
| 186 | 176 | } |
| 187 | 177 | |
| 188 | 178 | /** |
| 189 | - * Assign menu languages and translations based on ( temporary ) locations. | |
| 179 | + * Assign menu languages and translations based on ( temporary ) locations | |
| 190 | 180 | * |
| 191 | 181 | * @since 1.1 |
| 192 | 182 | * |
| 193 | - * @param mixed $mods Theme mods. | |
| 194 | - * @return mixed | |
| 183 | + * @param array $mods theme mods | |
| 184 | + * @return unmodified $mods | |
| 195 | 185 | */ |
| 196 | 186 | public function pre_update_option_theme_mods( $mods ) { |
| 197 | 187 | if ( current_user_can( 'edit_theme_options' ) && isset( $mods['nav_menu_locations'] ) ) { |
| 198 | 188 | |
| @@ -241,9 +231,9 @@ | ||
| 241 | 231 | $menus = is_array( $menus ) ? array_merge( $locations, $menus ) : $locations; |
| 242 | 232 | } |
| 243 | 233 | |
| 244 | 234 | if ( is_array( $menus ) ) { |
| 245 | - foreach ( array_keys( $menus ) as $loc ) { | |
| 235 | + foreach ( $menus as $loc => $menu ) { | |
| 246 | 236 | foreach ( $this->model->get_languages_list() as $lang ) { |
| 247 | 237 | 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 | 238 | $menus[ $this->combine_location( $loc, $lang ) ] = $this->options['nav_menus'][ $this->theme ][ $loc ][ $lang->slug ]; |
| 249 | 239 | } |
| @@ -259,9 +249,8 @@ | ||
| 259 | 249 | * |
| 260 | 250 | * @since 1.7.3 |
| 261 | 251 | * |
| 262 | 252 | * @param int $term_id nav menu id |
| 263 | - * @return void | |
| 264 | 253 | */ |
| 265 | 254 | public function delete_nav_menu( $term_id ) { |
| 266 | 255 | if ( isset( $this->options['nav_menus'] ) ) { |
| 267 | 256 | foreach ( $this->options['nav_menus'] as $theme => $locations ) { |