class-wpml-ls-slot.php
6.22 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
* Class WPML_LS_Slot
*/
class WPML_LS_Slot {
/* @var array $properties */
private $properties = array();
/* @var array $protected_properties */
private $protected_properties = array(
'slot_group',
'slot_slug',
);
/**
* WPML_Language_Switcher_Slot constructor.
*
* @param array $args
*/
public function __construct( array $args = array() ) {
$this->set_properties( $args );
}
/**
* @param string $property
*
* @return mixed
*/
public function get( $property ) {
return isset( $this->properties[ $property ] ) ? $this->properties[ $property ] : null;
}
/**
* @param string $property
* @param mixed $value
*/
public function set( $property, $value ) {
if ( ! in_array( $property, $this->protected_properties ) ) {
$allowed_properties = $this->get_allowed_properties();
if ( array_key_exists( $property, $allowed_properties ) ) {
$meta_data = $allowed_properties[ $property ];
$this->properties[ $property ] = $this->sanitize( $value, $meta_data );
}
}
}
/**
* @return mixed|string|null
*/
public function group() {
return $this->get( 'slot_group' );
}
/**
* @return mixed|string|null
*/
public function slug() {
return $this->get( 'slot_slug' );
}
/**
* @return bool
*/
public function is_menu() {
return $this->group() === 'menus';
}
/**
* @return bool
*/
public function is_sidebar() {
return $this->group() === 'sidebars';
}
/**
* @return bool
*/
public function is_footer() {
return $this->group() === 'statics' && $this->slug() === 'footer';
}
/**
* @return bool
*/
public function is_post_translations() {
return $this->group() === 'statics' && $this->slug() === 'post_translations';
}
/**
* @return bool
*/
public function is_shortcode_actions() {
return $this->group() === 'statics' && $this->slug() === 'shortcode_actions';
}
/**
* @return bool
*/
public function is_enabled() {
return $this->get( 'show' ) ? true : false;
}
/**
* @return mixed
*/
public function template() {
return $this->get( 'template' );
}
/**
* @return mixed
*/
public function template_string() {
return $this->get( 'template_string' );
}
/**
* @param array $args
*/
private function set_properties( array $args ) {
foreach ( $this->get_allowed_properties() as $allowed_property => $meta_data ) {
$value = null;
if ( isset( $args[ $allowed_property ] ) ) {
$value = $args[ $allowed_property ];
} elseif ( isset( $meta_data['set_missing_to'] ) ) {
$value = $meta_data['set_missing_to'];
}
$this->properties[ $allowed_property ] = $this->sanitize( $value, $meta_data );
}
}
/**
* @return array
*/
protected function get_allowed_properties() {
return array(
'slot_group' => array(
'type' => 'string',
'force_missing_to' => '',
),
'slot_slug' => array(
'type' => 'string',
'force_missing_to' => '',
),
'show' => array(
'type' => 'int',
'force_missing_to' => 0,
),
'is_hierarchical' => array(
'type' => 'int',
'force_missing_to' => 0,
),
'template' => array( 'type' => 'string' ),
'display_flags' => array(
'type' => 'int',
'force_missing_to' => 0,
),
'display_link_for_current_lang' => array(
'type' => 'int',
'force_missing_to' => 0,
),
'display_names_in_native_lang' => array(
'type' => 'int',
'force_missing_to' => 0,
),
'display_names_in_current_lang' => array(
'type' => 'int',
'force_missing_to' => 0,
),
'include_flag_width' => array(
'type' => 'int',
'set_missing_to' => \WPML_LS_Settings::DEFAULT_FLAG_WIDTH,
),
'include_flag_height' => array(
'type' => 'int',
'set_missing_to' => \WPML_LS_Settings::DEFAULT_FLAG_HEIGHT,
),
// Colors
'background_normal' => array( 'type' => 'string' ),
'border_normal' => array( 'type' => 'string' ),
'font_current_normal' => array( 'type' => 'string' ),
'font_current_hover' => array( 'type' => 'string' ),
'background_current_normal' => array( 'type' => 'string' ),
'background_current_hover' => array( 'type' => 'string' ),
'font_other_normal' => array( 'type' => 'string' ),
'font_other_hover' => array( 'type' => 'string' ),
'background_other_normal' => array( 'type' => 'string' ),
'background_other_hover' => array( 'type' => 'string' ),
'template_string' => array(
'type' => 'string',
'twig_string' => 1,
),
);
}
/**
* @param mixed $value
* @param array $meta_data
*
* @return mixed
*/
private function sanitize( $value, array $meta_data ) {
if ( ! is_null( $value ) ) {
switch ( $meta_data['type'] ) {
case 'string':
$value = (string) $value;
if ( array_key_exists( 'stripslashes', $meta_data ) && $meta_data['stripslashes'] ) {
$value = stripslashes( $value );
}
if ( array_key_exists( 'twig_string', $meta_data ) ) {
$value = preg_replace( '/<br\W*?\/>/', '', $value );
} else {
$value = sanitize_text_field( $value );
}
break;
case 'bool':
$value = (bool) $value;
break;
case 'int':
$value = (int) $value;
break;
}
} elseif ( array_key_exists( 'force_missing_to', $meta_data ) ) {
$value = $meta_data['force_missing_to'];
}
return $value;
}
/**
* The use of a plain object does not work in Twig
* e.g: slot_settings[ option.name ~ "_normal" ] (see in panel-colors.twig)
*
* @return array
*/
public function get_model() {
$model = array();
foreach ( $this->properties as $property => $value ) {
$model[ $property ] = $value;
}
return $model;
}
/**
* @param string $slug
*
* @return string|null
*/
protected function get_core_template( $slug ) {
$parameters = WPML_Language_Switcher::parameters();
$core_templates = isset( $parameters['core_templates'] ) ? $parameters['core_templates'] : array();
return isset( $core_templates[ $slug ] ) ? $core_templates[ $slug ] : null;
}
}