Skip to content
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
59 changes: 34 additions & 25 deletions concat-css.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand All @@ -63,23 +64,27 @@ 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<!-- No Concat CSS %s => Maybe Not Static File %s -->\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)
if ( isset( $extra['conditional'] ) ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
echo sprintf( "\n<!-- No Concat CSS %s => Has Conditional -->\n", esc_html( $handle ) );
}

$style_group['conditional-'. $style_index++ ] = $handle;
$do_concat = false;
}

Expand All @@ -88,6 +93,8 @@ function do_items( $handles = false, $group = false ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
echo sprintf( "\n<!-- No Concat CSS %s => Is RTL -->\n", esc_html( $handle ) );
}

$style_group['rtl-'. $style_index++ ] = $handle;
$do_concat = false;
}

Expand All @@ -97,6 +104,8 @@ function do_items( $handles = false, $group = false ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
echo sprintf( "\n<!-- No Concat CSS %s => External URL: %s -->\n", esc_html( $handle ), esc_url( $css_url ) );
}

$style_group['external-'. $style_index++ ] = $handle;
$do_concat = false;
}

Expand All @@ -107,6 +116,8 @@ function do_items( $handles = false, $group = false ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
echo sprintf( "\n<!-- No Concat CSS %s => Invalid Path %s -->\n", esc_html( $handle ), esc_html( $css_realpath ) );
}

$style_group['invalid-'. $style_index++ ] = $handle;
$do_concat = false;
}
}
Expand All @@ -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<!-- No Concat CSS %s => Excluded option -->\n", esc_html( $handle ) );
}

$style_group['excluded-'. $style_index++ ] = $handle;
$do_concat = false;
}
}

Expand All @@ -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 );
Expand All @@ -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 );
}
Expand Down
5 changes: 3 additions & 2 deletions concat-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 );
}
Expand Down
15 changes: 14 additions & 1 deletion dependency-path-mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion page-optimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
7 changes: 6 additions & 1 deletion service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down