Subscriber.php
2.96 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
namespace WP_Rocket\Engine\Optimization\DelayJS;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Event_Management\Subscriber_Interface;
class Subscriber implements Subscriber_Interface {
/**
* HTML instance.
*
* @since 3.7
*
* @var HTML
*/
private $html;
/**
* WP_Filesystem_Direct instance.
*
* @since 3.7
*
* @var \WP_Filesystem_Direct
*/
private $filesystem;
/**
* Options Data instance
*
* @var Options_Data
*/
private $options;
/**
* Script enqueued status.
*
* @since 3.7
*
* @var bool
*/
private $is_enqueued = false;
/**
* Subscriber constructor.
*
* @param HTML $html HTML Instance.
* @param \WP_Filesystem_Direct $filesystem The Filesystem object.
* @param Options_Data $options Options data instance.
*/
public function __construct( HTML $html, $filesystem, Options_Data $options ) {
$this->html = $html;
$this->filesystem = $filesystem;
$this->options = $options;
}
/**
* Return an array of events that this subscriber wants to listen to.
*
* @since 3.7
*
* @return array
*/
public static function get_subscribed_events() {
return [
'rocket_buffer' => [
[ 'delay_js', 26 ],
[ 'add_delay_js_script', 26 ],
],
'pre_get_rocket_option_minify_concatenate_js' => 'maybe_disable_option',
];
}
/**
* Modifies scripts HTML to apply delay JS attribute
*
* @since 3.7
*
* @param string $buffer_html Html for the page.
*
* @return string
*/
public function delay_js( $buffer_html ) {
return $this->html->delay_js( $buffer_html );
}
/**
* Displays the inline script to the head when the option is enabled.
*
* @since 3.9.4 Move meta charset to head.
* @since 3.9 Hooked on rocket_buffer, display the script right after <head>
* @since 3.7
*
* @param string $html HTML content.
*
* @return string
*/
public function add_delay_js_script( $html ): string {
if ( ! $this->html->is_allowed() ) {
return $html;
}
$pattern = '/<head[^>]*>/i';
$lazyload_script = $this->filesystem->get_contents( rocket_get_constant( 'WP_ROCKET_PATH' ) . 'assets/js/lazyload-scripts.min.js' );
$replaced_html = $html;
if ( false !== $lazyload_script ) {
$replaced_html = preg_replace( $pattern, "$0<script>{$lazyload_script}</script>", $replaced_html, 1 );
if ( empty( $replaced_html ) ) {
return $html;
}
}
$replaced_html = preg_replace( $pattern, '$0<script>' . $this->html->get_ie_fallback() . '</script>', $replaced_html, 1 );
if ( empty( $replaced_html ) ) {
return $html;
}
return $this->html->move_meta_charset_to_head( $replaced_html );
}
/**
* Disables defer JS if delay JS is enabled
*
* @since 3.9
*
* @param null $value Original value. Should be always null.
*
* @return null|false
*/
public function maybe_disable_option( $value ) {
if ( $this->html->is_allowed() ) {
return false;
}
return $value;
}
}