| 1 |
<?php |
| 2 |
|
| 3 |
// If this file is called directly, abort. |
| 4 |
if ( ! defined( 'WPINC' ) ) { |
| 5 |
die; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
/** |
| 10 |
* Get ars from href for nav menu switcher |
| 11 |
* |
| 12 |
* @param string $href |
| 13 |
* @return array|false |
| 14 |
*/ |
| 15 |
function wplng_switcher_nav_menu_args_from_href( $href ) { |
| 16 |
|
| 17 |
if ( empty( $href ) ) { |
| 18 |
return false; |
| 19 |
} |
| 20 |
|
| 21 |
// Get valid keys for nav menu switcher options |
| 22 |
|
| 23 |
$valid_name_format = wplng_data_switcher_nav_menu_valid_name_format(); |
| 24 |
$valid_flags_style = wplng_data_switcher_nav_menu_valid_flags_style(); |
| 25 |
$valid_layout = wplng_data_switcher_nav_menu_valid_layout(); |
| 26 |
|
| 27 |
// Create tht regex pattern |
| 28 |
|
| 29 |
$pattern = '/#wplng'; |
| 30 |
$pattern .= '-n(' . implode( '|', array_keys( $valid_name_format ) ) . ')'; |
| 31 |
$pattern .= '-f(' . implode( '|', array_keys( $valid_flags_style ) ) . ')'; |
| 32 |
$pattern .= '-l(' . implode( '|', array_keys( $valid_layout ) ) . ')'; |
| 33 |
$pattern .= '/'; |
| 34 |
|
| 35 |
// Apply and test the regex |
| 36 |
|
| 37 |
$match = array(); |
| 38 |
|
| 39 |
if ( ! preg_match( $pattern, $href, $match ) |
| 40 |
|| empty( $match[1] ) |
| 41 |
|| ! array_key_exists( $match[1], $valid_name_format ) |
| 42 |
|| empty( $match[2] ) |
| 43 |
|| ! array_key_exists( $match[2], $valid_flags_style ) |
| 44 |
|| empty( $match[3] ) |
| 45 |
|| ! array_key_exists( $match[3], $valid_layout ) |
| 46 |
) { |
| 47 |
return false; |
| 48 |
} |
| 49 |
|
| 50 |
// Check if names or fags are displayed |
| 51 |
if ( 'n' === $match[1] && 'n' === $match[2] ) { |
| 52 |
$match[1] = 'o'; |
| 53 |
} |
| 54 |
|
| 55 |
// Return the $args array checked |
| 56 |
|
| 57 |
return array( |
| 58 |
'name_format' => array( |
| 59 |
'value' => $match[1], |
| 60 |
'label' => $valid_name_format[ $match[1] ], |
| 61 |
), |
| 62 |
'flags_style' => array( |
| 63 |
'value' => $match[2], |
| 64 |
'label' => $valid_flags_style[ $match[2] ], |
| 65 |
), |
| 66 |
'layout' => array( |
| 67 |
'value' => $match[3], |
| 68 |
'label' => $valid_layout[ $match[3] ], |
| 69 |
), |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
/** |
| 75 |
* Set switcher in nav menu |
| 76 |
* |
| 77 |
* @param array $items |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
function wplng_switcher_nav_menu_replace_items( $items ) { |
| 81 |
|
| 82 |
$new_items = array(); |
| 83 |
|
| 84 |
foreach ( $items as $item ) { |
| 85 |
|
| 86 |
// Check if is on a translatable page |
| 87 |
|
| 88 |
if ( in_array( 'wplingua-menu-switcher-untreated', $item->classes ) |
| 89 |
&& ! wplng_url_is_translatable() |
| 90 |
) { |
| 91 |
continue; |
| 92 |
} |
| 93 |
|
| 94 |
// Check if is a wpLingua switcher link |
| 95 |
|
| 96 |
if ( empty( $item->classes ) |
| 97 |
|| ! in_array( 'wplingua-menu-switcher-untreated', $item->classes ) |
| 98 |
) { |
| 99 |
$new_items[] = $item; |
| 100 |
continue; |
| 101 |
} |
| 102 |
|
| 103 |
// Check if option in URL is valid |
| 104 |
|
| 105 |
$args = wplng_switcher_nav_menu_args_from_href( $item->url ); |
| 106 |
|
| 107 |
if ( false === $args ) { |
| 108 |
$new_items[] = $item; |
| 109 |
continue; |
| 110 |
} |
| 111 |
|
| 112 |
// Check if language target is empty, do not show the switcher |
| 113 |
|
| 114 |
$languages_target = wplng_get_languages_target(); |
| 115 |
|
| 116 |
if ( empty( $languages_target ) ) { |
| 117 |
continue; |
| 118 |
} |
| 119 |
|
| 120 |
$offset = $item->menu_order; |
| 121 |
$language_current_id = wplng_get_language_current_id(); |
| 122 |
$language_current = wplng_get_language_by_id( $language_current_id ); |
| 123 |
$language_website = wplng_get_language_website(); |
| 124 |
$url_website = wplng_get_url_original(); |
| 125 |
|
| 126 |
$url_website = remove_query_arg( |
| 127 |
'wplng-mode', |
| 128 |
$url_website |
| 129 |
); |
| 130 |
|
| 131 |
// Create $item_template and prepare classes |
| 132 |
|
| 133 |
$item_template = clone $item; |
| 134 |
|
| 135 |
$item_template->classes = array_merge( |
| 136 |
array_diff( |
| 137 |
$item_template->classes, |
| 138 |
array( |
| 139 |
'wplingua-menu-switcher-untreated', |
| 140 |
'menu-item-type-custom', |
| 141 |
'menu-item-object-custom', |
| 142 |
) |
| 143 |
), |
| 144 |
array( |
| 145 |
'wplingua-menu', |
| 146 |
) |
| 147 |
); |
| 148 |
|
| 149 |
/** |
| 150 |
* Add parent for sub-list |
| 151 |
*/ |
| 152 |
|
| 153 |
if ( 's' === $args['layout']['value'] ) { |
| 154 |
|
| 155 |
$new_item = clone $item_template; |
| 156 |
|
| 157 |
$title = ''; |
| 158 |
|
| 159 |
switch ( $args['name_format']['value'] ) { |
| 160 |
case 'o': |
| 161 |
$title = wplng_get_language_name_untranslated( |
| 162 |
$language_current_id |
| 163 |
); |
| 164 |
break; |
| 165 |
|
| 166 |
case 't': |
| 167 |
$title = wplng_get_language_name_translated( |
| 168 |
$language_current_id, |
| 169 |
$language_current_id |
| 170 |
); |
| 171 |
break; |
| 172 |
|
| 173 |
case 'i': |
| 174 |
$title = strtoupper( $language_current_id ); |
| 175 |
break; |
| 176 |
|
| 177 |
default: // case 'n' |
| 178 |
$title = ''; |
| 179 |
break; |
| 180 |
} |
| 181 |
|
| 182 |
$new_item->ID = 'wplng-language-' . $language_current_id; |
| 183 |
$new_item->title = $title; |
| 184 |
$new_item->attr_title = $title; |
| 185 |
$new_item->url = wplng_get_url_current(); |
| 186 |
$new_item->classes[] = 'wplng-language-parent'; |
| 187 |
$new_item->classes[] = 'wplng-language-current'; |
| 188 |
$new_item->wplng_lang_id = $language_current_id; |
| 189 |
|
| 190 |
if ( 'y' === $args['flags_style']['value'] ) { |
| 191 |
|
| 192 |
$new_item->wplng_flag = $language_current['flag']; |
| 193 |
|
| 194 |
$new_item->wplng_alt = wplng_get_language_name_translated( |
| 195 |
$language_current_id, |
| 196 |
$language_current_id |
| 197 |
); |
| 198 |
} |
| 199 |
|
| 200 |
$new_items[] = $new_item; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Add website language |
| 205 |
*/ |
| 206 |
|
| 207 |
if ( ! ( |
| 208 |
$language_current_id === $language_website['id'] |
| 209 |
&& 't' === $args['layout']['value'] |
| 210 |
) |
| 211 |
&& ( |
| 212 |
's' !== $args['layout']['value'] |
| 213 |
|| $language_current_id !== $language_website['id'] |
| 214 |
) |
| 215 |
) { |
| 216 |
|
| 217 |
$new_item = clone $item_template; |
| 218 |
|
| 219 |
$title = ''; |
| 220 |
|
| 221 |
switch ( $args['name_format']['value'] ) { |
| 222 |
case 'o': |
| 223 |
$title = wplng_get_language_name_untranslated( |
| 224 |
$language_website['id'] |
| 225 |
); |
| 226 |
break; |
| 227 |
|
| 228 |
case 't': |
| 229 |
$title = wplng_get_language_name_translated( |
| 230 |
$language_website['id'], |
| 231 |
$language_current_id |
| 232 |
); |
| 233 |
break; |
| 234 |
|
| 235 |
case 'i': |
| 236 |
$title = strtoupper( $language_website['id'] ); |
| 237 |
break; |
| 238 |
|
| 239 |
default: // case 'n' |
| 240 |
$title = ''; |
| 241 |
break; |
| 242 |
} |
| 243 |
|
| 244 |
$new_item->ID = 'wplng-language-' . $language_website['id']; |
| 245 |
$new_item->menu_order = $item_template->menu_order + $offset; |
| 246 |
$new_item->title = $title; |
| 247 |
$new_item->attr_title = $title; |
| 248 |
$new_item->url = $url_website; |
| 249 |
$new_item->db_id = 0; |
| 250 |
$new_item->classes[] = 'wplng-language-website'; |
| 251 |
$new_item->wplng_lang_id = $language_website['id']; |
| 252 |
|
| 253 |
if ( 's' === $args['layout']['value'] ) { |
| 254 |
$new_item->menu_item_parent = $item_template->ID; |
| 255 |
} |
| 256 |
|
| 257 |
if ( $language_current_id === $language_website['id'] ) { |
| 258 |
|
| 259 |
$new_item->classes[] = 'wplng-language-current'; |
| 260 |
|
| 261 |
if ( 'a' === $args['layout']['value'] ) { |
| 262 |
$new_item->current = true; |
| 263 |
$new_item->classes[] = 'current-menu-item'; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
if ( 'y' === $args['flags_style']['value'] ) { |
| 268 |
|
| 269 |
$new_item->wplng_flag = $language_website['flag']; |
| 270 |
|
| 271 |
$new_item->wplng_alt = wplng_get_language_name_translated( |
| 272 |
$language_website['id'], |
| 273 |
$language_current_id |
| 274 |
); |
| 275 |
|
| 276 |
} |
| 277 |
|
| 278 |
$new_items[] = $new_item; |
| 279 |
|
| 280 |
++$offset; |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Add target languages |
| 285 |
*/ |
| 286 |
|
| 287 |
foreach ( $languages_target as $language_target ) { |
| 288 |
|
| 289 |
if ( $language_current_id === $language_target['id'] |
| 290 |
&& ( |
| 291 |
's' === $args['layout']['value'] |
| 292 |
|| 't' === $args['layout']['value'] |
| 293 |
) |
| 294 |
) { |
| 295 |
continue; |
| 296 |
} |
| 297 |
|
| 298 |
$new_item = clone $item_template; |
| 299 |
|
| 300 |
$title = ''; |
| 301 |
|
| 302 |
switch ( $args['name_format']['value'] ) { |
| 303 |
case 'o': |
| 304 |
$title = wplng_get_language_name_untranslated( |
| 305 |
$language_target['id'] |
| 306 |
); |
| 307 |
break; |
| 308 |
|
| 309 |
case 't': |
| 310 |
$title = wplng_get_language_name_translated( |
| 311 |
$language_target['id'], |
| 312 |
$language_current_id |
| 313 |
); |
| 314 |
break; |
| 315 |
|
| 316 |
case 'i': // case 'i' |
| 317 |
$title = strtoupper( $language_target['id'] ); |
| 318 |
break; |
| 319 |
|
| 320 |
default: // case 'n' |
| 321 |
$title = ''; |
| 322 |
break; |
| 323 |
} |
| 324 |
|
| 325 |
$new_item->ID = 'wplng-language-' . $language_target['id']; |
| 326 |
$new_item->menu_order = $item_template->menu_order + $offset; |
| 327 |
$new_item->title = $title; |
| 328 |
$new_item->attr_title = $title; |
| 329 |
$new_item->url = wplng_get_url_current_for_language( $language_target['id'] ); |
| 330 |
$new_item->db_id = 0; |
| 331 |
$new_item->classes[] = 'wplng-language-target'; |
| 332 |
$new_item->wplng_lang_id = $language_target['id']; |
| 333 |
|
| 334 |
if ( 's' === $args['layout']['value'] ) { |
| 335 |
$new_item->menu_item_parent = $item_template->ID; |
| 336 |
} |
| 337 |
|
| 338 |
if ( $language_current_id === $language_target['id'] ) { |
| 339 |
|
| 340 |
$new_item->classes[] = 'wplng-language-current'; |
| 341 |
|
| 342 |
if ( 'a' === $args['layout']['value'] ) { |
| 343 |
$new_item->current = true; |
| 344 |
$new_item->classes[] = 'current-menu-item'; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
if ( 'y' === $args['flags_style']['value'] ) { |
| 349 |
$new_item->wplng_flag = $language_target['flag']; |
| 350 |
|
| 351 |
$new_item->wplng_alt = wplng_get_language_name_translated( |
| 352 |
$language_target['id'], |
| 353 |
$language_current_id |
| 354 |
); |
| 355 |
} |
| 356 |
|
| 357 |
$new_items[] = $new_item; |
| 358 |
|
| 359 |
++$offset; |
| 360 |
} |
| 361 |
} // End foreach $items |
| 362 |
|
| 363 |
return $new_items; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
/** |
| 368 |
* Add attribute in nav menu switcher |
| 369 |
* |
| 370 |
* @param array $atts |
| 371 |
* @param WP_Post $menu_item |
| 372 |
* @return array |
| 373 |
*/ |
| 374 |
function wplng_add_nav_menu_link_attributes_atts( $atts, $menu_item ) { |
| 375 |
|
| 376 |
if ( ! empty( $menu_item->wplng_flag ) ) { |
| 377 |
$atts['data-wplng-flag'] = $menu_item->wplng_flag; |
| 378 |
} |
| 379 |
|
| 380 |
if ( ! empty( $menu_item->wplng_alt ) ) { |
| 381 |
$atts['data-wplng-alt'] = $menu_item->wplng_alt; |
| 382 |
} |
| 383 |
|
| 384 |
if ( ! empty( $menu_item->wplng_lang_id ) ) { |
| 385 |
$atts['data-wplng-lang-id'] = $menu_item->wplng_lang_id; |
| 386 |
} |
| 387 |
|
| 388 |
if ( ! empty( $menu_item->classes ) |
| 389 |
&& in_array( 'wplng-language-current', $menu_item->classes ) |
| 390 |
) { |
| 391 |
$atts['onclick'] = 'event.preventDefault();'; |
| 392 |
} |
| 393 |
|
| 394 |
return $atts; |
| 395 |
} |
| 396 |
|