compress.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * Compression functions.
  4. *
  5. * @package Pen
  6. */
  7. if ( ! function_exists( 'pen_compress_css' ) ) {
  8. /**
  9. * Removes empty lines and white space from CSS.
  10. *
  11. * @param string $input The input CSS.
  12. *
  13. * @since Pen 1.0.0
  14. * @return string
  15. */
  16. function pen_compress_css( $input ) {
  17. // Removes blank lines.
  18. $output = preg_replace( '/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/', "\n", trim( $input ) );
  19. // Removes comments.
  20. $output = preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $output );
  21. // Removes indentation.
  22. $trimmed = array();
  23. $output = explode( "\n", $output );
  24. foreach ( $output as $output ) {
  25. $trimmed[] = trim( $output );
  26. }
  27. $output = implode( $trimmed );
  28. $search = array( '{ ', ' }', '; ', ', ', ' {', '} ', ': ', ' ,', ' ;', ';}' );
  29. $replace = array( '{', '}', ';', ',', '{', '}', ':', ',', ';', '}' );
  30. $output = str_replace( $search, $replace, $output );
  31. return $output;
  32. }
  33. }
  34. if ( ! function_exists( 'pen_compress_html' ) ) {
  35. /**
  36. * Compresses HTML code.
  37. *
  38. * @param string $input The input.
  39. *
  40. * @since Pen 1.0.0
  41. * @return string
  42. */
  43. function pen_compress_html( $input ) {
  44. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
  45. return $input;
  46. }
  47. $search = array( '/\>[^\S ]+/s', '/[^\S ]+\</s', '/(\s)+/s' );
  48. $replace = array( '>', '<', '\\1' );
  49. $output = preg_replace( $search, $replace, $input );
  50. $output = preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $output );
  51. $output = trim( $output );
  52. return $output;
  53. }
  54. }