From 712a9a4eb041f31b355b06adb326414e173d57db Mon Sep 17 00:00:00 2001 From: Erik Yo Date: Mon, 20 Dec 2021 14:53:22 +0100 Subject: [PATCH 01/15] as the https://github.com/Automattic/page-optimize/pull/47 says this plugin doesn't work when the website is installed into a subdirectory... but since it has been a long time and the pr is behind I'll make a new one --- page-optimize.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; } From 090c0140003e6319713c0a85f8fffaeab3d00993 Mon Sep 17 00:00:00 2001 From: Erik Yo Date: Mon, 20 Dec 2021 15:00:46 +0100 Subject: [PATCH 02/15] The base variable url is created since it is needed to be concatenated and siteurl returns also the name of the subdomain. It would be better to create in dependency-path-mapping a base path, but for the moment this fixes the issue --- concat-css.php | 8 ++++++-- concat-js.php | 7 ++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/concat-css.php b/concat-css.php index 18024d8..9e0220c 100644 --- a/concat-css.php +++ b/concat-css.php @@ -40,7 +40,11 @@ function do_items( $handles = false, $group = false ) { $stylesheets = array(); $siteurl = apply_filters( 'page_optimize_site_url', $this->base_url ); - $this->all_deps( $handles ); + // get the website base url + $url_raw = parse_url($siteurl); + $baseurl = $url_raw['scheme']."://".$url_raw['host']; + + $this->all_deps( $handles ); $stylesheet_group_index = 0; // Merge CSS into a single file @@ -178,7 +182,7 @@ function do_items( $handles = false, $group = false ) { $href = $siteurl . "/_static/??" . $path_str; } else { - $href = Page_Optimize_Utils::cache_bust_mtime( current( $css ), $siteurl ); + $href = Page_Optimize_Utils::cache_bust_mtime( current( $css ), $baseurl ); } $handles = array_keys( $css ); diff --git a/concat-js.php b/concat-js.php index 46d9020..941f262 100644 --- a/concat-js.php +++ b/concat-js.php @@ -59,6 +59,11 @@ 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 ); + + // get the website base url + $url_raw = parse_url($siteurl); + $baseurl = $url_raw['scheme']."://".$url_raw['host']; + $this->all_deps( $handles ); $level = 0; @@ -231,7 +236,7 @@ function do_items( $handles = false, $group = false ) { $href = $siteurl . "/_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 ); + $href = Page_Optimize_Utils::cache_bust_mtime( $js_array['paths'][0], $baseurl ); } $this->done = array_merge( $this->done, $js_array['handles'] ); From 183f8ef93505911927fe653757933da804a636a6 Mon Sep 17 00:00:00 2001 From: Erik Yo Date: Mon, 20 Dec 2021 15:06:19 +0100 Subject: [PATCH 03/15] adds the /wp-includes/ as check while the paths are converted with static fs paths. maybe it could be avoided but without this I miss the block style --- dependency-path-mapping.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dependency-path-mapping.php b/dependency-path-mapping.php index 7170edd..e450018 100644 --- a/dependency-path-mapping.php +++ b/dependency-path-mapping.php @@ -94,6 +94,8 @@ function uri_path_to_fs_path( $uri_path ) { $file_path = $this->plugin_dir . substr( $uri_path, strlen( $this->plugin_uri_path ) ); } else if ( isset( $this->content_uri_path ) && static::is_descendant_uri( $this->content_uri_path, $uri_path ) ) { $file_path = $this->content_dir . substr( $uri_path, strlen( $this->content_uri_path ) ); + } else if ( static::is_descendant_uri( '/wp-includes/', $uri_path ) ) { + $file_path = $this->site_dir . $uri_path; } else if ( static::is_descendant_uri( $this->site_uri_path, $uri_path ) ) { $file_path = $this->site_dir . substr( $uri_path, strlen( $this->site_uri_path ) ); } From 82379e74bb3d9617c7be641609651b92c137aff9 Mon Sep 17 00:00:00 2001 From: Erik Yo Date: Mon, 20 Dec 2021 15:10:09 +0100 Subject: [PATCH 04/15] this does not serve since the prefix is already present in front of every resource --- service.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service.php b/service.php index eea8aa1..25b96b5 100644 --- a/service.php +++ b/service.php @@ -188,7 +188,7 @@ function page_optimize_build_output() { } 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 ); From f5c2486ca8f1eaf4d4a2061c96a56f2d0fe46a7c Mon Sep 17 00:00:00 2001 From: Erik Yo Date: Mon, 20 Dec 2021 21:16:08 +0100 Subject: [PATCH 05/15] fix the backend js/css path since the backend section is also the broken, this solves temporarily. the issue was related to uri_path_to_fs_path() when serving files from /wp-admin and /wp-includes (eg. src="/wp-admin/css/blocks.css") because with is_descendant_uri we check if it starts with the name of the subfolder ($this->site_dir) --- dependency-path-mapping.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dependency-path-mapping.php b/dependency-path-mapping.php index e450018..4e44713 100644 --- a/dependency-path-mapping.php +++ b/dependency-path-mapping.php @@ -96,7 +96,9 @@ function uri_path_to_fs_path( $uri_path ) { $file_path = $this->content_dir . substr( $uri_path, strlen( $this->content_uri_path ) ); } else if ( static::is_descendant_uri( '/wp-includes/', $uri_path ) ) { $file_path = $this->site_dir . $uri_path; - } else if ( static::is_descendant_uri( $this->site_uri_path, $uri_path ) ) { + } else if ( static::is_descendant_uri( '/wp-admin/', $uri_path ) ) { + $file_path = $this->site_dir . $uri_path; + } else if ( static::is_descendant_uri( $this->site_uri_path, $uri_path ) ) { $file_path = $this->site_dir . substr( $uri_path, strlen( $this->site_uri_path ) ); } From e3772028035c06881cf802ff22f0f2cefd641f03 Mon Sep 17 00:00:00 2001 From: Erik Yo Date: Thu, 23 Dec 2021 10:20:05 +0100 Subject: [PATCH 06/15] add sub-folder to directory mappings then use it while build paths (remove the old fix) --- dependency-path-mapping.php | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/dependency-path-mapping.php b/dependency-path-mapping.php index 4e44713..fa6d257 100644 --- a/dependency-path-mapping.php +++ b/dependency-path-mapping.php @@ -10,8 +10,10 @@ class Page_Optimize_Dependency_Path_Mapping { public $site_url; // Save URI path and dir for mapping URIs to filesystem paths - public $site_uri_path = null; - public $site_dir = null; + public $base_url = null; + public $site_subdir_path = null; + public $site_uri_path = null; + public $site_dir = null; public $content_uri_path = null; public $content_dir = null; public $plugin_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. @@ -94,12 +107,8 @@ function uri_path_to_fs_path( $uri_path ) { $file_path = $this->plugin_dir . substr( $uri_path, strlen( $this->plugin_uri_path ) ); } else if ( isset( $this->content_uri_path ) && static::is_descendant_uri( $this->content_uri_path, $uri_path ) ) { $file_path = $this->content_dir . substr( $uri_path, strlen( $this->content_uri_path ) ); - } else if ( static::is_descendant_uri( '/wp-includes/', $uri_path ) ) { - $file_path = $this->site_dir . $uri_path; - } else if ( static::is_descendant_uri( '/wp-admin/', $uri_path ) ) { - $file_path = $this->site_dir . $uri_path; } else if ( static::is_descendant_uri( $this->site_uri_path, $uri_path ) ) { - $file_path = $this->site_dir . substr( $uri_path, strlen( $this->site_uri_path ) ); + $file_path = $this->site_dir . substr( $uri_path, strlen( $this->site_uri_path ) ); } if ( isset( $file_path ) && file_exists( $file_path ) ) { From 41304a6d37659d536c3d22f491acf97f6bebf07d Mon Sep 17 00:00:00 2001 From: Erik Yo Date: Thu, 23 Dec 2021 11:44:16 +0100 Subject: [PATCH 07/15] =?UTF-8?q?this=20will=20fix=20issues=20with=20.edit?= =?UTF-8?q?orconfig=20(was=20not=20applied=20=F0=9F=98=A5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- concat-css.php | 8 ++++---- concat-js.php | 2 +- dependency-path-mapping.php | 20 ++++++++++---------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/concat-css.php b/concat-css.php index 9e0220c..fac242f 100644 --- a/concat-css.php +++ b/concat-css.php @@ -40,11 +40,11 @@ function do_items( $handles = false, $group = false ) { $stylesheets = array(); $siteurl = apply_filters( 'page_optimize_site_url', $this->base_url ); - // get the website base url - $url_raw = parse_url($siteurl); - $baseurl = $url_raw['scheme']."://".$url_raw['host']; + // get the website base url + $url_raw = parse_url( $siteurl ); + $baseurl = $url_raw['scheme'] . "://" . $url_raw['host']; - $this->all_deps( $handles ); + $this->all_deps( $handles ); $stylesheet_group_index = 0; // Merge CSS into a single file diff --git a/concat-js.php b/concat-js.php index 941f262..a45604e 100644 --- a/concat-js.php +++ b/concat-js.php @@ -62,7 +62,7 @@ function do_items( $handles = false, $group = false ) { // get the website base url $url_raw = parse_url($siteurl); - $baseurl = $url_raw['scheme']."://".$url_raw['host']; + $baseurl = $url_raw['scheme']."://".$url_raw['host']; $this->all_deps( $handles ); $level = 0; diff --git a/dependency-path-mapping.php b/dependency-path-mapping.php index fa6d257..0d53458 100644 --- a/dependency-path-mapping.php +++ b/dependency-path-mapping.php @@ -10,10 +10,10 @@ 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 $base_url = null; + public $site_subdir_path = null; + public $site_uri_path = null; + public $site_dir = null; public $content_uri_path = null; public $content_dir = null; public $plugin_uri_path = null; @@ -31,15 +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 + // 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 ); + $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 = $url_parsed['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 @@ -107,8 +107,8 @@ function uri_path_to_fs_path( $uri_path ) { $file_path = $this->plugin_dir . substr( $uri_path, strlen( $this->plugin_uri_path ) ); } else if ( isset( $this->content_uri_path ) && static::is_descendant_uri( $this->content_uri_path, $uri_path ) ) { $file_path = $this->content_dir . substr( $uri_path, strlen( $this->content_uri_path ) ); - } else if ( static::is_descendant_uri( $this->site_uri_path, $uri_path ) ) { - $file_path = $this->site_dir . substr( $uri_path, strlen( $this->site_uri_path ) ); + } else if ( static::is_descendant_uri( $this->site_uri_path, $uri_path ) ) { + $file_path = $this->site_dir . substr( $uri_path, strlen( $this->site_uri_path ) ); } if ( isset( $file_path ) && file_exists( $file_path ) ) { From d910dc7b41c526918d7e37db34bc3bcc405f6c6f Mon Sep 17 00:00:00 2001 From: Erik Yo Date: Thu, 23 Dec 2021 20:39:19 +0100 Subject: [PATCH 08/15] use the new base_dir inside dependency-path-mapping to resolve paths --- concat-css.php | 8 +++----- concat-js.php | 10 +++------- dependency-path-mapping.php | 6 +++--- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/concat-css.php b/concat-css.php index fac242f..bbebc43 100644 --- a/concat-css.php +++ b/concat-css.php @@ -38,11 +38,9 @@ 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 - $url_raw = parse_url( $siteurl ); - $baseurl = $url_raw['scheme'] . "://" . $url_raw['host']; + $siteurl = apply_filters( 'page_optimize_site_url', $this->dependency_path_mapping->base_url ); $this->all_deps( $handles ); @@ -180,9 +178,9 @@ 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 ), $baseurl ); + $href = Page_Optimize_Utils::cache_bust_mtime( current( $css ), $siteurl ); } $handles = array_keys( $css ); diff --git a/concat-js.php b/concat-js.php index a45604e..3d03225 100644 --- a/concat-js.php +++ b/concat-js.php @@ -58,11 +58,7 @@ 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 ); - - // get the website base url - $url_raw = parse_url($siteurl); - $baseurl = $url_raw['scheme']."://".$url_raw['host']; + $siteurl = apply_filters( 'page_optimize_site_url', $this->dependency_path_mapping->base_url ); $this->all_deps( $handles ); $level = 0; @@ -234,9 +230,9 @@ 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], $baseurl ); + $href = Page_Optimize_Utils::cache_bust_mtime( $js_array['paths'][0], $siteurl ); } $this->done = array_merge( $this->done, $js_array['handles'] ); diff --git a/dependency-path-mapping.php b/dependency-path-mapping.php index 0d53458..48d6d45 100644 --- a/dependency-path-mapping.php +++ b/dependency-path-mapping.php @@ -96,9 +96,9 @@ function uri_path_to_fs_path( $uri_path ) { } // 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; - } + 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, From c37a1e283744468d1b93b094d0b62e231b7094bf Mon Sep 17 00:00:00 2001 From: Erik Yo Date: Sat, 25 Dec 2021 15:31:11 +0100 Subject: [PATCH 09/15] enqueue non-concatenated styles before the processed style --- concat-css.php | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/concat-css.php b/concat-css.php index bbebc43..72577fa 100644 --- a/concat-css.php +++ b/concat-css.php @@ -141,23 +141,16 @@ function do_items( $handles = false, $group = false ) { $stylesheets[ $concat_group ][ $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 ( 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 ); From 921facca2ded5f7399e953d6e352212c7ba84194 Mon Sep 17 00:00:00 2001 From: Erik Yo Date: Sat, 25 Dec 2021 16:01:07 +0100 Subject: [PATCH 10/15] Removes source mapping URLs as they are unnecessary after concatenation --- service.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/service.php b/service.php index 25b96b5..67754f8 100644 --- a/service.php +++ b/service.php @@ -200,6 +200,11 @@ function page_optimize_build_output() { $buf ); + // Removes source mapping URLs as they are unnecessary after concatenation + if ( false !== strpos( $buf, 'sourceMappingURL' ) ) { + $buf = preg_replace('/(\/\*# sourceMappingURL=.+\.map \*\/)/', "" , $buf ); + } + // The @charset rules must be on top of the output if ( 0 === strpos( $buf, '@charset' ) ) { preg_replace_callback( From b0972b5f60f4138a116f66c0c68e853a04a305bf Mon Sep 17 00:00:00 2001 From: Erik Yo Date: Sat, 25 Dec 2021 16:02:14 +0100 Subject: [PATCH 11/15] Removes $stylesheet_group_index variable since isn't used anymore --- concat-css.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concat-css.php b/concat-css.php index 72577fa..7ec687c 100644 --- a/concat-css.php +++ b/concat-css.php @@ -44,9 +44,9 @@ function do_items( $handles = false, $group = false ) { $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(); From 91caff71f93808b0695e3e9a360a755b4001cb30 Mon Sep 17 00:00:00 2001 From: Erik Yo Date: Sat, 25 Dec 2021 17:09:12 +0100 Subject: [PATCH 12/15] remove map link from concatenated file also for js files --- service.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/service.php b/service.php index 67754f8..d4dfe78 100644 --- a/service.php +++ b/service.php @@ -187,6 +187,11 @@ 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('/(\/\*# sourceMappingURL=.+\.map \*\/)/', "" , $buf ); + } + if ( 'text/css' == $mime_type ) { $dirpath = '/' . ltrim( dirname( $uri ), '/' ); @@ -200,11 +205,6 @@ function page_optimize_build_output() { $buf ); - // Removes source mapping URLs as they are unnecessary after concatenation - if ( false !== strpos( $buf, 'sourceMappingURL' ) ) { - $buf = preg_replace('/(\/\*# sourceMappingURL=.+\.map \*\/)/', "" , $buf ); - } - // The @charset rules must be on top of the output if ( 0 === strpos( $buf, '@charset' ) ) { preg_replace_callback( From 5a3f3da8c3b7175c5b5f39e0f669caf6c048f79a Mon Sep 17 00:00:00 2001 From: Erik Yo Date: Sun, 26 Dec 2021 11:52:53 +0100 Subject: [PATCH 13/15] loads css styles in the correct sequence --- concat-css.php | 34 ++++++++++++++++++++++++---------- service.php | 2 +- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/concat-css.php b/concat-css.php index 7ec687c..01677cb 100644 --- a/concat-css.php +++ b/concat-css.php @@ -45,12 +45,11 @@ function do_items( $handles = false, $group = false ) { $this->all_deps( $handles ); // 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 ); @@ -65,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; - // 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) @@ -82,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; } @@ -90,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; } @@ -99,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; } @@ -109,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; } } @@ -117,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; } } @@ -133,12 +144,15 @@ 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 { if ( $this->do_item( $handle, $group ) ) { @@ -150,7 +164,7 @@ function do_items( $handles = false, $group = false ) { foreach ( $stylesheets as $idx => $stylesheets_group ) { foreach ( $stylesheets_group as $media => $css ) { - if ( 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 ); diff --git a/service.php b/service.php index d4dfe78..34ef78f 100644 --- a/service.php +++ b/service.php @@ -189,7 +189,7 @@ function page_optimize_build_output() { // Removes source mapping URLs as they are unnecessary after concatenation if ( false !== strpos( $buf, 'sourceMappingURL' ) ) { - $buf = preg_replace('/(\/\*# sourceMappingURL=.+\.map \*\/)/', "" , $buf ); + $buf = preg_replace('/\/\*# sourceMappingURL=.+\.map \*\//', "" , $buf ); } if ( 'text/css' == $mime_type ) { From 71dae4d81c88d3ceef0b59446e01772bdc599c99 Mon Sep 17 00:00:00 2001 From: Erik Yo Date: Mon, 27 Dec 2021 11:57:37 +0100 Subject: [PATCH 14/15] concat by default and fixes sorting for inline --- concat-css.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/concat-css.php b/concat-css.php index 01677cb..f76864e 100644 --- a/concat-css.php +++ b/concat-css.php @@ -64,10 +64,10 @@ function do_items( $handles = false, $group = false ) { $css_url_parsed = parse_url( $obj->src ); $extra = $obj->extra; - // Only try to concat static css files - if ( false !== strpos( $css_url_parsed['path'], '.css' ) ) { - $do_concat = true; - } else { + // try to concat static css files + $do_concat = true; + + if ( 0 == strpos( $css_url_parsed['path'], '.css' ) ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { echo sprintf( "\n\n", esc_html( $handle ), esc_html( $obj->src ) ); From aba0c79e734a739490f62a0459defa3e8f08023d Mon Sep 17 00:00:00 2001 From: Erik Yo Date: Tue, 28 Dec 2021 00:06:43 +0100 Subject: [PATCH 15/15] sourcemaps: strip also this format -> '//# sourceMappingURL=scripts.js.map' https://github.com/Automattic/page-optimize/issues/53 --- service.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service.php b/service.php index 34ef78f..99a4615 100644 --- a/service.php +++ b/service.php @@ -189,7 +189,7 @@ function page_optimize_build_output() { // Removes source mapping URLs as they are unnecessary after concatenation if ( false !== strpos( $buf, 'sourceMappingURL' ) ) { - $buf = preg_replace('/\/\*# sourceMappingURL=.+\.map \*\//', "" , $buf ); + $buf = preg_replace('/\/[\/*].*sourceMapping.+/', "" , $buf ); } if ( 'text/css' == $mime_type ) {