| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* manages custom menus translations |
| 5 |
* common to admin and frontend for the customizer |
| 6 |
* |
| 7 |
* @since 1.7.7 |
| 8 |
*/ |
| 9 |
class PLL_Nav_Menu { |
| 10 |
public $model, $options; |
| 11 |
|
| 12 |
/** |
| 13 |
* constructor: setups filters and actions |
| 14 |
* |
| 15 |
* @since 1.7.7 |
| 16 |
* |
| 17 |
* @param object $polylang |
| 18 |
*/ |
| 19 |
public function __construct( &$polylang ) { |
| 20 |
$this->model = &$polylang->model; |
| 21 |
$this->options = &$polylang->options; |
| 22 |
|
| 23 |
// integration with WP customizer |
| 24 |
add_action( 'customize_register', array( &$this, 'create_nav_menu_locations' ), 5 ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* create temporary nav menu locations ( one per location and per language ) for all non-default language |
| 29 |
* to do only one time |
| 30 |
* |
| 31 |
* @since 1.2 |
| 32 |
*/ |
| 33 |
public function create_nav_menu_locations() { |
| 34 |
static $once; |
| 35 |
global $_wp_registered_nav_menus; |
| 36 |
|
| 37 |
if ( isset( $_wp_registered_nav_menus ) && ! $once ) { |
| 38 |
foreach ( $_wp_registered_nav_menus as $loc => $name ) { |
| 39 |
foreach ( $this->model->get_languages_list() as $lang ) { |
| 40 |
$arr[ $this->combine_location( $loc, $lang ) ] = $name . ' ' . $lang->name; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
$_wp_registered_nav_menus = $arr; |
| 45 |
$once = true; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* creates a temporary nav menu location from a location and a language |
| 51 |
* |
| 52 |
* @since 1.8 |
| 53 |
* |
| 54 |
* @param string $loc nav menu location |
| 55 |
* @param object $lang |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
public function combine_location( $loc, $lang ) { |
| 59 |
return $loc . ( strpos( $loc, '___' ) || $this->options['default_lang'] === $lang->slug ? '' : '___' . $lang->slug ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* get nav menu locations and language from a temporary locaction |
| 64 |
* |
| 65 |
* @since 1.8 |
| 66 |
* |
| 67 |
* @param string $loc temporary location |
| 68 |
* @return array |
| 69 |
* 'location' => nav menu location |
| 70 |
* 'lang' => language slug |
| 71 |
*/ |
| 72 |
public function explode_location( $loc ) { |
| 73 |
$infos = explode( '___', $loc ); |
| 74 |
if ( 1 == count( $infos ) ) { |
| 75 |
$infos[] = $this->options['default_lang']; |
| 76 |
} |
| 77 |
return array_combine( array( 'location', 'lang' ), $infos ); |
| 78 |
} |
| 79 |
} |
| 80 |
|