From 03ff040761a5ae9e414833ad5fbc8b4550c04cff Mon Sep 17 00:00:00 2001 From: Alexei Skorobogatko Date: Tue, 7 Jul 2020 21:30:31 +0300 Subject: [PATCH] Loading the static when WordPress is in a subdirectory. When WordPress is installed into a subdirectory this plugin cannot load static files and breaks the frontend. This patch adds the site uri path to the generated static links. --- concat-css.php | 7 +++++-- concat-js.php | 7 +++++-- page-optimize.php | 24 +++++++++++++++++++++++- service.php | 2 +- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/concat-css.php b/concat-css.php index 18024d8..e283059 100644 --- a/concat-css.php +++ b/concat-css.php @@ -39,6 +39,7 @@ function do_items( $handles = false, $group = false ) { $handles = false === $handles ? $this->queue : (array) $handles; $stylesheets = array(); $siteurl = apply_filters( 'page_optimize_site_url', $this->base_url ); + $site_uri_path = page_optimize_get_uri_path( $siteurl ); $this->all_deps( $handles ); @@ -136,7 +137,9 @@ function do_items( $handles = false, $group = false ) { $media = 'all'; } - $stylesheets[ $concat_group ][ $media ][ $handle ] = $css_url_parsed['path']; + $css_uri_path = page_optimize_remove_uri_prefix( $site_uri_path, $css_url_parsed['path'] ); + + $stylesheets[ $concat_group ][ $media ][ $handle ] = $css_uri_path; $this->done[] = $handle; } else { $stylesheet_group_index ++; @@ -158,7 +161,7 @@ function do_items( $handles = false, $group = false ) { } elseif ( count( $css ) > 1 ) { $fs_paths = array(); foreach ( $css as $css_uri_path ) { - $fs_paths[] = $this->dependency_path_mapping->uri_path_to_fs_path( $css_uri_path ); + $fs_paths[] = $this->dependency_path_mapping->uri_path_to_fs_path( $site_uri_path . $css_uri_path ); } $mtime = max( array_map( 'filemtime', $fs_paths ) ); diff --git a/concat-js.php b/concat-js.php index 46d9020..c000bde 100644 --- a/concat-js.php +++ b/concat-js.php @@ -59,6 +59,7 @@ function do_items( $handles = false, $group = false ) { $handles = false === $handles ? $this->queue : (array) $handles; $javascripts = array(); $siteurl = apply_filters( 'page_optimize_site_url', $this->base_url ); + $site_uri_path = page_optimize_get_uri_path( $siteurl ); $this->all_deps( $handles ); $level = 0; @@ -171,7 +172,9 @@ function do_items( $handles = false, $group = false ) { $javascripts[ $level ]['type'] = 'concat'; } - $javascripts[ $level ]['paths'][] = $js_url_parsed['path']; + $js_uri_path = page_optimize_remove_uri_prefix( $site_uri_path, $js_url_parsed['path'] ); + + $javascripts[ $level ]['paths'][] = $js_uri_path; $javascripts[ $level ]['handles'][] = $handle; } else { @@ -211,7 +214,7 @@ function do_items( $handles = false, $group = false ) { if ( isset( $js_array['paths'] ) && count( $js_array['paths'] ) > 1 ) { $fs_paths = array(); foreach ( $js_array['paths'] as $js_url ) { - $fs_paths[] = $this->dependency_path_mapping->uri_path_to_fs_path( $js_url ); + $fs_paths[] = $this->dependency_path_mapping->uri_path_to_fs_path( $site_uri_path . $js_url ); } $mtime = max( array_map( 'filemtime', $fs_paths ) ); diff --git a/page-optimize.php b/page-optimize.php index 57174b1..7b8a8be 100644 --- a/page-optimize.php +++ b/page-optimize.php @@ -26,7 +26,7 @@ // TODO: Copy tests from nginx-http-concat and/or write them // TODO: Make concat URL dir configurable -if ( isset( $_SERVER['REQUEST_URI'] ) && '/_static/' === substr( $_SERVER['REQUEST_URI'], 0, 9 ) ) { +if ( isset( $_SERVER['REQUEST_URI'] ) && false !== strpos( $_SERVER['REQUEST_URI'], '/_static/??' ) ) { require_once __DIR__ . '/service.php'; exit; } @@ -213,6 +213,28 @@ function page_optimize_starts_with( $prefix, $str ) { return substr( $str, 0, $prefix_length ) === $prefix; } +/** + * Returns the uri path from the url. + */ +function page_optimize_get_uri_path( $url ) { + $path = trailingslashit( parse_url( $url, PHP_URL_PATH ) ); + return $path == '/' ? '' : $path; +} + +/** + * Removes the path prefix from the uri string. + */ +function page_optimize_remove_uri_prefix( $prefix, $uri ) { + if ( empty( $prefix ) ) { + return $uri; + } + if ( ! page_optimize_starts_with( $prefix, $uri ) ) { + return $uri; + } + + return '/' . ltrim( substr( $uri, strlen($prefix) ), '/' ); +} + /** * Answers whether the plugin should provide concat resource URIs * that are relative to a common ancestor directory. Assuming a common ancestor diff --git a/service.php b/service.php index eea8aa1..7455279 100644 --- a/service.php +++ b/service.php @@ -152,7 +152,7 @@ function page_optimize_build_output() { } foreach ( $args as $uri ) { - $fullpath = page_optimize_get_path( $uri ); + $fullpath = page_optimize_get_path( $subdir_path_prefix . $uri ); if ( ! file_exists( $fullpath ) ) { page_optimize_status_exit( 404 );