PluginProbe
Polylang / 2.7.0.1
Polylang v2.7.0.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 2.7.0.1, at include/nav-menu.php

149 lines 4.0 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 $arr = array();
61
62 if ( isset( $_wp_registered_nav_menus ) && ! $once ) {
63 foreach ( $_wp_registered_nav_menus as $loc => $name ) {
64 foreach ( $this->model->get_languages_list() as $lang ) {
65 $arr[ $this->combine_location( $loc, $lang ) ] = $name . ' ' . $lang->name;
66 }
67 }
68
69 $_wp_registered_nav_menus = $arr;
70 $once = true;
71 }
72 }
73
74 /**
75 * Creates a temporary nav menu location from a location and a language
76 *
77 * @since 1.8
78 *
79 * @param string $loc nav menu location
80 * @param object $lang
81 * @return string
82 */
83 public function combine_location( $loc, $lang ) {
84 return $loc . ( strpos( $loc, '___' ) || $this->options['default_lang'] === $lang->slug ? '' : '___' . $lang->slug );
85 }
86
87 /**
88 * Get nav menu locations and language from a temporary location
89 *
90 * @since 1.8
91 *
92 * @param string $loc temporary location
93 * @return array
94 * 'location' => nav menu location
95 * 'lang' => language slug
96 */
97 public function explode_location( $loc ) {
98 $infos = explode( '___', $loc );
99 if ( 1 == count( $infos ) ) {
100 $infos[] = $this->options['default_lang'];
101 }
102 return array_combine( array( 'location', 'lang' ), $infos );
103 }
104
105 /**
106 * Filters the option nav_menu_options for auto added pages to menu
107 *
108 * @since 0.9.4
109 *
110 * @param array $options
111 * @return array Modified options
112 */
113 public function nav_menu_options( $options ) {
114 $options['auto_add'] = array_intersect( $options['auto_add'], $this->auto_add_menus );
115 return $options;
116 }
117
118 /**
119 * Filters _wp_auto_add_pages_to_menu by language
120 *
121 * @since 0.9.4
122 *
123 * @param string $new_status Transition to this post status.
124 * @param string $old_status Previous post status.
125 * @param object $post Post data.
126 */
127 public function auto_add_pages_to_menu( $new_status, $old_status, $post ) {
128 if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type || ! empty( $post->post_parent ) ) {
129 return;
130 }
131
132 if ( ! empty( $this->options['nav_menus'][ $this->theme ] ) ) {
133 $this->auto_add_menus = array();
134
135 $lang = $this->model->post->get_language( $post->ID );
136 $lang = empty( $lang ) ? $this->options['default_lang'] : $lang->slug; // If the page has no language yet, the default language will be assigned
137
138 // Get all the menus in the page language
139 foreach ( $this->options['nav_menus'][ $this->theme ] as $loc ) {
140 if ( ! empty( $loc[ $lang ] ) ) {
141 $this->auto_add_menus[] = $loc[ $lang ];
142 }
143 }
144
145 add_filter( 'option_nav_menu_options', array( $this, 'nav_menu_options' ) );
146 }
147 }
148 }
149