| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Manages custom menus translations |
| 8 |
* Common to admin and frontend for the customizer |
| 9 |
* |
| 10 |
* @since 1.7.7 |
| 11 |
*/ |
| 12 |
class PLL_Nav_Menu { |
| 13 |
public $model, $options; |
| 14 |
|
| 15 |
/** |
| 16 |
* Constructor: setups filters and actions |
| 17 |
* |
| 18 |
* @since 1.7.7 |
| 19 |
* |
| 20 |
* @param object $polylang |
| 21 |
*/ |
| 22 |
public function __construct( &$polylang ) { |
| 23 |
$this->model = &$polylang->model; |
| 24 |
$this->options = &$polylang->options; |
| 25 |
|
| 26 |
$this->theme = get_option( 'stylesheet' ); |
| 27 |
|
| 28 |
add_filter( 'wp_setup_nav_menu_item', array( $this, 'wp_setup_nav_menu_item' ) ); |
| 29 |
|
| 30 |
// Integration with WP customizer |
| 31 |
add_action( 'customize_register', array( $this, 'create_nav_menu_locations' ), 5 ); |
| 32 |
|
| 33 |
// Filter _wp_auto_add_pages_to_menu by language |
| 34 |
add_action( 'transition_post_status', array( $this, 'auto_add_pages_to_menu' ), 5, 3 ); // before _wp_auto_add_pages_to_menu |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Assigns the title and label to the language switcher menu items |
| 39 |
* |
| 40 |
* @since 2.6 |
| 41 |
* |
| 42 |
* @param object $item Menu item. |
| 43 |
* @return object |
| 44 |
*/ |
| 45 |
public function wp_setup_nav_menu_item( $item ) { |
| 46 |
if ( '#pll_switcher' === $item->url ) { |
| 47 |
$item->post_title = __( 'Languages', 'polylang' ); |
| 48 |
$item->type_label = __( 'Language switcher', 'polylang' ); |
| 49 |
} |
| 50 |
return $item; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Create temporary nav menu locations ( one per location and per language ) for all non-default language |
| 55 |
* to do only one time |
| 56 |
* |
| 57 |
* @since 1.2 |
| 58 |
*/ |
| 59 |
public function create_nav_menu_locations() { |
| 60 |
static $once; |
| 61 |
global $_wp_registered_nav_menus; |
| 62 |
|
| 63 |
$arr = array(); |
| 64 |
|
| 65 |
if ( isset( $_wp_registered_nav_menus ) && ! $once ) { |
| 66 |
foreach ( $_wp_registered_nav_menus as $loc => $name ) { |
| 67 |
foreach ( $this->model->get_languages_list() as $lang ) { |
| 68 |
$arr[ $this->combine_location( $loc, $lang ) ] = $name . ' ' . $lang->name; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
$_wp_registered_nav_menus = $arr; |
| 73 |
$once = true; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Creates a temporary nav menu location from a location and a language |
| 79 |
* |
| 80 |
* @since 1.8 |
| 81 |
* |
| 82 |
* @param string $loc nav menu location |
| 83 |
* @param object $lang |
| 84 |
* @return string |
| 85 |
*/ |
| 86 |
public function combine_location( $loc, $lang ) { |
| 87 |
return $loc . ( strpos( $loc, '___' ) || $this->options['default_lang'] === $lang->slug ? '' : '___' . $lang->slug ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Get nav menu locations and language from a temporary location |
| 92 |
* |
| 93 |
* @since 1.8 |
| 94 |
* |
| 95 |
* @param string $loc temporary location |
| 96 |
* @return array |
| 97 |
* 'location' => nav menu location |
| 98 |
* 'lang' => language slug |
| 99 |
*/ |
| 100 |
public function explode_location( $loc ) { |
| 101 |
$infos = explode( '___', $loc ); |
| 102 |
if ( 1 == count( $infos ) ) { |
| 103 |
$infos[] = $this->options['default_lang']; |
| 104 |
} |
| 105 |
return array_combine( array( 'location', 'lang' ), $infos ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Filters the option nav_menu_options for auto added pages to menu |
| 110 |
* |
| 111 |
* @since 0.9.4 |
| 112 |
* |
| 113 |
* @param array $options |
| 114 |
* @return array Modified options |
| 115 |
*/ |
| 116 |
public function nav_menu_options( $options ) { |
| 117 |
$options['auto_add'] = array_intersect( $options['auto_add'], $this->auto_add_menus ); |
| 118 |
return $options; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Filters _wp_auto_add_pages_to_menu by language |
| 123 |
* |
| 124 |
* @since 0.9.4 |
| 125 |
* |
| 126 |
* @param string $new_status Transition to this post status. |
| 127 |
* @param string $old_status Previous post status. |
| 128 |
* @param object $post Post data. |
| 129 |
*/ |
| 130 |
public function auto_add_pages_to_menu( $new_status, $old_status, $post ) { |
| 131 |
if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type || ! empty( $post->post_parent ) ) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
if ( ! empty( $this->options['nav_menus'][ $this->theme ] ) ) { |
| 136 |
$this->auto_add_menus = array(); |
| 137 |
|
| 138 |
$lang = $this->model->post->get_language( $post->ID ); |
| 139 |
$lang = empty( $lang ) ? $this->options['default_lang'] : $lang->slug; // If the page has no language yet, the default language will be assigned |
| 140 |
|
| 141 |
// Get all the menus in the page language |
| 142 |
foreach ( $this->options['nav_menus'][ $this->theme ] as $loc ) { |
| 143 |
if ( ! empty( $loc[ $lang ] ) ) { |
| 144 |
$this->auto_add_menus[] = $loc[ $lang ]; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
add_filter( 'option_nav_menu_options', array( $this, 'nav_menu_options' ) ); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|