-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass-wp-instantclick.php
121 lines (91 loc) · 3.9 KB
/
class-wp-instantclick.php
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
// Exit if accessed directly
if ( !defined ( 'ABSPATH' ) )
exit;
if ( !class_exists( 'WP_InstantClick' ) ):
class WP_InstantClick
{
const VERSION = '1.0.1';
const SCRIPT_HANDLE = 'instantclick';
private static $no_instant = array( self::SCRIPT_HANDLE );
private static $_enabled = false;
private static $_preload_method = '';
public static function enable() {
if ( self::is_enabled() )
return;
self::_add_action_or_do( 'init', array( __CLASS__, '_register_scripts' ), 1 );
if ( !is_admin() )
self::_add_action_or_do( 'wp_enqueue_scripts', array( __CLASS__, '_enqueue_scripts' ), 1 );
self::$_enabled = true;
}
private static function _add_action_or_do( $hook, $callback, $priority = 10 ) {
if ( did_action( $hook ) || doing_action( $hook ) )
call_user_func( $callback );
else
add_action( $hook, $callback, $priority );
}
public static function is_enabled() {
return (bool) self::$_enabled;
}
public static function no_instant( $handle ) {
if ( !in_array( $handle, self::$no_instant ) )
self::$no_instant[] = $handle;
}
public static function preload_on_mousedown() {
self::$_preload_method = "'mousedown'";
}
public static function preload_on_hover( $delay ) {
if ( is_numeric( $delay ) && $delay > 0 )
self::$_preload_method = (int) $delay;
else
self::$_preload_method = 0;
}
//
// HOOKABLES
//
public static function _register_scripts() {
$min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
wp_register_script( self::SCRIPT_HANDLE, self::get_url() . "js/instantclick$min.js", array(), '3.0.1', true );
}
/**
* Class should be able to be instantiated from a theme or plugin,
* so we need to find out the url to the current folder in a
* roundabout way.
*/
public static function get_url() {
// get and normalize framework dirname
$dirname = str_replace( '\\' ,'/', dirname( __FILE__ ) ); // standardize slash
$dirname = preg_replace( '|/+|', '/', $dirname ); // normalize duplicate slash
// get and normalize WP content directory
$wp_content_dir = str_replace( '\\', '/', WP_CONTENT_DIR ); // standardize slash
// build relative url
$relative_url = str_replace( $wp_content_dir, "", $dirname );
// finally base url
return trailingslashit( content_url() . $relative_url );
}
public static function _enqueue_scripts() {
wp_enqueue_script( self::SCRIPT_HANDLE );
add_filter( 'script_loader_src', array( __CLASS__, '_script_loader' ), 10, 2 );
}
public static function _script_loader( $src, $handle ) {
if ( in_array( $handle, self::$no_instant ) ) {
?><script src='<?php echo $src ?>' data-no-instant></script>
<?php
$src = false;
}
if ( self::SCRIPT_HANDLE === $handle ) {
add_action( 'wp_footer', array( __CLASS__, '_output_script_init' ), 9999 );
}
return $src;
}
public static function _output_script_init() {
?>
<script data-no-instant>
<?php do_action( 'instantclick_before_init' ); ?>
InstantClick.init(<?php echo self::$_preload_method ?>);
<?php do_action( 'instantclick_after_init' ); ?>
</script>
<?php
}
}
endif;