3.15.3
This commit is contained in:
@@ -0,0 +1,201 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\DelayJS\Admin;
|
||||
|
||||
use WP_Rocket\Admin\Options;
|
||||
use WP_Rocket\Engine\Admin\Settings\Settings as AdminSettings;
|
||||
|
||||
class Settings {
|
||||
|
||||
/**
|
||||
* Options instance.
|
||||
*
|
||||
* @var Options
|
||||
*/
|
||||
protected $options_api;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param Options $options_api Options instance.
|
||||
*/
|
||||
public function __construct( Options $options_api ) {
|
||||
$this->options_api = $options_api;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the delay JS options to the WP Rocket options array
|
||||
*
|
||||
* @since 3.9 Removed delay_js_scripts key, added delay_js_exclusions.
|
||||
* @since 3.7
|
||||
*
|
||||
* @param array $options WP Rocket options array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function add_options( $options ): array {
|
||||
$options = (array) $options;
|
||||
|
||||
$options['delay_js'] = 0;
|
||||
$options['delay_js_exclusions'] = [];
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the delay_js_exclusions default value for users with delay JS enabled on upgrade
|
||||
*
|
||||
* @since 3.9 Sets the delay_js_exclusions default value if delay_js is 1
|
||||
* @since 3.7
|
||||
*
|
||||
* @param string $old_version Previous plugin version.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function set_option_on_update( $old_version ) {
|
||||
if ( version_compare( $old_version, '3.9', '>=' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$options = $this->options_api->get( 'settings', [] );
|
||||
|
||||
$options['delay_js_exclusions'] = [];
|
||||
|
||||
if (
|
||||
isset( $options['delay_js'] )
|
||||
&&
|
||||
1 === (int) $options['delay_js']
|
||||
) {
|
||||
$options['minify_concatenate_js'] = 0;
|
||||
}
|
||||
|
||||
$this->options_api->set( 'settings', $options );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes delay JS options when saving the settings
|
||||
*
|
||||
* @since 3.9
|
||||
*
|
||||
* @param array $input Array of values submitted from the form.
|
||||
* @param AdminSettings $settings Settings class instance.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function sanitize_options( $input, $settings ): array {
|
||||
$input['delay_js'] = $settings->sanitize_checkbox( $input, 'delay_js' );
|
||||
$input['delay_js_exclusions'] =
|
||||
! empty( $input['delay_js_exclusions'] )
|
||||
?
|
||||
rocket_sanitize_textarea_field( 'delay_js_exclusions', $input['delay_js_exclusions'] )
|
||||
:
|
||||
[];
|
||||
|
||||
return $input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable combine JS option when delay JS is enabled
|
||||
*
|
||||
* @since 3.9
|
||||
*
|
||||
* @param array $value The new, unserialized option value.
|
||||
* @param array $old_value The old option value.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function maybe_disable_combine_js( $value, $old_value ): array {
|
||||
if ( ! isset( $value['delay_js'], $value['minify_concatenate_js'] ) ) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
if (
|
||||
0 === $value['minify_concatenate_js']
|
||||
||
|
||||
0 === $value['delay_js']
|
||||
) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
if (
|
||||
isset( $old_value['delay_js'], $old_value['minify_concatenate_js'] )
|
||||
&&
|
||||
$value['delay_js'] === $old_value['delay_js']
|
||||
&&
|
||||
1 === $value['delay_js']
|
||||
&&
|
||||
0 === $old_value['minify_concatenate_js']
|
||||
) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$value['minify_concatenate_js'] = 0;
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default exclusion list.
|
||||
*
|
||||
* @since 3.9.1
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public static function get_delay_js_default_exclusions(): array {
|
||||
|
||||
$exclusions = [
|
||||
'\/jquery(-migrate)?-?([0-9.]+)?(.min|.slim|.slim.min)?.js(\?(.*))?( |\'|"|>)',
|
||||
'js-(before|after)',
|
||||
];
|
||||
|
||||
$wp_content = wp_parse_url( content_url( '/' ), PHP_URL_PATH );
|
||||
$wp_includes = wp_parse_url( includes_url( '/' ), PHP_URL_PATH );
|
||||
$pattern = '(?:placeholder)(.*)';
|
||||
$paths = [];
|
||||
|
||||
if ( ! $wp_content && ! $wp_includes ) {
|
||||
return $exclusions;
|
||||
}
|
||||
|
||||
if ( $wp_content ) {
|
||||
$paths[] = $wp_content;
|
||||
}
|
||||
|
||||
if ( $wp_includes ) {
|
||||
$paths[] = $wp_includes;
|
||||
}
|
||||
|
||||
$exclusions[] = str_replace( 'placeholder', implode( '|', $paths ), $pattern );
|
||||
|
||||
return $exclusions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if current exclusion list has the default list.
|
||||
*
|
||||
* @since 3.9.1
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function exclusion_list_has_default(): bool {
|
||||
$current_list = get_rocket_option( 'delay_js_exclusions', [] );
|
||||
if ( empty( $current_list ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$default_list = self::get_delay_js_default_exclusions();
|
||||
if ( count( $current_list ) < count( $default_list ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$current_list = array_flip( $current_list );
|
||||
|
||||
foreach ( $default_list as $item ) {
|
||||
if ( ! isset( $current_list[ $item ] ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,535 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\DelayJS\Admin;
|
||||
|
||||
use WP_Rocket\Admin\Options;
|
||||
use WP_Rocket\Admin\Options_Data;
|
||||
use WP_Rocket\Engine\Optimization\DynamicLists\DelayJSLists\DataManager;
|
||||
use WP_Rocket\Engine\Optimization\DynamicLists\DynamicLists;
|
||||
use WP_Theme;
|
||||
|
||||
class SiteList {
|
||||
/**
|
||||
* Delay JS data manager.
|
||||
*
|
||||
* @var DataManager
|
||||
*/
|
||||
protected $dynamic_lists;
|
||||
|
||||
/**
|
||||
* Options instance.
|
||||
*
|
||||
* @var Options_Data
|
||||
*/
|
||||
protected $options;
|
||||
|
||||
/**
|
||||
* Options API instance.
|
||||
*
|
||||
* @var Options
|
||||
*/
|
||||
private $options_api;
|
||||
|
||||
/**
|
||||
* SiteList Constructor.
|
||||
*
|
||||
* @param DynamicLists $dynamic_lists Dynamic Lists instance.
|
||||
* @param Options_Data $options Options instance.
|
||||
* @param Options $options_api Options API instance.
|
||||
*/
|
||||
public function __construct( DynamicLists $dynamic_lists, Options_Data $options, Options $options_api ) {
|
||||
$this->dynamic_lists = $dynamic_lists;
|
||||
$this->options = $options;
|
||||
$this->options_api = $options_api;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if plugin is in the list and return it if found.
|
||||
*
|
||||
* @param string $item_id Plugin ID like wp-rocket/wp-rocket.php.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_plugin_in_list( string $item_id ) {
|
||||
$list = $this->dynamic_lists->get_delayjs_list();
|
||||
return ! empty( $list->plugins->$item_id ) ? (array) $list->plugins->$item_id : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if theme is in the list and return it if found.
|
||||
*
|
||||
* @param string $item_id Theme ID (directory name) like twentytwenty.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_theme_in_list( string $item_id ) {
|
||||
$list = $this->dynamic_lists->get_delayjs_list();
|
||||
return ! empty( $list->themes->$item_id ) ? (array) $list->themes->$item_id : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if script is in the list and return it if found.
|
||||
*
|
||||
* @param string $item_id Script ID.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_script_in_list( string $item_id ) {
|
||||
$scripts = $this->get_scripts_from_list();
|
||||
return ! empty( $scripts[ $item_id ] ) ? (array) $scripts[ $item_id ] : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all scripts from the list.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_scripts_from_list() {
|
||||
$list = $this->dynamic_lists->get_delayjs_list();
|
||||
return ! empty( $list->scripts ) ? (array) $list->scripts : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all plugins from the list.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_plugins_from_list() {
|
||||
$list = $this->dynamic_lists->get_delayjs_list();
|
||||
return ! empty( $list->plugins ) ? (array) $list->plugins : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all themes from the list.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_themes_from_list() {
|
||||
$list = $this->dynamic_lists->get_delayjs_list();
|
||||
return ! empty( $list->themes ) ? (array) $list->themes : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of exclusions from the API list.
|
||||
*
|
||||
* @param string $item_id Item ID to get exclusions for (plugin slug, theme slug, script ID).
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_delayjs_exclusions_by_id( string $item_id ) {
|
||||
$item = $this->get_script_in_list( $item_id );
|
||||
if ( $item ) {
|
||||
return $item['exclusions'];
|
||||
}
|
||||
|
||||
$item = $this->get_plugin_in_list( $item_id );
|
||||
if ( $item ) {
|
||||
return $item['exclusions'];
|
||||
}
|
||||
|
||||
$item = $this->get_theme_in_list( $item_id );
|
||||
if ( $item ) {
|
||||
return $item['exclusions'];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all exclusions (merged together) for a list of item IDs.
|
||||
*
|
||||
* @param array $items List of items.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_delayjs_items_exclusions( array $items ) {
|
||||
if ( empty( $items ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$exclusions = [];
|
||||
|
||||
foreach ( $items as $item ) {
|
||||
$exclusions = array_merge( $exclusions, $this->get_delayjs_exclusions_by_id( $item ) );
|
||||
}
|
||||
|
||||
return $exclusions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get active theme ID.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_active_theme() {
|
||||
return $this->get_theme_name( wp_get_theme() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get active plugins (list of IDs).
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_active_plugins() {
|
||||
$plugins = (array) get_option( 'active_plugins', [] );
|
||||
|
||||
if ( ! is_multisite() ) {
|
||||
return $plugins;
|
||||
}
|
||||
|
||||
return array_merge(
|
||||
$plugins,
|
||||
array_keys( (array) get_site_option( 'active_sitewide_plugins', [] ) )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the list of scripts, plugins and theme for the view.
|
||||
*
|
||||
* @return array|array[]
|
||||
*/
|
||||
public function prepare_delayjs_ui_list() {
|
||||
$full_list = [
|
||||
'scripts' => [
|
||||
'title' => __( 'Analytics & Ads', 'rocket' ),
|
||||
'items' => [],
|
||||
'dashicon-class' => 'analytics',
|
||||
],
|
||||
'plugins' => [
|
||||
'title' => __( 'Plugins', 'rocket' ),
|
||||
'items' => [],
|
||||
'dashicon-class' => 'admin-plugins',
|
||||
],
|
||||
'themes' => [
|
||||
'title' => __( 'Themes', 'rocket' ),
|
||||
'items' => [],
|
||||
'dashicon-class' => 'admin-appearance',
|
||||
],
|
||||
];
|
||||
|
||||
// Scripts.
|
||||
$scripts = $this->get_scripts_from_list();
|
||||
foreach ( $scripts as $script_key => $script ) {
|
||||
$full_list['scripts']['items'][] = [
|
||||
'id' => $script_key,
|
||||
'title' => $script->title,
|
||||
'icon' => $this->get_icon( $script ),
|
||||
];
|
||||
}
|
||||
|
||||
$active_plugins = $this->get_active_plugins();
|
||||
foreach ( $this->get_plugins_from_list() as $plugin_key => $plugin ) {
|
||||
if ( ! in_array( $plugin->condition, $active_plugins, true ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$full_list['plugins']['items'][] = [
|
||||
'id' => $plugin_key,
|
||||
'title' => $plugin->title,
|
||||
'icon' => $this->get_icon( $plugin ),
|
||||
];
|
||||
}
|
||||
|
||||
$active_theme = $this->get_active_theme();
|
||||
foreach ( $this->get_themes_from_list() as $theme_key => $theme ) {
|
||||
if ( $theme->condition !== $active_theme ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$full_list['themes']['items'][] = [
|
||||
'id' => $theme_key,
|
||||
'title' => $theme->title,
|
||||
'icon' => $this->get_icon( $theme ),
|
||||
];
|
||||
}
|
||||
|
||||
return $full_list;
|
||||
}
|
||||
/**
|
||||
* Fetch the icon.
|
||||
*
|
||||
* @param array $item item from the list.
|
||||
* @return string
|
||||
*/
|
||||
private function get_icon( $item ) {
|
||||
if ( empty( $item ) || empty( $item->icon_url ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $item->icon_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes delay JS options when saving the settings
|
||||
*
|
||||
* @since 3.13
|
||||
*
|
||||
* @param array $input Array of values submitted from the form.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function sanitize_options( $input ): array {
|
||||
if ( empty( $input['delay_js_exclusions_selected'] ) ) {
|
||||
$input['delay_js_exclusions_selected'] = [];
|
||||
$input['delay_js_exclusions_selected_exclusions'] = [];
|
||||
|
||||
return $input;
|
||||
}
|
||||
|
||||
$input['delay_js_exclusions_selected_exclusions'] = $this->get_delayjs_items_exclusions( $input['delay_js_exclusions_selected'] );
|
||||
|
||||
return $input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh exclusions option based on selected items option.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function refresh_exclusions_option() {
|
||||
$slug = rocket_get_constant( 'WP_ROCKET_SLUG', 'wp_rocket_settings' );
|
||||
$options = get_option( $slug, [] );
|
||||
if ( empty( $options ) ) {
|
||||
return;
|
||||
}
|
||||
$options['delay_js_exclusions_selected_exclusions'] = $this->get_delayjs_items_exclusions( $options['delay_js_exclusions_selected'] ?? [] );
|
||||
update_option( $slug, $options );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get plugin item ids from the list using plugin base.
|
||||
*
|
||||
* @param string $plugin_base Plugin basename.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
private function get_plugin_item_ids( string $plugin_base ) {
|
||||
$item_ids = [];
|
||||
foreach ( $this->get_plugins_from_list() as $plugin_key => $plugin ) {
|
||||
if ( $plugin_base !== $plugin->condition ) {
|
||||
continue;
|
||||
}
|
||||
$item_ids[ $plugin_key ] = $plugin->is_default;
|
||||
}
|
||||
return $item_ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add plugin exclusions only if plugin is default is checked in backend.
|
||||
*
|
||||
* @param string $plugin_base Plugin basename.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function add_default_plugin_exclusions( string $plugin_base ) {
|
||||
$plugin_item_ids = $this->get_plugin_item_ids( $plugin_base );
|
||||
if ( empty( $plugin_item_ids ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$selected_items = $this->options->get( 'delay_js_exclusions_selected', [] );
|
||||
if ( empty( $selected_items ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$current_selected_items = $selected_items;
|
||||
|
||||
foreach ( $plugin_item_ids as $plugin_item_id => $plugin_is_default ) {
|
||||
if ( ! $plugin_is_default ) {
|
||||
continue;
|
||||
}
|
||||
$selected_items[] = $plugin_item_id;
|
||||
}
|
||||
|
||||
if ( $current_selected_items === $selected_items ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->options->set( 'delay_js_exclusions_selected', $selected_items );
|
||||
$this->options->set(
|
||||
'delay_js_exclusions_selected_exclusions',
|
||||
$this->get_delayjs_items_exclusions( $selected_items )
|
||||
);
|
||||
|
||||
$this->options_api->set( 'settings', $this->options->get_options() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove plugin selections from settings.
|
||||
*
|
||||
* @param string $plugin_base Plugin basename.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function remove_plugin_selection( $plugin_base ) {
|
||||
$plugin_item_ids = $this->get_plugin_item_ids( $plugin_base );
|
||||
if ( empty( $plugin_item_ids ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$selected_items = $this->options->get( 'delay_js_exclusions_selected', [] );
|
||||
if ( empty( $selected_items ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$current_selected_items = $selected_items;
|
||||
|
||||
foreach ( $plugin_item_ids as $plugin_item_id => $plugin_is_default ) {
|
||||
$selected_item_key = array_search( $plugin_item_id, $selected_items, true );
|
||||
if ( false === $selected_item_key ) {
|
||||
continue;
|
||||
}
|
||||
unset( $selected_items[ $selected_item_key ] );
|
||||
}
|
||||
|
||||
if ( $current_selected_items === $selected_items ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->options->set( 'delay_js_exclusions_selected', $selected_items );
|
||||
$this->options->set(
|
||||
'delay_js_exclusions_selected_exclusions',
|
||||
$this->get_delayjs_items_exclusions( $selected_items )
|
||||
);
|
||||
|
||||
$this->options_api->set( 'settings', $this->options->get_options() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get theme name from theme object.
|
||||
*
|
||||
* @param WP_Theme $theme Theme to get its name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function get_theme_name( WP_Theme $theme ) {
|
||||
$parent = $theme->get_template();
|
||||
if ( ! empty( $parent ) ) {
|
||||
return $parent;
|
||||
}
|
||||
|
||||
return $theme->get( 'Name' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Theme item ids from the list using theme name.
|
||||
*
|
||||
* @param string $theme_name Theme name.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
private function get_theme_item_ids( $theme_name ) {
|
||||
$item_ids = [];
|
||||
foreach ( $this->get_themes_from_list() as $theme_key => $theme ) {
|
||||
if ( $theme_name !== $theme->condition ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$item_ids[] = $theme_key;
|
||||
}
|
||||
return $item_ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the old theme with the new theme exclusions.
|
||||
*
|
||||
* @param WP_Theme $new_theme WP_Theme instance of the new theme.
|
||||
* @param WP_Theme $old_theme WP_Theme instance of the old theme.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function replace_theme_selection( $new_theme, $old_theme ) {
|
||||
$new_theme_ids = $this->get_theme_item_ids( $this->get_theme_name( $new_theme ) );
|
||||
$old_theme_ids = $this->get_theme_item_ids( $this->get_theme_name( $old_theme ) );
|
||||
|
||||
if ( empty( $new_theme_ids ) && empty( $old_theme_ids ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$selected_items = $this->options->get( 'delay_js_exclusions_selected', [] );
|
||||
if ( empty( $selected_items ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$current_selected_items = $selected_items;
|
||||
|
||||
if ( ! empty( $old_theme_ids ) ) {
|
||||
foreach ( $old_theme_ids as $old_theme_id ) {
|
||||
$selected_item_key = array_search( $old_theme_id, $selected_items, true );
|
||||
if ( false === $selected_item_key ) {
|
||||
continue;
|
||||
}
|
||||
unset( $selected_items[ $selected_item_key ] );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $new_theme_ids ) ) {
|
||||
$themes = $this->get_themes_from_list();
|
||||
foreach ( $new_theme_ids as $new_theme_id ) {
|
||||
if ( ! $themes[ $new_theme_id ]->is_default ) {
|
||||
continue;
|
||||
}
|
||||
$selected_items[] = $new_theme_id;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $current_selected_items === $selected_items ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->options->set( 'delay_js_exclusions_selected', $selected_items );
|
||||
$this->options->set(
|
||||
'delay_js_exclusions_selected_exclusions',
|
||||
$this->get_delayjs_items_exclusions( $selected_items )
|
||||
);
|
||||
|
||||
$this->options_api->set( 'settings', $this->options->get_options() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default items from the list with their exclusions.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_default_exclusions() {
|
||||
$items = [];
|
||||
|
||||
$scripts = $this->get_scripts_from_list();
|
||||
foreach ( $scripts as $script_key => $script ) {
|
||||
if ( ! $script->is_default ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$items[ $script_key ] = $script->exclusions;
|
||||
}
|
||||
|
||||
$active_plugins = $this->get_active_plugins();
|
||||
foreach ( $this->get_plugins_from_list() as $plugin_key => $plugin ) {
|
||||
if ( ! in_array( $plugin->condition, $active_plugins, true ) || ! $plugin->is_default ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$items[ $plugin_key ] = $plugin->exclusions;
|
||||
}
|
||||
|
||||
$active_theme = $this->get_active_theme();
|
||||
foreach ( $this->get_themes_from_list() as $theme_key => $theme ) {
|
||||
if ( $theme->condition !== $active_theme || ! $theme->is_default ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$items[ $theme_key ] = $theme->exclusions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the delay JS default exclusions list.
|
||||
* Key is the plugin/theme/script unique ID and value is array of exclusions
|
||||
*
|
||||
* @since 3.13
|
||||
*
|
||||
* @param array $items Array of default excluded items.
|
||||
*/
|
||||
return apply_filters( 'rocket_delay_js_default_exclusions', $items );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\DelayJS\Admin;
|
||||
|
||||
use WP_Rocket\Engine\Admin\Settings\Settings as AdminSettings;
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
use WP_Theme;
|
||||
|
||||
class Subscriber implements Subscriber_Interface {
|
||||
/**
|
||||
* Settings instance
|
||||
*
|
||||
* @var Settings
|
||||
*/
|
||||
private $settings;
|
||||
|
||||
/**
|
||||
* Site List instance.
|
||||
*
|
||||
* @var SiteList
|
||||
*/
|
||||
private $site_list;
|
||||
|
||||
/**
|
||||
* Instantiate the class
|
||||
*
|
||||
* @param Settings $settings Settings instance.
|
||||
* @param SiteList $site_list DelayJS Site List instance.
|
||||
*/
|
||||
public function __construct( Settings $settings, SiteList $site_list ) {
|
||||
$this->settings = $settings;
|
||||
$this->site_list = $site_list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of events that this subscriber listens to.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
return [
|
||||
'rocket_first_install_options' => [
|
||||
[ 'add_options' ],
|
||||
[ 'add_default_exclusions_options' ],
|
||||
],
|
||||
'wp_rocket_upgrade' => [ 'set_option_on_update', 13, 2 ],
|
||||
'rocket_input_sanitize' => [
|
||||
[ 'sanitize_options', 13, 2 ],
|
||||
[ 'sanitize_selected_exclusions', 14 ],
|
||||
],
|
||||
'pre_update_option_wp_rocket_settings' => [ 'maybe_disable_combine_js', 11, 2 ],
|
||||
'rocket_after_save_dynamic_lists' => 'refresh_exclusions_option',
|
||||
'activate_plugin' => 'add_plugin_exclusions',
|
||||
'deactivate_plugin' => 'remove_plugin_exclusions',
|
||||
'switch_theme' => [ 'handle_switch_theme_exclusions', 10, 3 ],
|
||||
'rocket_meta_boxes_fields' => [ 'add_meta_box', 6 ],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the delay JS options to the WP Rocket options array
|
||||
*
|
||||
* @param array $options WP Rocket options array.
|
||||
*
|
||||
* @return array
|
||||
* @since 3.7
|
||||
*/
|
||||
public function add_options( $options ): array {
|
||||
return $this->settings->add_options( $options );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the delay JS exclusions options to the WP Rocket options array
|
||||
* based on the default items in the list.
|
||||
*
|
||||
* @param array $options WP Rocket options array.
|
||||
*
|
||||
* @return array
|
||||
* @since 3.13
|
||||
*/
|
||||
public function add_default_exclusions_options( $options ): array {
|
||||
$default_exclusions = $this->site_list->get_default_exclusions();
|
||||
|
||||
if ( empty( $default_exclusions ) ) {
|
||||
$options['delay_js_exclusions_selected'] = [];
|
||||
$options['delay_js_exclusions_selected_exclusions'] = [];
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
$options['delay_js_exclusions_selected'] = array_keys( $default_exclusions );
|
||||
$options['delay_js_exclusions_selected_exclusions'] = array_merge( ...array_values( $default_exclusions ) );
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the delay_js_exclusions default value for users with delay JS enabled on upgrade
|
||||
*
|
||||
* @param string $new_version New plugin version.
|
||||
* @param string $old_version Previous plugin version.
|
||||
*
|
||||
* @return void
|
||||
* @since 3.7
|
||||
*
|
||||
* @since 3.9 Sets the delay_js_exclusions default value if delay_js is 1
|
||||
*/
|
||||
public function set_option_on_update( $new_version, $old_version ) {
|
||||
$this->settings->set_option_on_update( $old_version );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes Delay JS options values when the settings form is submitted
|
||||
*
|
||||
* @param array $input Array of values submitted from the form.
|
||||
* @param AdminSettings $settings Settings class instance.
|
||||
*
|
||||
* @return array
|
||||
* @since 3.9
|
||||
*/
|
||||
public function sanitize_options( $input, AdminSettings $settings ): array {
|
||||
return $this->settings->sanitize_options( $input, $settings );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes delay JS selected exclusions options when saving the settings.
|
||||
*
|
||||
* @since 3.13
|
||||
*
|
||||
* @param array $input Array of values submitted from the form.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function sanitize_selected_exclusions( $input ) {
|
||||
return $this->site_list->sanitize_options( $input );
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable combine JS option when delay JS is enabled
|
||||
*
|
||||
* @param array $value The new, unserialized option value.
|
||||
* @param array $old_value The old option value.
|
||||
*
|
||||
* @return array
|
||||
* @since 3.9
|
||||
*/
|
||||
public function maybe_disable_combine_js( $value, $old_value ): array {
|
||||
return $this->settings->maybe_disable_combine_js( $value, $old_value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh exclusions option when the dynamic list is updated weekly or manually.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function refresh_exclusions_option() {
|
||||
$this->site_list->refresh_exclusions_option();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove plugin from exclusions list once deactivated.
|
||||
*
|
||||
* @param string $plugin Plugin basename.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function remove_plugin_exclusions( string $plugin ) {
|
||||
if ( plugin_basename( WP_ROCKET_FILE ) === $plugin ) {
|
||||
return;
|
||||
}
|
||||
$this->site_list->remove_plugin_selection( $plugin );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle switch theme exclusions, remove the old theme exclusions and add the new one.
|
||||
*
|
||||
* @param string $new_name Name of the new theme.
|
||||
* @param WP_Theme $new_theme WP_Theme instance of the new theme.
|
||||
* @param WP_Theme $old_theme WP_Theme instance of the old theme.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle_switch_theme_exclusions( string $new_name, WP_Theme $new_theme, WP_Theme $old_theme ) {
|
||||
$this->site_list->replace_theme_selection( $new_theme, $old_theme );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add plugin exclusions with plugin activation for default checked plugins.
|
||||
*
|
||||
* @param string $plugin Plugin basename.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function add_plugin_exclusions( string $plugin ) {
|
||||
if ( plugin_basename( WP_ROCKET_FILE ) === $plugin ) {
|
||||
return;
|
||||
}
|
||||
$this->site_list->add_default_plugin_exclusions( $plugin );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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['delay_js'] = __( 'Delay JavaScript execution', 'rocket' );
|
||||
|
||||
return $fields;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,304 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\DelayJS;
|
||||
|
||||
use WP_Rocket\Admin\Options_Data;
|
||||
use WP_Rocket\Engine\Optimization\DynamicLists\DefaultLists\DataManager;
|
||||
use WP_Rocket\Engine\Optimization\RegexTrait;
|
||||
use WP_Rocket\Logger\Logger;
|
||||
|
||||
class HTML {
|
||||
use RegexTrait;
|
||||
|
||||
/**
|
||||
* Plugin options instance.
|
||||
*
|
||||
* @var Options_Data
|
||||
*/
|
||||
protected $options;
|
||||
|
||||
/**
|
||||
* DataManager instance
|
||||
*
|
||||
* @var DataManager
|
||||
*/
|
||||
private $data_manager;
|
||||
|
||||
/**
|
||||
* Array of excluded patterns from delay JS
|
||||
*
|
||||
* @since 3.9
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $excluded = [];
|
||||
|
||||
/**
|
||||
* Allowed type attributes.
|
||||
*
|
||||
* @var array Array of allowed type attributes.
|
||||
*/
|
||||
private $allowed_types = [
|
||||
'text/javascript',
|
||||
'module',
|
||||
'application/javascript',
|
||||
'application/ecmascript',
|
||||
'application/x-ecmascript',
|
||||
'application/x-javascript',
|
||||
'text/ecmascript',
|
||||
'text/javascript1.0',
|
||||
'text/javascript1.1',
|
||||
'text/javascript1.2',
|
||||
'text/javascript1.3',
|
||||
'text/javascript1.4',
|
||||
'text/javascript1.5',
|
||||
'text/jscript',
|
||||
'text/livescript',
|
||||
'text/x-ecmascript',
|
||||
'text/x-javascript',
|
||||
];
|
||||
|
||||
/**
|
||||
* Logger instance.
|
||||
*
|
||||
* @var Logger
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* Creates an instance of HTML.
|
||||
*
|
||||
* @param Options_Data $options Plugin options instance.
|
||||
* @param DataManager $data_manager DataManager instance.
|
||||
* @param Logger $logger Logger instance.
|
||||
*/
|
||||
public function __construct( Options_Data $options, DataManager $data_manager, Logger $logger ) {
|
||||
$this->options = $options;
|
||||
$this->data_manager = $data_manager;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjust HTML to have delay js structure.
|
||||
*
|
||||
* @since 3.9 Updated to use exclusions list instead of inclusions list.
|
||||
* @since 3.7
|
||||
*
|
||||
* @param string $html Buffer html for the page.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function delay_js( $html ): string {
|
||||
if ( ! $this->is_allowed() ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$this->set_exclusions();
|
||||
|
||||
$this->excluded = array_merge( $this->excluded, $this->options->get( 'delay_js_exclusions', [] ) );
|
||||
$this->excluded = array_merge( $this->excluded, $this->options->get( 'delay_js_exclusions_selected_exclusions', [] ) );
|
||||
|
||||
/**
|
||||
* Filters the delay JS exclusions array
|
||||
*
|
||||
* @since 3.9
|
||||
*
|
||||
* @param array $excluded Array of excluded patterns.
|
||||
*/
|
||||
$this->excluded = (array) apply_filters( 'rocket_delay_js_exclusions', $this->excluded );
|
||||
$this->excluded = array_map(
|
||||
function ( $value ) {
|
||||
if ( ! is_string( $value ) ) {
|
||||
$value = (string) $value;
|
||||
}
|
||||
|
||||
return str_replace(
|
||||
[ '+', '?ver', '#' ],
|
||||
[ '\+', '\?ver', '\#' ],
|
||||
$value
|
||||
);
|
||||
},
|
||||
$this->excluded
|
||||
);
|
||||
|
||||
return $this->parse( $html );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if is allowed to Delay JS.
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_allowed(): bool {
|
||||
|
||||
if ( rocket_bypass() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( rocket_get_constant( 'DONOTROCKETOPTIMIZE' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( is_rocket_post_excluded_option( 'delay_js' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) $this->options->get( 'delay_js', 0 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets Javascript to redirect IE visitors to the uncached page
|
||||
*
|
||||
* @since 3.9
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_ie_fallback(): string {
|
||||
return 'if(navigator.userAgent.match(/MSIE|Internet Explorer/i)||navigator.userAgent.match(/Trident\/7\..*?rv:11/i)){var href=document.location.href;if(!href.match(/[?&]nowprocket/)){if(href.indexOf("?")==-1){if(href.indexOf("#")==-1){document.location.href=href+"?nowprocket=1"}else{document.location.href=href.replace("#","?nowprocket=1#")}}else{if(href.indexOf("#")==-1){document.location.href=href+"&nowprocket=1"}else{document.location.href=href.replace("#","&nowprocket=1#")}}}}';
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the html and add/remove attributes from specific scripts.
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @param string $html Buffer html for the page.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function parse( $html ): string {
|
||||
|
||||
if ( empty( $html ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$result = $this->replace_xmp_tags( $html );
|
||||
$result = $this->replace_svg_tags( $result );
|
||||
|
||||
$replaced_html = preg_replace_callback(
|
||||
'/<\s*script(?<attr>\s*[^>]*?)?>(?<content>.*?)?<\s*\/\s*script\s*>/ims',
|
||||
[
|
||||
$this,
|
||||
'replace_scripts',
|
||||
],
|
||||
$result
|
||||
);
|
||||
if ( empty( $replaced_html ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$replaced_html = $this->restore_xmp_tags( $replaced_html );
|
||||
return $this->restore_svg_tags( $replaced_html );
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback method for preg_replace_callback that is used to adjust attributes for scripts.
|
||||
*
|
||||
* @since 3.9 Use exclusions list & fake type attribute.
|
||||
* @since 3.7
|
||||
*
|
||||
* @param array $matches Matches array for scripts regex.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function replace_scripts( $matches ): string {
|
||||
foreach ( $this->excluded as $pattern ) {
|
||||
if ( preg_match( "#{$pattern}#i", $matches[0] ) ) {
|
||||
$this->logger->debug( "DelayJS: Script {$matches[0]} excluded by $pattern" );
|
||||
return $matches[0];
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty( $matches['attr'] ) ) {
|
||||
return '<script type="rocketlazyloadscript">' . $matches['content'] . '</script>';
|
||||
}
|
||||
|
||||
$type_regex = '/type\s*=\s*(["\'])(?<type>.*)\1/i';
|
||||
preg_match( $type_regex . 'U', $matches['attr'], $type_matches );
|
||||
if (
|
||||
! empty( $type_matches )
|
||||
&&
|
||||
! empty( trim( $type_matches['type'] ) )
|
||||
&&
|
||||
! in_array( trim( $type_matches['type'] ), $this->allowed_types, true )
|
||||
) {
|
||||
return $matches[0];
|
||||
}
|
||||
|
||||
$matches['attr'] = preg_replace( $type_regex, 'data-rocket-type=$1$2$1', $matches['attr'] );
|
||||
// To remove type attribute without any value.
|
||||
$matches['attr'] = preg_replace( '/(\s+type\s+)|(^type\s+)|(\s+type$)/i', '', $matches['attr'] );
|
||||
|
||||
// Checks if script has src attribute so then treat as external script and replace src with data-rocket-src.
|
||||
$src_regex = '/src\s*=\s*(["\'])(.*)\1/i';
|
||||
$matches['attr'] = preg_replace( $src_regex, 'data-rocket-src=$1$2$1', $matches['attr'] );
|
||||
|
||||
return '<script type="rocketlazyloadscript" ' . trim( $matches['attr'] ) . '>' . $matches['content'] . '</script>';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Move meta charset to head if not found to the top of page content.
|
||||
*
|
||||
* @since 3.9.4
|
||||
*
|
||||
* @param string $html Html content.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function move_meta_charset_to_head( $html ): string {
|
||||
$meta_pattern = "#<meta[^h]*(http-equiv[^=]*=[^\'\"]*[\'\" ]Content-Type[\'\"][ ]*[^>]*|)(charset[^=]*=[ ]*[\'\" ]*[^\'\"> ][^\'\">]+[^\'\"> ][\'\" ]*|charset[^=]*=*[^\'\"> ][^\'\">]+[^\'\"> ])([^>]*|)>(?=.*</head>)#Usmi";
|
||||
|
||||
if ( ! preg_match( $meta_pattern, $html, $matches ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$replaced_html = preg_replace( "$meta_pattern", '', $html );
|
||||
|
||||
if ( empty( $replaced_html ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
if ( preg_match( '/<head\b/i', $replaced_html ) ) {
|
||||
$replaced_html = preg_replace( '/(<head\b[^>]*?>)/i', "\${1}{$matches[0]}", $replaced_html, 1 );
|
||||
|
||||
if ( empty( $replaced_html ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
return $replaced_html;
|
||||
}
|
||||
|
||||
if ( preg_match( '/<html\b/i', $replaced_html ) ) {
|
||||
$replaced_html = preg_replace( '/(<html\b[^>]*?>)/i', "\${1}{$matches[0]}", $replaced_html, 1 );
|
||||
|
||||
if ( empty( $replaced_html ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
return $replaced_html;
|
||||
}
|
||||
|
||||
$replaced_html = preg_replace( '/(<\w+)/', "{$matches[0]}\${1}", $replaced_html, 1 );
|
||||
|
||||
if ( empty( $replaced_html ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
return $replaced_html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get exclusions
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function set_exclusions() {
|
||||
$lists = $this->data_manager->get_lists();
|
||||
|
||||
$this->excluded = isset( $lists->delay_js_exclusions ) ? $lists->delay_js_exclusions : [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Engine\Optimization\DelayJS;
|
||||
|
||||
use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
|
||||
use WP_Rocket\Engine\Optimization\DelayJS\Admin\Settings;
|
||||
use WP_Rocket\Engine\Optimization\DelayJS\Admin\SiteList;
|
||||
use WP_Rocket\Engine\Optimization\DelayJS\Admin\Subscriber as AdminSubscriber;
|
||||
use WP_Rocket\Logger\Logger;
|
||||
|
||||
/**
|
||||
* Service provider for the WP Rocket Delay JS
|
||||
*
|
||||
* @since 3.7
|
||||
*/
|
||||
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 = [
|
||||
'delay_js_settings',
|
||||
'delay_js_admin_subscriber',
|
||||
'delay_js_html',
|
||||
'delay_js_subscriber',
|
||||
'delay_js_sitelist',
|
||||
];
|
||||
|
||||
/**
|
||||
* Registers items with the container
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register() {
|
||||
$this->getContainer()->add( 'delay_js_sitelist', SiteList::class )
|
||||
->addArgument( $this->getContainer()->get( 'dynamic_lists' ) )
|
||||
->addArgument( $this->getContainer()->get( 'options' ) )
|
||||
->addArgument( $this->getContainer()->get( 'options_api' ) );
|
||||
$this->getContainer()->add( 'delay_js_settings', Settings::class )
|
||||
->addArgument( $this->getContainer()->get( 'options_api' ) );
|
||||
$this->getContainer()->share( 'delay_js_admin_subscriber', AdminSubscriber::class )
|
||||
->addArgument( $this->getContainer()->get( 'delay_js_settings' ) )
|
||||
->addArgument( $this->getContainer()->get( 'delay_js_sitelist' ) )
|
||||
->addTag( 'admin_subscriber' );
|
||||
$this->getContainer()->add( 'delay_js_html', HTML::class )
|
||||
->addArgument( $this->getContainer()->get( 'options' ) )
|
||||
->addArgument( $this->getContainer()->get( 'dynamic_lists_defaultlists_data_manager' ) )
|
||||
->addArgument( new Logger() );
|
||||
$this->getContainer()->share( 'delay_js_subscriber', Subscriber::class )
|
||||
->addArgument( $this->getContainer()->get( 'delay_js_html' ) )
|
||||
->addArgument( rocket_direct_filesystem() )
|
||||
->addArgument( $this->getContainer()->get( 'options' ) )
|
||||
->addTag( 'front_subscriber' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\DelayJS;
|
||||
|
||||
use WP_Rocket\Admin\Options_Data;
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
|
||||
class Subscriber implements Subscriber_Interface {
|
||||
|
||||
/**
|
||||
* HTML instance.
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @var HTML
|
||||
*/
|
||||
private $html;
|
||||
|
||||
/**
|
||||
* WP_Filesystem_Direct instance.
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @var \WP_Filesystem_Direct
|
||||
*/
|
||||
private $filesystem;
|
||||
|
||||
/**
|
||||
* Options Data instance
|
||||
*
|
||||
* @var Options_Data
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* Script enqueued status.
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $is_enqueued = false;
|
||||
|
||||
/**
|
||||
* Subscriber constructor.
|
||||
*
|
||||
* @param HTML $html HTML Instance.
|
||||
* @param \WP_Filesystem_Direct $filesystem The Filesystem object.
|
||||
* @param Options_Data $options Options data instance.
|
||||
*/
|
||||
public function __construct( HTML $html, $filesystem, Options_Data $options ) {
|
||||
$this->html = $html;
|
||||
$this->filesystem = $filesystem;
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of events that this subscriber wants to listen to.
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
return [
|
||||
'rocket_buffer' => [
|
||||
[ 'delay_js', 26 ],
|
||||
[ 'add_delay_js_script', 26 ],
|
||||
],
|
||||
'pre_get_rocket_option_minify_concatenate_js' => 'maybe_disable_option',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies scripts HTML to apply delay JS attribute
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @param string $buffer_html Html for the page.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function delay_js( $buffer_html ) {
|
||||
return $this->html->delay_js( $buffer_html );
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the inline script to the head when the option is enabled.
|
||||
*
|
||||
* @since 3.9.4 Move meta charset to head.
|
||||
* @since 3.9 Hooked on rocket_buffer, display the script right after <head>
|
||||
* @since 3.7
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function add_delay_js_script( $html ): string {
|
||||
if ( ! $this->html->is_allowed() ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$pattern = '/<head[^>]*>/i';
|
||||
|
||||
$lazyload_script = $this->filesystem->get_contents( rocket_get_constant( 'WP_ROCKET_PATH' ) . 'assets/js/lazyload-scripts.min.js' );
|
||||
|
||||
$replaced_html = $html;
|
||||
|
||||
if ( false !== $lazyload_script ) {
|
||||
$replaced_html = preg_replace( $pattern, "$0<script>{$lazyload_script}</script>", $replaced_html, 1 );
|
||||
|
||||
if ( empty( $replaced_html ) ) {
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
|
||||
$replaced_html = preg_replace( $pattern, '$0<script>' . $this->html->get_ie_fallback() . '</script>', $replaced_html, 1 );
|
||||
|
||||
if ( empty( $replaced_html ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
return $this->html->move_meta_charset_to_head( $replaced_html );
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables defer JS if delay JS is enabled
|
||||
*
|
||||
* @since 3.9
|
||||
*
|
||||
* @param null $value Original value. Should be always null.
|
||||
*
|
||||
* @return null|false
|
||||
*/
|
||||
public function maybe_disable_option( $value ) {
|
||||
if ( $this->html->is_allowed() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user