| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * Manages custom menus translations |
| 8 | 5 | * Common to admin and frontend for the customizer |
| @@ -9,35 +6,11 @@ | ||
| 9 | 6 | * |
| 10 | 7 | * @since 1.7.7 |
| 11 | 8 | */ |
| 12 | 9 | class PLL_Nav_Menu { |
| 13 | - /** | |
| 14 | - * Stores the plugin options. | |
| 15 | - * | |
| 16 | - * @var array | |
| 17 | - */ | |
| 18 | - public $options; | |
| 10 | + public $model, $options; | |
| 19 | 11 | |
| 20 | 12 | /** |
| 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 | 13 | * Constructor: setups filters and actions |
| 41 | 14 | * |
| 42 | 15 | * @since 1.7.7 |
| 43 | 16 | * |
| @@ -46,49 +19,22 @@ | ||
| 46 | 19 | public function __construct( &$polylang ) { |
| 47 | 20 | $this->model = &$polylang->model; |
| 48 | 21 | $this->options = &$polylang->options; |
| 49 | 22 | |
| 50 | - $this->theme = get_option( 'stylesheet' ); | |
| 51 | - | |
| 52 | - add_filter( 'wp_setup_nav_menu_item', array( $this, 'wp_setup_nav_menu_item' ) ); | |
| 53 | - | |
| 54 | 23 | // Integration with WP customizer |
| 55 | 24 | 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 | 25 | } |
| 60 | 26 | |
| 61 | 27 | /** |
| 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 ( '#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 | 28 | * Create temporary nav menu locations ( one per location and per language ) for all non-default language |
| 79 | 29 | * to do only one time |
| 80 | 30 | * |
| 81 | 31 | * @since 1.2 |
| 82 | - * | |
| 83 | - * @return void | |
| 84 | 32 | */ |
| 85 | 33 | public function create_nav_menu_locations() { |
| 86 | 34 | static $once; |
| 87 | 35 | global $_wp_registered_nav_menus; |
| 88 | 36 | |
| 89 | - $arr = array(); | |
| 90 | - | |
| 91 | 37 | if ( isset( $_wp_registered_nav_menus ) && ! $once ) { |
| 92 | 38 | foreach ( $_wp_registered_nav_menus as $loc => $name ) { |
| 93 | 39 | foreach ( $this->model->get_languages_list() as $lang ) { |
| 94 | 40 | $arr[ $this->combine_location( $loc, $lang ) ] = $name . ' ' . $lang->name; |
| @@ -104,10 +50,10 @@ | ||
| 104 | 50 | * Creates a temporary nav menu location from a location and a language |
| 105 | 51 | * |
| 106 | 52 | * @since 1.8 |
| 107 | 53 | * |
| 108 | - * @param string $loc Nav menu location. | |
| 109 | - * @param PLL_Language $lang Language object. | |
| 54 | + * @param string $loc nav menu location | |
| 55 | + * @param object $lang | |
| 110 | 56 | * @return string |
| 111 | 57 | */ |
| 112 | 58 | public function combine_location( $loc, $lang ) { |
| 113 | 59 | return $loc . ( strpos( $loc, '___' ) || $this->options['default_lang'] === $lang->slug ? '' : '___' . $lang->slug ); |
| @@ -113,17 +59,16 @@ | ||
| 113 | 59 | return $loc . ( strpos( $loc, '___' ) || $this->options['default_lang'] === $lang->slug ? '' : '___' . $lang->slug ); |
| 114 | 60 | } |
| 115 | 61 | |
| 116 | 62 | /** |
| 117 | - * Get nav menu locations and language from a temporary location. | |
| 63 | + * Get nav menu locations and language from a temporary location | |
| 118 | 64 | * |
| 119 | 65 | * @since 1.8 |
| 120 | 66 | * |
| 121 | - * @param string $loc Temporary location. | |
| 122 | - * @return string[] { | |
| 123 | - * @type string $location Nav menu location. | |
| 124 | - * @type string $lang Language code. | |
| 125 | - * } | |
| 67 | + * @param string $loc temporary location | |
| 68 | + * @return array | |
| 69 | + * 'location' => nav menu location | |
| 70 | + * 'lang' => language slug | |
| 126 | 71 | */ |
| 127 | 72 | public function explode_location( $loc ) { |
| 128 | 73 | $infos = explode( '___', $loc ); |
| 129 | 74 | if ( 1 == count( $infos ) ) { |
| @@ -129,49 +74,6 @@ | ||
| 129 | 74 | if ( 1 == count( $infos ) ) { |
| 130 | 75 | $infos[] = $this->options['default_lang']; |
| 131 | 76 | } |
| 132 | 77 | 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 | 78 | } |
| 177 | 79 | } |