PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 2.0
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v2.0
trunk 1.0 1.0.1 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1 2.1.1 2.1.2 2.2 2.2.1 All 69 releases
powered-cache / includes / classes / Optimizer / Helper.php

Helper.php in Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score 2.0, at includes/classes/Optimizer/Helper.php

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