/* Protected Static Members */
/** @var array List of common image files extensions and MIME-types */
- protected static $mimeTypes = array(
+ protected static $mimeTypes = [
'gif' => 'image/gif',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'tiff' => 'image/tiff',
'xbm' => 'image/x-xbitmap',
'svg' => 'image/svg+xml',
- );
+ ];
/* Static Methods */
/**
- * Gets a list of local file paths which are referenced in a CSS style sheet.
- *
- * If you wish non-existent files to be listed too, use getAllLocalFileReferences().
- *
- * For backwards-compatibility, if the second parameter is not given or null,
- * this function will return an empty array instead of erroring out.
- *
- * @param string $source CSS stylesheet source to process
- * @param string $path File path where the source was read from
- * @return array List of local file references
- */
- public static function getLocalFileReferences( $source, $path = null ) {
- if ( $path === null ) {
- return array();
- }
-
- $files = self::getAllLocalFileReferences( $source, $path );
-
- // Skip non-existent files
- $files = array_filter( $files, function ( $file ) {
- return file_exists( $file );
- } );
-
- return $files;
- }
-
- /**
- * Gets a list of local file paths which are referenced in a CSS style sheet, including
- * non-existent files.
+ * Get a list of local files referenced in a stylesheet (includes non-existent files).
*
* @param string $source CSS stylesheet source to process
* @param string $path File path where the source was read from
* @return array List of local file references
*/
- public static function getAllLocalFileReferences( $source, $path ) {
+ public static function getLocalFileReferences( $source, $path ) {
$stripped = preg_replace( '/' . self::COMMENT_REGEX . '/s', '', $source );
$path = rtrim( $path, '/' ) . '/';
- $files = array();
+ $files = [];
$rFlags = PREG_OFFSET_CAPTURE | PREG_SET_ORDER;
if ( preg_match_all( '/' . self::URL_REGEX . '/', $stripped, $matches, $rFlags ) ) {
if ( strstr( $value, "\0" ) ) {
throw new Exception( "Invalid character in CSS string" );
}
- $value = strtr( $value, array( '\\' => '\\\\', '"' => '\\"' ) );
+ $value = strtr( $value, [ '\\' => '\\\\', '"' => '\\"' ] );
$value = preg_replace_callback( '/[\x01-\x1f\x7f-\x9f]/', function ( $match ) {
return '\\' . base_convert( ord( $match[0] ), 10, 16 ) . ' ';
}, $value );
}
/**
- * @param $file string
+ * @param string $file
* @return bool|string
*/
public static function getMimeType( $file ) {
if ( preg_match( '!^[\w\d:@/~.%+;,?&=-]+$!', $url ) ) {
return "url($url)";
} else {
- return 'url("' . strtr( $url, array( '\\' => '\\\\', '"' => '\\"' ) ) . '")';
+ return 'url("' . strtr( $url, [ '\\' => '\\\\', '"' => '\\"' ] ) . '")';
}
}
// Replace all comments by a placeholder so they will not interfere with the remapping.
// Warning: This will also catch on anything looking like the start of a comment between
// quotation marks (e.g. "foo /* bar").
- $comments = array();
+ $comments = [];
$pattern = '/(?!' . CSSMin::EMBED_REGEX . ')(' . CSSMin::COMMENT_REGEX . ')/s';
if ( $embedData ) {
// Remember the occurring MIME types to avoid fallbacks when embedding some files.
- $mimeTypes = array();
+ $mimeTypes = [];
$ruleWithEmbedded = preg_replace_callback(
$pattern,
$url = $match['file'] . $match['query'];
$file = $local . $match['file'];
if (
- !CSSMin::isRemoteUrl( $url ) && !CSSMin::isLocalUrl( $url )
+ !self::isRemoteUrl( $url ) && !self::isLocalUrl( $url )
&& file_exists( $file )
) {
$mimeTypes[ CSSMin::getMimeType( $file ) ] = true;
);
// Are all referenced images SVGs?
- $needsEmbedFallback = $mimeTypes !== array( 'image/svg+xml' => true );
+ $needsEmbedFallback = $mimeTypes !== [ 'image/svg+xml' => true ];
}
if ( !$embedData || $ruleWithEmbedded === $ruleWithRemapped ) {
/**
* Is this CSS rule referencing a remote URL?
*
- * @private Until we require PHP 5.5 and we can access self:: from closures.
* @param string $maybeUrl
* @return bool
*/
- public static function isRemoteUrl( $maybeUrl ) {
+ protected static function isRemoteUrl( $maybeUrl ) {
if ( substr( $maybeUrl, 0, 2 ) === '//' || parse_url( $maybeUrl, PHP_URL_SCHEME ) ) {
return true;
}
/**
* Is this CSS rule referencing a local URL?
*
- * @private Until we require PHP 5.5 and we can access self:: from closures.
* @param string $maybeUrl
* @return bool
*/
- public static function isLocalUrl( $maybeUrl ) {
+ protected static function isLocalUrl( $maybeUrl ) {
if ( $maybeUrl !== '' && $maybeUrl[0] === '/' && !self::isRemoteUrl( $maybeUrl ) ) {
return true;
}
public static function minify( $css ) {
return trim(
str_replace(
- array( '; ', ': ', ' {', '{ ', ', ', '} ', ';}' ),
- array( ';', ':', '{', '{', ',', '}', '}' ),
- preg_replace( array( '/\s+/', '/\/\*.*?\*\//s' ), array( ' ', '' ), $css )
+ [ '; ', ': ', ' {', '{ ', ', ', '} ', ';}' ],
+ [ ';', ':', '{', '{', ',', '}', '}' ],
+ preg_replace( [ '/\s+/', '/\/\*.*?\*\//s' ], [ ' ', '' ], $css )
)
);
}