PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.6.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.6.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / minify / class-wp-optimize-minify-print.php

class-wp-optimize-minify-print.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.6.0, at minify/class-wp-optimize-minify-print.php

194 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) die('No direct access allowed');
3
4 if (!class_exists('WP_Optimize_Minify_Functions')) {
5 include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-functions.php';
6 }
7
8 class WP_Optimize_Minify_Print {
9
10 /**
11 * Load the script using loadAsync JavaScript
12 *
13 * @param string $href
14 * @param boolean $print
15 * @return string|void
16 */
17 public static function async_script($href, $print = true) {
18 $wpo_minify_options = wp_optimize_minify_config()->get();
19 $tag = '<script>' . "\n";
20 $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
21 $tag .= 'var wpo_server_info_js = ' . json_encode(array("user_agent" => $user_agent)) . "\n";
22
23 $exclude_js_from_page_speed_tools = $wpo_minify_options['exclude_js_from_page_speed_tools'];
24 $enable_defer_js = $wpo_minify_options['enable_defer_js'];
25 $is_conditional_loading = $exclude_js_from_page_speed_tools && 'all' !== $enable_defer_js;
26 if ($is_conditional_loading) {
27 $tag .= 'if (!(navigator.userAgent + " " + wpo_server_info_js.user_agent).match(/' . implode("|", $wpo_minify_options['ualist']) .'/i)) {' . "\n";
28 }
29 $tag .= " loadAsync('$href', null);" . "\n";
30 if ($is_conditional_loading) {
31 $tag .= '}';
32 }
33 $tag .= '</script>' . "\n";
34
35 if ($print) {
36 echo $tag;
37 } else {
38 return $tag;
39 }
40 }
41
42 /**
43 * Print a style that will be loaded async using 'preload'
44 *
45 * @param string $href
46 * @param string $media
47 * @return void
48 */
49 public static function async_style($href, $media = 'all') {
50 echo '<link rel="preload" href="'.$href.'" as="style" media="'.$media.'" onload="this.onload=null;this.rel=\'stylesheet\'">' . "\n";
51 // fix for firefox not supporting preload
52 echo '<link rel="stylesheet" href="'.$href.'" media="'.$media.'">' . "\n";
53 echo '<noscript><link rel="stylesheet" href="'.$href.'" media="'.$media.'"></noscript>' . "\n";
54 echo '<!--[if IE]><link rel="stylesheet" href="'.$href.'" media="'.$media.'"><![endif]-->' . "\n";
55 }
56
57 /**
58 * Print a style that will be loaded by JS
59 *
60 * @param string $href
61 * @return void
62 */
63 public static function exclude_style($href) {
64 $wpo_minify_options = wp_optimize_minify_config()->get();
65 // make a stylesheet, hide from PageSpeedIndex
66 $cssguid = 'wpo_min'.hash('adler32', $href);
67 $tag = '<script>' . "\n";
68 $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
69 $tag .= 'var wpo_server_info_css = ' . json_encode(array("user_agent" => $user_agent)) . "\n";
70
71 $exclude_css_from_page_speed_tools = $wpo_minify_options['exclude_css_from_page_speed_tools'];
72 if ($exclude_css_from_page_speed_tools) {
73 $tag .= 'if (!(navigator.userAgent + " " + wpo_server_info_css.user_agent).match(/'.implode('|', $wpo_minify_options['ualist']).'/i)){' . "\n";
74 }
75 $tag .= ' var '.$cssguid.'=document.createElement("link");'.$cssguid.'.rel="stylesheet",'.$cssguid.'.type="text/css",'.$cssguid.'.media="async",'.$cssguid.'.href="'.$href.'",'.$cssguid.'.onload=function() {'.$cssguid.'.media="all"},document.getElementsByTagName("head")[0].appendChild('.$cssguid.');' . "\n";
76 if ($exclude_css_from_page_speed_tools) {
77 $tag .= '}';
78 }
79 $tag .= '</script>' . "\n";
80 echo $tag;
81 }
82
83 /**
84 * Inline a single style
85 *
86 * @param string $handle
87 * @param string $href
88 * @return boolean
89 */
90 public static function inline_style($handle, $href) {
91 $wpo_minify_options = wp_optimize_minify_config()->get();
92
93 // font awesome processing, inline
94 // download, minify, cache
95 $tkey = 'css-'.hash('adler32', $href).'.css';
96 $json = WP_Optimize_Minify_Cache_Functions::get_transient($tkey);
97 if (false === $json) {
98 $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $wpo_minify_options['enable_css_minification'], 'css', $handle);
99 if ($wpo_minify_options['debug']) {
100 echo "<!-- wpo_min DEBUG: Uncached file processing now for $href -->" . "\n";
101 }
102 WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json);
103 }
104
105 // decode
106 $res = json_decode($json, true);
107
108 // add font-display
109 // https://developers.google.com/web/updates/2016/02/font-display
110 $res['code'] = str_ireplace('font-style:normal;', 'font-display:block;font-style:normal;', $res['code']);
111
112 // inline css or fail
113 if (false != $res['status']) {
114 echo '<style type="text/css" media="all">' . "\n";
115 echo $res['code'] . "\n";
116 echo '</style>' . "\n";
117 return true;
118 } else {
119 return false;
120 }
121 }
122
123 /**
124 * Print a normal style tag pointing the href
125 *
126 * @param string $href
127 * @return void
128 */
129 public static function style($href) {
130 echo '<link rel="stylesheet" href="'.$href.'" media="all">' . "\n";
131 }
132
133 /**
134 * Write header
135 *
136 * @param string $file
137 * @param string $headers
138 * @return void
139 */
140 public static function write_header($file, $headers) {
141 file_put_contents($file, $headers);
142 WP_Optimize_Minify_Cache_Functions::fix_permission_bits($file);
143 }
144
145 /**
146 * Write CSS
147 *
148 * @param string $file
149 * @param string $code
150 * @param string $log
151 * @return void
152 */
153 public static function write_combined_asset($file, $code, $log) {
154 file_put_contents($file.'.json', json_encode($log));
155 file_put_contents($file, $code);
156 // permissions
157 WP_Optimize_Minify_Cache_Functions::fix_permission_bits($file.'.json');
158 WP_Optimize_Minify_Cache_Functions::fix_permission_bits($file);
159
160 if (function_exists('gzencode')) {
161 file_put_contents($file.'.gz', gzencode(file_get_contents($file), 9));
162 WP_Optimize_Minify_Cache_Functions::fix_permission_bits($file.'.gz');
163 }
164
165 // brotli static support
166 if (function_exists('brotli_compress')) {
167 file_put_contents($file.'.br', brotli_compress(file_get_contents($file), 11));
168 WP_Optimize_Minify_Cache_Functions::fix_permission_bits($file.'.br');
169 }
170 }
171 /**
172 * Load async scripts with callback
173 *
174 * @return void
175 */
176 public static function add_load_async() {
177 $min_or_not_internal = WP_Optimize()->get_min_or_not_internal_string();
178 $contents = file_get_contents(trailingslashit(WPO_PLUGIN_MAIN_PATH) . "js/loadAsync$min_or_not_internal.js");
179 echo "<script>$contents</script>\n";
180 }
181
182 /**
183 * Defer CSS globally from the header (order matters)
184 * Dev: https://www.filamentgroup.com/lab/async-css.html
185 *
186 * @return void
187 */
188 public static function add_load_css() {
189 $min_or_not_internal = WP_Optimize()->get_min_or_not_internal_string();
190 $contents = file_get_contents(trailingslashit(WPO_PLUGIN_MAIN_PATH) . "js/loadCSS$min_or_not_internal.js");
191 echo "<script>$contents</script>" . "\n";
192 }
193 }
194