W3TCMinify_Extract

The W3 Total Cache W3TC Minify Extract class.

Defined (1)

The class is defined in the following location(s).

/Minify_Extract.php  
  1. class Minify_Extract { 
  2. /** 
  3. * Extracts JS files from content 
  4. * w3tc-url-escaping: When rendered, URLs need to be escaped via 
  5. * htmlspecialchars instead of esc_attr to not change the way it is encoded 
  6. * in source html. E.g. html contains a&b Using esc_attr that will not 
  7. * double escape it as a result config value will be a&b. 
  8. * @param string $content 
  9. * @return array 
  10. */ 
  11. static public function extract_js( $content ) { 
  12. $matches = null; 
  13. $files = array(); 
  14.  
  15. $content = preg_replace( '~<!--.*?-->~s', '', $content ); 
  16.  
  17. if ( preg_match_all( '~<script\s+[^<>]*src=["\']?([^"\']+)["\']?[^<>]*>\s*</script>~is',  
  18. $content, $matches ) ) { 
  19. $files = $matches[1]; 
  20.  
  21. $files = array_values( array_unique( $files ) ); 
  22.  
  23. return $files; 
  24.  
  25. /** 
  26. * Extract CSS files from content 
  27. * @param string $content 
  28. * @return array 
  29. */ 
  30. static public function extract_css( $content ) { 
  31. $content = preg_replace( '~<!--.*?-->~s', '', $content ); 
  32.  
  33. $tags_files = array(); 
  34.  
  35. $matches = null; 
  36. if ( preg_match_all( '~<link\s+([^>]+)/?>(.*</link>)?~Uis', $content,  
  37. $matches, PREG_SET_ORDER ) ) { 
  38. foreach ( $matches as $match ) { 
  39. $attrs = array(); 
  40. $attr_matches = null; 
  41. if ( preg_match_all( '~(\w+)=["\']([^"\']*)["\']~', $match[1],  
  42. $attr_matches, PREG_SET_ORDER ) ) { 
  43. foreach ( $attr_matches as $attr_match ) { 
  44. $attrs[$attr_match[1]] = trim( $attr_match[2] ); 
  45.  
  46. if ( isset( $attrs['href'] ) && isset( $attrs['rel'] ) && 
  47. stristr( $attrs['rel'], 'stylesheet' ) !== false && 
  48. ( !isset( $attrs['media'] ) || stristr( $attrs['media'], 'print' ) === false ) ) { 
  49. $tags_files[] = array( $match[0], $attrs['href'] ); 
  50.  
  51.  
  52. if ( preg_match_all( '~@import\s+(url\s*)?\(?["\']?\s*([^"\'\)\s]+)\s*["\']?\)?[^;]*;?~is',  
  53. $content, $matches, PREG_SET_ORDER ) ) { 
  54. foreach ( $matches as $match ) 
  55. $tags_files[] = array( $match[0], $match[2] ); 
  56.  
  57. return $tags_files;