class-wpml-ls-public-api.php
2.95 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* Class WPML_LS_Public_API
*/
class WPML_LS_Public_API {
/** @var WPML_LS_Settings $settings */
private $settings;
/** @var WPML_LS_Render $render */
private $render;
/** @var SitePress $sitepress */
protected $sitepress;
/** @var WPML_LS_Slot_Factory */
private $slot_factory;
/**
* WPML_LS_Public_API constructor.
*
* @param WPML_LS_Settings $settings
* @param WPML_LS_Render $render
* @param SitePress $sitepress
* @param WPML_LS_Slot_Factory $slot_factory
*/
public function __construct(
WPML_LS_Settings $settings,
WPML_LS_Render $render,
SitePress $sitepress,
WPML_LS_Slot_Factory $slot_factory = null
) {
$this->settings = $settings;
$this->render = $render;
$this->sitepress = $sitepress;
$this->slot_factory = $slot_factory;
}
/**
* @param array $args
* @param string|null $twig_template
*
* @return string
*/
protected function render( $args, $twig_template = null ) {
$defaults_slot_args = $this->get_default_slot_args( $args );
$slot_args = array_merge( $defaults_slot_args, $args );
$slot = $this->get_slot_factory()->get_slot( $slot_args );
$slot->set( 'show', 1 );
$slot->set( 'template_string', $twig_template );
if ( $slot->is_post_translations() ) {
$output = $this->render->post_translations_label( $slot );
} else {
$output = $this->render->render( $slot );
}
return $output;
}
/**
* @param array $args
*
* @return array
*/
private function get_default_slot_args( $args ) {
$type = 'custom';
if ( isset( $args['type'] ) ) {
$type = $args['type'];
}
switch ( $type ) {
case 'footer':
$default_slot = $this->settings->get_slot( 'statics', 'footer' );
break;
case 'post_translations':
$default_slot = $this->settings->get_slot( 'statics', 'post_translations' );
break;
case 'widget':
$default_slot = $this->get_slot_factory()->get_default_slot( 'sidebars' );
break;
case 'custom':
default:
$default_slot = $this->settings->get_slot( 'statics', 'shortcode_actions' );
break;
}
return $default_slot->get_model();
}
/**
* @param array $args
*
* @return array
*/
protected function convert_shortcode_args_aliases( $args ) {
$aliases_map = self::get_argument_aliases();
foreach ( $aliases_map as $alias => $key ) {
if ( array_key_exists( $alias, $args ) ) {
$args[ $key ] = $args[ $alias ];
unset( $args[ $alias ] );
}
}
return $args;
}
/**
* @return array
*/
public static function get_argument_aliases() {
return array(
'flags' => 'display_flags',
'link_current' => 'display_link_for_current_lang',
'native' => 'display_names_in_native_lang',
'translated' => 'display_names_in_current_lang',
);
}
/**
* @return WPML_LS_Slot_Factory
*/
private function get_slot_factory() {
if ( ! $this->slot_factory ) {
$this->slot_factory = new WPML_LS_Slot_Factory();
}
return $this->slot_factory;
}
}