-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunction.cssembed.php
More file actions
executable file
·51 lines (47 loc) · 1.31 KB
/
function.cssembed.php
File metadata and controls
executable file
·51 lines (47 loc) · 1.31 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
<?php
/*
* Smarty plugin
* ------------------------------------------------------------
* Type: function
* Name: cssembed
* Purpose: Automatically creates a <link> tag or embeds in-page CSS if small enough (performance purposes)
* Author: Pierre-Jean Parra
* Version: 1.0
*
* ------------------------------------------------------------
*/
function smarty_function_cssembed($params, &$smarty)
{
$url = parse_url($params['href']);
$filename = realpath($_SERVER['DOCUMENT_ROOT'] . $url['path']);
if ( ! isset($params['threshold'])) {
$params['threshold'] = 10000;
}
if (isset($params['media'])) {
$mediaQuery = ' media="' . $params['media'] . '"';
}
else {
$mediaQuery = '';
}
if ($params['scoped'] === true) {
$scoped = ' scoped';
}
else {
$scoped = '';
}
// Big file, let's include it
if ($params['force_embed'] !== true && filesize($filename) > $params['threshold']) {
if ( ! empty($url['query'])) {
$urlQuery = '?' . $url['query'];
}
else {
$urlQuery = '';
}
return '<link rel="stylesheet"' . $mediaQuery . ' href="' . $url['path'] . $urlQuery . '">';
}
// Small file, let's embed it directly on the page
else {
return '<style' . $mediaQuery . $scoped . '>' . file_get_contents($filename) . '</style>';
}
}
?>