PluginProbe
Polylang / 2.6.2
Polylang v2.6.2
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / include / nav-menu.php

nav-menu.php in Polylang 2.6.2, at include/nav-menu.php

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