| 1 |
<?php |
| 2 |
/** |
| 3 |
* Helpers for file optimizer |
| 4 |
* |
| 5 |
* @package PoweredCache\Optimizer |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace PoweredCache\Optimizer; |
| 9 |
|
| 10 |
// phpcs:disable WordPress.WhiteSpace.PrecisionAlignment.Found |
| 11 |
|
| 12 |
/** |
| 13 |
* Class Helper |
| 14 |
*/ |
| 15 |
class Helper { |
| 16 |
|
| 17 |
/** |
| 18 |
* Check if given url is an internal URL |
| 19 |
* |
| 20 |
* @param string $test_url URL against website |
| 21 |
* @param string $site_url Site URL |
| 22 |
* |
| 23 |
* @return bool |
| 24 |
*/ |
| 25 |
public static function is_internal_url( $test_url, $site_url ) { |
| 26 |
$test_url_parsed = wp_parse_url( $test_url ); |
| 27 |
$site_url_parsed = wp_parse_url( $site_url ); |
| 28 |
|
| 29 |
if ( isset( $test_url_parsed['host'] ) |
| 30 |
&& $test_url_parsed['host'] !== $site_url_parsed['host'] ) { |
| 31 |
return false; |
| 32 |
} |
| 33 |
|
| 34 |
if ( isset( $site_url_parsed['path'] ) |
| 35 |
&& 0 !== strpos( $test_url_parsed['path'], $site_url_parsed['path'] ) |
| 36 |
&& isset( $test_url_parsed['host'] ) // and if the URL of enqueued style is not relative |
| 37 |
) { |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
return true; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get the realpath of given URL |
| 46 |
* |
| 47 |
* @param string $url URL |
| 48 |
* @param string $site_url Site URL |
| 49 |
* |
| 50 |
* @return bool|string |
| 51 |
*/ |
| 52 |
public static function realpath( $url, $site_url ) { |
| 53 |
$url_path = wp_parse_url( $url, PHP_URL_PATH ); |
| 54 |
$site_url_path = wp_parse_url( $site_url, PHP_URL_PATH ); |
| 55 |
// To avoid partial matches; subdir install at `/wp` would match `/wp-includes` |
| 56 |
$site_url_path = is_null( $site_url_path ) ? '/' : trailingslashit( $site_url_path ); |
| 57 |
|
| 58 |
// If this is a subdirectory site, we need to strip off the subdir from the URL. |
| 59 |
// In a multisite install, the subdir is virtual and therefore not needed in the path. |
| 60 |
// In a single-site subdir install, the subdir is included in the ABSPATH and therefore ends up duplicated. |
| 61 |
if ( $site_url_path && '/' !== $site_url_path |
| 62 |
&& 0 === strpos( $url_path, $site_url_path ) ) { |
| 63 |
$url_path_without_subdir = preg_replace( '#^' . $site_url_path . '#', '', $url_path, 1 ); |
| 64 |
|
| 65 |
return wp_normalize_path( realpath( ABSPATH . $url_path_without_subdir ) ); |
| 66 |
} |
| 67 |
|
| 68 |
return wp_normalize_path( realpath( ABSPATH . $url_path ) ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Replace path in the buffer |
| 73 |
* |
| 74 |
* @param string $buf buffer |
| 75 |
* @param string $dirpath Directory path |
| 76 |
* |
| 77 |
* @return string|string[]|null |
| 78 |
*/ |
| 79 |
public static function relative_path_replace( $buf, $dirpath ) { |
| 80 |
// url(relative/path/to/file) -> url(/absolute/and/not/relative/path/to/file) |
| 81 |
$buf = preg_replace( |
| 82 |
'/(:?\s*url\s*\()\s*(?:\'|")?\s*([^\/\'"\s\)](?:(?<!data:|http:|https:|[\(\'"]#|%23).)*)[\'"\s]*\)/isU', |
| 83 |
'$1' . ( '/' === $dirpath ? '/' : $dirpath . '/' ) . '$2)', |
| 84 |
$buf |
| 85 |
); |
| 86 |
|
| 87 |
return $buf; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Get the optimizer URL for given resource(s) |
| 92 |
* |
| 93 |
* @param string $path The list of the optimized files |
| 94 |
* @param bool $minify minify flag |
| 95 |
* |
| 96 |
* @return string |
| 97 |
*/ |
| 98 |
public static function get_optimized_url( $path, $minify ) { |
| 99 |
$settings = \PoweredCache\Utils\get_settings(); |
| 100 |
$optimizer_url = POWERED_CACHE_URL . 'includes/file-optimizer.php??'; |
| 101 |
if ( $settings['rewrite_file_optimizer'] ) { |
| 102 |
$optimizer_url = site_url() . '/_static/??'; |
| 103 |
} |
| 104 |
|
| 105 |
$optimized_url = $optimizer_url . $path . '&minify=' . absint( $minify ); |
| 106 |
|
| 107 |
$optimized_url = esc_url_raw( apply_filters( 'powered_cache_fo_optimized_url', $optimized_url, $path, $minify ) ); |
| 108 |
|
| 109 |
return $optimized_url; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Get the optimizer relative path |
| 114 |
* This is used for rewrite_file_optimizer option |
| 115 |
* |
| 116 |
* @return mixed|null |
| 117 |
* @since 3.3.2 |
| 118 |
*/ |
| 119 |
public static function get_file_optimizer_relative_path() { |
| 120 |
// Calculate the relative path from the WordPress root to file-optimizer.php |
| 121 |
$relative_path = str_replace( ABSPATH, '', POWERED_CACHE_PATH . 'includes/file-optimizer.php' ); |
| 122 |
$relative_path = ltrim( $relative_path, '/' ); // Remove leading slash |
| 123 |
/** |
| 124 |
* Filters relative path for file optimizer |
| 125 |
* |
| 126 |
* @hook powered_cache_fo_relative_path |
| 127 |
* |
| 128 |
* @param {string} relative path of file-optimizer.php |
| 129 |
* |
| 130 |
* @return {string} New value. |
| 131 |
* @since 3.3.2 |
| 132 |
*/ |
| 133 |
$relative_path = apply_filters( 'powered_cache_fo_relative_path', $relative_path ); |
| 134 |
|
| 135 |
return $relative_path; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Check if the given SRC excluded |
| 140 |
* |
| 141 |
* @param string $src URL |
| 142 |
* |
| 143 |
* @return bool |
| 144 |
*/ |
| 145 |
public static function is_excluded_js( $src ) { |
| 146 |
$excluded_files = self::get_excluded_js(); |
| 147 |
$excluded_files = implode( '|', $excluded_files ); |
| 148 |
|
| 149 |
if ( ! empty( $excluded_files ) && preg_match( '#(' . $excluded_files . ')#', $src ) ) { |
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Check if the given SRC excluded |
| 158 |
* |
| 159 |
* @param string $src URL |
| 160 |
* |
| 161 |
* @return bool |
| 162 |
*/ |
| 163 |
public static function is_excluded_css( $src ) { |
| 164 |
$excluded_files = self::get_excluded_css(); |
| 165 |
$excluded_files = implode( '|', $excluded_files ); |
| 166 |
|
| 167 |
if ( ! empty( $excluded_files ) && preg_match( '#(' . $excluded_files . ')#', $src ) ) { |
| 168 |
return true; |
| 169 |
} |
| 170 |
|
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Check if excluded or not from defer |
| 176 |
* |
| 177 |
* @param string $tag Script tag |
| 178 |
* |
| 179 |
* @return bool |
| 180 |
*/ |
| 181 |
public static function is_defer_excluded( $tag ) { |
| 182 |
$excluded_files = self::get_defer_exclusions(); |
| 183 |
$excluded_files = implode( '|', $excluded_files ); |
| 184 |
|
| 185 |
if ( false !== stripos( $tag, 'data-no-defer' ) ) { |
| 186 |
return true; |
| 187 |
} |
| 188 |
|
| 189 |
if ( ! empty( $excluded_files ) && preg_match( '#(' . $excluded_files . ')#', $tag ) ) { |
| 190 |
return true; |
| 191 |
} |
| 192 |
|
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Check if excluded or not from delay |
| 198 |
* |
| 199 |
* @param string $tag Script tag |
| 200 |
* |
| 201 |
* @return bool |
| 202 |
*/ |
| 203 |
public static function is_delay_excluded( $tag ) { |
| 204 |
$excluded_files = self::get_delay_exclusions(); |
| 205 |
$excluded_files = implode( '|', $excluded_files ); |
| 206 |
|
| 207 |
if ( false !== stripos( $tag, 'data-no-delay' ) ) { |
| 208 |
return true; |
| 209 |
} |
| 210 |
|
| 211 |
if ( ! empty( $excluded_files ) && preg_match( '#(' . $excluded_files . ')#', $tag ) ) { |
| 212 |
return true; |
| 213 |
} |
| 214 |
|
| 215 |
return false; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Get defer exclusion list |
| 220 |
* |
| 221 |
* @return array |
| 222 |
* @since 3.2 |
| 223 |
*/ |
| 224 |
public static function get_defer_exclusions() { |
| 225 |
$settings = \PoweredCache\Utils\get_settings(); |
| 226 |
$excluded_files = preg_split( '#(\r\n|\n|\r)#', $settings['js_defer_exclusions'], - 1, PREG_SPLIT_NO_EMPTY ); |
| 227 |
|
| 228 |
/** |
| 229 |
* Filter the defer exclusions |
| 230 |
* |
| 231 |
* @hook powered_cache_defer_exclusions |
| 232 |
* |
| 233 |
* @param {array} $settings Excluded files |
| 234 |
* |
| 235 |
* @return {array} New value |
| 236 |
* @since 3.2 |
| 237 |
*/ |
| 238 |
return (array) apply_filters( 'powered_cache_defer_exclusions', $excluded_files ); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Get delay exclusion list |
| 243 |
* |
| 244 |
* @return array |
| 245 |
* @since 3.2 |
| 246 |
*/ |
| 247 |
public static function get_delay_exclusions() { |
| 248 |
$settings = \PoweredCache\Utils\get_settings(); |
| 249 |
$excluded_files = preg_split( '#(\r\n|\n|\r)#', $settings['js_delay_exclusions'], - 1, PREG_SPLIT_NO_EMPTY ); |
| 250 |
|
| 251 |
$excluded_files[] = 'wp-includes/js/dist/interactivity.min.js'; |
| 252 |
$excluded_files[] = 'wp-includes/blocks/image/view.min.js'; // lightbox doesnt work in delayed mode |
| 253 |
|
| 254 |
/** |
| 255 |
* Filter the delay exclusions |
| 256 |
* |
| 257 |
* @hook powered_cache_delay_exclusions |
| 258 |
* |
| 259 |
* @param {array} $settings Excluded files |
| 260 |
* |
| 261 |
* @return {array} New value |
| 262 |
* @since 3.2 |
| 263 |
*/ |
| 264 |
return (array) apply_filters( 'powered_cache_delay_exclusions', $excluded_files ); |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Get JS exclusion list |
| 269 |
* |
| 270 |
* @return array |
| 271 |
* @since 3.2 |
| 272 |
*/ |
| 273 |
public static function get_excluded_js() { |
| 274 |
$settings = \PoweredCache\Utils\get_settings(); |
| 275 |
$excluded_files = preg_split( '#(\r\n|\n|\r)#', $settings['excluded_js_files'], - 1, PREG_SPLIT_NO_EMPTY ); |
| 276 |
|
| 277 |
/** |
| 278 |
* Filter the excluded JS files |
| 279 |
* |
| 280 |
* @hook powered_cache_fo_excluded_js_files |
| 281 |
* |
| 282 |
* @param {array} $settings Excluded files |
| 283 |
* |
| 284 |
* @return {array} New value |
| 285 |
* @since 3.2 |
| 286 |
*/ |
| 287 |
return (array) apply_filters( 'powered_cache_fo_excluded_js_files', $excluded_files ); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Get CSS exclusion list |
| 292 |
* |
| 293 |
* @return array |
| 294 |
* @since 3.2 |
| 295 |
*/ |
| 296 |
public static function get_excluded_css() { |
| 297 |
$settings = \PoweredCache\Utils\get_settings(); |
| 298 |
$excluded_files = preg_split( '#(\r\n|\n|\r)#', $settings['excluded_css_files'], - 1, PREG_SPLIT_NO_EMPTY ); |
| 299 |
|
| 300 |
/** |
| 301 |
* Filter the excluded css files |
| 302 |
* |
| 303 |
* @hook powered_cache_fo_excluded_css_files |
| 304 |
* |
| 305 |
* @param {array} $settings Excluded files |
| 306 |
* |
| 307 |
* @return {array} New value |
| 308 |
* @since 3.2 |
| 309 |
*/ |
| 310 |
return (array) apply_filters( 'powered_cache_fo_excluded_css_files', $excluded_files ); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
} |
| 315 |
|