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

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