PluginProbe
Polylang / 3.8.6
Polylang v3.8.6
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 / src / nav-menu.php

nav-menu.php in Polylang 3.8.6, at src/nav-menu.php

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