From b05d417631e0bb5e88b9f7fc40f2103bfae45f39 Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Fri, 23 Feb 2024 21:39:54 +0200 Subject: [PATCH] For CSS the order is important (#118) Adding all after styles after the bundle breaks the order of styles so we keep them in the bundle instead. --- minit.php | 2 +- src/minit-assets.php | 5 ++++- src/minit-css.php | 20 +++++++------------- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/minit.php b/minit.php index 6b45ef3..1f1266d 100644 --- a/minit.php +++ b/minit.php @@ -4,7 +4,7 @@ Plugin URI: https://github.com/kasparsd/minit GitHub URI: https://github.com/kasparsd/minit Description: Combine JS and CSS files and serve them from the uploads folder. -Version: 1.4.1 +Version: 1.5.0 Author: Kaspars Dambis Author URI: https://kaspars.net */ diff --git a/src/minit-assets.php b/src/minit-assets.php index 49b3501..35525eb 100644 --- a/src/minit-assets.php +++ b/src/minit-assets.php @@ -49,6 +49,9 @@ function register( $todo ) { /** * Get a cache key for a set of assets for the current request. * + * TODO: Account for before and after values being included + * in the bundle in case of CSS. + * * @param array $handles List of asset handle strings. * * @return string @@ -61,7 +64,7 @@ public function cache_key( $handles = array() ) { 'minit_cache_ver-' . $this->file_cache()->version(), // Use a global cache version key to purge cache. ); - // Include individual scripts versions in the cache key + // Include individual scripts versions in the cache key. foreach ( $handles as $handle ) { $ver[] = sprintf( '%s-%s', $handle, $this->handler->registered[ $handle ]->ver ); } diff --git a/src/minit-css.php b/src/minit-css.php index 59ad068..3dc0cc4 100644 --- a/src/minit-css.php +++ b/src/minit-css.php @@ -55,23 +55,17 @@ public function process( $todo ) { // Add our Minit style since wp_enqueue_script won't do it at this point $todo[] = self::ASSET_HANDLE; - // Add inline styles for all minited styles - foreach ( $this->done as $script ) { - // Can this return an array instead? - $inline_styles = $this->handler->get_data( $script, 'after' ); - - if ( ! empty( $inline_styles ) ) { - $this->handler->add_inline_style( - self::ASSET_HANDLE, - implode( "\n", $inline_styles ) - ); - } - } - return $todo; } public function minit_item( $content, $handle, $src ) { + // Append all inline styles right after to preserve order. + $inline_styles = $this->handler->get_data( $handle, 'after' ); + + if ( ! empty( $inline_styles ) ) { + $content .= implode( "\n", $inline_styles ); + } + if ( empty( $content ) ) { return $content; }