| @@ -14,9 +14,9 @@ | ||
| 14 | 14 | * Constructor: setups filters and actions |
| 15 | 15 | * |
| 16 | 16 | * @since 1.2 |
| 17 | 17 | * |
| 18 | - * @param object $polylang | |
| 18 | + * @param object $polylang The Polylang object. | |
| 19 | 19 | */ |
| 20 | 20 | public function __construct( &$polylang ) { |
| 21 | 21 | parent::__construct( $polylang ); |
| 22 | 22 | |
| @@ -71,9 +71,9 @@ | ||
| 71 | 71 | <label class="menu-item-title"> |
| 72 | 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' ); ?> |
| 73 | 73 | </label> |
| 74 | 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' ); ?>"> | |
| 75 | + <input type="hidden" class="menu-item-title" name="menu-item[<?php echo (int) $_nav_menu_placeholder; ?>][menu-item-title]" value="<?php esc_attr_e( 'Languages', 'polylang' ); ?>"> | |
| 76 | 76 | <input type="hidden" class="menu-item-url" name="menu-item[<?php echo (int) $_nav_menu_placeholder; ?>][menu-item-url]" value="#pll_switcher"> |
| 77 | 77 | </li> |
| 78 | 78 | </ul> |
| 79 | 79 | </div> |
| @@ -100,9 +100,9 @@ | ||
| 100 | 100 | return; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | $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 ); | |
| 104 | + wp_enqueue_script( 'pll_nav_menu', plugins_url( "/js/build/nav-menu{$suffix}.js", POLYLANG_ROOT_FILE ), array(), POLYLANG_VERSION ); | |
| 105 | 105 | |
| 106 | 106 | $data = array( |
| 107 | 107 | 'strings' => PLL_Switcher::get_switcher_options( 'menu', 'string' ), // The strings for the options |
| 108 | 108 | 'title' => __( 'Languages', 'polylang' ), // The title |
| @@ -129,14 +129,14 @@ | ||
| 129 | 129 | wp_localize_script( 'pll_nav_menu', 'pll_data', $data ); |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | /** |
| 133 | - * Save our menu item options | |
| 133 | + * Save our menu item options. | |
| 134 | 134 | * |
| 135 | 135 | * @since 1.1 |
| 136 | 136 | * |
| 137 | - * @param int $menu_id not used | |
| 138 | - * @param int $menu_item_db_id | |
| 137 | + * @param int $menu_id ID of the updated menu. | |
| 138 | + * @param int $menu_item_db_id ID of the updated menu item. | |
| 139 | 139 | * @return void |
| 140 | 140 | */ |
| 141 | 141 | public function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0 ) { |
| 142 | 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 |
| @@ -163,31 +163,35 @@ | ||
| 163 | 163 | } |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | /** |
| 167 | - * Assign menu languages and translations based on ( temporary ) locations | |
| 167 | + * Assigns menu languages and translations based on (temporary) locations. | |
| 168 | 168 | * |
| 169 | 169 | * @since 1.8 |
| 170 | 170 | * |
| 171 | - * @param array $locations nav menu locations | |
| 171 | + * @param array $locations Nav menu locations. | |
| 172 | 172 | * @return array |
| 173 | 173 | */ |
| 174 | 174 | public function update_nav_menu_locations( $locations ) { |
| 175 | - // Extract language and menu from locations | |
| 175 | + // Extract language and menu from locations. | |
| 176 | + $nav_menus = $this->options->get( 'nav_menus' ); | |
| 177 | + | |
| 176 | 178 | foreach ( $locations as $loc => $menu ) { |
| 177 | 179 | $infos = $this->explode_location( $loc ); |
| 178 | - $this->options['nav_menus'][ $this->theme ][ $infos['location'] ][ $infos['lang'] ] = $menu; | |
| 179 | - if ( $this->options['default_lang'] != $infos['lang'] ) { | |
| 180 | - unset( $locations[ $loc ] ); // Remove temporary locations before database update | |
| 180 | + $nav_menus[ $this->theme ][ $infos['location'] ][ $infos['lang'] ] = $menu; | |
| 181 | + | |
| 182 | + if ( $this->options->get( 'default_lang' ) !== $infos['lang'] ) { | |
| 183 | + unset( $locations[ $loc ] ); // Remove temporary locations before database update. | |
| 181 | 184 | } |
| 182 | 185 | } |
| 183 | 186 | |
| 184 | - update_option( 'polylang', $this->options ); | |
| 187 | + $this->options->set( 'nav_menus', $nav_menus ); | |
| 188 | + | |
| 185 | 189 | return $locations; |
| 186 | 190 | } |
| 187 | 191 | |
| 188 | 192 | /** |
| 189 | - * Assign menu languages and translations based on ( temporary ) locations. | |
| 193 | + * Assigns menu languages and translations based on (temporary) locations. | |
| 190 | 194 | * |
| 191 | 195 | * @since 1.1 |
| 192 | 196 | * |
| 193 | 197 | * @param mixed $mods Theme mods. |
| @@ -193,14 +197,17 @@ | ||
| 193 | 197 | * @param mixed $mods Theme mods. |
| 194 | 198 | * @return mixed |
| 195 | 199 | */ |
| 196 | 200 | public function pre_update_option_theme_mods( $mods ) { |
| 197 | - if ( current_user_can( 'edit_theme_options' ) && isset( $mods['nav_menu_locations'] ) ) { | |
| 201 | + if ( current_user_can( 'edit_theme_options' ) && is_array( $mods ) && isset( $mods['nav_menu_locations'] ) ) { | |
| 198 | 202 | |
| 199 | 203 | // Manage Locations tab in Appearance -> Menus |
| 200 | 204 | if ( isset( $_GET['action'] ) && 'locations' === $_GET['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 201 | 205 | check_admin_referer( 'save-menu-locations' ); |
| 202 | - $this->options['nav_menus'][ $this->theme ] = array(); | |
| 206 | + | |
| 207 | + $nav_menus = $this->options->get( 'nav_menus' ); | |
| 208 | + $nav_menus[ $this->theme ] = array(); | |
| 209 | + $this->options->set( 'nav_menus', $nav_menus ); | |
| 203 | 210 | } |
| 204 | 211 | |
| 205 | 212 | // Edit Menus tab in Appearance -> Menus |
| 206 | 213 | // Add the test of $_POST['update-nav-menu-nonce'] to avoid conflict with Vantage theme |
| @@ -205,9 +212,12 @@ | ||
| 205 | 212 | // Edit Menus tab in Appearance -> Menus |
| 206 | 213 | // Add the test of $_POST['update-nav-menu-nonce'] to avoid conflict with Vantage theme |
| 207 | 214 | elseif ( isset( $_POST['action'], $_POST['update-nav-menu-nonce'] ) && 'update' === $_POST['action'] ) { |
| 208 | 215 | check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' ); |
| 209 | - $this->options['nav_menus'][ $this->theme ] = array(); | |
| 216 | + | |
| 217 | + $nav_menus = $this->options->get( 'nav_menus' ); | |
| 218 | + $nav_menus[ $this->theme ] = array(); | |
| 219 | + $this->options->set( 'nav_menus', $nav_menus ); | |
| 210 | 220 | } |
| 211 | 221 | |
| 212 | 222 | // Customizer |
| 213 | 223 | // Don't reset locations in this case. |
| @@ -229,10 +239,10 @@ | ||
| 229 | 239 | * Fills temporary menu locations based on menus translations |
| 230 | 240 | * |
| 231 | 241 | * @since 1.2 |
| 232 | 242 | * |
| 233 | - * @param bool|array $menus | |
| 234 | - * @return bool|array modified list of menu locations | |
| 243 | + * @param bool|array $menus Associative array of registered navigation menu IDs keyed by their location name. | |
| 244 | + * @return bool|array | |
| 235 | 245 | */ |
| 236 | 246 | public function theme_mod_nav_menu_locations( $menus ) { |
| 237 | 247 | // Prefill locations with 0 value in case a location does not exist in $menus |
| 238 | 248 | $locations = get_registered_nav_menus(); |
| @@ -262,19 +272,23 @@ | ||
| 262 | 272 | * @param int $term_id nav menu id |
| 263 | 273 | * @return void |
| 264 | 274 | */ |
| 265 | 275 | public function delete_nav_menu( $term_id ) { |
| 266 | - if ( isset( $this->options['nav_menus'] ) ) { | |
| 267 | - foreach ( $this->options['nav_menus'] as $theme => $locations ) { | |
| 268 | - foreach ( $locations as $loc => $languages ) { | |
| 269 | - foreach ( $languages as $lang => $menu_id ) { | |
| 270 | - if ( $menu_id === $term_id ) { | |
| 271 | - unset( $this->options['nav_menus'][ $theme ][ $loc ][ $lang ] ); | |
| 272 | - } | |
| 276 | + $nav_menus = $this->options->get( 'nav_menus' ); | |
| 277 | + | |
| 278 | + if ( empty( $nav_menus ) ) { | |
| 279 | + return; | |
| 280 | + } | |
| 281 | + | |
| 282 | + foreach ( $nav_menus as $theme => $locations ) { | |
| 283 | + foreach ( $locations as $loc => $languages ) { | |
| 284 | + foreach ( $languages as $lang => $menu_id ) { | |
| 285 | + if ( $menu_id === $term_id ) { | |
| 286 | + unset( $nav_menus[ $theme ][ $loc ][ $lang ] ); | |
| 273 | 287 | } |
| 274 | 288 | } |
| 275 | 289 | } |
| 290 | + } | |
| 276 | 291 | |
| 277 | - update_option( 'polylang', $this->options ); | |
| 278 | - } | |
| 292 | + $this->options->set( 'nav_menus', $nav_menus ); | |
| 279 | 293 | } |
| 280 | 294 | } |