Skip to content

Commit

Permalink
Account for block styles with relative CSS URLs (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasparsd authored Sep 10, 2024
1 parent b05d417 commit 504c39d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 103 deletions.
2 changes: 1 addition & 1 deletion minit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.5.0
Version: 1.6.0
Author: Kaspars Dambis
Author URI: https://kaspars.net
*/
Expand Down
68 changes: 29 additions & 39 deletions src/minit-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,32 +108,19 @@ function minit() {
continue;
}

// Ignore pseudo packages such as jquery which return src as empty string.
if ( empty( $this->handler->registered[ $handle ]->src ) ) {
$done[ $handle ] = null;

continue;
}

// Get the relative URL of the asset.
$src = $this->get_asset_relative_path( $handle );

// Skip if the file is not hosted locally.
if ( empty( $src ) || ! file_exists( ABSPATH . $src ) ) {
continue;
}

$item = $this->minit_item( file_get_contents( ABSPATH . $src ), $handle, $src );

$item = apply_filters(
'minit-item-' . $this->extension,
$item,
$this->handler,
$handle
);

if ( false !== $item ) {
$done[ $handle ] = $item;
// Ignore pseudo packages such as jquery which return src as empty string.
if ( ! empty( $src ) && is_readable( ABSPATH . $src ) ) {
$item = $this->minit_item( file_get_contents( ABSPATH . $src ), $handle, $src );

$done[ $handle ] = apply_filters(
'minit-item-' . $this->extension,
$item,
$this->handler,
$handle
);
}
}

Expand Down Expand Up @@ -243,27 +230,30 @@ protected function get_asset_relative_path( $handle ) {
return false;
}

$item_url = $this->handler->registered[ $handle ]->src;
if ( ! empty( $this->handler->registered[ $handle ]->src ) ) {
$item_url = $this->handler->registered[ $handle ]->src;

if ( empty( $item_url ) ) {
return false;
}
// Inline block scripts are sometimes relative URLs.
if ( 0 === strpos( $item_url, '/' ) ) {
return $item_url;
}

// Remove protocol reference from the local base URL
$base_url = preg_replace( '/^(https?:)/i', '', $this->handler->base_url );
// Remove protocol reference from the local base URL
$base_url = preg_replace( '/^(https?:)/i', '', $this->handler->base_url );

// Check if this is a local asset which we can include
$src_parts = explode( $base_url, $item_url );
// Check if this is a local asset which we can include
$src_parts = explode( $base_url, $item_url );

if ( empty( $src_parts ) ) {
return false;
}

// Get the trailing part of the local URL
$maybe_relative = array_pop( $src_parts );
// Get the trailing part of the local URL
if ( ! empty( $src_parts ) ) {
return array_pop( $src_parts );
}
} elseif ( ! empty( $this->handler->registered[ $handle ]->extra[ 'path' ] ) ) {
$item_path = $this->handler->registered[ $handle ]->extra[ 'path' ];

if ( file_exists( ABSPATH . $maybe_relative ) ) {
return $maybe_relative;
if ( 0 === strpos( $item_path, ABSPATH ) ) {
return str_replace( ABSPATH, '', $item_path );
}
}

return false;
Expand Down
63 changes: 0 additions & 63 deletions src/minit-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ public function init() {
// Print our JS file
add_filter( 'print_scripts_array', array( $this, 'process' ), 20 );

// Print external scripts asynchronously in the footer
add_action( 'wp_print_footer_scripts', array( $this, 'print_async_scripts' ), 20 );

// Load our JS files asynchronously
add_filter( 'script_loader_tag', array( $this, 'script_tag_async' ), 20, 3 );
}
Expand Down Expand Up @@ -126,66 +123,6 @@ protected function get_script_data( $handles, $keys ) {
return $extra;
}


public function print_async_scripts() {
$async_queue = array();

$minit_exclude = (array) apply_filters(
'minit-exclude-js',
array()
);

foreach ( $this->handler->queue as $handle ) {
// Skip asyncing explicitly excluded script handles
if ( in_array( $handle, $minit_exclude, true ) ) {
continue;
}

$script_relative_path = $this->get_asset_relative_path( $handle );

if ( ! $script_relative_path ) {
// Add this script to our async queue
$async_queue[] = $handle;
}
}

if ( empty( $async_queue ) ) {
return;
}

$scripts = array();

foreach ( $async_queue as $handle ) {
$scripts[] = array(
'id' => 'async-script-' . sanitize_key( $handle ),
'src' => $this->handler->registered[ $handle ]->src,
);
}

?>
<!-- Asynchronous scripts by Minit -->
<script id="minit-async-scripts" type="text/javascript">
(function() {
var scripts = <?php echo wp_json_encode( $scripts ); ?>;
var fjs = document.getElementById( 'minit-async-scripts' );

function minitLoadScript( url, id ) {
var js = document.createElement('script');
js.type = 'text/javascript';
js.src = url;
js.async = true;
js.id = id;
fjs.parentNode.insertBefore(js, fjs);
};

scripts.map( function( script ) {
minitLoadScript( script.src, script.id );
} );
})();
</script>
<?php
}

/**
* Check if the script has any "after" logic defined.
*
Expand Down

0 comments on commit 504c39d

Please sign in to comment.