PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/www/wp-content/mu-plugins/http-concat/cssconcat.php

https://gitlab.com/Blueprint-Marketing/vip-quickstart
PHP | 193 lines | 133 code | 38 blank | 22 comment | 35 complexity | 46c9bfa069ec737aafaf84601a0a45a4 MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: CSS Concat
  4. Plugin URI: http://wp-plugins.org/#
  5. Description: Concatenates CSS
  6. Author: Automattic
  7. Version: 0.01
  8. Author URI: http://automattic.com/
  9. */
  10. if ( ! defined( 'ALLOW_GZIP_COMPRESSION' ) )
  11. define( 'ALLOW_GZIP_COMPRESSION', true );
  12. class WPcom_CSS_Concat extends WP_Styles {
  13. private $old_styles;
  14. public $allow_gzip_compression;
  15. function __construct( $styles ) {
  16. $this->old_styles = $styles;
  17. // Unset all the object properties except our private copy of the styles object.
  18. // We have to unset everything so that the overload methods talk to $this->old_styles->whatever
  19. // instead of $this->whatever.
  20. foreach ( array_keys( get_object_vars( $this ) ) as $key ) {
  21. if ( 'old_styles' === $key ) {
  22. continue;
  23. }
  24. unset( $this->$key );
  25. }
  26. parent::__construct();
  27. }
  28. function do_items( $handles = false, $group = false ) {
  29. $handles = false === $handles ? $this->queue : (array) $handles;
  30. $stylesheets = array();
  31. $siteurl = site_url();
  32. $this->all_deps( $handles );
  33. $stylesheet_group_index = 0;
  34. foreach( $this->to_do as $key => $handle ) {
  35. $obj = $this->registered[$handle];
  36. $obj->src = apply_filters( 'style_loader_src', $obj->src, $obj->handle );
  37. // Core is kind of broken and returns "true" for src of "colors" handle
  38. // http://core.trac.wordpress.org/attachment/ticket/16827/colors-hacked-fixed.diff
  39. // http://core.trac.wordpress.org/ticket/20729
  40. if ( 'colors' == $obj->handle && true === $obj->src ) {
  41. $css_url = parse_url( wp_style_loader_src( $obj->src, $obj->handle ) );
  42. } else {
  43. $css_url = parse_url( $obj->src );
  44. }
  45. $extra = $obj->extra;
  46. // Don't concat by default
  47. $do_concat = false;
  48. // Only try to concat static css files
  49. if ( false !== strpos( $css_url['path'], '.css' ) )
  50. $do_concat = true;
  51. // Don't try to concat styles which are loaded conditionally (like IE stuff)
  52. if ( isset( $extra['conditional'] ) )
  53. $do_concat = false;
  54. // Don't concat rtl stuff for now until concat supports it correctly
  55. if ( 'rtl' === $this->text_direction && ! empty( $extra['rtl'] ) )
  56. $do_concat = false;
  57. // Don't try to concat externally hosted scripts
  58. if ( ( isset( $css_url['host'] ) && ( preg_replace( '/https?:\/\//', '', $siteurl ) != $css_url['host'] ) ) )
  59. $do_concat = false;
  60. // Concat and canonicalize the paths only for
  61. // existing scripts that aren't outside ABSPATH
  62. $css_realpath = realpath( ABSPATH . $css_url['path'] );
  63. if ( ! $css_realpath || 0 !== strpos( $css_realpath, ABSPATH ) )
  64. $do_concat = false;
  65. else
  66. $css_url['path'] = substr( $css_realpath, strlen( ABSPATH ) - 1 );
  67. // Allow plugins to disable concatenation of certain stylesheets.
  68. $do_concat = apply_filters( 'css_do_concat', $do_concat, $handle );
  69. if ( true === $do_concat ) {
  70. $media = $obj->args;
  71. if ( empty( $media ) ) {
  72. $media = 'all';
  73. }
  74. if ( empty( $stylesheets[ $stylesheet_group_index ] ) || ! is_array( $stylesheets[ $stylesheet_group_index ] ) ) {
  75. $stylesheets[ $stylesheet_group_index ] = array();
  76. }
  77. $stylesheets[ $stylesheet_group_index ][ $media ][ $handle ] = $css_url['path'];
  78. $this->done[] = $handle;
  79. } else {
  80. $stylesheet_group_index++;
  81. $stylesheets[ $stylesheet_group_index ][ 'noconcat' ][] = $handle;
  82. $stylesheet_group_index++;
  83. }
  84. unset( $this->to_do[$key] );
  85. }
  86. foreach( $stylesheets as $idx => $stylesheets_group ) {
  87. foreach( $stylesheets_group as $media => $css ) {
  88. if ( 'noconcat' == $media ) {
  89. foreach( $css as $handle ) {
  90. if ( $this->do_item( $handle, $group ) )
  91. $this->done[] = $handle;
  92. }
  93. continue;
  94. } elseif ( count( $css ) > 1) {
  95. $paths = array_map( function( $url ) { return ABSPATH . $url; }, $css );
  96. $mtime = max( array_map( 'filemtime', $paths ) );
  97. $path_str = implode( $css, ',' ) . "?m=${mtime}j";
  98. if ( $this->allow_gzip_compression ) {
  99. $path_64 = base64_encode( gzcompress( $path_str ) );
  100. if ( strlen( $path_str ) > ( strlen( $path_64 ) + 1 ) )
  101. $path_str = '-' . $path_64;
  102. }
  103. $href = $siteurl . "/_static/??" . $path_str;
  104. } else {
  105. $href = $this->cache_bust_mtime( $siteurl . current( $css ) );
  106. }
  107. echo apply_filters( 'style_loader_tag', "<link rel='stylesheet' id='$media-css-$idx' href='$href' type='text/css' media='$media' />\n", $handle );
  108. array_map( array( $this, 'print_inline_style' ), array_keys( $css ) );
  109. }
  110. }
  111. return $this->done;
  112. }
  113. function cache_bust_mtime( $url ) {
  114. if ( strpos( $url, '?m=' ) )
  115. return $url;
  116. $parts = parse_url( $url );
  117. if ( ! isset( $parts['path'] ) || empty( $parts['path'] ) )
  118. return $url;
  119. $file = ABSPATH . ltrim( $parts['path'], '/' );
  120. $mtime = false;
  121. if ( file_exists( $file ) )
  122. $mtime = filemtime( $file );
  123. if ( ! $mtime )
  124. return $url;
  125. if ( false === strpos( $url, '?' ) ) {
  126. $q = '';
  127. } else {
  128. list( $url, $q ) = explode( '?', $url, 2 );
  129. if ( strlen( $q ) )
  130. $q = '&amp;' . $q;
  131. }
  132. return "$url?m={$mtime}g{$q}";
  133. }
  134. function __isset( $key ) {
  135. return isset( $this->old_styles->$key );
  136. }
  137. function __unset( $key ) {
  138. unset( $this->old_styles->$key );
  139. }
  140. function &__get( $key ) {
  141. return $this->old_styles->$key;
  142. }
  143. function __set( $key, $value ) {
  144. $this->old_styles->$key = $value;
  145. }
  146. }
  147. function css_concat_init() {
  148. global $wp_styles;
  149. if ( ! ( $wp_styles instanceof WP_Styles ) ) {
  150. $wp_styles = new WP_Styles();
  151. }
  152. $wp_styles = new WPcom_CSS_Concat( $wp_styles );
  153. $wp_styles->allow_gzip_compression = ALLOW_GZIP_COMPRESSION;
  154. }
  155. add_action( 'init', 'css_concat_init' );