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