'text/css', 'js' => 'application/javascript', ); /* Constants */ // By default determine the document root from this scripts path in the plugins dir (you can hardcode this define) define( 'CONCAT_FILES_ROOT', substr( dirname( __DIR__ ), 0, strpos( dirname( __DIR__ ), '/wp-content' ) ) ); define( 'POWERED_CACHE_FO_CACHE_DIR', CONCAT_FILES_ROOT . '/wp-content/cache/min/' ); if ( ! file_exists( POWERED_CACHE_FO_CACHE_DIR ) ) { mkdir( POWERED_CACHE_FO_CACHE_DIR, 0775, true ); } function concat_http_status_exit( $status ) { switch ( $status ) { case 200: $text = 'OK'; break; case 400: $text = 'Bad Request'; break; case 403: $text = 'Forbidden'; break; case 404: $text = 'Not found'; break; case 500: $text = 'Internal Server Error'; break; default: $text = ''; } $protocol = $_SERVER['SERVER_PROTOCOL']; if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol ) { $protocol = 'HTTP/1.0'; } @header( "$protocol $status $text", true, $status ); exit(); } function concat_get_mtype( $file ) { global $concat_types; $lastdot_pos = strrpos( $file, '.' ); if ( false === $lastdot_pos ) { return false; } $ext = substr( $file, $lastdot_pos + 1 ); return isset( $concat_types[ $ext ] ) ? $concat_types[ $ext ] : false; } function concat_get_path( $uri ) { if ( ! strlen( $uri ) ) { concat_http_status_exit( 400 ); } if ( false !== strpos( $uri, '..' ) || false !== strpos( $uri, "\0" ) ) { concat_http_status_exit( 400 ); } return CONCAT_FILES_ROOT . ( '/' != $uri[0] ? '/' : '' ) . $uri; } function relative_path_replace( $buf, $dirpath ) { // url(relative/path/to/file) -> url(/absolute/and/not/relative/path/to/file) $buf = preg_replace( '/(:?\s*url\s*\()\s*(?:\'|")?\s*([^\/\'"\s\)](?:(? $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. $subdir_path_prefix = ''; $request_path = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ); $_static_index = strpos( $request_path, '/_static/' ); if ( $_static_index > 0 ) { $subdir_path_prefix = substr( $request_path, 0, $_static_index ); } unset( $request_path, $_static_index ); $last_modified = 0; $pre_output = ''; $output = ''; $do_minify = (bool) stripos( $_SERVER['REQUEST_URI'], 'minify=1' ); $hash = sha1( $_SERVER['REQUEST_URI'] ); $latest_file = end( $args ); $cache_file_name = POWERED_CACHE_FO_CACHE_DIR . $hash; if ( 'application/javascript' == concat_get_mtype( $latest_file ) ) { $cache_file_name .= '.js'; } elseif ( 'text/css' == concat_get_mtype( $latest_file ) ) { $cache_file_name .= '.css'; } if ( file_exists( $cache_file_name ) ) { $buf = @file_get_contents( $cache_file_name ); if ( $buf ) { $stat = stat( $cache_file_name ); header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $stat['mtime'] ) . ' GMT' ); header( 'Content-Length: ' . ( strlen( $pre_output ) + strlen( $buf ) ) ); header( 'Content-Type: ' . concat_get_mtype( $latest_file ) ); echo $pre_output . $buf; exit; } } foreach ( $args as $uri ) { $fullpath = concat_get_path( $uri ); if ( ! file_exists( $fullpath ) ) { concat_http_status_exit( 404 ); } $mime_type = concat_get_mtype( $fullpath ); if ( ! in_array( $mime_type, $concat_types ) ) { concat_http_status_exit( 400 ); } if ( $concat_unique ) { if ( ! isset( $last_mime_type ) ) { $last_mime_type = $mime_type; } if ( $last_mime_type != $mime_type ) { concat_http_status_exit( 400 ); } } $stat = stat( $fullpath ); if ( false === $stat ) { concat_http_status_exit( 500 ); } if ( $stat['mtime'] > $last_modified ) { $last_modified = $stat['mtime']; } $buf = file_get_contents( $fullpath ); if ( false === $buf ) { concat_http_status_exit( 500 ); } if ( 'text/css' == $mime_type ) { $dirpath = $subdir_path_prefix . dirname( $uri ); // url(relative/path/to/file) -> url(/absolute/and/not/relative/path/to/file) $buf = relative_path_replace( $buf, $dirpath ); // AlphaImageLoader(...src='relative/path/to/file'...) -> AlphaImageLoader(...src='/absolute/path/to/file'...) $buf = preg_replace( '/(Microsoft.AlphaImageLoader\s*\([^\)]*src=(?:\'|")?)([^\/\'"\s\)](?:(?@charset\s+[\'"][^\'"]+[\'"];)/i', function ( $match ) { global $pre_output; if ( 0 === strpos( $pre_output, '@charset' ) ) { return ''; } $pre_output = $match[0] . "\n" . $pre_output; return ''; }, $buf ); } // Move the @import rules on top of the concatenated output. // Only @charset rule are allowed before them. if ( false !== strpos( $buf, '@import' ) ) { $buf = preg_replace_callback( '/(?P@import\s+(?:url\s*\()?[\'"\s]*)(?P[^\'"\s](?:https?:\/\/.+\/?)?.+?)(?P[\'"\s\)]*;)/i', function ( $match ) use ( $dirpath ) { global $pre_output; if ( 0 !== strpos( $match['path'], 'http' ) && '/' != $match['path'][0] ) { $pre_output .= $match['pre_path'] . ( $dirpath == '/' ? '/' : $dirpath . '/' ) . $match['path'] . $match['post_path'] . "\n"; } else { $pre_output .= $match[0] . "\n"; } return ''; }, $buf ); } } if ( 'application/javascript' == $mime_type ) { $output .= "$buf;\n"; } else { $output .= "$buf"; } } if ( $do_minify && false === stripos( $uri, '.min' ) ) { if ( 'application/javascript' == $mime_type ) { $js_minify = new JS( $output ); $output = $js_minify->minify(); } elseif ( 'text/css' == $mime_type ) { $css_minify = new CSS( $output ); $output = $css_minify->minify(); } } header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $last_modified ) . ' GMT' ); header( 'Content-Length: ' . ( strlen( $pre_output ) + strlen( $output ) ) ); header( "Content-Type: $mime_type" ); echo $pre_output . $output; $cache_file_name = POWERED_CACHE_FO_CACHE_DIR . $hash; if ( 'application/javascript' == $mime_type ) { $cache_file_name .= '.js'; } elseif ( 'text/css' == $mime_type ) { $cache_file_name .= '.css'; } file_put_contents( $cache_file_name, $pre_output . $output );