-
Notifications
You must be signed in to change notification settings - Fork 210
/
AbstractMinifySubscriber.php
133 lines (114 loc) · 3.04 KB
/
AbstractMinifySubscriber.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
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace WP_Rocket\Engine\Optimization\Minify;
use WP_Filesystem_Direct;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Event_Management\Subscriber_Interface;
/**
* Parent class for minify subscribers.
*/
abstract class AbstractMinifySubscriber implements Subscriber_Interface {
/**
* Plugin options.
*
* @var Options_Data
*/
protected $options;
/**
* Processor instance.
*
* @var ProcessorInterface
*/
protected $processor;
/**
* Filesystem instance
*
* @var WP_Filesystem_Direct
*/
protected $filesystem;
/**
* Creates an instance of inheriting class.
*
* @since 3.1
*
* @param Options_Data $options Plugin options.
* @param WP_Filesystem_Direct $filesystem Filesystem instance.
*/
public function __construct( Options_Data $options, $filesystem ) {
$this->options = $options;
$this->filesystem = $filesystem;
}
/**
* Sets the type of processor to use
*
* @since 3.1
*
* @param ProcessorInterface $processor Processor instance.
* @return void
*/
protected function set_processor_type( ProcessorInterface $processor ) {
$this->processor = $processor;
}
/**
* Processes the HTML to perform an optimization and return the new content
*
* @since 3.1
*
* @param string $html HTML content.
* @return string
*/
abstract public function process( $html );
/**
* Checks if files can be optimized
*
* @since 3.1
*/
abstract protected function is_allowed();
/**
* Fix issue with SSL and minification
*
* @since 2.3
*
* @param string $url An url to filter to set the scheme to https if needed.
* @return string
*/
public function fix_ssl_minify( $url ) {
if ( ! is_ssl() ) {
return $url;
}
if ( 0 === strpos( $url, 'https://' ) ) {
return $url;
}
// This filter is documented in inc/Engine/Admin/Settings/Settings.php.
if ( in_array( wp_parse_url( $url, PHP_URL_HOST ), apply_filters( 'rocket_cdn_hosts', [], ( $this->get_zones() ) ), true ) ) {
return $url;
}
return str_replace( 'http://', 'https://', $url );
}
/**
* Compatibility with multilingual plugins & multidomain configuration
*
* @since 2.6.13 Regression Fix: Apply CDN on minified CSS and JS files by checking the CNAME host
* @since 2.6.8
*
* @param string $url Minified file URL.
* @return string Updated minified file URL
*/
public function i18n_multidomain_url( $url ) {
if ( ! rocket_has_i18n() ) {
return $url;
}
$url_host = wp_parse_url( $url, PHP_URL_HOST );
if ( isset( $_SERVER['HTTP_HOST'] ) && $url_host === $_SERVER['HTTP_HOST'] ) {
return $url;
}
if ( ! in_array( $_SERVER['HTTP_HOST'], get_rocket_i18n_host(), true ) ) {
return $url;
}
// This filter is documented in inc/Engine/Admin/Settings/Settings.php.
$cdn_hosts = apply_filters( 'rocket_cdn_hosts', [], ( $this->get_zones() ) );
if ( in_array( $url_host, $cdn_hosts, true ) ) {
return $url;
}
return str_replace( $url_host, sanitize_text_field( $_SERVER['HTTP_HOST'] ), $url ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash
}
}