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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user