| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace WP_Syntex\Polylang\Switcher\Layout; |
| 7 |
|
| 8 |
use PLL_Links; |
| 9 |
use PLL_Language; |
| 10 |
use WP_Syntex\Polylang\Switcher\Settings\Settings; |
| 11 |
use WP_Syntex\Polylang\Switcher\Element\Abstract_Element; |
| 12 |
|
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
/** |
| 16 |
* Abstract class to display a certain type of language switcher. |
| 17 |
* |
| 18 |
* @since 3.9 |
| 19 |
*/ |
| 20 |
abstract class Abstract_Layout { |
| 21 |
/** |
| 22 |
* @var Settings |
| 23 |
*/ |
| 24 |
protected Settings $settings; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var PLL_Links |
| 28 |
*/ |
| 29 |
protected PLL_Links $links; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var Abstract_Element[] |
| 33 |
*/ |
| 34 |
protected array $elements = array(); |
| 35 |
|
| 36 |
/** |
| 37 |
* Constructor. |
| 38 |
* |
| 39 |
* @since 3.9 |
| 40 |
* |
| 41 |
* @param Settings $settings Instance of `Settings`. |
| 42 |
* @param PLL_Links $links Instance of `PLL_Links`. |
| 43 |
*/ |
| 44 |
public function __construct( Settings $settings, PLL_Links $links ) { |
| 45 |
$this->settings = $settings; |
| 46 |
$this->links = $links; |
| 47 |
|
| 48 |
foreach ( $this->links->model->languages->get_list() as $language ) { |
| 49 |
$this->elements[ $language->slug ] = $this->get_element( $language ); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Returns the markup of the switcher. |
| 55 |
* |
| 56 |
* @since 3.9 |
| 57 |
* |
| 58 |
* @return string |
| 59 |
*/ |
| 60 |
abstract public function get(): string; |
| 61 |
|
| 62 |
/** |
| 63 |
* Returns the switcher's data. |
| 64 |
* |
| 65 |
* @since 3.9 |
| 66 |
* |
| 67 |
* @return Abstract_Element[] |
| 68 |
* |
| 69 |
* @phpstan-return array<non-empty-string, Abstract_Element> |
| 70 |
*/ |
| 71 |
public function get_elements(): array { |
| 72 |
$out = array(); |
| 73 |
$is_first = true; |
| 74 |
|
| 75 |
foreach ( $this->elements as $element ) { |
| 76 |
if ( $this->settings->hide_current && $element->is_current ) { |
| 77 |
// Hide current item. |
| 78 |
continue; |
| 79 |
} |
| 80 |
|
| 81 |
if ( $this->settings->hide_if_empty && $element->is_empty ) { |
| 82 |
// Hide empty item. |
| 83 |
continue; |
| 84 |
} |
| 85 |
|
| 86 |
if ( $this->settings->hide_if_no_translation && ! $element->has_translations ) { |
| 87 |
// Hide item with no translations. |
| 88 |
continue; |
| 89 |
} |
| 90 |
|
| 91 |
if ( empty( $element->url ) ) { |
| 92 |
// Failed to get a URL. |
| 93 |
continue; |
| 94 |
} |
| 95 |
|
| 96 |
if ( $is_first && ! in_array( 'lang-item-first', $element->item_classes, true ) ) { |
| 97 |
// This is the first element of the list, for example the first `<li>` in the `<ul>` tag. |
| 98 |
$is_first = false; |
| 99 |
$element->item_classes[] = 'lang-item-first'; |
| 100 |
} |
| 101 |
|
| 102 |
$out[ $element->slug ] = $element; |
| 103 |
} |
| 104 |
|
| 105 |
return $out; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Returns an instance of `Abstract_Element`. |
| 110 |
* |
| 111 |
* @since 3.9 |
| 112 |
* |
| 113 |
* @param PLL_Language $language Instance of `PLL_Language`. |
| 114 |
* @return Abstract_Element |
| 115 |
*/ |
| 116 |
abstract protected function get_element( PLL_Language $language ): Abstract_Element; |
| 117 |
|
| 118 |
/** |
| 119 |
* Returns the list of HTML classes to add to the wrapper tag. |
| 120 |
* |
| 121 |
* @since 3.9 |
| 122 |
* |
| 123 |
* @return string[] |
| 124 |
*/ |
| 125 |
protected function get_wrapper_classes(): array { |
| 126 |
$classes = array_merge( |
| 127 |
$this->settings->wrapper_classes, |
| 128 |
array( |
| 129 |
'pll-switcher', |
| 130 |
"pll-layout-{$this->settings->layout}", |
| 131 |
"pll-alignment-{$this->settings->alignment}", |
| 132 |
) |
| 133 |
); |
| 134 |
|
| 135 |
if ( $this->settings->show_flags ) { |
| 136 |
$classes[] = 'pll-aspect-ratio-' . str_replace( ':', '', $this->settings->flag_aspect_ratio ); |
| 137 |
} |
| 138 |
|
| 139 |
return $classes; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Returns the name of the tag to use as "navigation", depending if the current theme supports HTML5 features. |
| 144 |
* |
| 145 |
* @since 3.9 |
| 146 |
* |
| 147 |
* @return string |
| 148 |
*/ |
| 149 |
protected function get_nav_tag(): string { |
| 150 |
$format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml'; |
| 151 |
|
| 152 |
/** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */ |
| 153 |
$format = apply_filters( 'navigation_widgets_format', $format ); |
| 154 |
|
| 155 |
return 'html5' === $format ? 'nav' : 'div'; |
| 156 |
} |
| 157 |
} |
| 158 |
|