/
css.php
222 lines (203 loc) · 4.54 KB
/
css.php
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
<?php
/**
* Bootswatch CSS.
*
* @package Bootswatch
*/
/**
* Add body class `fixed-navbar` if navbar is fixed.
*/
add_action(
'body_class',
function( $body_classes ) {
if ( bootswatch_has( 'fixed_navbar' ) ) {
return array_merge( $body_classes, array( 'fixed-navbar' ) );
}
return $body_classes;
}
);
/**
* Add inline CSS in header.
*/
add_action(
'wp_head',
function() {
echo '<style>' . bootswatch_generate_inline_css( bootswatch_get_option( 'theme', 'bootstrap' ) ) . '</style>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
);
/**
* Generates inline CSS that corrects display, see comments with LESS code.
*
* If `$theme` is empty, the function will use bootstrap.
*
* @param String $theme The bootswatch theme.
* @return String The CSS code.
*/
function bootswatch_generate_inline_css( $theme = 'bootstrap' ) {
if ( ! class_exists( 'Less_Parser' ) ) {
return bootswatch_bootstrap_inline_css();
}
/**
* Prepare replaceemts.
*/
$options = get_theme_mod( 'bootswatch' );
$replacements = array(
'{{custom_header_percentage_size}}' => '100vh',
);
if ( isset( $options['custom_header_percentage_size'] ) ) {
$size = intval( $options['custom_header_percentage_size'] );
if ( $size > 0 ) {
$replacements['{{custom_header_percentage_size}}'] = $size . 'vh';
}
}
/**
* Prepare LESS parser.
*/
switch ( $theme ) {
case 'bootstrap':
$variables_path = bootswatch_get_bootstrap_part_path( 'variables' );
break;
default:
$variables_path = bootswatch_get_theme_part_path( $theme, 'variables' );
break;
}
$less_parser = new Less_Parser(
array(
'compress' => true,
)
);
$less_parser->parseFile( $variables_path );
/**
* The styles.
*/
$less = '
// Use fixed position for admin bar if navbar position is fixed.
body.fixed-navbar #wpadminbar {
position: fixed;
}
// Fix overlapping with Bootstrap fixed navbar.
body.fixed-navbar {
padding-top: (@navbar-height + @navbar-margin-bottom);
}
// Fix overlapping with WordPress admin bar.
body.admin-bar .navbar-fixed-top{
top: 32px;
@media screen and (max-width:782px) {
top: 46px;
}
}
// Custom header defaults.
.custom-header {
overflow: hidden;
padding-left: 0;
padding-right: 0;
position: relative;
top: -@navbar-margin-bottom;
width: 100%;
iframe,
img,
video {
object-fit: cover;
width: 100%;
}
&,
iframe,
img,
video {
height: calc(~"{{custom_header_percentage_size}} -" @navbar-height);
}
}
// Custom header when #wpadminbar is visible.
body.admin-bar .custom-header {
&,
iframe,
img,
video {
height: calc(~"{{custom_header_percentage_size}} - 32px" - @navbar-height);
}
@media screen and (max-width:782px) {
&,
iframe,
img,
video {
height: calc(~"{{custom_header_percentage_size}} - 46px" - @navbar-height);
}
}
}
// Fix screen reader text (14px).
.screen-reader-text:focus {
font-size: 1.4rem;
}
';
/**
* Apply replacements.
*/
$less = str_replace( array_keys( $replacements ), array_values( $replacements ), $less );
/**
* Parse Less and return CSS.
*/
return $less_parser->parse( $less )->getCss();
}
/**
* Return Bootstrap inline CSS code.
*
* This CSS code can be obtained by running `var_dump(bootswatch_generate_inline_css()`.
*
* @return String CSS code.
*/
function bootswatch_bootstrap_inline_css() {
$css = '
body.fixed-navbar #wpadminbar {
position: fixed;
}
body.fixed-navbar {
padding-top: 70px;
}
body.admin-bar .navbar-fixed-top {
top: 32px;
}
@media screen and (max-width: 782px) {
body.admin-bar .navbar-fixed-top {
top: 46px;
}
}
.custom-header {
overflow: hidden;
padding-left: 0;
padding-right: 0;
position: relative;
top: -20px;
width: 100%;
}
.custom-header iframe,
.custom-header img,
.custom-header video {
object-fit: cover;
width: 100%;
}
.custom-header,
.custom-header iframe,
.custom-header img,
.custom-header video {
height: calc(100vh - 50px);
}
body.admin-bar .custom-header,
body.admin-bar .custom-header iframe,
body.admin-bar .custom-header img,
body.admin-bar .custom-header video {
height: calc(100vh - 32px - 50px);
}
@media screen and (max-width: 782px) {
body.admin-bar .custom-header,
body.admin-bar .custom-header iframe,
body.admin-bar .custom-header img,
body.admin-bar .custom-header video {
height: calc(100vh - 46px - 50px);
}
}
.screen-reader-text:focus {
font-size: 1.4rem;
}
';
return $css;
}