Commit inicial powerservice
This commit is contained in:
+167
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\Minify\JS;
|
||||
|
||||
use WP_Rocket\Admin\Options_Data;
|
||||
use WP_Rocket\Engine\Optimization\AbstractOptimization;
|
||||
use WP_Rocket\Engine\Optimization\AssetsLocalCache;
|
||||
|
||||
/**
|
||||
* Abstract class for JS optimization
|
||||
*
|
||||
* @since 3.1
|
||||
*/
|
||||
abstract class AbstractJSOptimization extends AbstractOptimization {
|
||||
const FILE_TYPE = 'js';
|
||||
|
||||
/**
|
||||
* Assets local cache instance
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @var AssetsLocalCache
|
||||
*/
|
||||
protected $local_cache;
|
||||
|
||||
/**
|
||||
* Creates an instance of inheriting class.
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @param Options_Data $options Options instance.
|
||||
* @param AssetsLocalCache $local_cache Assets local cache instance.
|
||||
*/
|
||||
public function __construct( Options_Data $options, AssetsLocalCache $local_cache ) {
|
||||
$this->options = $options;
|
||||
$this->local_cache = $local_cache;
|
||||
$this->minify_key = $this->options->get( 'minify_js_key', create_rocket_uniqid() );
|
||||
$this->excluded_files = $this->get_excluded_files();
|
||||
$this->init_base_path_and_url();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all files to exclude from minification/concatenation.
|
||||
*
|
||||
* @since 2.11
|
||||
*
|
||||
* @return string A list of files to exclude, ready to be used in a regex pattern.
|
||||
*/
|
||||
protected function get_excluded_files() {
|
||||
$excluded_files = $this->options->get( 'exclude_js', [] );
|
||||
|
||||
/**
|
||||
* Filter JS files to exclude from minification/concatenation.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @param array $js_files List of excluded JS files.
|
||||
*/
|
||||
$excluded_files = (array) apply_filters( 'rocket_exclude_js', $excluded_files );
|
||||
|
||||
if ( empty( $excluded_files ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
foreach ( $excluded_files as $i => $excluded_file ) {
|
||||
// Escape characters for future use in regex pattern.
|
||||
$excluded_files[ $i ] = str_replace( '#', '\#', $excluded_file );
|
||||
}
|
||||
|
||||
return implode( '|', $excluded_files );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the CDN zones.
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_zones() {
|
||||
return [ 'all', 'css_and_js', self::FILE_TYPE ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if it is a file excluded from minification.
|
||||
*
|
||||
* @since 2.11
|
||||
*
|
||||
* @param array $tag Tag corresponding to a JS file.
|
||||
*
|
||||
* @return bool True if it is a file excluded, false otherwise
|
||||
*/
|
||||
protected function is_minify_excluded_file( array $tag ) {
|
||||
if ( ! isset( $tag[0], $tag['url'] ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// File should not be minified.
|
||||
if (
|
||||
false !== strpos( $tag[0], 'data-minify=' )
|
||||
||
|
||||
false !== strpos( $tag[0], 'data-no-minify=' )
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$file_path = wp_parse_url( $tag['url'], PHP_URL_PATH );
|
||||
|
||||
// File extension is not js.
|
||||
if ( pathinfo( $file_path, PATHINFO_EXTENSION ) !== self::FILE_TYPE ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( ! empty( $this->excluded_files ) ) {
|
||||
// File is excluded from minification/concatenation.
|
||||
if ( preg_match( '#(' . $this->excluded_files . ')#', $file_path ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the minify URL.
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @param string $filename Minified filename.
|
||||
* @param string $original_url Original URL for this file. Optional.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_minify_url( $filename, $original_url = '' ) {
|
||||
$minify_url = $this->minify_base_url . $filename;
|
||||
|
||||
/**
|
||||
* Filters JS file URL with CDN hostname
|
||||
*
|
||||
* @since 2.1
|
||||
*
|
||||
* @param string $minify_url Minified file URL.
|
||||
* @param string $original_url Original URL for this file.
|
||||
*/
|
||||
return apply_filters( 'rocket_js_url', $minify_url, $original_url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Patterns in URL excluded from being combined
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_excluded_external_file_path() {
|
||||
$excluded_external = $this->options->get( 'exclude_js', [] );
|
||||
|
||||
/**
|
||||
* Filters JS externals files to exclude from the combine process
|
||||
*
|
||||
* @since 2.2
|
||||
*
|
||||
* @param array $pattern Patterns to match.
|
||||
*/
|
||||
return apply_filters( 'rocket_minify_excluded_external_js', $excluded_external );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,510 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Engine\Optimization\Minify\JS;
|
||||
|
||||
use WP_Rocket\Dependencies\Minify\JS as MinifyJS;
|
||||
use WP_Rocket\Admin\Options_Data;
|
||||
use WP_Rocket\Engine\Optimization\AssetsLocalCache;
|
||||
use WP_Rocket\Engine\Optimization\DeferJS\DeferJS;
|
||||
use WP_Rocket\Engine\Optimization\Minify\ProcessorInterface;
|
||||
use WP_Rocket\Logger\Logger;
|
||||
|
||||
/**
|
||||
* Combines JS files
|
||||
*
|
||||
* @since 3.1
|
||||
*/
|
||||
class Combine extends AbstractJSOptimization implements ProcessorInterface {
|
||||
/**
|
||||
* Minifier instance
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @var MinifyJS
|
||||
*/
|
||||
private $minifier;
|
||||
|
||||
/**
|
||||
* Excluded defer JS pattern
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $excluded_defer_js;
|
||||
|
||||
/**
|
||||
* Scripts to combine
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $scripts = [];
|
||||
|
||||
/**
|
||||
* Inline scripts excluded from combined and moved after the combined file
|
||||
*
|
||||
* @since 3.1.4
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $move_after = [];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @param Options_Data $options Plugin options instance.
|
||||
* @param MinifyJS $minifier Minifier instance.
|
||||
* @param AssetsLocalCache $local_cache Assets local cache instance.
|
||||
* @param DeferJS $defer_js Defer JS instance.
|
||||
*/
|
||||
public function __construct( Options_Data $options, MinifyJS $minifier, AssetsLocalCache $local_cache, DeferJS $defer_js ) {
|
||||
parent::__construct( $options, $local_cache );
|
||||
|
||||
$this->minifier = $minifier;
|
||||
$this->excluded_defer_js = implode( '|', $defer_js->get_excluded() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Minifies and combines JavaScripts into one
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @return string
|
||||
*/
|
||||
public function optimize( $html ) {
|
||||
Logger::info( 'JS COMBINE PROCESS STARTED.', [ 'js combine process' ] );
|
||||
|
||||
$html_nocomments = $this->hide_comments( $html );
|
||||
$scripts = $this->find( '<script.*<\/script>', $html_nocomments );
|
||||
|
||||
if ( ! $scripts ) {
|
||||
Logger::debug( 'No `<script>` tags found.', [ 'js combine process' ] );
|
||||
return $html;
|
||||
}
|
||||
|
||||
Logger::debug(
|
||||
'Found ' . count( $scripts ) . ' `<script>` tag(s).',
|
||||
[
|
||||
'js combine process',
|
||||
'tags' => $scripts,
|
||||
]
|
||||
);
|
||||
|
||||
$combine_scripts = $this->parse( $scripts );
|
||||
|
||||
if ( empty( $combine_scripts ) ) {
|
||||
Logger::debug( 'No `<script>` tags to optimize.', [ 'js combine process' ] );
|
||||
return $html;
|
||||
}
|
||||
|
||||
Logger::debug(
|
||||
count( $combine_scripts ) . ' `<script>` tag(s) remaining.',
|
||||
[
|
||||
'js combine process',
|
||||
'tags' => $combine_scripts,
|
||||
]
|
||||
);
|
||||
|
||||
$content = $this->get_content();
|
||||
|
||||
if ( empty( $content ) ) {
|
||||
Logger::debug( 'No JS content.', [ 'js combine process' ] );
|
||||
return $html;
|
||||
}
|
||||
|
||||
$minify_url = $this->combine( $content );
|
||||
|
||||
if ( ! $minify_url ) {
|
||||
Logger::error( 'JS combine process failed.', [ 'js combine process' ] );
|
||||
return $html;
|
||||
}
|
||||
|
||||
$move_after = '';
|
||||
|
||||
if ( ! empty( $this->move_after ) ) {
|
||||
foreach ( $this->move_after as $script ) {
|
||||
$move_after .= $script;
|
||||
$html = str_replace( $script, '', $html );
|
||||
}
|
||||
}
|
||||
|
||||
// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
|
||||
$html = str_replace( '</body>', '<script src="' . esc_url( $minify_url ) . '" data-minify="1"></script>' . $move_after . '</body>', $html );
|
||||
|
||||
foreach ( $combine_scripts as $script ) {
|
||||
$html = str_replace( $script[0], '', $html );
|
||||
}
|
||||
|
||||
Logger::info(
|
||||
'Combined JS file successfully added.',
|
||||
[
|
||||
'js combine process',
|
||||
'url' => $minify_url,
|
||||
]
|
||||
);
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses found nodes to keep only the ones to combine
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @param Array $scripts scripts corresponding to JS file or content.
|
||||
* @return array
|
||||
*/
|
||||
protected function parse( $scripts ) {
|
||||
$excluded_externals = implode( '|', $this->get_excluded_external_file_path() );
|
||||
$scripts = array_map(
|
||||
function ( $script ) use ( $excluded_externals ) {
|
||||
preg_match( '/<script\s+([^>]+[\s\'"])?src\s*=\s*[\'"]\s*?(?<url>[^\'"]+\.js(?:\?[^\'"]*)?)\s*?[\'"]([^>]+)?\/?>/Umsi', $script[0], $matches );
|
||||
|
||||
if ( isset( $matches['url'] ) ) {
|
||||
if ( $this->is_external_file( $matches['url'] ) ) {
|
||||
if ( preg_match( '#(' . $excluded_externals . ')#', $matches['url'] ) ) {
|
||||
Logger::debug(
|
||||
'Script is external.',
|
||||
[
|
||||
'js combine process',
|
||||
'tag' => $matches[0],
|
||||
]
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $this->is_defer_excluded( $matches['url'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->scripts[] = [
|
||||
'type' => 'url',
|
||||
'content' => $matches['url'],
|
||||
];
|
||||
|
||||
return $script;
|
||||
}
|
||||
|
||||
if ( $this->is_minify_excluded_file( $matches ) ) {
|
||||
Logger::debug(
|
||||
'Script is excluded.',
|
||||
[
|
||||
'js combine process',
|
||||
'tag' => $matches[0],
|
||||
]
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $this->is_defer_excluded( $matches['url'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$file_path = $this->get_file_path( strtok( $matches['url'], '?' ) );
|
||||
|
||||
if ( ! $file_path ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->scripts[] = [
|
||||
'type' => 'file',
|
||||
'content' => $file_path,
|
||||
];
|
||||
} else {
|
||||
preg_match( '/<script\b(?<attrs>[^>]*)>(?:\/\*\s*<!\[CDATA\[\s*\*\/)?\s*(?<content>[\s\S]*?)\s*(?:\/\*\s*\]\]>\s*\*\/)?<\/script>/msi', $script[0], $matches_inline );
|
||||
|
||||
$matches_inline = array_merge(
|
||||
[
|
||||
'attrs' => '',
|
||||
'content' => '',
|
||||
],
|
||||
$matches_inline
|
||||
);
|
||||
|
||||
if ( preg_last_error() === PREG_BACKTRACK_LIMIT_ERROR ) {
|
||||
Logger::debug(
|
||||
'PCRE regex execution Catastrophic Backtracking',
|
||||
[
|
||||
'inline JS backtracking error',
|
||||
'content' => $matches_inline['content'],
|
||||
]
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if ( strpos( $matches_inline['attrs'], 'type' ) !== false && ! preg_match( '/type\s*=\s*["\']?(?:text|application)\/(?:(?:x\-)?javascript|ecmascript)["\']?/i', $matches_inline['attrs'] ) ) {
|
||||
Logger::debug(
|
||||
'Inline script is not JS.',
|
||||
[
|
||||
'js combine process',
|
||||
'attributes' => $matches_inline['attrs'],
|
||||
]
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if ( false !== strpos( $matches_inline['attrs'], 'src=' ) ) {
|
||||
Logger::debug(
|
||||
'Inline script has a `src` attribute.',
|
||||
[
|
||||
'js combine process',
|
||||
'attributes' => $matches_inline['attrs'],
|
||||
]
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if ( in_array( $matches_inline['content'], $this->get_localized_scripts(), true ) ) {
|
||||
Logger::debug(
|
||||
'Inline script is a localize script',
|
||||
[
|
||||
'js combine process',
|
||||
'excluded_content' => $matches_inline['content'],
|
||||
]
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $this->is_delayed_script( $matches_inline['attrs'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ( $this->get_excluded_inline_content() as $excluded_content ) {
|
||||
if ( false !== strpos( $matches_inline['content'], $excluded_content ) ) {
|
||||
Logger::debug(
|
||||
'Inline script has excluded content.',
|
||||
[
|
||||
'js combine process',
|
||||
'excluded_content' => $excluded_content,
|
||||
]
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( $this->get_move_after_inline_scripts() as $move_after_script ) {
|
||||
if ( false !== strpos( $matches_inline['content'], $move_after_script ) ) {
|
||||
$this->move_after[] = $script[0];
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$this->scripts[] = [
|
||||
'type' => 'inline',
|
||||
'content' => $matches_inline['content'],
|
||||
];
|
||||
}
|
||||
|
||||
return $script;
|
||||
},
|
||||
$scripts
|
||||
);
|
||||
|
||||
return array_filter( $scripts );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets content for each script either from inline or from src
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_content() {
|
||||
$content = '';
|
||||
|
||||
foreach ( $this->scripts as $script ) {
|
||||
if ( 'file' === $script['type'] ) {
|
||||
$file_content = $this->get_file_content( $script['content'] );
|
||||
$content .= $file_content;
|
||||
|
||||
$this->add_to_minify( $file_content );
|
||||
} elseif ( 'url' === $script['type'] ) {
|
||||
$file_content = $this->local_cache->get_content( rocket_add_url_protocol( $script['content'] ) );
|
||||
$content .= $file_content;
|
||||
|
||||
$this->add_to_minify( $file_content );
|
||||
} elseif ( 'inline' === $script['type'] ) {
|
||||
$inline_js = rtrim( $script['content'], ";\n\t\r" ) . ';';
|
||||
$content .= $inline_js;
|
||||
|
||||
$this->add_to_minify( $inline_js );
|
||||
}
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the minify URL if the minification is successful
|
||||
*
|
||||
* @since 2.11
|
||||
*
|
||||
* @param string $content Content to minify & combine.
|
||||
|
||||
* @return string|bool The minify URL if successful, false otherwise
|
||||
*/
|
||||
protected function combine( $content ) {
|
||||
if ( empty( $content ) ) {
|
||||
return false; // phpcs:ignore Universal.CodeAnalysis.ConstructorDestructorReturn.ReturnValueFound
|
||||
}
|
||||
|
||||
$filename = md5( $content . $this->minify_key ) . '.js';
|
||||
$minified_file = $this->minify_base_path . $filename;
|
||||
if ( ! rocket_direct_filesystem()->is_readable( $minified_file ) ) {
|
||||
$minified_content = $this->minify();
|
||||
|
||||
if ( ! $minified_content ) {
|
||||
return false; // phpcs:ignore Universal.CodeAnalysis.ConstructorDestructorReturn.ReturnValueFound
|
||||
}
|
||||
|
||||
$minify_filepath = $this->write_file( $minified_content, $minified_file );
|
||||
|
||||
if ( ! $minify_filepath ) {
|
||||
return false; // phpcs:ignore Universal.CodeAnalysis.ConstructorDestructorReturn.ReturnValueFound
|
||||
}
|
||||
}
|
||||
|
||||
return $this->get_minify_url( $filename ); // phpcs:ignore Universal.CodeAnalysis.ConstructorDestructorReturn.ReturnValueFound
|
||||
}
|
||||
|
||||
/**
|
||||
* Minifies the content
|
||||
*
|
||||
* @since 2.11
|
||||
*
|
||||
* @return string|bool Minified content, false if empty
|
||||
*/
|
||||
protected function minify() {
|
||||
$minified_content = $this->minifier->minify();
|
||||
|
||||
if ( empty( $minified_content ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $minified_content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds content to the minifier
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @param string $content Content to minify/combine.
|
||||
* @return void
|
||||
*/
|
||||
protected function add_to_minify( $content ) {
|
||||
$this->minifier->add( $content );
|
||||
}
|
||||
|
||||
/**
|
||||
* Patterns in content excluded from being combined
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_excluded_inline_content() {
|
||||
$excluded_inline = $this->options->get( 'exclude_inline_js', [] );
|
||||
|
||||
/**
|
||||
* Filters inline JS excluded from being combined
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @param array $pattern Patterns to match.
|
||||
*/
|
||||
return apply_filters( 'rocket_excluded_inline_js_content', $excluded_inline );
|
||||
}
|
||||
|
||||
/**
|
||||
* Patterns of inline JS to move after the combined JS file
|
||||
*
|
||||
* @since 3.1.4
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_move_after_inline_scripts() {
|
||||
/**
|
||||
* Filters inline JS to move after the combined JS file
|
||||
*
|
||||
* @since 3.1.4
|
||||
*
|
||||
* @param array $move_after_scripts Patterns to match.
|
||||
*/
|
||||
return apply_filters( 'rocket_move_after_combine_js', [] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all localized scripts data to exclude them from combine.
|
||||
*
|
||||
* @since 3.1.3
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_localized_scripts() {
|
||||
static $localized_scripts;
|
||||
|
||||
if ( isset( $localized_scripts ) ) {
|
||||
return $localized_scripts;
|
||||
}
|
||||
|
||||
$localized_scripts = [];
|
||||
|
||||
foreach ( array_unique( wp_scripts()->queue ) as $item ) {
|
||||
$data = wp_scripts()->print_extra_script( $item, false );
|
||||
|
||||
if ( empty( $data ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$localized_scripts[] = $data;
|
||||
}
|
||||
|
||||
return $localized_scripts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this script a delayed script or not.
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @param string $script_attributes Attributes beside the opening of script tag.
|
||||
*
|
||||
* @return bool True if it's a delayed script and false if not.
|
||||
*/
|
||||
private function is_delayed_script( $script_attributes ) {
|
||||
return false !== strpos( $script_attributes, 'data-rocketlazyloadscript=' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the current URL is excluded from defer JS
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param string $url URL to check.
|
||||
* @return boolean
|
||||
*/
|
||||
private function is_defer_excluded( string $url ): bool {
|
||||
if (
|
||||
! empty( $this->excluded_defer_js )
|
||||
&&
|
||||
preg_match( '#(' . $this->excluded_defer_js . ')#i', $url )
|
||||
) {
|
||||
Logger::debug(
|
||||
'Script is excluded from defer JS.',
|
||||
[
|
||||
'js combine process',
|
||||
'url' => $url,
|
||||
]
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,352 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Engine\Optimization\Minify\JS;
|
||||
|
||||
use WP_Rocket\Dependencies\Minify as Minifier;
|
||||
use WP_Rocket\Engine\Optimization\Minify\ProcessorInterface;
|
||||
use WP_Rocket\Logger\Logger;
|
||||
|
||||
/**
|
||||
* Minify JS files
|
||||
*
|
||||
* @since 3.1
|
||||
*/
|
||||
class Minify extends AbstractJSOptimization implements ProcessorInterface {
|
||||
/**
|
||||
* Minifies JS files
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @return string
|
||||
*/
|
||||
public function optimize( $html ) {
|
||||
Logger::info( 'JS MINIFICATION PROCESS STARTED.', [ 'js minification process' ] );
|
||||
|
||||
$scripts = $this->get_scripts( $html );
|
||||
|
||||
if ( empty( $scripts ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
foreach ( $scripts as $script ) {
|
||||
global $wp_scripts;
|
||||
|
||||
$is_external_url = $this->is_external_file( $script['url'] );
|
||||
|
||||
if (
|
||||
! $is_external_url
|
||||
&&
|
||||
preg_match( '/[-.]min\.js/iU', $script['url'] )
|
||||
) {
|
||||
Logger::debug(
|
||||
'Script is already minified.',
|
||||
[
|
||||
'js minification process',
|
||||
'tag' => $script[0],
|
||||
]
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
$is_external_url
|
||||
&&
|
||||
$this->is_excluded_external( $script['url'] )
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( $this->is_minify_excluded_file( $script ) ) {
|
||||
Logger::debug(
|
||||
'Script is excluded.',
|
||||
[
|
||||
'js minification process',
|
||||
'tag' => $script[0],
|
||||
]
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Don't minify jQuery included in WP core since it's already minified but without .min in the filename.
|
||||
if ( ! empty( $wp_scripts->registered['jquery-core']->src ) && false !== strpos( $script['url'], $wp_scripts->registered['jquery-core']->src ) ) {
|
||||
Logger::debug(
|
||||
'jQuery script is already minified.',
|
||||
[
|
||||
'js minification process',
|
||||
'tag' => $script[0],
|
||||
]
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
$integrity_validated = $this->local_cache->validate_integrity( $script );
|
||||
|
||||
if ( false === $integrity_validated ) {
|
||||
Logger::debug(
|
||||
'Script integrity attribute not valid.',
|
||||
[
|
||||
'js minification process',
|
||||
'tag' => $script[0],
|
||||
]
|
||||
);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$script['final'] = $integrity_validated;
|
||||
|
||||
$minify_url = $this->replace_url( strtok( $script['url'], '?' ) );
|
||||
|
||||
if ( ! $minify_url ) {
|
||||
Logger::error(
|
||||
'Script minification failed.',
|
||||
[
|
||||
'js minification process',
|
||||
'tag' => $script[0],
|
||||
]
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
$html = $this->replace_script( $script, $minify_url, $html );
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all script tags from HTML.
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @return array Array with script tags, empty array if no script tags found.
|
||||
*/
|
||||
private function get_scripts( $html ) {
|
||||
$html_nocomments = $this->hide_comments( $html );
|
||||
$scripts = $this->find( '<script\s+([^>]+[\s\'"])?src\s*=\s*[\'"]\s*?(?<url>[^\'"]+\.js(?:\?[^\'"]*)?)\s*?[\'"]([^>]+)?\/?>', $html_nocomments );
|
||||
|
||||
if ( ! $scripts ) {
|
||||
Logger::debug( 'No `<script>` tags found.', [ 'js minification process' ] );
|
||||
return [];
|
||||
}
|
||||
|
||||
Logger::debug(
|
||||
'Found ' . count( $scripts ) . ' <link> tags.',
|
||||
[
|
||||
'js minification process',
|
||||
'tags' => $scripts,
|
||||
]
|
||||
);
|
||||
|
||||
return $scripts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the provided external URL is excluded from minify
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @param string $url External URL to check.
|
||||
* @return boolean
|
||||
*/
|
||||
private function is_excluded_external( $url ) {
|
||||
$excluded_externals = $this->get_excluded_external_file_path();
|
||||
$excluded_externals[] = 'google-analytics.com/analytics.js';
|
||||
|
||||
foreach ( $excluded_externals as $excluded ) {
|
||||
if ( false !== strpos( $url, $excluded ) ) {
|
||||
Logger::debug(
|
||||
'Script is external.',
|
||||
[
|
||||
'js combine process',
|
||||
'url' => $url,
|
||||
]
|
||||
);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the minify URL if the minification is successful
|
||||
*
|
||||
* @since 2.11
|
||||
*
|
||||
* @param string $url Original file URL.
|
||||
|
||||
* @return string|bool The minify URL if successful, false otherwise
|
||||
*/
|
||||
protected function replace_url( $url ) {
|
||||
if ( empty( $url ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// This filter is documented in /inc/classes/optimization/class-abstract-optimization.php.
|
||||
$url = apply_filters( 'rocket_asset_url', $url, $this->get_zones() );
|
||||
|
||||
$parsed_url = wp_parse_url( $url );
|
||||
|
||||
if ( empty( $parsed_url['path'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! empty( $parsed_url['host'] ) ) {
|
||||
$url = rocket_add_url_protocol( $url );
|
||||
}
|
||||
|
||||
$filename = ltrim( rocket_realpath( $parsed_url['path'] ), '/' );
|
||||
$minified_file = rawurldecode( $this->minify_base_path . $filename );
|
||||
|
||||
if ( rocket_direct_filesystem()->exists( $minified_file ) ) {
|
||||
Logger::debug(
|
||||
'Minified JS file already exists.',
|
||||
[
|
||||
'js minification process',
|
||||
'path' => $minified_file,
|
||||
]
|
||||
);
|
||||
return $this->get_full_minified_url( $minified_file, $this->get_minify_url( $filename, $url ) );
|
||||
}
|
||||
|
||||
$is_external_url = $this->is_external_file( $url );
|
||||
$file_path = $is_external_url ? $this->local_cache->get_filepath( rocket_add_url_protocol( $url ) ) : $this->get_file_path( $url );
|
||||
|
||||
if ( ! $file_path ) {
|
||||
Logger::error(
|
||||
'Couldn’t get the file path from the URL.',
|
||||
[
|
||||
'js minification process',
|
||||
'url' => $url,
|
||||
]
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
$file_content = $is_external_url ? $this->local_cache->get_content( rocket_add_url_protocol( $url ) ) : $this->get_file_content( $file_path );
|
||||
|
||||
if ( empty( $file_content ) ) {
|
||||
Logger::error(
|
||||
'No file content.',
|
||||
[
|
||||
'js minification process',
|
||||
'path' => $file_path,
|
||||
]
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
$minified_content = $this->minify( $file_content );
|
||||
|
||||
if ( empty( $minified_content ) ) {
|
||||
Logger::error(
|
||||
'No minified content.',
|
||||
[
|
||||
'js minification process',
|
||||
'path' => $minified_file,
|
||||
]
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$save_minify_file = $this->save_minify_file( $minified_file, $minified_content );
|
||||
|
||||
if ( ! $save_minify_file ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->get_full_minified_url( $minified_file, $this->get_minify_url( $filename, $url ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace old script tag with the minified tag.
|
||||
*
|
||||
* @param array $script Script matched data.
|
||||
* @param string $minify_url Minified URL.
|
||||
* @param string $html HTML content.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function replace_script( $script, $minify_url, $html ) {
|
||||
$replace_script = str_replace( $script['url'], $minify_url, $script['final'] );
|
||||
$replace_script = str_replace( '<script', '<script data-minify="1"', $replace_script );
|
||||
$html = str_replace( $script[0], $replace_script, $html );
|
||||
|
||||
Logger::info(
|
||||
'Script minification succeeded.',
|
||||
[
|
||||
'js minification process',
|
||||
'url' => $minify_url,
|
||||
]
|
||||
);
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save minified JS file.
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @param string $minified_file Minified file path.
|
||||
* @param string $minified_content Minified HTML content.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function save_minify_file( $minified_file, $minified_content ) {
|
||||
$save_minify_file = $this->write_file( $minified_content, $minified_file );
|
||||
|
||||
if ( ! $save_minify_file ) {
|
||||
Logger::error(
|
||||
'Minified JS file could not be created.',
|
||||
[
|
||||
'js minification process',
|
||||
'path' => $minified_file,
|
||||
]
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::debug(
|
||||
'Minified JS file successfully created.',
|
||||
[
|
||||
'js minification process',
|
||||
'path' => $minified_file,
|
||||
]
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Minifies the content
|
||||
*
|
||||
* @since 2.11
|
||||
*
|
||||
* @param string $file_content Content to minify.
|
||||
* @return string
|
||||
*/
|
||||
protected function minify( $file_content ) {
|
||||
$minifier = $this->get_minifier( $file_content );
|
||||
$minified_content = $minifier->minify();
|
||||
|
||||
if ( empty( $minified_content ) ) {
|
||||
return ''; // phpcs:ignore Universal.CodeAnalysis.ConstructorDestructorReturn.ReturnValueFound
|
||||
}
|
||||
|
||||
return $minified_content; // phpcs:ignore Universal.CodeAnalysis.ConstructorDestructorReturn.ReturnValueFound
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new minifier instance
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @param string $file_content Content to minify.
|
||||
* @return Minifier\JS
|
||||
*/
|
||||
protected function get_minifier( $file_content ) {
|
||||
return new Minifier\JS( $file_content );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Engine\Optimization\Minify\JS;
|
||||
|
||||
use WP_Rocket\Dependencies\Minify\JS as MinifyJS;
|
||||
use WP_Rocket\Engine\Optimization\AssetsLocalCache;
|
||||
use WP_Rocket\Engine\Optimization\DeferJS\DeferJS;
|
||||
use WP_Rocket\Engine\Optimization\Minify\AbstractMinifySubscriber;
|
||||
|
||||
/**
|
||||
* Minify/Combine JS subscriber
|
||||
*
|
||||
* @since 3.1
|
||||
*/
|
||||
class Subscriber extends AbstractMinifySubscriber {
|
||||
/**
|
||||
* Return an array of events that this subscriber wants to listen to.
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
$events = [
|
||||
'rocket_js_url' => [
|
||||
[ 'fix_ssl_minify' ],
|
||||
[ 'i18n_multidomain_url' ],
|
||||
],
|
||||
'rocket_buffer' => [ 'process', 22 ],
|
||||
];
|
||||
|
||||
return $events;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the HTML to Minify/Combine JS.
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @return string
|
||||
*/
|
||||
public function process( $html ) {
|
||||
if ( ! $this->is_allowed() ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$assets_local_cache = new AssetsLocalCache( rocket_get_constant( 'WP_ROCKET_MINIFY_CACHE_PATH' ), $this->filesystem );
|
||||
$container = apply_filters( 'rocket_container', null );
|
||||
|
||||
if ( $this->options->get( 'minify_js' ) && $this->options->get( 'minify_concatenate_js' ) ) {
|
||||
$this->set_processor_type( new Combine( $this->options, new MinifyJS(), $assets_local_cache, $container->get( 'defer_js' ) ) );
|
||||
} elseif ( $this->options->get( 'minify_js' ) && ! $this->options->get( 'minify_concatenate_js' ) ) {
|
||||
$this->set_processor_type( new Minify( $this->options, $assets_local_cache ) );
|
||||
}
|
||||
|
||||
return $this->processor->optimize( $html );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if is allowed to Minify/Combine JS.
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function is_allowed() {
|
||||
if ( rocket_get_constant( 'DONOTROCKETOPTIMIZE' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! (bool) $this->options->get( 'minify_js', 0 ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ! is_rocket_post_excluded_option( 'minify_js' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of CDN zones for JS files.
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_zones() {
|
||||
return [ 'all', 'css_and_js', 'js' ];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user