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