PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.22
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.22
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.2.22, at minify/class-wp-optimize-minify-print.php

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