Commit inicial powerservice
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\DeferJS;
|
||||
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
|
||||
class AdminSubscriber implements Subscriber_Interface {
|
||||
/**
|
||||
* DeferJS instance
|
||||
*
|
||||
* @var DeferJS
|
||||
*/
|
||||
private $defer_js;
|
||||
|
||||
/**
|
||||
* Instantiate the class
|
||||
*
|
||||
* @param DeferJS $defer_js DeferJS instance.
|
||||
*/
|
||||
public function __construct( DeferJS $defer_js ) {
|
||||
$this->defer_js = $defer_js;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns array of events this listen to.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events(): array {
|
||||
return [
|
||||
'rocket_first_install_options' => 'add_defer_js_option',
|
||||
'wp_rocket_upgrade' => [ 'exclude_jquery_defer', 14, 2 ],
|
||||
'rocket_meta_boxes_fields' => [ 'add_meta_box', 5 ],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds defer js option to WP Rocket options array
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param array $options WP Rocket options array.
|
||||
* @return array
|
||||
*/
|
||||
public function add_defer_js_option( array $options ): array {
|
||||
return $this->defer_js->add_option( $options );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds jQuery to defer JS exclusion field if safe mode was enabled before 3.8
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param string $new_version New plugin version.
|
||||
* @param string $old_version Previous plugin version.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function exclude_jquery_defer( $new_version, $old_version ) {
|
||||
if ( version_compare( $old_version, '3.8', '>' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->defer_js->exclude_jquery_upgrade();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the field to the WP Rocket metabox on the post edit page.
|
||||
*
|
||||
* @param string[] $fields Metaboxes fields.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function add_meta_box( array $fields ) {
|
||||
$fields['defer_all_js'] = __( 'Load JavaScript deferred', 'rocket' );
|
||||
|
||||
return $fields;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,315 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\DeferJS;
|
||||
|
||||
use WP_Rocket\Admin\Options_Data;
|
||||
use WP_Rocket\Engine\Optimization\DynamicLists\DefaultLists\DataManager;
|
||||
|
||||
class DeferJS {
|
||||
/**
|
||||
* Options instance
|
||||
*
|
||||
* @var Options_Data
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* DataManager instance
|
||||
*
|
||||
* @var DataManager
|
||||
*/
|
||||
private $data_manager;
|
||||
|
||||
/**
|
||||
* Instantiate the class
|
||||
*
|
||||
* @param Options_Data $options Options instance.
|
||||
* @param DataManager $data_manager DataManager instance.
|
||||
*/
|
||||
public function __construct( Options_Data $options, DataManager $data_manager ) {
|
||||
$this->options = $options;
|
||||
$this->data_manager = $data_manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the exclude defer JS option in WP Rocket options array
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param array $options WP Rocket options array.
|
||||
* @return array
|
||||
*/
|
||||
public function add_option( array $options ): array {
|
||||
$options['exclude_defer_js'] = [];
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defer all JS files.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @return string
|
||||
*/
|
||||
public function defer_js( string $html ): string {
|
||||
if ( ! $this->can_defer_js() ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$buffer_nocomments = preg_replace( '/<!--(.*)-->/Uis', '', $html );
|
||||
|
||||
preg_match_all( '#<script\s+(?:[^>]+[\s\'"])?src\s*=\s*[\'"]\s*?(?<url>[^\'"]+)\s*?[\'"](?:[^>]+)?\/?>#i', $buffer_nocomments, $matches, PREG_SET_ORDER );
|
||||
|
||||
if ( empty( $matches ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$exclude_defer_js = implode( '|', $this->get_excluded() );
|
||||
|
||||
if ( ! @preg_replace( '#(' . $exclude_defer_js . ')#i', '', 'dummy-string' ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
|
||||
return $html;
|
||||
}
|
||||
|
||||
foreach ( $matches as $tag ) {
|
||||
if ( preg_match( '#(' . $exclude_defer_js . ')#i', $tag['url'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( preg_match( '/\s+(?:async|defer)/i', $tag[0] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$deferred_tag = str_replace( '>', ' defer>', $tag[0] );
|
||||
$html = str_replace( $tag[0], $deferred_tag, $html );
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defers inline JS containing jQuery calls
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @return string
|
||||
*/
|
||||
public function defer_inline_js( string $html ): string {
|
||||
if ( ! $this->can_defer_js() ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$exclude_defer_js = implode( '|', $this->get_excluded() );
|
||||
|
||||
if ( preg_match( '/(jquery(?:.*)?\.js)/i', $exclude_defer_js ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$buffer_nocomments = preg_replace( '/<!--(.*)-->/Uis', '', $html );
|
||||
|
||||
preg_match_all( '#<script(?:[^>]*)>(?<content>[\s\S]*?)</script>#msi', $buffer_nocomments, $matches, PREG_SET_ORDER );
|
||||
|
||||
if ( empty( $matches ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the patterns used to find jQuery in inline JS
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param string $jquery_patterns A RegEx pattern to find jQuery inline JS.
|
||||
*/
|
||||
$jquery_patterns = apply_filters( 'rocket_defer_jquery_patterns', 'jQuery|\$\.\(|\$\(' );
|
||||
|
||||
$inline_exclusions = $this->get_inline_exclusions_list_pattern();
|
||||
|
||||
foreach ( $matches as $inline_js ) {
|
||||
if ( empty( $inline_js['content'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( preg_match( '/(application\/ld\+json)/i', $inline_js[0] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( empty( $inline_exclusions ) || preg_match( "/({$inline_exclusions})/msi", $inline_js['content'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( ! empty( $jquery_patterns ) && ! preg_match( "/({$jquery_patterns})/msi", $inline_js['content'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$tag = str_replace( $inline_js['content'], $this->inline_js_wrapper( $inline_js['content'] ), $inline_js[0] );
|
||||
$html = str_replace( $inline_js[0], $tag, $html );
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the JS wrapper for inline jQuery code
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param string $content Inline JS content.
|
||||
* @return string
|
||||
*/
|
||||
private function inline_js_wrapper( string $content ): string {
|
||||
return "window.addEventListener('DOMContentLoaded', function() {" . $content . '});';
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if we can defer JS
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
private function can_defer_js(): bool {
|
||||
if ( rocket_get_constant( 'DONOTROCKETOPTIMIZE' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! $this->options->get( 'defer_all_js', 0 ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ! is_rocket_post_excluded_option( 'defer_all_js' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of JS files to be excluded from defer JS.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_excluded(): array {
|
||||
if ( ! $this->can_defer_js() ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$exclude_defer_js = array_unique( array_merge( $this->get_external_exclusions(), $this->options->get( 'exclude_defer_js', [] ) ) );
|
||||
|
||||
/**
|
||||
* Filter list of Deferred JavaScript files
|
||||
*
|
||||
* @since 2.10
|
||||
*
|
||||
* @param array $exclude_defer_js An array of URLs for the JS files to be excluded.
|
||||
*/
|
||||
$exclude_defer_js = apply_filters( 'rocket_exclude_defer_js', $exclude_defer_js );
|
||||
|
||||
foreach ( $exclude_defer_js as $i => $exclude ) {
|
||||
$exclude_defer_js[ $i ] = str_replace( '#', '\#', $exclude );
|
||||
}
|
||||
|
||||
return $exclude_defer_js;
|
||||
}
|
||||
|
||||
/**
|
||||
* Excludes jQuery from combine JS when defer and combine are enabled
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param array $excluded_files Array of excluded files from combine JS.
|
||||
* @return array
|
||||
*/
|
||||
public function exclude_jquery_combine( array $excluded_files ): array {
|
||||
if ( ! $this->can_defer_js() ) {
|
||||
return $excluded_files;
|
||||
}
|
||||
|
||||
if ( ! (bool) $this->options->get( 'minify_concatenate_js', 0 ) ) {
|
||||
return $excluded_files;
|
||||
}
|
||||
|
||||
$excluded_files[] = '/jquery-?[0-9.]*(.min|.slim|.slim.min)?.js';
|
||||
|
||||
return $excluded_files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds jQuery to defer JS exclusion field if safe mode was enabled before 3.8
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function exclude_jquery_upgrade() {
|
||||
$options = get_option( 'wp_rocket_settings' );
|
||||
|
||||
if ( ! isset( $options['defer_all_js_safe'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! (bool) $options['defer_all_js_safe'] ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$options['exclude_defer_js'][] = '/jquery-?[0-9.]*(.min|.slim|.slim.min)?.js';
|
||||
|
||||
update_option( 'wp_rocket_settings', $options );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get exclusion list pattern.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function get_inline_exclusions_list_pattern() {
|
||||
$inline_exclusions_list = $this->get_inline_exclusions();
|
||||
|
||||
/**
|
||||
* Filters the patterns used to find inline JS that should not be deferred
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param array $inline_exclusions_list Array of inline JS that should not be deferred.
|
||||
*/
|
||||
$additional_inline_exclusions_list = apply_filters( 'rocket_defer_inline_exclusions', null );
|
||||
|
||||
$inline_exclusions = '';
|
||||
|
||||
// Check if filter return is string so convert it to array for backward compatibility.
|
||||
if ( is_string( $additional_inline_exclusions_list ) ) {
|
||||
$additional_inline_exclusions_list = explode( '|', $additional_inline_exclusions_list );
|
||||
}
|
||||
|
||||
// Cast filter return to array.
|
||||
$inline_exclusions_list = array_merge( $inline_exclusions_list, (array) $additional_inline_exclusions_list );
|
||||
|
||||
foreach ( $inline_exclusions_list as $inline_exclusions_item ) {
|
||||
$inline_exclusions .= preg_quote( (string) $inline_exclusions_item, '#' ) . '|';
|
||||
}
|
||||
|
||||
return rtrim( $inline_exclusions, '|' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get inline exclusions list
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_inline_exclusions(): array {
|
||||
$lists = $this->data_manager->get_lists();
|
||||
|
||||
return isset( $lists->defer_js_inline_exclusions ) ? $lists->defer_js_inline_exclusions : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get external exclusions list
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_external_exclusions(): array {
|
||||
$lists = $this->data_manager->get_lists();
|
||||
|
||||
return isset( $lists->defer_js_external_exclusions ) ? $lists->defer_js_external_exclusions : [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Engine\Optimization\DeferJS;
|
||||
|
||||
use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
|
||||
|
||||
/**
|
||||
* Service provider for the WP Rocket Defer JS
|
||||
*
|
||||
* @since 3.8
|
||||
*/
|
||||
class ServiceProvider extends AbstractServiceProvider {
|
||||
|
||||
/**
|
||||
* The provides array is a way to let the container
|
||||
* know that a service is provided by this service
|
||||
* provider. Every service that is registered via
|
||||
* this service provider must have an alias added
|
||||
* to this array or it will be ignored.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $provides = [
|
||||
'defer_js',
|
||||
'defer_js_admin_subscriber',
|
||||
'defer_js_subscriber',
|
||||
];
|
||||
|
||||
/**
|
||||
* Registers items with the container
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register() {
|
||||
$this->getContainer()->add( 'defer_js', DeferJS::class )
|
||||
->addArgument( $this->getContainer()->get( 'options' ) )
|
||||
->addArgument( $this->getContainer()->get( 'dynamic_lists_defaultlists_data_manager' ) );
|
||||
$this->getContainer()->share( 'defer_js_admin_subscriber', AdminSubscriber::class )
|
||||
->addArgument( $this->getContainer()->get( 'defer_js' ) )
|
||||
->addTag( 'admin_subscriber' );
|
||||
$this->getContainer()->share( 'defer_js_subscriber', Subscriber::class )
|
||||
->addArgument( $this->getContainer()->get( 'defer_js' ) )
|
||||
->addTag( 'front_subscriber' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\DeferJS;
|
||||
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
|
||||
class Subscriber implements Subscriber_Interface {
|
||||
/**
|
||||
* DeferJS instance
|
||||
*
|
||||
* @var DeferJS
|
||||
*/
|
||||
private $defer_js;
|
||||
|
||||
/**
|
||||
* Instantiate the class
|
||||
*
|
||||
* @param DeferJS $defer_js DeferJS instance.
|
||||
*/
|
||||
public function __construct( DeferJS $defer_js ) {
|
||||
$this->defer_js = $defer_js;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns array of events this listen to.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events(): array {
|
||||
return [
|
||||
'rocket_buffer' => [
|
||||
[ 'defer_js', 24 ],
|
||||
[ 'defer_inline_js', 25 ],
|
||||
],
|
||||
'rocket_exclude_js' => 'exclude_jquery_combine',
|
||||
'rocket_minify_excluded_external_js' => 'exclude_jquery_combine',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the defer attribute to JS files
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @return string
|
||||
*/
|
||||
public function defer_js( string $html ): string {
|
||||
return $this->defer_js->defer_js( $html );
|
||||
}
|
||||
|
||||
/**
|
||||
* Defers inline JS containing jQuery calls
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @return string
|
||||
*/
|
||||
public function defer_inline_js( string $html ): string {
|
||||
return $this->defer_js->defer_inline_js( $html );
|
||||
}
|
||||
|
||||
/**
|
||||
* Excludes jQuery from combine JS when defer and combine are enabled
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param array $excluded_files Array of excluded files from combine JS.
|
||||
* @return array
|
||||
*/
|
||||
public function exclude_jquery_combine( array $excluded_files ): array {
|
||||
return $this->defer_js->exclude_jquery_combine( $excluded_files );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user