| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace WP_Syntex\Polylang\Switcher; |
| 7 |
|
| 8 |
use PLL_Links; |
| 9 |
use WP_Syntex\Polylang\Switcher\Settings\Settings; |
| 10 |
|
| 11 |
defined( 'ABSPATH' ) || exit; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class that can display a language switcher. |
| 15 |
* |
| 16 |
* @since 3.9 |
| 17 |
*/ |
| 18 |
class Switcher { |
| 19 |
/** |
| 20 |
* @var Settings |
| 21 |
*/ |
| 22 |
private Settings $settings; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var PLL_Links |
| 26 |
*/ |
| 27 |
private PLL_Links $links; |
| 28 |
|
| 29 |
/** |
| 30 |
* Constructor. |
| 31 |
* |
| 32 |
* @since 3.9 |
| 33 |
* |
| 34 |
* @param Settings $settings Instance of `Settings`. |
| 35 |
* @param PLL_Links $links Instance of `PLL_Links`. |
| 36 |
*/ |
| 37 |
public function __construct( Settings $settings, PLL_Links $links ) { |
| 38 |
$this->settings = $settings; |
| 39 |
$this->links = $links; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Prints the switcher. |
| 44 |
* |
| 45 |
* @since 3.9 |
| 46 |
* |
| 47 |
* @return void |
| 48 |
*/ |
| 49 |
public function print(): void { |
| 50 |
echo $this->get(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Returns the switcher's markup. |
| 55 |
* |
| 56 |
* @since 3.9 |
| 57 |
* |
| 58 |
* @return string |
| 59 |
*/ |
| 60 |
public function get(): string { |
| 61 |
$switcher_class = $this->settings->get_switcher_class(); |
| 62 |
|
| 63 |
if ( ! class_exists( $switcher_class ) ) { |
| 64 |
return ''; |
| 65 |
} |
| 66 |
|
| 67 |
$html = ( new $switcher_class( $this->settings, $this->links ) )->get(); |
| 68 |
|
| 69 |
if ( has_filter( 'pll_the_languages' ) ) { |
| 70 |
/** |
| 71 |
* Filter the whole HTML markup returned by the 'pll_the_languages' template tag. |
| 72 |
* |
| 73 |
* @since 0.8 |
| 74 |
* @since 3.9 Deprecated. |
| 75 |
* @deprecated |
| 76 |
* |
| 77 |
* @param string $html HTML returned/outputted by the template tag. |
| 78 |
* @param array $args Arguments passed to the template tag. |
| 79 |
*/ |
| 80 |
$html = (string) apply_filters_deprecated( |
| 81 |
'pll_the_languages', |
| 82 |
array( $html, $this->settings->get_legacy() ), |
| 83 |
'3.9.0', |
| 84 |
'pll_language_switcher_output' |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Filter the whole switcher markup. |
| 90 |
* |
| 91 |
* @since 3.9 |
| 92 |
* |
| 93 |
* @param string $html Switcher markup. |
| 94 |
* @param Settings $settings Switcher settings. |
| 95 |
* @param Switcher $switcher Switcher's instance. |
| 96 |
*/ |
| 97 |
return (string) apply_filters( 'pll_language_switcher_output', $html, $this->settings, $this ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Returns the switcher's raw data. |
| 102 |
* |
| 103 |
* @since 3.9 |
| 104 |
* |
| 105 |
* @return Element\Abstract_Element[] |
| 106 |
*/ |
| 107 |
public function get_elements(): array { |
| 108 |
$switcher_class = $this->settings->get_switcher_class(); |
| 109 |
|
| 110 |
if ( ! class_exists( $switcher_class ) ) { |
| 111 |
return array(); |
| 112 |
} |
| 113 |
|
| 114 |
$switcher = new $switcher_class( $this->settings, $this->links ); |
| 115 |
|
| 116 |
return $switcher->get_elements(); |
| 117 |
} |
| 118 |
} |
| 119 |
|