diff --git a/cssconcat.php b/cssconcat.php index 24b21e8..3907c7f 100644 --- a/cssconcat.php +++ b/cssconcat.php @@ -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; @@ -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; @@ -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) { @@ -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 ); } @@ -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}"; @@ -193,3 +226,4 @@ function css_concat_init() { } add_action( 'init', 'css_concat_init' ); + diff --git a/jsconcat.php b/jsconcat.php index ab6d5b1..64b9431 100644 --- a/jsconcat.php +++ b/jsconcat.php @@ -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; @@ -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; @@ -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; @@ -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; } @@ -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; @@ -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'] ); @@ -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 ); } @@ -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}"; diff --git a/ngx-http-concat.php b/ngx-http-concat.php index 851f06c..4692478 100644 --- a/ngx-http-concat.php +++ b/ngx-http-concat.php @@ -60,8 +60,9 @@ function concat_get_mtype( $file ) { global $concat_types; $lastdot_pos = strrpos( $file, '.' ); - if ( false === $lastdot_pos ) + if ( false === $lastdot_pos ) { return false; + } $ext = substr( $file, $lastdot_pos + 1 ); @@ -69,25 +70,29 @@ function concat_get_mtype( $file ) { } function concat_get_path( $uri ) { - if ( ! strlen( $uri ) ) + if ( ! strlen( $uri ) ) { concat_http_status_exit( 400 ); + } - if ( false !== strpos( $uri, '..' ) || false !== strpos( $uri, "\0" ) ) + if ( false !== strpos( $uri, '..' ) || false !== strpos( $uri, "\0" ) ) { concat_http_status_exit( 400 ); + } return CONCAT_FILES_ROOT . ( '/' != $uri[0] ? '/' : '' ) . $uri; } /* Main() */ -if ( !in_array( $_SERVER['REQUEST_METHOD'], array( 'GET', 'HEAD' ) ) ) +if ( !in_array( $_SERVER['REQUEST_METHOD'], array( 'GET', 'HEAD' ) ) ) { concat_http_status_exit( 400 ); +} // /_static/??/foo/bar.css,/foo1/bar/baz.css?m=293847g // or // /_static/??-eJzTT8vP109KLNJLLi7W0QdyDEE8IK4CiVjn2hpZGluYmKcDABRMDPM= $args = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY ); -if ( ! $args || false === strpos( $args, '?' ) ) +if ( ! $args || false === strpos( $args, '?' ) ) { concat_http_status_exit( 400 ); +} $args = substr( $args, strpos( $args, '?' ) + 1 ); @@ -105,17 +110,20 @@ function concat_get_path( $uri ) { // /foo/bar.css,/foo1/bar/baz.css?m=293847g $version_string_pos = strpos( $args, '?' ); -if ( false !== $version_string_pos ) +if ( false !== $version_string_pos ) { $args = substr( $args, 0, $version_string_pos ); +} // /foo/bar.css,/foo1/bar/baz.css $args = explode( ',', $args ); -if ( ! $args ) +if ( ! $args ) { concat_http_status_exit( 400 ); +} // array( '/foo/bar.css', '/foo1/bar/baz.css' ) -if ( 0 == count( $args ) || count( $args ) > $concat_max_files ) +if ( 0 == count( $args ) || count( $args ) > $concat_max_files ) { concat_http_status_exit( 400 ); +} // If we're in a subdirectory context, use that as the root. // We can't assume that the root serves the same content as the subdir. @@ -136,31 +144,38 @@ function concat_get_path( $uri ) { foreach ( $args as $uri ) { $fullpath = concat_get_path( $uri ); - if ( ! file_exists( $fullpath ) ) + if ( ! file_exists( $fullpath ) ) { concat_http_status_exit( 404 ); + } $mime_type = concat_get_mtype( $fullpath ); - if ( ! in_array( $mime_type, $concat_types ) ) + if ( ! in_array( $mime_type, $concat_types ) ) { concat_http_status_exit( 400 ); + } if ( $concat_unique ) { - if ( ! isset( $last_mime_type ) ) + if ( ! isset( $last_mime_type ) ) { $last_mime_type = $mime_type; + } - if ( $last_mime_type != $mime_type ) + if ( $last_mime_type != $mime_type ) { concat_http_status_exit( 400 ); + } } $stat = stat( $fullpath ); - if ( false === $stat ) + if ( false === $stat ) { concat_http_status_exit( 500 ); + } - if ( $stat['mtime'] > $last_modified ) + if ( $stat['mtime'] > $last_modified ) { $last_modified = $stat['mtime']; + } $buf = file_get_contents( $fullpath ); - if ( false === $buf ) + if ( false === $buf ) { concat_http_status_exit( 500 ); + } if ( 'text/css' == $mime_type ) { $dirpath = $subdir_path_prefix . dirname( $uri ); @@ -201,11 +216,12 @@ function ( $match ) { function ( $match ) use ( $dirpath ) { global $pre_output; - if ( 0 !== strpos( $match['path'], 'http' ) && '/' != $match['path'][0] ) + if ( 0 !== strpos( $match['path'], 'http' ) && '/' != $match['path'][0] ) { $pre_output .= $match['pre_path'] . ( $dirpath == '/' ? '/' : $dirpath . '/' ) . $match['path'] . $match['post_path'] . "\n"; - else + } else { $pre_output .= $match[0] . "\n"; + } return ''; }, @@ -216,10 +232,11 @@ function ( $match ) use ( $dirpath ) { $buf = $css_minify->run( $buf ); } - if ( 'application/javascript' == $mime_type ) + if ( 'application/javascript' == $mime_type ) { $output .= "$buf;\n"; - else + } else { $output .= "$buf"; + } } header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $last_modified ) . ' GMT' );