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