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
66 changes: 50 additions & 16 deletions cssconcat.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@

require_once( dirname( __FILE__ ) . '/concat-utils.php' );

if ( ! defined( 'ALLOW_GZIP_COMPRESSION' ) )
if ( ! defined( 'ALLOW_GZIP_COMPRESSION' ) ) {
define( 'ALLOW_GZIP_COMPRESSION', true );
}

class WPcom_CSS_Concat extends WP_Styles {
private $old_styles;
Expand Down Expand Up @@ -62,40 +63,57 @@ function do_items( $handles = false, $group = false ) {
$do_concat = false;

// Only try to concat static css files
if ( false !== strpos( $css_url_parsed['path'], '.css' ) )
if ( false !== strpos( $css_url_parsed['path'], '.css' ) ) {
$do_concat = true;
}

// Don't try to concat styles which are loaded conditionally (like IE stuff)
if ( isset( $extra['conditional'] ) )
if ( isset( $extra['conditional'] ) ) {
do_action( 'qm/vip_concat_info', sprintf( '[css] Skipped: %s. Reason: is conditional', $css_url ) );
$do_concat = false;
}

// Don't concat rtl stuff for now until concat supports it correctly
if ( 'rtl' === $this->text_direction && ! empty( $extra['rtl'] ) )
if ( 'rtl' === $this->text_direction && ! empty( $extra['rtl'] ) ) {
do_action( 'qm/vip_concat_info', sprintf( '[css] Skipped: %s. Reason: is rtl', $css_url ) );
$do_concat = false;
}

// Don't try to concat externally hosted scripts
$is_internal_url = WPCOM_Concat_Utils::is_internal_url( $css_url, $siteurl );
if ( ! $is_internal_url ) {
do_action( 'qm/vip_concat_info', sprintf( '[css] Skipped: %s. Reason: not internal url', $css_url ) );
$do_concat = false;
}

// Concat and canonicalize the paths only for
// existing scripts that aren't outside ABSPATH
$css_realpath = WPCOM_Concat_Utils::realpath( $css_url, $siteurl );
if ( ! $css_realpath || 0 !== strpos( $css_realpath, ABSPATH ) )
if ( ! $css_realpath || 0 !== strpos( $css_realpath, ABSPATH ) ) {
if ( $css_url ) {
// check for url reduces noise of inline styles
do_action( 'qm/vip_concat_info', sprintf( '[css] Skipped: %s. Reason: outside ABSPATH', $css_url ) );
}
$do_concat = false;
else
} else {
$css_url_parsed['path'] = substr( $css_realpath, strlen( ABSPATH ) - 1 );
}

// Allow plugins to disable concatenation of certain stylesheets.
$do_concat = apply_filters( 'css_do_concat', $do_concat, $handle );
$do_concat_after = apply_filters( 'css_do_concat', $do_concat, $handle );
if ( $do_concat && ! $do_concat_after ) {
do_action( 'qm/vip_concat_info', sprintf( '[css] Skipped: %s. Reason: filtered', $css_url ) );
}
$do_concat = $do_concat_after;

if ( true === $do_concat ) {
$media = $obj->args;
if( empty( $media ) )
if ( empty( $media ) ) {
$media = 'all';
if ( ! isset( $stylesheets[ $stylesheet_group_index ] ) || ( isset( $stylesheets[ $stylesheet_group_index ] ) && ! is_array( $stylesheets[ $stylesheet_group_index ] ) ) )
}
if ( ! isset( $stylesheets[ $stylesheet_group_index ] ) || ( isset( $stylesheets[ $stylesheet_group_index ] ) && ! is_array( $stylesheets[ $stylesheet_group_index ] ) ) ) {
$stylesheets[ $stylesheet_group_index ] = array();
}

$stylesheets[ $stylesheet_group_index ][ $media ][ $handle ] = $css_url_parsed['path'];
$this->done[] = $handle;
Expand All @@ -112,8 +130,9 @@ function do_items( $handles = false, $group = false ) {
if ( 'noconcat' == $media ) {

foreach( $css as $handle ) {
if ( $this->do_item( $handle, $group ) )
if ( $this->do_item( $handle, $group ) ) {
$this->done[] = $handle;
}
}
continue;
} elseif ( count( $css ) > 1) {
Expand All @@ -123,12 +142,21 @@ function do_items( $handles = false, $group = false ) {

if ( $this->allow_gzip_compression ) {
$path_64 = base64_encode( gzcompress( $path_str ) );
if ( strlen( $path_str ) > ( strlen( $path_64 ) + 1 ) )
if ( strlen( $path_str ) > ( strlen( $path_64 ) + 1 ) ) {
$path_str = '-' . $path_64;
}
}

$count = count( $paths );
if ( $count > 150 ) {
do_action( 'qm/vip_concat_warn', sprintf( '[css] TOO LARGE Bundle %s contains %d items: %s', $path_str, $count, "\n" . implode("\n", $paths ) ) );
} else {
do_action( 'qm/vip_concat_info', sprintf( '[css] Bundle %s contains %d items: %s', $path_str, $count, "\n" . implode("\n", $paths ) ) );
}

$href = $siteurl . "/_static/??" . $path_str;
} else {
do_action( 'qm/vip_concat_info', sprintf( '[css] Skipped: %s. Reason: lonely (nothing to concatenate with)', implode( $css ) ) );
$href = $this->cache_bust_mtime( $siteurl . current( $css ), $siteurl );
}

Expand All @@ -141,28 +169,33 @@ function do_items( $handles = false, $group = false ) {
}

function cache_bust_mtime( $url, $siteurl ) {
if ( strpos( $url, '?m=' ) )
if ( strpos( $url, '?m=' ) ) {
return $url;
}

$parts = parse_url( $url );
if ( ! isset( $parts['path'] ) || empty( $parts['path'] ) )
if ( ! isset( $parts['path'] ) || empty( $parts['path'] ) ) {
return $url;
}

$file = WPCOM_Concat_Utils::realpath( $url, $siteurl );

$mtime = false;
if ( file_exists( $file ) )
if ( file_exists( $file ) ) {
$mtime = filemtime( $file );
}

if ( ! $mtime )
if ( ! $mtime ) {
return $url;
}

if ( false === strpos( $url, '?' ) ) {
$q = '';
} else {
list( $url, $q ) = explode( '?', $url, 2 );
if ( strlen( $q ) )
if ( strlen( $q ) ) {
$q = '&' . $q;
}
}

return "$url?m={$mtime}g{$q}";
Expand Down Expand Up @@ -193,3 +226,4 @@ function css_concat_init() {
}

add_action( 'init', 'css_concat_init' );

58 changes: 43 additions & 15 deletions jsconcat.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@

require_once( dirname( __FILE__ ) . '/concat-utils.php' );

if ( ! defined( 'ALLOW_GZIP_COMPRESSION' ) )
if ( ! defined( 'ALLOW_GZIP_COMPRESSION' ) ) {
define( 'ALLOW_GZIP_COMPRESSION', true );
}

class WPcom_JS_Concat extends WP_Scripts {
private $old_scripts;
Expand Down Expand Up @@ -64,8 +65,9 @@ function do_items( $handles = false, $group = false ) {
$level = 0;

foreach( $this->to_do as $key => $handle ) {
if ( in_array( $handle, $this->done ) || !isset( $this->registered[$handle] ) )
if ( in_array( $handle, $this->done ) || !isset( $this->registered[$handle] ) ) {
continue;
}

if ( 0 === $group && $this->groups[$handle] > 0 ) {
$this->in_footer[] = $handle;
Expand All @@ -81,8 +83,9 @@ function do_items( $handles = false, $group = false ) {
continue;
}

if ( false === $group && in_array( $handle, $this->in_footer, true ) )
if ( false === $group && in_array( $handle, $this->in_footer, true ) ) {
$this->in_footer = array_diff( $this->in_footer, (array) $handle );
}

$obj = $this->registered[$handle];
$js_url = $obj->src;
Expand All @@ -93,24 +96,29 @@ function do_items( $handles = false, $group = false ) {
$do_concat = false;

// Only try to concat static js files
if ( false !== strpos( $js_url_parsed['path'], '.js' ) )
if ( false !== strpos( $js_url_parsed['path'], '.js' ) ) {
$do_concat = true;
}

// Don't try to concat externally hosted scripts
$is_internal_url = WPCOM_Concat_Utils::is_internal_url( $js_url, $siteurl );
if ( ! $is_internal_url ) {
do_action( 'qm/vip_concat_info', sprintf( '[js] Skipped: %s. Reason: not internal URL', $js_url ) );
$do_concat = false;
}

// Concat and canonicalize the paths only for
// existing scripts that aren't outside ABSPATH
$js_realpath = WPCOM_Concat_Utils::realpath( $js_url, $siteurl );
if ( ! $js_realpath || 0 !== strpos( $js_realpath, ABSPATH ) )
if ( ! $js_realpath || 0 !== strpos( $js_realpath, ABSPATH ) ) {
do_action( 'qm/vip_concat_info', sprintf( '[js] Skipped: %s. Reason: outside ABSPATH', $js_url ) );
$do_concat = false;
else
} else {
$js_url_parsed['path'] = substr( $js_realpath, strlen( ABSPATH ) - 1 );
}

if ( $this->has_inline_content( $handle ) ) {
do_action( 'qm/vip_concat_info', sprintf( '[js] Skipped: %s. Reason: has inline content', $js_url ) );
$do_concat = false;
}

Expand All @@ -120,11 +128,16 @@ function do_items( $handles = false, $group = false ) {
}

// Allow plugins to disable concatenation of certain scripts.
$do_concat = apply_filters( 'js_do_concat', $do_concat, $handle );
$do_concat_after = apply_filters( 'js_do_concat', $do_concat, $handle );
if ( $do_concat && ! $do_concat_after ) {
do_action( 'qm/vip_concat_info', sprintf( '[js] Skipped: %s. Reason: filtered', $js_url ) );
}
$do_concat = $do_concat_after;

if ( true === $do_concat ) {
if ( !isset( $javascripts[$level] ) )
if ( !isset( $javascripts[$level] ) ) {
$javascripts[$level]['type'] = 'concat';
}

$javascripts[$level]['paths'][] = $js_url_parsed['path'];
$javascripts[$level]['handles'][] = $handle;
Expand All @@ -138,13 +151,15 @@ function do_items( $handles = false, $group = false ) {
unset( $this->to_do[$key] );
}

if ( empty( $javascripts ) )
if ( empty( $javascripts ) ) {
return $this->done;
}

foreach ( $javascripts as $js_array ) {
if ( 'do_item' == $js_array['type'] ) {
if ( $this->do_item( $js_array['handle'], $group ) )
if ( $this->do_item( $js_array['handle'], $group ) ) {
$this->done[] = $js_array['handle'];
}
} else if ( 'concat' == $js_array['type'] ) {
array_map( array( $this, 'print_extra_script' ), $js_array['handles'] );

Expand All @@ -159,8 +174,16 @@ function do_items( $handles = false, $group = false ) {
$path_str = '-' . $path_64;
}

$count = count( $paths );
if ( $count > 150 ) {
do_action( 'qm/vip_concat_warn', sprintf( '[js] TOO LARGE Bundle %s contains %d items: %s', $path_str, $count, "\n" . implode("\n", $paths ) ) );
} else {
do_action( 'qm/vip_concat_info', sprintf( '[js] Bundle %s contains %d items: %s', $path_str, $count, "\n" . implode("\n", $paths ) ) );
}

$href = $siteurl . "/_static/??" . $path_str;
} elseif ( isset( $js_array['paths'] ) && is_array( $js_array['paths'] ) ) {
do_action( 'qm/vip_concat_info', sprintf( '[js] Skipped: %s. Reason: lonely (nothing to concatenate with)', implode( $js_array['paths'] ) ) );
$href = $this->cache_bust_mtime( $siteurl . $js_array['paths'][0], $siteurl );
}

Expand Down Expand Up @@ -219,28 +242,33 @@ function do_items( $handles = false, $group = false ) {
}

function cache_bust_mtime( $url, $siteurl ) {
if ( strpos( $url, '?m=' ) )
if ( strpos( $url, '?m=' ) ) {
return $url;
}

$parts = parse_url( $url );
if ( ! isset( $parts['path'] ) || empty( $parts['path'] ) )
if ( ! isset( $parts['path'] ) || empty( $parts['path'] ) ) {
return $url;
}

$file = WPCOM_Concat_Utils::realpath( $url, $siteurl );

$mtime = false;
if ( file_exists( $file ) )
if ( file_exists( $file ) ) {
$mtime = filemtime( $file );
}

if ( ! $mtime )
if ( ! $mtime ) {
return $url;
}

if ( false === strpos( $url, '?' ) ) {
$q = '';
} else {
list( $url, $q ) = explode( '?', $url, 2 );
if ( strlen( $q ) )
if ( strlen( $q ) ) {
$q = '&' . $q;
}
}

return "$url?m={$mtime}g{$q}";
Expand Down
Loading
Loading