diff --git a/concat-css.php b/concat-css.php index 18024d8..f76864e 100644 --- a/concat-css.php +++ b/concat-css.php @@ -38,17 +38,18 @@ function __construct( $styles ) { 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 ); + + // get the website base url + $siteurl = apply_filters( 'page_optimize_site_url', $this->dependency_path_mapping->base_url ); $this->all_deps( $handles ); - $stylesheet_group_index = 0; // Merge CSS into a single file - $concat_group = 'concat'; - // Concat group on top (first array element gets processed earlier) - $stylesheets[ $concat_group ] = array(); + $style_index = 0; + $style_group = array(); foreach ( $this->to_do as $key => $handle ) { + $obj = $this->registered[ $handle ]; $obj->src = apply_filters( 'style_loader_src', $obj->src, $obj->handle ); @@ -63,16 +64,18 @@ function do_items( $handles = false, $group = false ) { $css_url_parsed = parse_url( $obj->src ); $extra = $obj->extra; - // Don't concat by default - $do_concat = false; + // try to concat static css files + $do_concat = true; + + if ( 0 == strpos( $css_url_parsed['path'], '.css' ) ) { - // Only try to concat static css files - if ( false !== strpos( $css_url_parsed['path'], '.css' ) ) { - $do_concat = true; - } else { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { echo sprintf( "\n\n", esc_html( $handle ), esc_html( $obj->src ) ); } + + $style_group['not-static-'. $style_index++ ] = $handle; + + $do_concat = false; } // Don't try to concat styles which are loaded conditionally (like IE stuff) @@ -80,6 +83,8 @@ function do_items( $handles = false, $group = false ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { echo sprintf( "\n\n", esc_html( $handle ) ); } + + $style_group['conditional-'. $style_index++ ] = $handle; $do_concat = false; } @@ -88,6 +93,8 @@ function do_items( $handles = false, $group = false ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { echo sprintf( "\n\n", esc_html( $handle ) ); } + + $style_group['rtl-'. $style_index++ ] = $handle; $do_concat = false; } @@ -97,6 +104,8 @@ function do_items( $handles = false, $group = false ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { echo sprintf( "\n\n", esc_html( $handle ), esc_url( $css_url ) ); } + + $style_group['external-'. $style_index++ ] = $handle; $do_concat = false; } @@ -107,6 +116,8 @@ function do_items( $handles = false, $group = false ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { echo sprintf( "\n\n", esc_html( $handle ), esc_html( $css_realpath ) ); } + + $style_group['invalid-'. $style_index++ ] = $handle; $do_concat = false; } } @@ -115,10 +126,12 @@ function do_items( $handles = false, $group = false ) { $exclude_list = page_optimize_css_exclude_list(); foreach ( $exclude_list as $exclude ) { if ( $do_concat && $handle === $exclude ) { - $do_concat = false; if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { echo sprintf( "\n\n", esc_html( $handle ) ); } + + $style_group['excluded-'. $style_index++ ] = $handle; + $do_concat = false; } } @@ -131,31 +144,27 @@ function do_items( $handles = false, $group = false ) { $do_concat = apply_filters( 'css_do_concat', $do_concat, $handle ); if ( true === $do_concat ) { + + $style_group[ 'concat-' . $style_index ][] = $handle; + $media = $obj->args; if ( empty( $media ) ) { $media = 'all'; } - $stylesheets[ $concat_group ][ $media ][ $handle ] = $css_url_parsed['path']; + $stylesheets[ 'concat-' . $style_index ][ $media ][ $handle ] = $css_url_parsed['path']; $this->done[] = $handle; } else { - $stylesheet_group_index ++; - $stylesheets[ $stylesheet_group_index ]['noconcat'][] = $handle; - $stylesheet_group_index ++; + if ( $this->do_item( $handle, $group ) ) { + $this->done[] = $handle; + } } unset( $this->to_do[ $key ] ); } foreach ( $stylesheets as $idx => $stylesheets_group ) { foreach ( $stylesheets_group as $media => $css ) { - if ( 'noconcat' == $media ) { - foreach ( $css as $handle ) { - if ( $this->do_item( $handle, $group ) ) { - $this->done[] = $handle; - } - } - continue; - } elseif ( count( $css ) > 1 ) { + if ( !empty( $css ) ) { $fs_paths = array(); foreach ( $css as $css_uri_path ) { $fs_paths[] = $this->dependency_path_mapping->uri_path_to_fs_path( $css_uri_path ); @@ -176,7 +185,7 @@ function do_items( $handles = false, $group = false ) { } } - $href = $siteurl . "/_static/??" . $path_str; + $href = $siteurl . $this->dependency_path_mapping->site_subdir_path . "/_static/??" . $path_str; } else { $href = Page_Optimize_Utils::cache_bust_mtime( current( $css ), $siteurl ); } diff --git a/concat-js.php b/concat-js.php index 46d9020..3d03225 100644 --- a/concat-js.php +++ b/concat-js.php @@ -58,7 +58,8 @@ protected function has_inline_content( $handle ) { 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 ); + $siteurl = apply_filters( 'page_optimize_site_url', $this->dependency_path_mapping->base_url ); + $this->all_deps( $handles ); $level = 0; @@ -229,7 +230,7 @@ function do_items( $handles = false, $group = false ) { } } - $href = $siteurl . "/_static/??" . $path_str; + $href = $siteurl . $this->dependency_path_mapping->site_subdir_path . "/_static/??" . $path_str; } elseif ( isset( $js_array['paths'] ) && is_array( $js_array['paths'] ) ) { $href = Page_Optimize_Utils::cache_bust_mtime( $js_array['paths'][0], $siteurl ); } diff --git a/dependency-path-mapping.php b/dependency-path-mapping.php index 7170edd..48d6d45 100644 --- a/dependency-path-mapping.php +++ b/dependency-path-mapping.php @@ -10,6 +10,8 @@ class Page_Optimize_Dependency_Path_Mapping { public $site_url; // Save URI path and dir for mapping URIs to filesystem paths + public $base_url = null; + public $site_subdir_path = null; public $site_uri_path = null; public $site_dir = null; public $content_uri_path = null; @@ -29,9 +31,15 @@ function __construct( if ( null === $site_url ) { $site_url = is_multisite() ? get_site_url( get_current_blog_id() ) : get_site_url(); } + // parse the site url for further use + $url_parsed = parse_url($site_url); + + $this->base_url = $url_parsed['scheme'].'://'.$url_parsed['host']; + $this->site_subdir_path = str_replace( $this->base_url, "", $site_url ); + $site_url = trailingslashit( $site_url ); $this->site_url = $site_url; - $this->site_uri_path = parse_url( $site_url, PHP_URL_PATH ); + $this->site_uri_path = $url_parsed['path']; $this->site_dir = trailingslashit( $site_dir ); // Only resolve content URLs if they are under the site URL @@ -87,6 +95,11 @@ function uri_path_to_fs_path( $uri_path ) { return false; } + // Adds the sub-directory path if not present, like all other resources have if the website is hosted into a sub-folder + if ( ! empty( $this->site_subdir_path ) && ! page_optimize_starts_with( $this->site_subdir_path, $uri_path ) ) { + $uri_path = $this->site_subdir_path . $uri_path; + } + // The plugin URI path may be contained within the content URI path, so we check it before the content URI. // And both the plugin and content URI paths must be contained within the site URI path, // so we check them before checking the site URI. diff --git a/page-optimize.php b/page-optimize.php index 4d3ac9b..9732798 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 ( $_SERVER['REQUEST_URI'] != false && stripos( $_SERVER['REQUEST_URI'], '/_static/' ) !== false ) { require_once __DIR__ . '/service.php'; exit; } diff --git a/service.php b/service.php index eea8aa1..99a4615 100644 --- a/service.php +++ b/service.php @@ -187,8 +187,13 @@ function page_optimize_build_output() { page_optimize_status_exit( 500 ); } + // Removes source mapping URLs as they are unnecessary after concatenation + if ( false !== strpos( $buf, 'sourceMappingURL' ) ) { + $buf = preg_replace('/\/[\/*].*sourceMapping.+/', "" , $buf ); + } + if ( 'text/css' == $mime_type ) { - $dirpath = '/' . ltrim( $subdir_path_prefix . dirname( $uri ), '/' ); + $dirpath = '/' . ltrim( dirname( $uri ), '/' ); // url(relative/path/to/file) -> url(/absolute/and/not/relative/path/to/file) $buf = page_optimize_relative_path_replace( $buf, $dirpath );