| 1 | <?php |
||
| 2 | /* |
||
| 3 | * Plugin Name: Snapmunk Optimizer |
||
| 4 | * Plugin URI: https://github.com/pothi/ |
||
| 5 | * Version: 0.1 |
||
| 6 | * Description: Automatically optimize CSS and JS (among other things) |
||
| 7 | * Author: Pothi Kalimuthu |
||
| 8 | * Author URI: http://pothi.info |
||
| 9 | * License: GPL |
||
| 10 | */ |
||
| 11 | |||
| 12 | if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
||
| 13 | |||
| 14 | // directly from https://wordpress.org/plugins/disable-emojis/ |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Disable the emoji's |
||
| 18 | */ |
||
| 19 | function tiny_disable_emojis() { |
||
| 20 | remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); |
||
| 21 | remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); |
||
| 22 | remove_action( 'wp_print_styles', 'print_emoji_styles' ); |
||
| 23 | remove_action( 'admin_print_styles', 'print_emoji_styles' ); |
||
| 24 | remove_filter( 'the_content_feed', 'wp_staticize_emoji' ); |
||
| 25 | remove_filter( 'comment_text_rss', 'wp_staticize_emoji' ); |
||
| 26 | remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' ); |
||
| 27 | add_filter( 'tiny_mce_plugins', 'tiny_disable_emojis_tinymce' ); |
||
| 28 | } |
||
| 29 | // add_action( 'init', 'tiny_disable_emojis' ); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Filter function used to remove the tinymce emoji plugin. |
||
| 33 | * |
||
| 34 | * @param array $plugins |
||
| 35 | * @return array Difference betwen the two arrays |
||
| 36 | */ |
||
| 37 | function tiny_disable_emojis_tinymce( $plugins ) { |
||
| 38 | if ( is_array( $plugins ) ) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 39 | return array_diff( $plugins, array( 'wpemoji' ) ); |
||
| 40 | } else { |
||
| 41 | return array(); |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | /* loads loadCSS and script.js */ |
||
| 46 | function tiny_hook_header_javascript () { |
||
| 47 | $code = <<<'EOD' |
||
| 48 | <script type="text/javascript"> |
||
| 49 | document.documentElement.className = 'js'; |
||
| 50 | |||
| 51 | // https://github.com/filamentgroup/loadCSS |
||
| 52 | function loadCSS( href, before, media, callback ){ |
||
| 53 | "use strict"; |
||
| 54 | var ss = window.document.createElement( "link" ); |
||
| 55 | var ref = before || window.document.getElementsByTagName( "script" )[ 0 ]; |
||
| 56 | var sheets = window.document.styleSheets; |
||
| 57 | ss.rel = "stylesheet"; |
||
| 58 | ss.href = href; |
||
| 59 | ss.media = "only x"; |
||
| 60 | if( callback ) { |
||
| 61 | ss.onload = callback; |
||
| 62 | } |
||
| 63 | ref.parentNode.insertBefore( ss, ref ); |
||
| 64 | ss.onloadcssdefined = function( cb ){ |
||
| 65 | var defined; |
||
| 66 | for( var i = 0; i < sheets.length; i++ ){ |
||
| 67 | if( sheets[ i ].href && sheets[ i ].href.indexOf( href ) > -1 ){ |
||
| 68 | defined = true; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | if( defined ){ |
||
| 72 | cb(); |
||
| 73 | } else { |
||
| 74 | setTimeout(function() { |
||
| 75 | ss.onloadcssdefined( cb ); |
||
| 76 | }); |
||
| 77 | } |
||
| 78 | }; |
||
| 79 | ss.onloadcssdefined(function() { |
||
| 80 | ss.media = media || "all"; |
||
| 81 | }); |
||
| 82 | return ss; |
||
| 83 | } |
||
| 84 | |||
| 85 | // script.js |
||
| 86 | (function (name, definition) { |
||
| 87 | if (typeof module != 'undefined' && module.exports) module.exports = definition() |
||
| 88 | else if (typeof define == 'function' && define.amd) define(definition) |
||
| 89 | else this[name] = definition() |
||
| 90 | })('$script', function () { |
||
| 91 | var doc = document |
||
| 92 | , head = doc.getElementsByTagName('head')[0] |
||
| 93 | , s = 'string' |
||
| 94 | , f = false |
||
| 95 | , push = 'push' |
||
| 96 | , readyState = 'readyState' |
||
| 97 | , onreadystatechange = 'onreadystatechange' |
||
| 98 | , list = {} |
||
| 99 | , ids = {} |
||
| 100 | , delay = {} |
||
| 101 | , scripts = {} |
||
| 102 | , scriptpath |
||
| 103 | , urlArgs |
||
| 104 | |||
| 105 | function every(ar, fn) { |
||
| 106 | for (var i = 0, j = ar.length; i < j; ++i) if (!fn(ar[i])) return f |
||
| 107 | return 1 |
||
| 108 | } |
||
| 109 | function each(ar, fn) { |
||
| 110 | every(ar, function (el) { |
||
| 111 | return !fn(el) |
||
| 112 | }) |
||
| 113 | } |
||
| 114 | |||
| 115 | function $script(paths, idOrDone, optDone) { |
||
| 116 | paths = paths[push] ? paths : [paths] |
||
| 117 | var idOrDoneIsDone = idOrDone && idOrDone.call |
||
| 118 | , done = idOrDoneIsDone ? idOrDone : optDone |
||
| 119 | , id = idOrDoneIsDone ? paths.join('') : idOrDone |
||
| 120 | , queue = paths.length |
||
| 121 | function loopFn(item) { |
||
| 122 | return item.call ? item() : list[item] |
||
| 123 | } |
||
| 124 | function callback() { |
||
| 125 | if (!--queue) { |
||
| 126 | list[id] = 1 |
||
| 127 | done && done() |
||
| 128 | for (var dset in delay) { |
||
| 129 | every(dset.split('|'), loopFn) && !each(delay[dset], loopFn) && (delay[dset] = []) |
||
| 130 | } |
||
| 131 | } |
||
| 132 | } |
||
| 133 | setTimeout(function () { |
||
| 134 | each(paths, function loading(path, force) { |
||
| 135 | if (path === null) return callback() |
||
| 136 | path = !force && path.indexOf('.js') === -1 && !/^https?:\/\//.test(path) && scriptpath ? scriptpath + path + '.js' : path |
||
| 137 | if (scripts[path]) { |
||
| 138 | if (id) ids[id] = 1 |
||
| 139 | return (scripts[path] == 2) ? callback() : setTimeout(function () { loading(path, true) }, 0) |
||
| 140 | } |
||
| 141 | |||
| 142 | scripts[path] = 1 |
||
| 143 | if (id) ids[id] = 1 |
||
| 144 | create(path, callback) |
||
| 145 | }) |
||
| 146 | }, 0) |
||
| 147 | return $script |
||
| 148 | } |
||
| 149 | |||
| 150 | function create(path, fn) { |
||
| 151 | var el = doc.createElement('script'), loaded |
||
| 152 | el.onload = el.onerror = el[onreadystatechange] = function () { |
||
| 153 | if ((el[readyState] && !(/^c|loade/.test(el[readyState]))) || loaded) return; |
||
| 154 | el.onload = el[onreadystatechange] = null |
||
| 155 | loaded = 1 |
||
| 156 | scripts[path] = 2 |
||
| 157 | fn() |
||
| 158 | } |
||
| 159 | el.async = 1 |
||
| 160 | el.src = urlArgs ? path + (path.indexOf('?') === -1 ? '?' : '&') + urlArgs : path; |
||
| 161 | head.insertBefore(el, head.lastChild) |
||
| 162 | } |
||
| 163 | |||
| 164 | $script.get = create |
||
| 165 | |||
| 166 | $script.order = function (scripts, id, done) { |
||
| 167 | (function callback(s) { |
||
| 168 | s = scripts.shift() |
||
| 169 | !scripts.length ? $script(s, id, done) : $script(s, callback) |
||
| 170 | }()) |
||
| 171 | } |
||
| 172 | |||
| 173 | $script.path = function (p) { |
||
| 174 | scriptpath = p |
||
| 175 | } |
||
| 176 | $script.urlArgs = function (str) { |
||
| 177 | urlArgs = str; |
||
| 178 | } |
||
| 179 | $script.ready = function (deps, ready, req) { |
||
| 180 | deps = deps[push] ? deps : [deps] |
||
| 181 | var missing = []; |
||
| 182 | !each(deps, function (dep) { |
||
| 183 | list[dep] || missing[push](dep); |
||
| 184 | }) && every(deps, function (dep) {return list[dep]}) ? |
||
| 185 | ready() : !function (key) { |
||
| 186 | delay[key] = delay[key] || [] |
||
| 187 | delay[key][push](ready) |
||
| 188 | req && req(missing) |
||
| 189 | }(deps.join('|')) |
||
| 190 | return $script |
||
| 191 | } |
||
| 192 | |||
| 193 | $script.done = function (idOrDone) { |
||
| 194 | $script([null], idOrDone) |
||
| 195 | } |
||
| 196 | |||
| 197 | return $script |
||
| 198 | }); |
||
| 199 | </script> |
||
| 200 | |||
| 201 | <script async> |
||
| 202 | // loadCSS( 'fontURL' ); |
||
| 203 | // loadCSS( '/wp-content/themes/theme_name/style.css' ); |
||
| 204 | </script> |
||
| 205 | <noscript> |
||
| 206 | <!-- <link rel='stylesheet' id='divi-fonts-css' href='fontURL' type='text/css' media='all' /> --> |
||
| 207 | <!-- <link rel='stylesheet' id='divi-fonts-css' href='themestyleURL' type='text/css' media='all' /> --> |
||
| 208 | </noscript> |
||
| 209 | EOD; |
||
| 210 | |||
| 211 | echo $code; |
||
| 212 | } |
||
| 213 | add_action( 'wp_head', 'tiny_hook_header_javascript', 999 ); |
||
| 214 | |||
| 215 | /* loads loadCSS and script.js */ |
||
| 216 | function tiny_hook_critical_css () { |
||
| 217 | $code = <<<'EOD' |
||
| 218 | <style> |
||
| 219 | /* Critical CSS - uncompressed CSS can be found at critical.css in the plugin's folder */ |
||
| 220 | EOD; |
||
| 221 | |||
| 222 | echo $code; |
||
| 223 | } |
||
| 224 | add_action( 'wp_head', 'tiny_hook_critical_css', 0 ); |
||
| 225 | |||
| 226 | /* load javascript at the footer */ |
||
| 227 | function tiny_hook_footer_javascript () { |
||
| 228 | $code = <<<'EOD' |
||
| 229 | <script type="text/javascript"> |
||
| 230 | |||
| 231 | /* <![CDATA[ */ |
||
| 232 | var somevar = 'somevalue'; |
||
| 233 | /* ]]> */ |
||
| 234 | |||
| 235 | // let's load jquery from Microsoft CDN |
||
| 236 | $script( '//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.2.min.js', 'jquery' ); |
||
| 237 | |||
| 238 | $script.ready('jquery', function() { |
||
| 239 | $script( '//ajax.aspnetcdn.com/ajax/jquery.migrate/jquery-migrate-1.2.1.min.js' ); |
||
| 240 | try{jQuery.noConflict();}catch(e){}; |
||
| 241 | $script( '/wp-content/themes/theme_name/js/somescript.js' ); |
||
| 242 | }); |
||
| 243 | </script> |
||
| 244 | EOD; |
||
| 245 | |||
| 246 | echo $code; |
||
| 247 | } |
||
| 248 | add_action( 'wp_footer', 'tiny_hook_footer_javascript', 999 ); |
||
| 249 | |||
| 250 | // de_queue script that can be loaded alternatively |
||
| 251 | add_action( 'wp_enqueue_scripts', function() { |
||
| 252 | // Uncomment the following, if granvillephotobooth.com start using comments |
||
| 253 | // if( is_home() || is_front_page() ) |
||
| 254 | // wp_dequeue_script( 'comment-reply' ); |
||
| 255 | |||
| 256 | // use this with care!!! |
||
| 257 | // if( !is_admin() ) |
||
| 258 | // wp_deregister_script( 'jquery' ); |
||
| 259 | |||
| 260 | // sample: let's defer theme scripts inc lightbox script |
||
| 261 | wp_dequeue_script( 'js_handle' ); |
||
| 262 | }, 20); |
||
| 263 | |||
| 264 | // de_register the styles that load asynchronously |
||
| 265 | add_action( 'wp_enqueue_scripts', function() { |
||
| 266 | // theme styles |
||
| 267 | wp_deregister_style( 'style_handle' ); |
||
| 268 | }, 20 ); |
||
| 269 | |||
| 270 | // optimizi home page image/s |
||
| 271 | // add_filter('the_content', 'tiny_jetpack_images_optimization'); |
||
| 272 | function tiny_jetpack_images_optimization() { |
||
| 273 | if( wp_is_mobile() ) { |
||
| 274 | // let's do something - resize images |
||
| 275 | } |
||
| 276 | // return $content; |
||
| 277 | } |
||
| 278 | |||
| 279 | // Add vary: user-agent header |
||
| 280 | add_filter('wp_headers', function ($headers) { |
||
| 281 | $headers['Vary'] = 'User-Agent'; |
||
| 282 | return $headers; |
||
| 283 | }); |
||
| 284 |