| @@ -4,8 +4,11 @@ | ||
| 4 | 4 | * |
| 5 | 5 | * @package Polylang |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | +use WP_Syntex\Polylang\Switcher\Switcher; | |
| 9 | +use WP_Syntex\Polylang\Switcher\Settings\Settings; | |
| 10 | + | |
| 8 | 11 | /** |
| 9 | 12 | * Template tag: displays the language switcher. |
| 10 | 13 | * The function does nothing if used outside the frontend. |
| 11 | 14 | * |
| @@ -10,26 +13,44 @@ | ||
| 10 | 13 | * The function does nothing if used outside the frontend. |
| 11 | 14 | * |
| 12 | 15 | * @api |
| 13 | 16 | * @since 0.5 |
| 17 | + * @since 3.9 When the layout `select` is used, the value of each `option` is now always a URL, and the `data-lang` | |
| 18 | + * attributes are not added anymore. | |
| 19 | + * Returns `void` when printing the markup directly. | |
| 14 | 20 | * |
| 15 | 21 | * @param array $args { |
| 16 | - * Optional array of arguments. | |
| 22 | + * Optional switcher settings. | |
| 17 | 23 | * |
| 18 | - * @type int $dropdown The list is displayed as dropdown if set to 1, defaults to 0. | |
| 19 | - * @type int $echo Echoes the list if set to 1, defaults to 1. | |
| 20 | - * @type int $hide_if_empty Hides languages with no posts ( or pages ) if set to 1, defaults to 1. | |
| 21 | - * @type int $show_flags Displays flags if set to 1, defaults to 0. | |
| 22 | - * @type int $show_names Shows language names if set to 1, defaults to 1. | |
| 23 | - * @type string $display_names_as Whether to display the language name or its slug, valid options are 'slug' and 'name', defaults to name. | |
| 24 | - * @type int $force_home Will always link to the homepage in the translated language if set to 1, defaults to 0. | |
| 25 | - * @type int $hide_if_no_translation Hides the link if there is no translation if set to 1, defaults to 0. | |
| 26 | - * @type int $hide_current Hides the current language if set to 1, defaults to 0. | |
| 27 | - * @type int $post_id Returns links to the translations of the post defined by post_id if set, defaults to not set. | |
| 28 | - * @type int $raw Return a raw array instead of html markup if set to 1, defaults to 0. | |
| 29 | - * @type string $item_spacing Whether to preserve or discard whitespace between list items, valid options are 'preserve' and 'discard', defaults to 'preserve'. | |
| 24 | + * @type string $layout Layout of the switcher. Possible values are `horizontal`, `vertical`, | |
| 25 | + * `dropdown`, and `select`. Default is `vertical`. | |
| 26 | + * @type string $alignment Alignment of the items. Possible values are `left`, `center`, `right`, | |
| 27 | + * `stretched`. Default is `left` or `right`, depending on `is_rtl()`. | |
| 28 | + * @type bool $show_wrapper Display the wrapper or not. Default is `false` for legacy calls (list | |
| 29 | + * items only, or bare `<select>` for dropdown). Default is `true` when | |
| 30 | + * `layout` is `select`. Omitting this argument on a bare list call triggers | |
| 31 | + * a `_doing_it_wrong()` notice: pass `show_wrapper` => false explicitly to | |
| 32 | + * keep the current behavior. | |
| 33 | + * @type bool $show_flags Display the flags or not. Default is `false`. | |
| 34 | + * @type string $flag_aspect_ratio Flags aspect ratio. Possible values are `3:2` and `1:1`. Default is `3:2`. | |
| 35 | + * @type string $show_labels Display the labels. Possible values are an empty string (no labels), | |
| 36 | + * `names` (language names), `codes` (languages codes). Default is `names`. | |
| 37 | + * @type bool $hide_if_empty Hide languages that don't have any posts. Default is `true`. | |
| 38 | + * @type bool $hide_if_no_translation Hide languages that don't have translations. Default is `false`. | |
| 39 | + * @type bool $hide_current Hide the current language. Default is `false`. | |
| 40 | + * @type bool $force_home Force elements to link to the home pages instead of the translations. | |
| 41 | + * Default is `false`. | |
| 42 | + * @type int $post_id Build the links according to the translations of the given post ID. | |
| 43 | + * Default is `0`. | |
| 44 | + * @type string[] $wrapper_classes HTML classes to add to the wrapper. Default is an empty array. | |
| 45 | + * @type string[] $item_classes HTML classes to add to each item. Default is an empty array. | |
| 46 | + * @type string[] $link_classes HTML classes to add to each link. Default is an empty array. | |
| 47 | + * @type string $unique_id A unique identifier. Default is an empty string: a default unique | |
| 48 | + * identifier will be automatically generated. | |
| 49 | + * @type bool $echo Whether to print the HTML markup or return it. Default is `true`. | |
| 50 | + * @type bool $raw Whether to return a raw array instead of HTML markup. Default is `false`. | |
| 30 | 51 | * } |
| 31 | - * @return string|array Either the html markup of the switcher or the raw elements to build a custom language switcher. | |
| 52 | + * @return string|array|void Either the html markup of the switcher or the raw elements to build a custom language switcher. | |
| 32 | 53 | */ |
| 33 | 54 | function pll_the_languages( $args = array() ) { |
| 34 | 55 | if ( empty( PLL()->links ) ) { |
| 35 | 56 | return empty( $args['raw'] ) ? '' : array(); |
| @@ -34,10 +55,69 @@ | ||
| 34 | 55 | if ( empty( PLL()->links ) ) { |
| 35 | 56 | return empty( $args['raw'] ) ? '' : array(); |
| 36 | 57 | } |
| 37 | 58 | |
| 38 | - $switcher = new PLL_Switcher(); | |
| 39 | - return $switcher->the_languages( PLL()->links, $args ); | |
| 59 | + if ( Settings::is_legacy( $args ) ) { | |
| 60 | + _deprecated_argument( | |
| 61 | + 'pll_the_languages()', | |
| 62 | + '3.9', | |
| 63 | + 'See the documentation of pll_the_languages()' | |
| 64 | + ); | |
| 65 | + } | |
| 66 | + | |
| 67 | + if ( empty( $args['raw'] ) && ! isset( $args['show_wrapper'] ) ) { | |
| 68 | + if ( ! isset( $args['layout'] ) ) { | |
| 69 | + // Backward compatibility with Polylang < 3.9. | |
| 70 | + $args['show_wrapper'] = false; | |
| 71 | + | |
| 72 | + if ( empty( $args['dropdown'] ) ) { | |
| 73 | + _doing_it_wrong( | |
| 74 | + 'pll_the_languages()', | |
| 75 | + 'pll_the_languages() does not output a wrapper by default. Pass `show_wrapper` => false explicitly to keep this behavior. In a near future, pll_the_languages() will output a `<ul>` wrapper as well (i.e. `show_wrapper` will default to `true`).', | |
| 76 | + '3.9' | |
| 77 | + ); | |
| 78 | + } | |
| 79 | + } | |
| 80 | + } | |
| 81 | + | |
| 82 | + $settings = new Settings( $args ); | |
| 83 | + | |
| 84 | + // Backward compatibility: legacy dropdown switchers ignored `hide_current` unless `raw` was set. | |
| 85 | + $settings->hide_current = ! empty( $args['hide_current'] ) && ! ( ! empty( $args['dropdown'] ) && empty( $args['raw'] ) ); | |
| 86 | + | |
| 87 | + $switcher = new Switcher( $settings, PLL()->links ); | |
| 88 | + | |
| 89 | + if ( ! empty( $args['raw'] ) ) { | |
| 90 | + $elements = array(); | |
| 91 | + | |
| 92 | + foreach ( $switcher->get_elements() as $slug => $element ) { | |
| 93 | + $language = PLL()->links->model->languages->get( $slug ); | |
| 94 | + | |
| 95 | + if ( empty( $language ) ) { | |
| 96 | + // Should not happen. | |
| 97 | + continue; | |
| 98 | + } | |
| 99 | + | |
| 100 | + $element = get_object_vars( $element ); | |
| 101 | + | |
| 102 | + $element['is_rtl'] = 'rtl' === $element['direction']; | |
| 103 | + $element['name'] = 'codes' === $settings->show_labels ? $element['slug'] : $language->name; | |
| 104 | + $element['flag'] = ! empty( $settings->show_flags ) ? $element['flag'] : $language->get_display_flag_url(); | |
| 105 | + $element['current_lang'] = $element['is_current']; | |
| 106 | + $element['no_translation'] = ! $element['has_translations']; | |
| 107 | + $element['classes'] = $element['item_classes']; | |
| 108 | + | |
| 109 | + $elements[ $slug ] = $element; | |
| 110 | + } | |
| 111 | + | |
| 112 | + return $elements; | |
| 113 | + } | |
| 114 | + | |
| 115 | + if ( isset( $args['echo'] ) && empty( $args['echo'] ) ) { | |
| 116 | + return $switcher->get(); | |
| 117 | + } | |
| 118 | + | |
| 119 | + $switcher->print(); | |
| 40 | 120 | } |
| 41 | 121 | |
| 42 | 122 | /** |
| 43 | 123 | * Returns the current language on frontend. |