Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter local path from URL - second try #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions include/minit-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,19 @@ function minit() {
continue;
}

// Get the relative URL of the asset
$src = $this->get_asset_relative_path( $handle );
// Get the absolute local path of the asset
$local_path = $this->get_asset_local_path( $handle );

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

$item = $this->minit_item( file_get_contents( ABSPATH . $src ), $handle, $src );
$item = $this->minit_item(
file_get_contents( $local_path ),
$handle,
$this->handler->registered[ $handle ]->src
);

$item = apply_filters(
'minit-item-' . $this->extension,
Expand Down Expand Up @@ -178,11 +182,11 @@ abstract function process( $todo );
*
* @param string $source Asset source
* @param string $handle Asset handle
* @param string $src Relative URL of the asset
* @param string $url Absolute URL of the asset
*
* @return string Asset source
*/
function minit_item( $source, $handle, $src ) {
function minit_item( $source, $handle, $url ) {

return $source;

Expand Down Expand Up @@ -212,13 +216,13 @@ protected function mark_done( $handles ) {


/**
* Return asset URL relative to the `base_url`.
* Return asset's absolute local path.
*
* @param string $handle Asset handle
*
* @return string|boolean Asset URL relative to the base URL or `false` if not found
* @return string|boolean Asset absolute local path or `false` if not found
*/
protected function get_asset_relative_path( $handle ) {
protected function get_asset_local_path( $handle ) {

if ( ! isset( $this->handler->registered[ $handle ] ) ) {
return false;
Expand All @@ -230,6 +234,17 @@ protected function get_asset_relative_path( $handle ) {
return false;
}

$local_path = $this->get_local_path_from_url( $item_url );

$local_path = apply_filters( 'minit-asset-local-path', $local_path, $item_url, $this->handler->base_url );

return $local_path;

}


protected function get_local_path_from_url( $item_url ) {

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

Expand All @@ -244,7 +259,7 @@ protected function get_asset_relative_path( $handle ) {
$maybe_relative = array_pop( $src_parts );

if ( file_exists( ABSPATH . $maybe_relative ) ) {
return $maybe_relative;
return ABSPATH . $maybe_relative;
}

return false;
Expand Down
18 changes: 9 additions & 9 deletions include/minit-css.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,27 @@ function process( $todo ) {
}


function minit_item( $content, $handle, $src ) {
function minit_item( $content, $handle, $url ) {

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

// Exclude styles with media queries from being included in Minit
$content = $this->exclude_with_media_query( $content, $handle, $src );
$content = $this->exclude_with_media_query( $content, $handle );

// Make all asset URLs absolute
$content = $this->resolve_urls( $content, $handle, $src );
$content = $this->resolve_urls( $content, $handle, $url );

// Add support for relative CSS imports
$content = $this->resolve_imports( $content, $handle, $src );
$content = $this->resolve_imports( $content, $handle, $url );

return $content;

}


private function resolve_urls( $content, $handle, $src ) {
private function resolve_urls( $content, $handle, $url ) {

if ( ! $content ) {
return $content;
Expand All @@ -86,7 +86,7 @@ private function resolve_urls( $content, $handle, $src ) {
// Make all local asset URLs absolute
$content = preg_replace(
'/url\(["\' ]?+(?!data:|https?:|\/\/)(.*?)["\' ]?\)/i',
sprintf( "url('%s/$1')", $this->handler->base_url . dirname( $src ) ),
sprintf( "url('%s/$1')", dirname( $url ) ),
$content
);

Expand All @@ -95,7 +95,7 @@ private function resolve_urls( $content, $handle, $src ) {
}


private function resolve_imports( $content, $handle, $src ) {
private function resolve_imports( $content, $handle, $url ) {

if ( ! $content ) {
return $content;
Expand All @@ -104,7 +104,7 @@ private function resolve_imports( $content, $handle, $src ) {
// Make all import asset URLs absolute
$content = preg_replace(
'/@import\s+(url\()?["\'](?!https?:|\/\/)(.*?)["\'](\)?)/i',
sprintf( "@import url('%s/$2')", $this->handler->base_url . dirname( $src ) ),
sprintf( "@import url('%s/$2')", dirname( $url ) ),
$content
);

Expand All @@ -113,7 +113,7 @@ private function resolve_imports( $content, $handle, $src ) {
}


private function exclude_with_media_query( $content, $handle, $src ) {
private function exclude_with_media_query( $content, $handle ) {

if ( ! $content ) {
return $content;
Expand Down
2 changes: 1 addition & 1 deletion include/minit-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function print_async_scripts() {
continue;
}

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

if ( ! $script_relative_path ) {
// Add this script to our async queue
Expand Down