| @@ -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,31 +23,41 @@ | ||
| 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 | + * | |
| 37 | + * @return void | |
| 36 | 38 | */ |
| 37 | 39 | public function admin_init() { |
| 38 | 40 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 39 | 41 | add_action( 'wp_update_nav_menu_item', array( $this, 'wp_update_nav_menu_item' ), 10, 2 ); |
| 40 | 42 | |
| 41 | - // Translation of menus based on chosen locations | |
| 43 | + // Translation of menus based on chosen locations. | |
| 42 | 44 | add_filter( 'pre_update_option_theme_mods_' . $this->theme, array( $this, 'pre_update_option_theme_mods' ) ); |
| 43 | 45 | add_action( 'delete_nav_menu', array( $this, 'delete_nav_menu' ) ); |
| 44 | 46 | |
| 45 | - // 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() { | |
| 46 | 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 |
| 47 | 59 | add_meta_box( 'pll_lang_switch_box', __( 'Language switcher', 'polylang' ), array( $this, 'lang_switch' ), 'nav-menus', 'side', 'high' ); |
| 48 | - | |
| 49 | - $this->create_nav_menu_locations(); | |
| 50 | 60 | } |
| 51 | 61 | |
| 52 | 62 | /** |
| 53 | 63 | * Language switcher metabox |
| @@ -54,8 +64,10 @@ | ||
| 54 | 64 | * The checkbox and all hidden fields are important |
| 55 | 65 | * 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 | 66 | * |
| 57 | 67 | * @since 1.1 |
| 68 | + * | |
| 69 | + * @return void | |
| 58 | 70 | */ |
| 59 | 71 | public function lang_switch() { |
| 60 | 72 | global $_nav_menu_placeholder, $nav_menu_selected_id; |
| 61 | 73 | $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1; |
| @@ -67,9 +79,9 @@ | ||
| 67 | 79 | <label class="menu-item-title"> |
| 68 | 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' ); ?> |
| 69 | 81 | </label> |
| 70 | 82 | <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' ); ?>"> | |
| 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' ); ?>"> | |
| 72 | 84 | <input type="hidden" class="menu-item-url" name="menu-item[<?php echo (int) $_nav_menu_placeholder; ?>][menu-item-url]" value="#pll_switcher"> |
| 73 | 85 | </li> |
| 74 | 86 | </ul> |
| 75 | 87 | </div> |
| @@ -86,17 +98,19 @@ | ||
| 86 | 98 | /** |
| 87 | 99 | * Prepares javascript to modify the language switcher menu item |
| 88 | 100 | * |
| 89 | 101 | * @since 1.1 |
| 102 | + * | |
| 103 | + * @return void | |
| 90 | 104 | */ |
| 91 | 105 | public function admin_enqueue_scripts() { |
| 92 | 106 | $screen = get_current_screen(); |
| 93 | - if ( 'nav-menus' != $screen->base ) { | |
| 107 | + if ( empty( $screen ) || 'nav-menus' !== $screen->base ) { | |
| 94 | 108 | return; |
| 95 | 109 | } |
| 96 | 110 | |
| 97 | 111 | $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 ); | |
| 112 | + wp_enqueue_script( 'pll_nav_menu', plugins_url( "/js/build/nav-menu{$suffix}.js", POLYLANG_ROOT_FILE ), array(), POLYLANG_VERSION ); | |
| 99 | 113 | |
| 100 | 114 | $data = array( |
| 101 | 115 | 'strings' => PLL_Switcher::get_switcher_options( 'menu', 'string' ), // The strings for the options |
| 102 | 116 | 'title' => __( 'Languages', 'polylang' ), // The title |
| @@ -123,14 +137,15 @@ | ||
| 123 | 137 | wp_localize_script( 'pll_nav_menu', 'pll_data', $data ); |
| 124 | 138 | } |
| 125 | 139 | |
| 126 | 140 | /** |
| 127 | - * Save our menu item options | |
| 141 | + * Save our menu item options. | |
| 128 | 142 | * |
| 129 | 143 | * @since 1.1 |
| 130 | 144 | * |
| 131 | - * @param int $menu_id not used | |
| 132 | - * @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. | |
| 147 | + * @return void | |
| 133 | 148 | */ |
| 134 | 149 | public function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0 ) { |
| 135 | 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 |
| 136 | 151 | return; |
| @@ -156,44 +171,51 @@ | ||
| 156 | 171 | } |
| 157 | 172 | } |
| 158 | 173 | |
| 159 | 174 | /** |
| 160 | - * Assign menu languages and translations based on ( temporary ) locations | |
| 175 | + * Assigns menu languages and translations based on (temporary) locations. | |
| 161 | 176 | * |
| 162 | 177 | * @since 1.8 |
| 163 | 178 | * |
| 164 | - * @param array $locations nav menu locations | |
| 179 | + * @param array $locations Nav menu locations. | |
| 165 | 180 | * @return array |
| 166 | 181 | */ |
| 167 | 182 | public function update_nav_menu_locations( $locations ) { |
| 168 | - // Extract language and menu from locations | |
| 183 | + // Extract language and menu from locations. | |
| 184 | + $nav_menus = $this->options->get( 'nav_menus' ); | |
| 185 | + | |
| 169 | 186 | foreach ( $locations as $loc => $menu ) { |
| 170 | 187 | $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 | |
| 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. | |
| 174 | 192 | } |
| 175 | 193 | } |
| 176 | 194 | |
| 177 | - update_option( 'polylang', $this->options ); | |
| 195 | + $this->options->set( 'nav_menus', $nav_menus ); | |
| 196 | + | |
| 178 | 197 | return $locations; |
| 179 | 198 | } |
| 180 | 199 | |
| 181 | 200 | /** |
| 182 | - * Assign menu languages and translations based on ( temporary ) locations | |
| 201 | + * Assigns menu languages and translations based on (temporary) locations. | |
| 183 | 202 | * |
| 184 | 203 | * @since 1.1 |
| 185 | 204 | * |
| 186 | - * @param array $mods theme mods | |
| 187 | - * @return unmodified $mods | |
| 205 | + * @param mixed $mods Theme mods. | |
| 206 | + * @return mixed | |
| 188 | 207 | */ |
| 189 | 208 | public function pre_update_option_theme_mods( $mods ) { |
| 190 | - 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'] ) ) { | |
| 191 | 210 | |
| 192 | 211 | // Manage Locations tab in Appearance -> Menus |
| 193 | 212 | if ( isset( $_GET['action'] ) && 'locations' === $_GET['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 194 | 213 | check_admin_referer( 'save-menu-locations' ); |
| 195 | - $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 ); | |
| 196 | 218 | } |
| 197 | 219 | |
| 198 | 220 | // Edit Menus tab in Appearance -> Menus |
| 199 | 221 | // Add the test of $_POST['update-nav-menu-nonce'] to avoid conflict with Vantage theme |
| @@ -198,9 +220,12 @@ | ||
| 198 | 220 | // Edit Menus tab in Appearance -> Menus |
| 199 | 221 | // Add the test of $_POST['update-nav-menu-nonce'] to avoid conflict with Vantage theme |
| 200 | 222 | elseif ( isset( $_POST['action'], $_POST['update-nav-menu-nonce'] ) && 'update' === $_POST['action'] ) { |
| 201 | 223 | check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' ); |
| 202 | - $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 ); | |
| 203 | 228 | } |
| 204 | 229 | |
| 205 | 230 | // Customizer |
| 206 | 231 | // Don't reset locations in this case. |
| @@ -222,10 +247,10 @@ | ||
| 222 | 247 | * Fills temporary menu locations based on menus translations |
| 223 | 248 | * |
| 224 | 249 | * @since 1.2 |
| 225 | 250 | * |
| 226 | - * @param bool|array $menus | |
| 227 | - * @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 | |
| 228 | 253 | */ |
| 229 | 254 | public function theme_mod_nav_menu_locations( $menus ) { |
| 230 | 255 | // Prefill locations with 0 value in case a location does not exist in $menus |
| 231 | 256 | $locations = get_registered_nav_menus(); |
| @@ -252,21 +277,26 @@ | ||
| 252 | 277 | * |
| 253 | 278 | * @since 1.7.3 |
| 254 | 279 | * |
| 255 | 280 | * @param int $term_id nav menu id |
| 281 | + * @return void | |
| 256 | 282 | */ |
| 257 | 283 | 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 | - } | |
| 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 ] ); | |
| 265 | 295 | } |
| 266 | 296 | } |
| 267 | 297 | } |
| 298 | + } | |
| 268 | 299 | |
| 269 | - update_option( 'polylang', $this->options ); | |
| 270 | - } | |
| 300 | + $this->options->set( 'nav_menus', $nav_menus ); | |
| 271 | 301 | } |
| 272 | 302 | } |