| @@ -8,31 +8,10 @@ | ||
| 8 | 8 | * |
| 9 | 9 | * @since 1.2 |
| 10 | 10 | */ |
| 11 | 11 | class PLL_Switcher { |
| 12 | - const DEFAULTS = array( | |
| 13 | - 'dropdown' => 0, // Display as list and not as dropdown. | |
| 14 | - 'echo' => 1, // Echoes the list. | |
| 15 | - 'hide_if_empty' => 1, // Hides languages with no posts (or pages). | |
| 16 | - 'show_flags' => 0, // Don't show flags. | |
| 17 | - 'show_names' => 1, // Show language names. | |
| 18 | - 'display_names_as' => 'name', // Display the language name. | |
| 19 | - 'force_home' => 0, // Tries to find a translation. | |
| 20 | - 'hide_if_no_translation' => 0, // Don't hide the link if there is no translation. | |
| 21 | - 'hide_current' => 0, // Don't hide the current language. | |
| 22 | - 'post_id' => null, // Link to the translations of the current page. | |
| 23 | - 'raw' => 0, // Build the language switcher. | |
| 24 | - 'item_spacing' => 'preserve', // Preserve whitespace between list items. | |
| 25 | - 'admin_render' => 0, // Make the switcher in a frontend context. | |
| 26 | - 'admin_current_lang' => null, // Use the global current language. | |
| 27 | - ); | |
| 28 | 12 | |
| 29 | 13 | /** |
| 30 | - * @var PLL_Links | |
| 31 | - */ | |
| 32 | - protected $links; | |
| 33 | - | |
| 34 | - /** | |
| 35 | 14 | * Returns options available for the language switcher - menu or widget |
| 36 | 15 | * either strings to display the options or default values |
| 37 | 16 | * |
| 38 | 17 | * @since 0.7 |
| @@ -53,82 +32,31 @@ | ||
| 53 | 32 | return wp_list_pluck( $options, $key ); |
| 54 | 33 | } |
| 55 | 34 | |
| 56 | 35 | /** |
| 57 | - * Returns the current language code. | |
| 36 | + * Get the language elements for use in a walker | |
| 58 | 37 | * |
| 59 | - * @since 3.0 | |
| 38 | + * @see PLL_Switcher::the_languages() for the list of parameters accepted in $args | |
| 60 | 39 | * |
| 61 | - * @param array $args Arguments passed to {@see PLL_Switcher::the_languages()}. | |
| 62 | - * @return string | |
| 63 | - */ | |
| 64 | - protected function get_current_language( $args ) { | |
| 65 | - if ( $args['admin_current_lang'] ) { | |
| 66 | - return $args['admin_current_lang']; | |
| 67 | - } | |
| 68 | - | |
| 69 | - if ( isset( $this->links->curlang ) ) { | |
| 70 | - return $this->links->curlang->slug; | |
| 71 | - } | |
| 72 | - | |
| 73 | - return $this->links->options['default_lang']; | |
| 74 | - } | |
| 75 | - | |
| 76 | - /** | |
| 77 | - * Returns the link for a given language. | |
| 78 | - * | |
| 79 | - * @since 3.0 | |
| 80 | - * | |
| 81 | - * @param PLL_Language $language Language. | |
| 82 | - * @param array $args Arguments passed to {@see PLL_Switcher::the_languages()}. | |
| 83 | - * @return string|null | |
| 84 | - */ | |
| 85 | - protected function get_link( $language, $args ) { | |
| 86 | - global $post; | |
| 87 | - | |
| 88 | - // Priority to the post passed in parameters. | |
| 89 | - if ( null !== $args['post_id'] ) { | |
| 90 | - $tr_id = $this->links->model->post->get( $args['post_id'], $language ); | |
| 91 | - if ( $tr_id && $this->links->model->post->current_user_can_read( $tr_id ) ) { | |
| 92 | - return get_permalink( $tr_id ); | |
| 93 | - } | |
| 94 | - } | |
| 95 | - | |
| 96 | - // If we are on frontend. | |
| 97 | - if ( $this->links instanceof PLL_Frontend_Links ) { | |
| 98 | - return $this->links->get_translation_url( $language ); | |
| 99 | - } | |
| 100 | - | |
| 101 | - // For blocks in posts in REST requests. | |
| 102 | - if ( $post instanceof WP_Post ) { | |
| 103 | - $tr_id = $this->links->model->post->get( $post->ID, $language ); | |
| 104 | - if ( $tr_id && $this->links->model->post->current_user_can_read( $tr_id ) ) { | |
| 105 | - return get_permalink( $tr_id ); | |
| 106 | - } | |
| 107 | - } | |
| 108 | - | |
| 109 | - return null; | |
| 110 | - } | |
| 111 | - | |
| 112 | - /** | |
| 113 | - * Get the language elements for use in a walker | |
| 114 | - * | |
| 115 | 40 | * @since 1.2 |
| 116 | 41 | * |
| 117 | - * @param array $args Arguments passed to {@see PLL_Switcher::the_languages()}. | |
| 118 | - * @return array Language switcher elements. | |
| 42 | + * @param object $links instance of PLL_Frontend_Links | |
| 43 | + * @param array $args | |
| 44 | + * @return array | |
| 119 | 45 | */ |
| 120 | - protected function get_elements( $args ) { | |
| 46 | + protected function get_elements( $links, $args ) { | |
| 121 | 47 | $first = true; |
| 122 | 48 | $out = array(); |
| 123 | 49 | |
| 124 | - foreach ( $this->links->model->get_languages_list( array( 'hide_empty' => $args['hide_if_empty'] ) ) as $language ) { | |
| 50 | + foreach ( $links->model->get_languages_list( array( 'hide_empty' => $args['hide_if_empty'] ) ) as $language ) { | |
| 125 | 51 | $id = (int) $language->term_id; |
| 126 | 52 | $order = (int) $language->term_group; |
| 127 | 53 | $slug = $language->slug; |
| 128 | 54 | $locale = $language->get_locale( 'display' ); |
| 129 | 55 | $classes = array( 'lang-item', 'lang-item-' . $id, 'lang-item-' . esc_attr( $slug ) ); |
| 130 | - $current_lang = $this->get_current_language( $args ) === $slug; | |
| 56 | + $url = null; // Avoids potential notice | |
| 57 | + $curlang = 0 === $args['admin_render'] ? $links->curlang->slug : $args['admin_current_lang']; | |
| 58 | + $current_lang = $curlang == $slug; | |
| 131 | 59 | |
| 132 | 60 | if ( $current_lang ) { |
| 133 | 61 | if ( $args['hide_current'] && ! ( $args['dropdown'] && ! $args['raw'] ) ) { |
| 134 | 62 | continue; // Hide current language except for dropdown |
| @@ -136,9 +64,13 @@ | ||
| 136 | 64 | $classes[] = 'current-lang'; |
| 137 | 65 | } |
| 138 | 66 | } |
| 139 | 67 | |
| 140 | - $url = $this->get_link( $language, $args ); | |
| 68 | + if ( null !== $args['post_id'] && ( $tr_id = $links->model->post->get( $args['post_id'], $language ) ) && $links->model->post->current_user_can_read( $tr_id ) ) { | |
| 69 | + $url = get_permalink( $tr_id ); | |
| 70 | + } elseif ( null === $args['post_id'] && 0 === $args['admin_render'] ) { | |
| 71 | + $url = $links->get_translation_url( $language ); | |
| 72 | + } | |
| 141 | 73 | |
| 142 | 74 | if ( $no_translation = empty( $url ) ) { |
| 143 | 75 | $classes[] = 'no-translation'; |
| 144 | 76 | } |
| @@ -147,11 +79,11 @@ | ||
| 147 | 79 | * Filter the link in the language switcher |
| 148 | 80 | * |
| 149 | 81 | * @since 0.7 |
| 150 | 82 | * |
| 151 | - * @param string|null $url The link, null if no translation was found. | |
| 152 | - * @param string $slug The language code. | |
| 153 | - * @param string $locale The language locale | |
| 83 | + * @param string $url the link | |
| 84 | + * @param string $slug language code | |
| 85 | + * @param string $locale language locale | |
| 154 | 86 | */ |
| 155 | 87 | $url = apply_filters( 'pll_the_language_link', $url, $slug, $language->locale ); |
| 156 | 88 | |
| 157 | 89 | // Hide if no translation exists |
| @@ -158,9 +90,9 @@ | ||
| 158 | 90 | if ( empty( $url ) && $args['hide_if_no_translation'] ) { |
| 159 | 91 | continue; |
| 160 | 92 | } |
| 161 | 93 | |
| 162 | - $url = empty( $url ) || $args['force_home'] ? $this->links->get_home_url( $language ) : $url; // If the page is not translated, link to the home page | |
| 94 | + $url = empty( $url ) || $args['force_home'] ? $links->get_home_url( $language ) : $url; // If the page is not translated, link to the home page | |
| 163 | 95 | |
| 164 | 96 | $name = $args['show_names'] || ! $args['show_flags'] || $args['raw'] ? ( 'slug' == $args['display_names_as'] ? $slug : $language->name ) : ''; |
| 165 | 97 | $flag = $args['raw'] && ! $args['show_flags'] ? $language->get_display_flag_url() : ( $args['show_flags'] ? $language->get_display_flag() : '' ); |
| 166 | 98 | |
| @@ -176,36 +108,52 @@ | ||
| 176 | 108 | } |
| 177 | 109 | |
| 178 | 110 | /** |
| 179 | 111 | * Displays a language switcher |
| 180 | - * or returns the raw elements to build a custom language switcher. | |
| 112 | + * or returns the raw elements to build a custom language switcher | |
| 181 | 113 | * |
| 114 | + * List of parameters accepted in $args: | |
| 115 | + * | |
| 116 | + * dropdown => the list is displayed as dropdown if set, defaults to 0 | |
| 117 | + * echo => echoes the list if set to 1, defaults to 1 | |
| 118 | + * hide_if_empty => hides languages with no posts ( or pages ) if set to 1, defaults to 1 | |
| 119 | + * show_flags => displays flags if set to 1, defaults to 0 | |
| 120 | + * show_names => show language names if set to 1, defaults to 1 | |
| 121 | + * display_names_as => whether to display the language name or its slug, valid options are 'slug' and 'name', defaults to name | |
| 122 | + * force_home => will always link to home in translated language if set to 1, defaults to 0 | |
| 123 | + * hide_if_no_translation => hide the link if there is no translation if set to 1, defaults to 0 | |
| 124 | + * hide_current => hide the current language if set to 1, defaults to 0 | |
| 125 | + * post_id => returns links to translations of post defined by post_id if set, defaults not set | |
| 126 | + * raw => return a raw array instead of html markup if set to 1, defaults to 0 | |
| 127 | + * item_spacing => whether to preserve or discard whitespace between list items, valid options are 'preserve' and 'discard', defaults to preserve | |
| 128 | + * admin_render => allows to force the current language code in an admin context if set, default to 0. Need to set the admin_current_lang argument below | |
| 129 | + * admin_current_lang => the current language code in an admin context. Need to set the admin_render to 1, defaults not set | |
| 130 | + * | |
| 182 | 131 | * @since 0.1 |
| 183 | 132 | * |
| 184 | - * @param PLL_Links $links Instance of PLL_Links. | |
| 185 | - * @param array $args { | |
| 186 | - * Optional array of arguments. | |
| 187 | - * | |
| 188 | - * @type int $dropdown The list is displayed as dropdown if set, defaults to 0. | |
| 189 | - * @type int $echo Echoes the list if set to 1, defaults to 1. | |
| 190 | - * @type int $hide_if_empty Hides languages with no posts ( or pages ) if set to 1, defaults to 1. | |
| 191 | - * @type int $show_flags Displays flags if set to 1, defaults to 0. | |
| 192 | - * @type int $show_names Shows language names if set to 1, defaults to 1. | |
| 193 | - * @type string $display_names_as Whether to display the language name or its slug, valid options are 'slug' and 'name', defaults to name. | |
| 194 | - * @type int $force_home Will always link to home in translated language if set to 1, defaults to 0. | |
| 195 | - * @type int $hide_if_no_translation Hides the link if there is no translation if set to 1, defaults to 0. | |
| 196 | - * @type int $hide_current Hides the current language if set to 1, defaults to 0. | |
| 197 | - * @type int $post_id Returns links to the translations of the post defined by post_id if set, defaults not set. | |
| 198 | - * @type int $raw Return a raw array instead of html markup if set to 1, defaults to 0. | |
| 199 | - * @type string $item_spacing Whether to preserve or discard whitespace between list items, valid options are 'preserve' and 'discard', defaults to 'preserve'. | |
| 200 | - * @type int $admin_render Allows to force the current language code in an admin context if set, default to 0. Need to set the admin_current_lang argument below. | |
| 201 | - * @type string $admin_current_lang The current language code in an admin context. Need to set the admin_render to 1, defaults not set. | |
| 202 | - * } | |
| 133 | + * @param object $links instance of PLL_Frontend_Links | |
| 134 | + * @param array $args | |
| 203 | 135 | * @return string|array either the html markup of the switcher or the raw elements to build a custom language switcher |
| 204 | 136 | */ |
| 205 | - public function the_languages( $links, $args = array() ) { | |
| 206 | - $this->links = $links; | |
| 207 | - $args = wp_parse_args( $args, self::DEFAULTS ); | |
| 137 | + public function the_languages( $links, $args = '' ) { | |
| 138 | + $defaults = array( | |
| 139 | + 'dropdown' => 0, // display as list and not as dropdown | |
| 140 | + 'echo' => 1, // echoes the list | |
| 141 | + 'hide_if_empty' => 1, // hides languages with no posts ( or pages ) | |
| 142 | + 'menu' => 0, // not for nav menu ( this argument is deprecated since v1.1.1 ) | |
| 143 | + 'show_flags' => 0, // don't show flags | |
| 144 | + 'show_names' => 1, // show language names | |
| 145 | + 'display_names_as' => 'name', // valid options are slug and name | |
| 146 | + 'force_home' => 0, // tries to find a translation | |
| 147 | + 'hide_if_no_translation' => 0, // don't hide the link if there is no translation | |
| 148 | + 'hide_current' => 0, // don't hide current language | |
| 149 | + 'post_id' => null, // if not null, link to translations of post defined by post_id | |
| 150 | + 'raw' => 0, // set this to true to build your own custom language switcher | |
| 151 | + 'item_spacing' => 'preserve', // 'preserve' or 'discard' whitespace between list items | |
| 152 | + 'admin_render' => 0, // make the switcher in an frontend context | |
| 153 | + 'admin_current_lang' => null, // use when admin_render is set to 1, if not null use it instead of the current language | |
| 154 | + ); | |
| 155 | + $args = wp_parse_args( $args, $defaults ); | |
| 208 | 156 | |
| 209 | 157 | /** |
| 210 | 158 | * Filter the arguments of the 'pll_the_languages' template tag |
| 211 | 159 | * |
| @@ -219,9 +167,9 @@ | ||
| 219 | 167 | if ( $args['dropdown'] ) { |
| 220 | 168 | $args['show_names'] = 1; |
| 221 | 169 | } |
| 222 | 170 | |
| 223 | - $elements = $this->get_elements( $args ); | |
| 171 | + $elements = $this->get_elements( $links, $args ); | |
| 224 | 172 | |
| 225 | 173 | if ( $args['raw'] ) { |
| 226 | 174 | return $elements; |
| 227 | 175 | } |
| @@ -228,10 +176,11 @@ | ||
| 228 | 176 | |
| 229 | 177 | if ( $args['dropdown'] ) { |
| 230 | 178 | $args['name'] = 'lang_choice_' . $args['dropdown']; |
| 231 | 179 | $walker = new PLL_Walker_Dropdown(); |
| 232 | - $args['selected'] = $this->get_current_language( $args ); | |
| 233 | - } else { | |
| 180 | + $args['selected'] = 0 === $args['admin_render'] ? $links->curlang->slug : $args['admin_current_lang']; | |
| 181 | + } | |
| 182 | + else { | |
| 234 | 183 | $walker = new PLL_Walker_List(); |
| 235 | 184 | } |
| 236 | 185 | |
| 237 | 186 | /** |