# ezcache/1.3.11/includes/FileOptimizer/BaseFileOptimizer.php

ezCache, version 1.3.11. 113 lines.

- Page: https://pluginprobe.com/plugins/ezcache/1.3.11/code/includes/FileOptimizer/BaseFileOptimizer.php
- Raw: https://pluginprobe.com/plugins/ezcache/1.3.11/raw/includes/FileOptimizer/BaseFileOptimizer.php
- Modified: 2020-01-29T08:46:56+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/ezcache/1.3.11/code/includes/FileOptimizer/BaseFileOptimizer.php#L10-L20`.

```php
<?php
namespace Upress\EzCache\FileOptimizer;

use Upress\EzCache\Cache;
use Upress\EzCache\ThirdParty\Minify_CSS_UriRewriter;

abstract class BaseFileOptimizer {
	static $FILE_TYPE = '';

	function optimize( $html ) {
		return $html;
	}

	/**
	 * Check if the files is on a different host
	 *
	 * @param string $url
	 *
	 * @return bool
	 */
	function is_external_file( $url ) {
		$file       = wp_parse_url( $url );
		$wp_content = wp_parse_url( WP_CONTENT_URL );

		// URL has domain and domain is not part of the internal domains.
		if ( isset( $file['host'] ) && ! empty( $file['host'] ) && $file['host'] != $wp_content['host'] ) {
			return true;
		}

		// URL has no domain and doesn't contain the WP_CONTENT path or wp-includes.
		if ( ! isset( $file['host'] ) && ! preg_match( '#(' . $wp_content['path'] . '|wp-includes)#', $file['path'] ) ) {
			return true;
		}

		return false;
	}

	/**
	 * Get a path to the file by URL ignoring the query string
	 *
	 * @param string $url
	 *
	 * @return bool|string
	 */
	function get_file_path( $url ) {
		return Cache::url_to_path( strtok( $url, '?' ) );
	}

	/**
	 * Determines if it is a file excluded from minification
	 *
	 * @param array $tag Tag corresponding to a JS file.
	 *
	 * @return bool True if it is a file excluded, false otherwise
	 */
	function is_minify_excluded_file( $tag ) {
		// File should not be minified.
		if ( false !== strpos( $tag[0], 'data-minify=' ) || false !== strpos( $tag[0], 'data-no-minify=' ) ) {
			return true;
		}

		$file_path = $this->get_url_component( $tag[2], PHP_URL_PATH );

		if ( static::$FILE_TYPE && pathinfo( $file_path, PATHINFO_EXTENSION ) !== static::$FILE_TYPE ) {
			return true;
		}

		$excluded_files = apply_filters( 'ezcache_excluded_minify_files', [] );
		if ( ! empty( $excluded_files ) ) {
			if ( preg_match( '/^(' . preg_quote( $excluded_files, '/' ) . ')$/', $file_path ) ) {
				return true;
			}
		}

		return false;
	}

	/**
	 * Finds nodes matching the pattern in the HTML
	 *
	 * @param string $pattern Pattern to match.
	 * @param string $html HTML content.
	 *
	 * @return bool|array
	 */
	protected function find( $pattern, $html ) {
		$html_nocomments = preg_replace( '/<!--(.*)-->/uUis', '', $html );
		preg_match_all( '/' . $pattern . '/uUmsi', $html_nocomments, $matches, PREG_SET_ORDER );

		if ( empty( $matches ) ) {
			return false;
		}

		return $matches;
	}

	/**
	 * Rewrites the paths inside the CSS file content
	 *
	 * @param string $file    File path.
	 * @param string $content File content.
	 *
	 * @return string
	 */
	public function rewrite_paths( $file, $content ) {
		return Minify_CSS_UriRewriter::rewrite( $content, dirname( $file ) );
	}

	public function get_url_component( $url, $component = -1 ) {
		return _get_component_from_parsed_url_array( wp_parse_url( $url ), $component );
	}
}

```
