PluginProbe
Polylang / 3.7.1
Polylang v3.7.1
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 3.7.1, at include/nav-menu.php

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