| @@ -52,9 +52,9 @@ | ||
| 52 | 52 | public static function realpath( $url, $site_url ) { |
| 53 | 53 | $url_path = wp_parse_url( $url, PHP_URL_PATH ); |
| 54 | 54 | $site_url_path = wp_parse_url( $site_url, PHP_URL_PATH ); |
| 55 | 55 | // To avoid partial matches; subdir install at `/wp` would match `/wp-includes` |
| 56 | - $site_url_path = trailingslashit( $site_url_path ); | |
| 56 | + $site_url_path = is_null( $site_url_path ) ? '/' : trailingslashit( $site_url_path ); | |
| 57 | 57 | |
| 58 | 58 | // If this is a subdirectory site, we need to strip off the subdir from the URL. |
| 59 | 59 | // In a multisite install, the subdir is virtual and therefore not needed in the path. |
| 60 | 60 | // In a single-site subdir install, the subdir is included in the ABSPATH and therefore ends up duplicated. |
| @@ -95,12 +95,15 @@ | ||
| 95 | 95 | * |
| 96 | 96 | * @return string |
| 97 | 97 | */ |
| 98 | 98 | public static function get_optimized_url( $path, $minify ) { |
| 99 | - $path = wp_normalize_path( $path ); | |
| 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 | + } | |
| 100 | 104 | |
| 101 | - $optimizer_url = POWERED_CACHE_URL . 'includes/file-optimizer.php??'; | |
| 102 | - $optimized_url = $optimizer_url . $path . '?minify=' . absint( $minify ); | |
| 105 | + $optimized_url = $optimizer_url . $path . '&minify=' . absint( $minify ); | |
| 103 | 106 | |
| 104 | 107 | $optimized_url = esc_url_raw( apply_filters( 'powered_cache_fo_optimized_url', $optimized_url, $path, $minify ) ); |
| 105 | 108 | |
| 106 | 109 | return $optimized_url; |
| @@ -106,8 +109,34 @@ | ||
| 106 | 109 | return $optimized_url; |
| 107 | 110 | } |
| 108 | 111 | |
| 109 | 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 | + /** | |
| 110 | 139 | * Check if the given SRC excluded |
| 111 | 140 | * |
| 112 | 141 | * @param string $src URL |
| 113 | 142 | * |
| @@ -113,10 +142,9 @@ | ||
| 113 | 142 | * |
| 114 | 143 | * @return bool |
| 115 | 144 | */ |
| 116 | 145 | 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 ); | |
| 146 | + $excluded_files = self::get_excluded_js(); | |
| 119 | 147 | $excluded_files = implode( '|', $excluded_files ); |
| 120 | 148 | |
| 121 | 149 | if ( ! empty( $excluded_files ) && preg_match( '#(' . $excluded_files . ')#', $src ) ) { |
| 122 | 150 | return true; |
| @@ -132,10 +160,9 @@ | ||
| 132 | 160 | * |
| 133 | 161 | * @return bool |
| 134 | 162 | */ |
| 135 | 163 | 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 ); | |
| 164 | + $excluded_files = self::get_excluded_css(); | |
| 138 | 165 | $excluded_files = implode( '|', $excluded_files ); |
| 139 | 166 | |
| 140 | 167 | if ( ! empty( $excluded_files ) && preg_match( '#(' . $excluded_files . ')#', $src ) ) { |
| 141 | 168 | return true; |
| @@ -142,6 +169,146 @@ | ||
| 142 | 169 | } |
| 143 | 170 | |
| 144 | 171 | return false; |
| 145 | 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 | + | |
| 146 | 313 | |
| 147 | 314 | } |