| 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 = 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 |
$path = wp_normalize_path( $path ); |
| 100 |
|
| 101 |
$optimizer_url = POWERED_CACHE_URL . 'includes/file-optimizer.php??'; |
| 102 |
$optimized_url = $optimizer_url . $path . '?minify=' . absint( $minify ); |
| 103 |
|
| 104 |
$optimized_url = esc_url_raw( apply_filters( 'powered_cache_fo_optimized_url', $optimized_url, $path, $minify ) ); |
| 105 |
|
| 106 |
return $optimized_url; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Check if the given SRC excluded |
| 111 |
* |
| 112 |
* @param string $src URL |
| 113 |
* |
| 114 |
* @return bool |
| 115 |
*/ |
| 116 |
public static function is_excluded_js( $src ) { |
| 117 |
$settings = \PoweredCache\Utils\get_settings(); |
| 118 |
$excluded_files = preg_split( '#(\r\n|\n|\r)#', $settings['excluded_js_files'], - 1, PREG_SPLIT_NO_EMPTY ); |
| 119 |
$excluded_files = implode( '|', $excluded_files ); |
| 120 |
|
| 121 |
if ( ! empty( $excluded_files ) && preg_match( '#(' . $excluded_files . ')#', $src ) ) { |
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Check if the given SRC excluded |
| 130 |
* |
| 131 |
* @param string $src URL |
| 132 |
* |
| 133 |
* @return bool |
| 134 |
*/ |
| 135 |
public static function is_excluded_css( $src ) { |
| 136 |
$settings = \PoweredCache\Utils\get_settings(); |
| 137 |
$excluded_files = preg_split( '#(\r\n|\n|\r)#', $settings['excluded_css_files'], - 1, PREG_SPLIT_NO_EMPTY ); |
| 138 |
$excluded_files = implode( '|', $excluded_files ); |
| 139 |
|
| 140 |
if ( ! empty( $excluded_files ) && preg_match( '#(' . $excluded_files . ')#', $src ) ) { |
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
} |
| 148 |
|