class-wpml-ls-inline-styles.php
10.4 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
class WPML_LS_Inline_Styles {
/* @var WPML_LS_Templates $templates */
private $templates;
/* @var WPML_LS_Settings $settings */
private $settings;
/* @var WPML_LS_Model_Build $model_build */
private $model_build;
/**
* WPML_Language_Switcher_Render_Assets constructor.
*
* @param WPML_LS_Templates $templates
* @param WPML_LS_Settings $settings
* @param WPML_LS_Model_Build $model_build
*/
public function __construct( $templates, $settings, $model_build ) {
$this->templates = $templates;
$this->settings = $settings;
$this->model_build = $model_build;
}
public function init_hooks() {
add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts_action' ), 20 );
}
/**
* @param WPML_LS_Slot $slot
*
* @return string
*/
private function get_slot_color_picker_css( $slot ) {
$css = '';
if ( $slot->is_menu() ) {
$css = $this->get_slot_color_picker_css_for_menus( $slot );
} elseif ( ! $slot->is_post_translations() || $slot->is_footer() ) {
$css = $this->get_slot_color_picker_css_for_widgets_and_statics( $slot );
}
return $this->sanitize_css( $css );
}
/**
* @param WPML_LS_Slot $slot
*
* @return string
*/
private function get_slot_color_picker_css_for_menus( $slot ) {
$css = '';
$prefix = '.' . $this->model_build->get_css_prefix();
$menu_item_class = $prefix . 'slot-' . $slot->slug();
if ( $slot->get( 'background_other_normal' ) || $slot->get( 'font_other_normal' ) ) {
$css .= "$menu_item_class,";
$css .= " $menu_item_class a,";
$css .= " $menu_item_class a:visited{";
$css .= $slot->get( 'background_other_normal' ) ? "background-color:{$slot->get( 'background_other_normal' )};" : '';
$css .= $slot->get( 'font_other_normal' ) ? "color:{$slot->get( 'font_other_normal' )};" : '';
$css .= '}';
}
if ( $slot->get( 'font_other_hover' ) || $slot->get( 'background_other_hover' ) ) {
$css .= "$menu_item_class:hover,";
$css .= " $menu_item_class:hover a,";
$css .= " $menu_item_class a:hover{";
$css .= $slot->get( 'font_other_hover' ) ? "color:{$slot->get( 'font_other_hover' )};" : '';
$css .= $slot->get( 'background_other_hover' ) ? "background-color:{$slot->get( 'background_other_hover' )};" : '';
$css .= '}';
}
if ( $slot->get( 'font_current_normal' ) || $slot->get( 'background_current_normal' ) ) {
$css .= "$menu_item_class{$prefix}current-language,";
$css .= " $menu_item_class{$prefix}current-language a,";
$css .= " $menu_item_class{$prefix}current-language a:visited{";
$css .= $slot->get( 'font_current_normal' ) ? "color:{$slot->get( 'font_current_normal' )};" : '';
$css .= $slot->get( 'background_current_normal' ) ? "background-color:{$slot->get( 'background_current_normal' )};" : '';
$css .= '}';
}
if ( $slot->get( 'font_current_hover' ) || $slot->get( 'background_current_hover' ) ) {
$css .= "$menu_item_class{$prefix}current-language:hover,";
$css .= " $menu_item_class{$prefix}current-language:hover a,";
$css .= " $menu_item_class{$prefix}current-language a:hover{";
$css .= $slot->get( 'font_current_hover' ) ? "color:{$slot->get( 'font_current_hover' )};" : '';
$css .= $slot->get( 'background_current_hover' ) ? "background-color:{$slot->get( 'background_current_hover' )};" : '';
$css .= '}';
}
// Override parent menu styles for hierarchical menus
if ( $slot->get( 'is_hierarchical' ) ) {
if ( $slot->get( 'background_other_normal' ) || $slot->get( 'font_other_normal' ) ) {
$css .= "$menu_item_class{$prefix}current-language $menu_item_class,";
$css .= " $menu_item_class{$prefix}current-language $menu_item_class a,";
$css .= " $menu_item_class{$prefix}current-language $menu_item_class a:visited{";
$css .= $slot->get( 'background_other_normal' ) ? "background-color:{$slot->get( 'background_other_normal' )};" : '';
$css .= $slot->get( 'font_other_normal' ) ? "color:{$slot->get( 'font_other_normal' )};" : '';
$css .= '}';
}
if ( $slot->get( 'font_other_hover' ) || $slot->get( 'background_other_hover' ) ) {
$css .= "$menu_item_class{$prefix}current-language $menu_item_class:hover,";
$css .= " $menu_item_class{$prefix}current-language $menu_item_class:hover a,";
$css .= " $menu_item_class{$prefix}current-language $menu_item_class a:hover {";
$css .= $slot->get( 'font_other_hover' ) ? "color:{$slot->get( 'font_other_hover' )};" : '';
$css .= $slot->get( 'background_other_hover' ) ? "background-color:{$slot->get( 'background_other_hover' )};" : '';
$css .= '}';
}
}
return $css;
}
/**
* @param WPML_LS_Slot $slot
*
* @return string
*/
private function get_slot_color_picker_css_for_widgets_and_statics( $slot ) {
$css = '';
$prefix = '.' . $this->model_build->get_css_prefix();
$wrapper_class = '.' . $this->model_build->get_slot_css_main_class( $slot->group(), $slot->slug() );
if ( $slot->get( 'background_normal' ) ) {
$css .= "$wrapper_class{";
$css .= "background-color:{$slot->get( 'background_normal' )};";
$css .= '}';
}
if ( $slot->get( 'border_normal' ) ) {
$css .= "$wrapper_class, $wrapper_class {$prefix}sub-menu, $wrapper_class a {";
$css .= "border-color:{$slot->get( 'border_normal' )};";
$css .= '}';
}
if ( $slot->get( 'font_other_normal' ) || $slot->get( 'background_other_normal' ) ) {
$css .= "$wrapper_class a, $wrapper_class .wpml-ls-sub-menu a, $wrapper_class .wpml-ls-sub-menu a:link, $wrapper_class li:not(.wpml-ls-current-language) .wpml-ls-link, $wrapper_class li:not(.wpml-ls-current-language) .wpml-ls-link:link {";
$css .= $slot->get( 'font_other_normal' ) ? "color:{$slot->get( 'font_other_normal' )};" : '';
$css .= $slot->get( 'background_other_normal' ) ? "background-color:{$slot->get( 'background_other_normal' )};" : '';
$css .= '}';
}
if ( $slot->get( 'font_other_hover' ) || $slot->get( 'background_other_hover' ) ) {
$css .= "$wrapper_class a, $wrapper_class .wpml-ls-sub-menu a:hover,$wrapper_class .wpml-ls-sub-menu a:focus, $wrapper_class .wpml-ls-sub-menu a:link:hover, $wrapper_class .wpml-ls-sub-menu a:link:focus {";
$css .= $slot->get( 'font_other_hover' ) ? "color:{$slot->get( 'font_other_hover' )};" : '';
$css .= $slot->get( 'background_other_hover' ) ? "background-color:{$slot->get( 'background_other_hover' )};" : '';
$css .= '}';
}
if ( $slot->get( 'font_current_normal' ) || $slot->get( 'background_current_normal' ) ) {
$css .= "$wrapper_class {$prefix}current-language > a {";
$css .= $slot->get( 'font_current_normal' ) ? "color:{$slot->get( 'font_current_normal' )};" : '';
$css .= $slot->get( 'background_current_normal' ) ? "background-color:{$slot->get( 'background_current_normal' )};" : '';
$css .= '}';
}
if ( $slot->get( 'font_current_hover' ) || $slot->get( 'background_current_hover' ) ) {
$css .= "$wrapper_class {$prefix}current-language:hover>a, $wrapper_class {$prefix}current-language>a:focus {";
$css .= $slot->get( 'font_current_hover' ) ? "color:{$slot->get( 'font_current_hover' )};" : '';
$css .= $slot->get( 'background_current_hover' ) ? "background-color:{$slot->get( 'background_current_hover' )};" : '';
$css .= '}';
}
return $css;
}
/**
* @param array $slots
*
* @return string
*/
public function get_slots_inline_styles( $slots ) {
$all_styles = '';
if ( $this->settings->can_load_styles() ) {
foreach ( $slots as $slot ) {
/* @var WPML_LS_Slot $slot */
$css = $this->get_slot_color_picker_css( $slot );
if ( $css ) {
$style_id = 'wpml-ls-inline-styles-' . $slot->group() . '-' . $slot->slug();
$all_styles .= '<style type="text/css" id="' . $style_id . '">' . $css . '</style>' . PHP_EOL;
}
}
}
return $all_styles;
}
/**
* @return string
*/
public function get_additional_style() {
$css = $this->sanitize_css( $this->settings->get_setting( 'additional_css' ) );
if ( $css ) {
$css = '<style type="text/css" id="wpml-ls-inline-styles-additional-css">' . $css . '</style>' . PHP_EOL;
}
return $css;
}
public function wp_enqueue_scripts_action() {
$this->enqueue_inline_styles();
}
private function enqueue_inline_styles() {
if ( $this->settings->can_load_styles() ) {
$active_slots = $this->settings->get_active_slots();
$first_valid_handler = $this->get_first_valid_style_handler( $active_slots );
foreach ( $active_slots as $slot ) {
/* @var WPML_LS_Slot $slot */
$css = $this->get_slot_color_picker_css( $slot );
if ( empty( $css ) ) {
continue;
}
$template = $this->templates->get_template( $slot->template() );
if ( $template->has_styles() ) {
wp_add_inline_style( $template->get_inline_style_handler(), $css );
} elseif ( $first_valid_handler ) {
wp_add_inline_style( $first_valid_handler, $css );
} else {
echo $this->get_raw_inline_style_tag( $slot, $css );
}
}
if ( $first_valid_handler ) {
$additional_css = $this->sanitize_css( $this->settings->get_setting( 'additional_css' ) );
if ( ! empty( $additional_css ) ) {
wp_add_inline_style( $first_valid_handler, $additional_css );
}
} else {
echo $this->get_additional_style();
}
}
}
/**
* @param array $active_slots
*
* @return bool|mixed|null|string
*/
private function get_first_valid_style_handler( $active_slots ) {
$first_handler = null;
foreach ( $active_slots as $slot ) {
/* @var WPML_LS_Slot $slot */
$template = $this->templates->get_template( $slot->template() );
$handler = $template->get_inline_style_handler();
if ( $handler ) {
$first_handler = $handler;
break;
}
}
return $first_handler;
}
public function admin_output() {
if ( $this->settings->can_load_styles() ) {
$active_slots = $this->settings->get_active_slots();
foreach ( $active_slots as $slot ) {
/* @var WPML_LS_Slot $slot */
$css = $this->get_slot_color_picker_css( $slot );
echo $this->get_raw_inline_style_tag( $slot, $css );
}
echo $this->get_additional_style();
}
}
/**
* @param WPML_LS_Slot $slot
* @param string $css
*
* @return string
*/
private function get_raw_inline_style_tag( $slot, $css ) {
$style_id = 'wpml-ls-inline-styles-' . $slot->group() . '-' . $slot->slug();
return '<style type="text/css" id="' . $style_id . '">' . $css . '</style>' . PHP_EOL;
}
/**
* @param string $css
*
* @return string
*/
private function sanitize_css( $css ) {
$css = wp_strip_all_tags( $css );
$css = preg_replace( '/\s+/S', ' ', trim( $css ) );
return $css;
}
}