LanguageSwitcher.php
2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace WPML\BlockEditor\Blocks;
use WPML\BlockEditor\Loader;
use WPML\Element\API\Languages;
use WPML\FP\Lst;
use WPML\FP\Obj;
use WPML\FP\Relation;
use WPML\LIB\WP\Hooks;
use function WPML\Container\make;
use function WPML\FP\spreadArgs;
use WPML\BlockEditor\Blocks\LanguageSwitcher\Render;
class LanguageSwitcher {
const BLOCK_LANGUAGE_SWITCHER = 'wpml/language-switcher';
const BLOCK_NAVIGATION_LANGUAGE_SWITCHER = 'wpml/navigation-language-switcher';
/** @var Render */
private $render;
/**
* @param Render $render
*/
public function __construct( Render $render ) {
$this->render = $render;
}
/**
* Returns the data that needs to be localized in the JS script.
* @return array
*/
public function register() {
$this->registerLanguageSwitcherBlock();
$this->registerNavigationLanguageSwitcherBlock();
return $this->getLanguageSwitcherLocalisedData();
}
private function registerLanguageSwitcherBlock() {
$blockSettings = [
'render_callback' => [ $this->render, 'render_block' ],
];
register_block_type( self::BLOCK_LANGUAGE_SWITCHER, $blockSettings );
}
private function registerNavigationLanguageSwitcherBlock() {
$blockSettings = [
'render_callback' => [ $this->render, 'render_block' ],
'attributes' => [
'navigationLsHasSubMenuInSameBlock' => [
'type' => 'boolean',
'default' => false,
],
'layoutOpenOnClick' => [
'type' => 'boolean',
'default' => false,
],
'layoutShowArrow' => [
'type' => 'boolean',
'default' => true,
],
],
'uses_context' => [
'layout',
'showSubmenuIcon',
'openSubmenusOnClick',
'style',
'textColor',
'customTextColor',
'backgroundColor',
'customBackgroundColor',
'overlayTextColor',
'customOverlayTextColor',
'overlayBackgroundColor',
'customOverlayBackgroundColor',
'fontSize',
'customFontSize'
],
];
register_block_type( self::BLOCK_NAVIGATION_LANGUAGE_SWITCHER, $blockSettings );
}
public function render() {
/** @var \WPML_LS_Dependencies_Factory $lsFactory */
$lsFactory = make( \WPML_LS_Dependencies_Factory::class );
$shortcodeAPI = $lsFactory->shortcodes();
return $shortcodeAPI->callback( [] );
}
private function getLanguageSwitcherLocalisedData() {
$languages = Obj::values( Languages::withFlags( Languages::getActive() ) );
$activeLanguage = Lst::find( Relation::propEq( 'code', Languages::getCurrentCode() ), $languages );
$data = [
'languages' => $languages,
'activeLanguage' => $activeLanguage,
'isRtl' => Languages::isRtl( strval( Obj::prop( 'code', $activeLanguage ) ) ),
];
return ['languageSwitcher' => $data ];
}
}