| @@ -1,5 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * @package Polylang | |
| 4 | + */ | |
| 2 | 5 | |
| 3 | 6 | /** |
| 4 | 7 | * Manages custom menus translations as well as the language switcher menu item on admin side |
| 5 | 8 | * |
| @@ -11,9 +14,9 @@ | ||
| 11 | 14 | * Constructor: setups filters and actions |
| 12 | 15 | * |
| 13 | 16 | * @since 1.2 |
| 14 | 17 | * |
| 15 | - * @param object $polylang | |
| 18 | + * @param object $polylang The Polylang object. | |
| 16 | 19 | */ |
| 17 | 20 | public function __construct( &$polylang ) { |
| 18 | 21 | parent::__construct( $polylang ); |
| 19 | 22 | |
| @@ -29,8 +32,10 @@ | ||
| 29 | 32 | * Setups filters and terms |
| 30 | 33 | * adds the language switcher metabox and create new nav menu locations |
| 31 | 34 | * |
| 32 | 35 | * @since 1.1 |
| 36 | + * | |
| 37 | + * @return void | |
| 33 | 38 | */ |
| 34 | 39 | public function admin_init() { |
| 35 | 40 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 36 | 41 | add_action( 'wp_update_nav_menu_item', array( $this, 'wp_update_nav_menu_item' ), 10, 2 ); |
| @@ -51,8 +56,10 @@ | ||
| 51 | 56 | * The checkbox and all hidden fields are important |
| 52 | 57 | * 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/ |
| 53 | 58 | * |
| 54 | 59 | * @since 1.1 |
| 60 | + * | |
| 61 | + * @return void | |
| 55 | 62 | */ |
| 56 | 63 | public function lang_switch() { |
| 57 | 64 | global $_nav_menu_placeholder, $nav_menu_selected_id; |
| 58 | 65 | $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1; |
| @@ -64,9 +71,9 @@ | ||
| 64 | 71 | <label class="menu-item-title"> |
| 65 | 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' ); ?> |
| 66 | 73 | </label> |
| 67 | 74 | <input type="hidden" class="menu-item-type" name="menu-item[<?php echo (int) $_nav_menu_placeholder; ?>][menu-item-type]" value="custom"> |
| 68 | - <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' ); ?>"> | |
| 69 | 76 | <input type="hidden" class="menu-item-url" name="menu-item[<?php echo (int) $_nav_menu_placeholder; ?>][menu-item-url]" value="#pll_switcher"> |
| 70 | 77 | </li> |
| 71 | 78 | </ul> |
| 72 | 79 | </div> |
| @@ -83,17 +90,19 @@ | ||
| 83 | 90 | /** |
| 84 | 91 | * Prepares javascript to modify the language switcher menu item |
| 85 | 92 | * |
| 86 | 93 | * @since 1.1 |
| 94 | + * | |
| 95 | + * @return void | |
| 87 | 96 | */ |
| 88 | 97 | public function admin_enqueue_scripts() { |
| 89 | 98 | $screen = get_current_screen(); |
| 90 | - if ( 'nav-menus' != $screen->base ) { | |
| 99 | + if ( empty( $screen ) || 'nav-menus' !== $screen->base ) { | |
| 91 | 100 | return; |
| 92 | 101 | } |
| 93 | 102 | |
| 94 | 103 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 95 | - wp_enqueue_script( 'pll_nav_menu', plugins_url( '/js/nav-menu' . $suffix . '.js', POLYLANG_FILE ), 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 ); | |
| 96 | 105 | |
| 97 | 106 | $data = array( |
| 98 | 107 | 'strings' => PLL_Switcher::get_switcher_options( 'menu', 'string' ), // The strings for the options |
| 99 | 108 | 'title' => __( 'Languages', 'polylang' ), // The title |
| @@ -120,14 +129,15 @@ | ||
| 120 | 129 | wp_localize_script( 'pll_nav_menu', 'pll_data', $data ); |
| 121 | 130 | } |
| 122 | 131 | |
| 123 | 132 | /** |
| 124 | - * Save our menu item options | |
| 133 | + * Save our menu item options. | |
| 125 | 134 | * |
| 126 | 135 | * @since 1.1 |
| 127 | 136 | * |
| 128 | - * @param int $menu_id not used | |
| 129 | - * @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 | + * @return void | |
| 130 | 140 | */ |
| 131 | 141 | public function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0 ) { |
| 132 | 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 |
| 133 | 143 | return; |
| @@ -153,44 +163,51 @@ | ||
| 153 | 163 | } |
| 154 | 164 | } |
| 155 | 165 | |
| 156 | 166 | /** |
| 157 | - * Assign menu languages and translations based on ( temporary ) locations | |
| 167 | + * Assigns menu languages and translations based on (temporary) locations. | |
| 158 | 168 | * |
| 159 | 169 | * @since 1.8 |
| 160 | 170 | * |
| 161 | - * @param array $locations nav menu locations | |
| 171 | + * @param array $locations Nav menu locations. | |
| 162 | 172 | * @return array |
| 163 | 173 | */ |
| 164 | 174 | public function update_nav_menu_locations( $locations ) { |
| 165 | - // Extract language and menu from locations | |
| 175 | + // Extract language and menu from locations. | |
| 176 | + $nav_menus = $this->options->get( 'nav_menus' ); | |
| 177 | + | |
| 166 | 178 | foreach ( $locations as $loc => $menu ) { |
| 167 | 179 | $infos = $this->explode_location( $loc ); |
| 168 | - $this->options['nav_menus'][ $this->theme ][ $infos['location'] ][ $infos['lang'] ] = $menu; | |
| 169 | - if ( $this->options['default_lang'] != $infos['lang'] ) { | |
| 170 | - 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. | |
| 171 | 184 | } |
| 172 | 185 | } |
| 173 | 186 | |
| 174 | - update_option( 'polylang', $this->options ); | |
| 187 | + $this->options->set( 'nav_menus', $nav_menus ); | |
| 188 | + | |
| 175 | 189 | return $locations; |
| 176 | 190 | } |
| 177 | 191 | |
| 178 | 192 | /** |
| 179 | - * Assign menu languages and translations based on ( temporary ) locations | |
| 193 | + * Assigns menu languages and translations based on (temporary) locations. | |
| 180 | 194 | * |
| 181 | 195 | * @since 1.1 |
| 182 | 196 | * |
| 183 | - * @param array $mods theme mods | |
| 184 | - * @return unmodified $mods | |
| 197 | + * @param mixed $mods Theme mods. | |
| 198 | + * @return mixed | |
| 185 | 199 | */ |
| 186 | 200 | public function pre_update_option_theme_mods( $mods ) { |
| 187 | - 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'] ) ) { | |
| 188 | 202 | |
| 189 | 203 | // Manage Locations tab in Appearance -> Menus |
| 190 | 204 | if ( isset( $_GET['action'] ) && 'locations' === $_GET['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 191 | 205 | check_admin_referer( 'save-menu-locations' ); |
| 192 | - $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 ); | |
| 193 | 210 | } |
| 194 | 211 | |
| 195 | 212 | // Edit Menus tab in Appearance -> Menus |
| 196 | 213 | // Add the test of $_POST['update-nav-menu-nonce'] to avoid conflict with Vantage theme |
| @@ -195,9 +212,12 @@ | ||
| 195 | 212 | // Edit Menus tab in Appearance -> Menus |
| 196 | 213 | // Add the test of $_POST['update-nav-menu-nonce'] to avoid conflict with Vantage theme |
| 197 | 214 | elseif ( isset( $_POST['action'], $_POST['update-nav-menu-nonce'] ) && 'update' === $_POST['action'] ) { |
| 198 | 215 | check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' ); |
| 199 | - $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 ); | |
| 200 | 220 | } |
| 201 | 221 | |
| 202 | 222 | // Customizer |
| 203 | 223 | // Don't reset locations in this case. |
| @@ -219,10 +239,10 @@ | ||
| 219 | 239 | * Fills temporary menu locations based on menus translations |
| 220 | 240 | * |
| 221 | 241 | * @since 1.2 |
| 222 | 242 | * |
| 223 | - * @param bool|array $menus | |
| 224 | - * @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 | |
| 225 | 245 | */ |
| 226 | 246 | public function theme_mod_nav_menu_locations( $menus ) { |
| 227 | 247 | // Prefill locations with 0 value in case a location does not exist in $menus |
| 228 | 248 | $locations = get_registered_nav_menus(); |
| @@ -249,21 +269,26 @@ | ||
| 249 | 269 | * |
| 250 | 270 | * @since 1.7.3 |
| 251 | 271 | * |
| 252 | 272 | * @param int $term_id nav menu id |
| 273 | + * @return void | |
| 253 | 274 | */ |
| 254 | 275 | public function delete_nav_menu( $term_id ) { |
| 255 | - if ( isset( $this->options['nav_menus'] ) ) { | |
| 256 | - foreach ( $this->options['nav_menus'] as $theme => $locations ) { | |
| 257 | - foreach ( $locations as $loc => $languages ) { | |
| 258 | - foreach ( $languages as $lang => $menu_id ) { | |
| 259 | - if ( $menu_id === $term_id ) { | |
| 260 | - unset( $this->options['nav_menus'][ $theme ][ $loc ][ $lang ] ); | |
| 261 | - } | |
| 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 ] ); | |
| 262 | 287 | } |
| 263 | 288 | } |
| 264 | 289 | } |
| 290 | + } | |
| 265 | 291 | |
| 266 | - update_option( 'polylang', $this->options ); | |
| 267 | - } | |
| 292 | + $this->options->set( 'nav_menus', $nav_menus ); | |
| 268 | 293 | } |
| 269 | 294 | } |