| @@ -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 | |
| @@ -23,15 +23,15 @@ | ||
| 23 | 23 | // Populates nav menus locations |
| 24 | 24 | // Since WP 4.4, must be done before customize_register is fired |
| 25 | 25 | add_filter( 'theme_mod_nav_menu_locations', array( $this, 'theme_mod_nav_menu_locations' ), 20 ); |
| 26 | 26 | |
| 27 | - // Integration in the WP menu interface | |
| 28 | - add_action( 'admin_init', array( $this, 'admin_init' ) ); // after Polylang upgrade | |
| 27 | + // Integration in the WP menu interface. | |
| 28 | + add_action( 'admin_init', array( $this, 'admin_init' ) ); // after Polylang upgrade. | |
| 29 | + add_action( 'load-nav-menus.php', array( $this, 'add_meta_box' ) ); | |
| 29 | 30 | } |
| 30 | 31 | |
| 31 | 32 | /** |
| 32 | - * Setups filters and terms | |
| 33 | - * adds the language switcher metabox and create new nav menu locations | |
| 33 | + * Setups filters and terms and create new nav menu locations. | |
| 34 | 34 | * |
| 35 | 35 | * @since 1.1 |
| 36 | 36 | * |
| 37 | 37 | * @return void |
| @@ -39,17 +39,25 @@ | ||
| 39 | 39 | public function admin_init() { |
| 40 | 40 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 41 | 41 | add_action( 'wp_update_nav_menu_item', array( $this, 'wp_update_nav_menu_item' ), 10, 2 ); |
| 42 | 42 | |
| 43 | - // Translation of menus based on chosen locations | |
| 43 | + // Translation of menus based on chosen locations. | |
| 44 | 44 | add_filter( 'pre_update_option_theme_mods_' . $this->theme, array( $this, 'pre_update_option_theme_mods' ) ); |
| 45 | 45 | add_action( 'delete_nav_menu', array( $this, 'delete_nav_menu' ) ); |
| 46 | 46 | |
| 47 | - // FIXME is it possible to choose the order ( after theme locations in WP3.5 and older ) ? | |
| 47 | + $this->create_nav_menu_locations(); | |
| 48 | + } | |
| 49 | + | |
| 50 | + /** | |
| 51 | + * Adds the language switcher metabox. | |
| 52 | + * | |
| 53 | + * @since 3.7.7 | |
| 54 | + * | |
| 55 | + * @return void | |
| 56 | + */ | |
| 57 | + public function add_meta_box() { | |
| 48 | 58 | // FIXME not displayed if Polylang is activated before the first time the user goes to nav menus http://core.trac.wordpress.org/ticket/16828 |
| 49 | 59 | add_meta_box( 'pll_lang_switch_box', __( 'Language switcher', 'polylang' ), array( $this, 'lang_switch' ), 'nav-menus', 'side', 'high' ); |
| 50 | - | |
| 51 | - $this->create_nav_menu_locations(); | |
| 52 | 60 | } |
| 53 | 61 | |
| 54 | 62 | /** |
| 55 | 63 | * Language switcher metabox |
| @@ -71,9 +79,9 @@ | ||
| 71 | 79 | <label class="menu-item-title"> |
| 72 | 80 | <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 | 81 | </label> |
| 74 | 82 | <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' ); ?>"> | |
| 83 | + <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 | 84 | <input type="hidden" class="menu-item-url" name="menu-item[<?php echo (int) $_nav_menu_placeholder; ?>][menu-item-url]" value="#pll_switcher"> |
| 77 | 85 | </li> |
| 78 | 86 | </ul> |
| 79 | 87 | </div> |
| @@ -100,9 +108,9 @@ | ||
| 100 | 108 | return; |
| 101 | 109 | } |
| 102 | 110 | |
| 103 | 111 | $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 ); | |
| 112 | + wp_enqueue_script( 'pll_nav_menu', plugins_url( "/js/build/nav-menu{$suffix}.js", POLYLANG_ROOT_FILE ), array(), POLYLANG_VERSION ); | |
| 105 | 113 | |
| 106 | 114 | $data = array( |
| 107 | 115 | 'strings' => PLL_Switcher::get_switcher_options( 'menu', 'string' ), // The strings for the options |
| 108 | 116 | 'title' => __( 'Languages', 'polylang' ), // The title |
| @@ -129,14 +137,14 @@ | ||
| 129 | 137 | wp_localize_script( 'pll_nav_menu', 'pll_data', $data ); |
| 130 | 138 | } |
| 131 | 139 | |
| 132 | 140 | /** |
| 133 | - * Save our menu item options | |
| 141 | + * Save our menu item options. | |
| 134 | 142 | * |
| 135 | 143 | * @since 1.1 |
| 136 | 144 | * |
| 137 | - * @param int $menu_id not used | |
| 138 | - * @param int $menu_item_db_id | |
| 145 | + * @param int $menu_id ID of the updated menu. | |
| 146 | + * @param int $menu_item_db_id ID of the updated menu item. | |
| 139 | 147 | * @return void |
| 140 | 148 | */ |
| 141 | 149 | public function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0 ) { |
| 142 | 150 | 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 +171,35 @@ | ||
| 163 | 171 | } |
| 164 | 172 | } |
| 165 | 173 | |
| 166 | 174 | /** |
| 167 | - * Assign menu languages and translations based on ( temporary ) locations | |
| 175 | + * Assigns menu languages and translations based on (temporary) locations. | |
| 168 | 176 | * |
| 169 | 177 | * @since 1.8 |
| 170 | 178 | * |
| 171 | - * @param array $locations nav menu locations | |
| 179 | + * @param array $locations Nav menu locations. | |
| 172 | 180 | * @return array |
| 173 | 181 | */ |
| 174 | 182 | public function update_nav_menu_locations( $locations ) { |
| 175 | - // Extract language and menu from locations | |
| 183 | + // Extract language and menu from locations. | |
| 184 | + $nav_menus = $this->options->get( 'nav_menus' ); | |
| 185 | + | |
| 176 | 186 | foreach ( $locations as $loc => $menu ) { |
| 177 | 187 | $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 | |
| 188 | + $nav_menus[ $this->theme ][ $infos['location'] ][ $infos['lang'] ] = $menu ?: 0; | |
| 189 | + | |
| 190 | + if ( $this->options->get( 'default_lang' ) !== $infos['lang'] ) { | |
| 191 | + unset( $locations[ $loc ] ); // Remove temporary locations before database update. | |
| 181 | 192 | } |
| 182 | 193 | } |
| 183 | 194 | |
| 184 | - update_option( 'polylang', $this->options ); | |
| 195 | + $this->options->set( 'nav_menus', $nav_menus ); | |
| 196 | + | |
| 185 | 197 | return $locations; |
| 186 | 198 | } |
| 187 | 199 | |
| 188 | 200 | /** |
| 189 | - * Assign menu languages and translations based on ( temporary ) locations. | |
| 201 | + * Assigns menu languages and translations based on (temporary) locations. | |
| 190 | 202 | * |
| 191 | 203 | * @since 1.1 |
| 192 | 204 | * |
| 193 | 205 | * @param mixed $mods Theme mods. |
| @@ -193,14 +205,17 @@ | ||
| 193 | 205 | * @param mixed $mods Theme mods. |
| 194 | 206 | * @return mixed |
| 195 | 207 | */ |
| 196 | 208 | public function pre_update_option_theme_mods( $mods ) { |
| 197 | - if ( current_user_can( 'edit_theme_options' ) && isset( $mods['nav_menu_locations'] ) ) { | |
| 209 | + if ( current_user_can( 'edit_theme_options' ) && is_array( $mods ) && isset( $mods['nav_menu_locations'] ) ) { | |
| 198 | 210 | |
| 199 | 211 | // Manage Locations tab in Appearance -> Menus |
| 200 | 212 | if ( isset( $_GET['action'] ) && 'locations' === $_GET['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 201 | 213 | check_admin_referer( 'save-menu-locations' ); |
| 202 | - $this->options['nav_menus'][ $this->theme ] = array(); | |
| 214 | + | |
| 215 | + $nav_menus = $this->options->get( 'nav_menus' ); | |
| 216 | + $nav_menus[ $this->theme ] = array(); | |
| 217 | + $this->options->set( 'nav_menus', $nav_menus ); | |
| 203 | 218 | } |
| 204 | 219 | |
| 205 | 220 | // Edit Menus tab in Appearance -> Menus |
| 206 | 221 | // Add the test of $_POST['update-nav-menu-nonce'] to avoid conflict with Vantage theme |
| @@ -205,9 +220,12 @@ | ||
| 205 | 220 | // Edit Menus tab in Appearance -> Menus |
| 206 | 221 | // Add the test of $_POST['update-nav-menu-nonce'] to avoid conflict with Vantage theme |
| 207 | 222 | elseif ( isset( $_POST['action'], $_POST['update-nav-menu-nonce'] ) && 'update' === $_POST['action'] ) { |
| 208 | 223 | check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' ); |
| 209 | - $this->options['nav_menus'][ $this->theme ] = array(); | |
| 224 | + | |
| 225 | + $nav_menus = $this->options->get( 'nav_menus' ); | |
| 226 | + $nav_menus[ $this->theme ] = array(); | |
| 227 | + $this->options->set( 'nav_menus', $nav_menus ); | |
| 210 | 228 | } |
| 211 | 229 | |
| 212 | 230 | // Customizer |
| 213 | 231 | // Don't reset locations in this case. |
| @@ -229,10 +247,10 @@ | ||
| 229 | 247 | * Fills temporary menu locations based on menus translations |
| 230 | 248 | * |
| 231 | 249 | * @since 1.2 |
| 232 | 250 | * |
| 233 | - * @param bool|array $menus | |
| 234 | - * @return bool|array modified list of menu locations | |
| 251 | + * @param bool|array $menus Associative array of registered navigation menu IDs keyed by their location name. | |
| 252 | + * @return bool|array | |
| 235 | 253 | */ |
| 236 | 254 | public function theme_mod_nav_menu_locations( $menus ) { |
| 237 | 255 | // Prefill locations with 0 value in case a location does not exist in $menus |
| 238 | 256 | $locations = get_registered_nav_menus(); |
| @@ -262,19 +280,23 @@ | ||
| 262 | 280 | * @param int $term_id nav menu id |
| 263 | 281 | * @return void |
| 264 | 282 | */ |
| 265 | 283 | 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 | - } | |
| 284 | + $nav_menus = $this->options->get( 'nav_menus' ); | |
| 285 | + | |
| 286 | + if ( empty( $nav_menus ) ) { | |
| 287 | + return; | |
| 288 | + } | |
| 289 | + | |
| 290 | + foreach ( $nav_menus as $theme => $locations ) { | |
| 291 | + foreach ( $locations as $loc => $languages ) { | |
| 292 | + foreach ( $languages as $lang => $menu_id ) { | |
| 293 | + if ( $menu_id === $term_id ) { | |
| 294 | + unset( $nav_menus[ $theme ][ $loc ][ $lang ] ); | |
| 273 | 295 | } |
| 274 | 296 | } |
| 275 | 297 | } |
| 298 | + } | |
| 276 | 299 | |
| 277 | - update_option( 'polylang', $this->options ); | |
| 278 | - } | |
| 300 | + $this->options->set( 'nav_menus', $nav_menus ); | |
| 279 | 301 | } |
| 280 | 302 | } |