|
| 1 | +<?php |
| 2 | +/* |
| 3 | +Plugin Name: Asynchronous Javascript |
| 4 | +Plugin URI: https://github.com/parisholley/wordpress-asynchronous-javascript |
| 5 | +Description: Improve page load performance by asynchronously loading javascript and files using head.js in your wordpress website. |
| 6 | +Version: 1.0 |
| 7 | +Author: Paris Holley |
| 8 | +Author URI: http://www.linkedin.com/in/parisholley |
| 9 | +Author Email: mail@parisholley.com |
| 10 | +License: |
| 11 | +
|
| 12 | + Copyright 2013 Paris Holley (mail@parisholley.com) |
| 13 | +
|
| 14 | + This program is free software; you can redistribute it and/or modify |
| 15 | + it under the terms of the GNU General Public License, version 2, as |
| 16 | + published by the Free Software Foundation. |
| 17 | +
|
| 18 | + This program is distributed in the hope that it will be useful, |
| 19 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | + GNU General Public License for more details. |
| 22 | +
|
| 23 | + You should have received a copy of the GNU General Public License |
| 24 | + along with this program; if not, write to the Free Software |
| 25 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 26 | + |
| 27 | +*/ |
| 28 | +class AsynchronousJS { |
| 29 | + private static $queue = array(); |
| 30 | + private static $depends = array(); |
| 31 | + private static $head_loaded = false; |
| 32 | + |
| 33 | + function init() { |
| 34 | + if(!defined('WP_ADMIN') || !WP_ADMIN){ |
| 35 | + add_action('wp_print_scripts', 'AsynchronousJS::action_prevent_script_output' ); |
| 36 | + add_filter('script_loader_src', 'AsynchronousJS::filter_queue_script', 10, 2 ); |
| 37 | + add_filter('print_footer_scripts', 'AsynchronousJS::filter_headjs' ); |
| 38 | + add_filter('print_head_scripts', 'AsynchronousJS::filter_headjs' ); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Prevent wordpress from outputing scripts to page |
| 44 | + **/ |
| 45 | + function action_prevent_script_output() { |
| 46 | + global $wp_scripts, $concatenate_scripts; |
| 47 | + |
| 48 | + $concatenate_scripts = true; |
| 49 | + $wp_scripts->do_concat = true; |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + /** |
| 54 | + * Wordpress has no ability to hook into script queuing, so this is a work around |
| 55 | + **/ |
| 56 | + function filter_queue_script($src, $handle) { |
| 57 | + self::$queue[$handle] = "{'{$handle}': '$src'}"; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Outputs headjs code in header or footer |
| 62 | + **/ |
| 63 | + function filter_headjs(){ |
| 64 | + if(count(self::$queue) > 0){ |
| 65 | + if(!self::$head_loaded){ |
| 66 | + echo '<script type="text/javascript" src="' . plugins_url( '/js/head.load.min.js', __FILE__ ) . '"></script>'; |
| 67 | + |
| 68 | + self::$head_loaded = true; |
| 69 | + } |
| 70 | + |
| 71 | + echo '<script type="text/javascript">head.js(' . implode(',', self::$queue) . ')</script>'; |
| 72 | + |
| 73 | + self::$queue = array(); |
| 74 | + } |
| 75 | + |
| 76 | + if(count(self::$depends) > 0){ |
| 77 | + foreach(self::$depends as $handle => $depend){ |
| 78 | + if(is_array($depend['deps'])){ |
| 79 | + echo '<script type="text/javascript">head.ready("' . implode(',', $depend['deps']) . '", function(){head.js({"' . $handle . '": "' . $depend['src'] . '"})})</script>'; |
| 80 | + }elseif(is_string($depend['deps'])){ |
| 81 | + echo '<script type="text/javascript">head.ready("' . $depend['deps'] . '", function(){head.js({"' . $handle . '": "' . $depend['src'] . '"})})</script>'; |
| 82 | + }else{ |
| 83 | + echo '<script type="text/javascript">head.js({"' . $handle . '": "' . $depend['src'] . '"});</script>'; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + self::$depends = array(); |
| 88 | + } |
| 89 | + |
| 90 | + return false; // prevent printing of javascript |
| 91 | + } |
| 92 | + |
| 93 | + function wp_enqueue_async_script($handle, $src, $deps){ |
| 94 | + self::$depends[$handle] = array( |
| 95 | + 'src' => $src, |
| 96 | + 'deps' => $deps |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +AsynchronousJS::init(); |
| 102 | +?> |
0 commit comments