This commit is contained in:
2024-05-20 15:37:46 +03:00
commit 00b7dbd0b7
10404 changed files with 3285853 additions and 0 deletions

View File

@ -0,0 +1,353 @@
<?php
namespace Sphere\Debloat;
use Sphere\Debloat\Admin\Cache;
use Sphere\Debloat\Admin\OptionsData;
/**
* Admin initialization.
*
* @author asadkn
* @since 1.0.0
*/
class Admin
{
/**
* @var Sphere\Debloat\Admin\Cache
*/
protected $cache;
/**
* Setup hooks
*/
public function init()
{
$this->cache = new Cache;
$this->cache->init();
add_action('cmb2_admin_init', [$this, 'setup_options']);
// Enqueue at a lower priority to be after CMB2.
add_action('admin_enqueue_scripts', [$this, 'register_assets'], 99);
// Page to delete cache.
add_action('admin_menu', function() {
add_submenu_page(
'',
'Delete Cache',
'Delete Cache',
'manage_options',
'debloat-delete-cache',
[$this, 'delete_cache']
);
});
// Empty cache on save.
add_action('cmb2_save_options-page_fields', [$this, '_delete_cache']);
/**
* Fix: CMB2 doesn't save unchecked making default => true impossible.
*/
add_filter('cmb2_sanitize_checkbox', function($override, $value) {
return is_null($value) ? '0' : $value;
}, 20, 2);
// Custom CMB2 field for manual callback
add_action('cmb2_render_manual', function($field) {
// Add attributes to an empty span for cmb2-conditional
if (!empty($field->args['attributes'])) {
printf('<meta name="%s" %s />',
$field->args('id'),
\CMB2_Utils::concat_attrs($field->args('attributes'))
);
}
if (!empty($field->args['render_html']) && is_callable($field->args['render_html'])) {
call_user_func($field->args['render_html'], $field);
}
if (!empty($field->args['desc'])) {
echo '<p class="cmb2-metabox-description">' . esc_html($field->args['desc']) . '</p>';
}
});
}
/**
* Register admin assets
*/
public function register_assets()
{
// Specific assets for option pages only
if (!empty($_GET['page']) && strpos($_GET['page'], 'debloat_options') !== false) {
wp_enqueue_script(
'debloat-cmb2-conditionals',
Plugin::get_instance()->dir_url . 'js/admin/cmb2-conditionals.js',
['jquery'],
Plugin::VERSION
);
wp_enqueue_script(
'debloat-options',
Plugin::get_instance()->dir_url . 'js/admin/options.js',
['jquery', 'debloat-cmb2-conditionals'],
Plugin::VERSION
);
wp_enqueue_style(
'debloat-admin-cmb2',
Plugin::get_instance()->dir_url . 'css/admin/cmb2.css',
['cmb2-styles'],
Plugin::VERSION
);
}
}
/**
* Delete Cache page.
*/
public function delete_cache()
{
check_admin_referer('debloat_delete_cache');
$this->_delete_cache();
echo '
<h2>Clearing Cache</h2>
<p>Caches cleared. You may also have to clear your cache plugins.</p>
<a href="' . esc_url(admin_url('admin.php?page=debloat_options')) . '">Back to Options</a>';
}
/**
* Callback: Delete the cache.
*
* @access private
*/
public function _delete_cache()
{
$this->cache->empty();
/**
* Hook after deleting cache.
*/
do_action('debloat/after_delete_cache');
}
/**
* Setup admin options with CMB2
*/
public function setup_options()
{
// Configure admin options
$options = new_cmb2_box([
'id' => 'debloat_options',
'title' => esc_html__('Debloat Plugin Settings', 'debloat'),
'object_types' => ['options-page'],
'option_key' => 'debloat_options',
'parent_slug' => 'options-general.php',
'menu_title' => esc_html__('Debloat: Optimize', 'debloat'),
'tab_group' => 'debloat_options',
'tab_title' => esc_html__('Optimize CSS', 'debloat'),
'classes' => 'sphere-cmb2-wrap',
'display_cb' => [$this, 'render_options_page'],
]);
$this->add_options(
OptionsData::get_css(),
$options
);
// Configure admin options
$js_options = new_cmb2_box([
'id' => 'debloat_options_js',
'title' => esc_html__('Optimize JS', 'debloat'),
'object_types' => ['options-page'],
'option_key' => 'debloat_options_js',
'parent_slug' => 'debloat_options',
'menu_title' => esc_html__('Optimize JS', 'debloat'),
'tab_group' => 'debloat_options',
'tab_title' => esc_html__('Optimize JS', 'debloat'),
'classes' => 'sphere-cmb2-wrap',
'display_cb' => [$this, 'render_options_page'],
]);
$this->add_options(
OptionsData::get_js(),
$js_options
);
// Configure admin options
$general_options = new_cmb2_box([
'id' => 'debloat_options_general',
'title' => esc_html__('General Settings', 'debloat'),
'object_types' => ['options-page'],
'option_key' => 'debloat_options_general',
'parent_slug' => 'debloat_options',
'menu_title' => esc_html__('General Settings', 'debloat'),
'tab_group' => 'debloat_options',
'tab_title' => esc_html__('General Settings', 'debloat'),
'display_cb' => [$this, 'render_options_page'],
'classes' => 'sphere-cmb2-wrap'
]);
$this->add_options(
OptionsData::get_general(),
$general_options
);
do_action('debloat/admin/after_options', $options);
}
/**
* Add options to CMB2 array.
*
* @param array $options
* @param \CMB2 $object
* @return void
*/
protected function add_options($options, $object)
{
return array_map(
function($option) use ($object) {
if (isset($option['attributes']['data-conditional-id'])) {
$condition = &$option['attributes']['data-conditional-id'];
if (is_array($condition)) {
$condition = json_encode($condition);
}
}
$field_id = $object->add_field($option);
if ($option['type'] === 'group') {
$this->add_option_group($option, $field_id, $object);
}
},
$options
);
}
protected function add_option_group($option, $group_id, $object)
{
if ($option['id'] === 'allow_conditionals_data') {
$object->add_group_field($group_id, [
'id' => 'type',
'name' => esc_html__('Condition Type', 'debloat'),
'type' => 'radio',
'default' => 'class',
'options' => [
'class' => esc_html__('Class - If a class (in "condition match") exists in HTML, keep classes matching "selector match".', 'debloat'),
'prefix' => esc_html__('Prefix - Condition matches the first class and keeps all the used child classes. Example: .s-dark will keep .s-dark .site-header.', 'debloat'),
],
]);
$object->add_group_field($group_id, [
'id' => 'match',
'name' => esc_html__('Condition Match', 'debloat'),
'desc' => esc_html__('Required. Usually a single class, example:', 'debloat') . '<code>.my-class</code>',
'type' => 'text',
'default' => '',
]);
$object->add_group_field($group_id, [
'id' => 'search',
'name' => esc_html__('Selectors Match', 'debloat'),
'desc' => esc_html__('Enter one per line. See example matchings in "Always Keep Selectors" above.', 'debloat'),
'type' => 'textarea_small',
'default' => '',
'attributes' => [
'data-conditional-id' => json_encode([$group_id, 'type']),
'data-conditional-value' => 'class'
]
]);
}
}
public function render_options_page($hookup)
{
?>
<div class="cmb2-options-page debloat-options option-<?php echo esc_attr( sanitize_html_class( $hookup->option_key ) ); ?>">
<div class="wrap">
<?php if ( $hookup->cmb->prop( 'title' ) ) : ?>
<h2><?php echo wp_kses_post( $hookup->cmb->prop( 'title' ) ); ?></h2>
<?php endif; ?>
</div>
<div class="wrap"><?php $hookup->options_page_tab_nav_output(); ?></div>
<div class="debloat-inner-wrap">
<form class="cmb-form debloat-options-form" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="POST" id="<?php echo esc_attr($hookup->cmb->cmb_id); ?>" enctype="multipart/form-data" encoding="multipart/form-data">
<input type="hidden" name="action" value="<?php echo esc_attr( $hookup->option_key ); ?>">
<div class="sphere-cmb2-wrap debloat-intro-info">
<div class="cmb2-wrap cmb2-metabox">
<div class="cmb-row">
<h3>Important: Debloat Plugin</h3>
<p>
This plugin is for advanced users. The features "Remove Unused CSS" and "Delay JS" are especially for advanced users only.
</p>
<ol>
<li>Use a cache plugin like W3 Total Cache, WP Super Cache, etc. <strong>Required</strong> for Remove Unused CSS feature.</li>
<li>Do <strong>NOT</strong> enable minification, CSS, or JS optimization via another plugin.</li>
<li>If your theme doesn't have it built-in, use a Lazyload plugin for images.</li>
</ol>
</div>
</div>
</div>
<?php $hookup->options_page_metabox(); ?>
<?php submit_button( esc_attr( $hookup->cmb->prop( 'save_button' ) ), 'primary', 'submit-cmb' ); ?>
</form>
<div class="debloat-sidebar">
<?php $this->cache_info(); ?>
</div>
</div>
</div>
<?php
}
public function cache_info()
{
$js_cache = Plugin::file_cache()->get_stats('js');
$css_cache = Plugin::file_cache()->get_stats('css');
// Number of css sheets in cache.
$css_sheets = count($this->cache->get_transients());
?>
<div class="sphere-cmb2-wrap debloat-cache-info">
<div class="cmb2-wrap cmb2-metabox">
<div class="cmb-row cmb-type-title">
<div class="cmb-td">
<h3 class="cmb2-metabox-title">
<?php esc_html_e('Cache Stats', 'debloat'); ?>
</h3>
</div>
</div>
<div class="cmb-row">
<?php if (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG): ?>
<p><strong>Minification Disabled</strong>: SCRIPT_DEBUG is enabled (likely in wp-config.php).</p>
<?php endif; ?>
<div class="cache-stats">
<p><?php printf(esc_html__('Minified CSS files: %d', 'debloat'), $css_cache); ?></p>
<p><?php printf(esc_html__('Minified JS files: %d', 'debloat'), $js_cache); ?></p>
<p><?php printf(esc_html__('Processed CSS Sheets: %d', 'debloat'), $css_sheets); ?></p>
</div>
<a href="<?php echo wp_nonce_url(admin_url('admin.php?page=debloat-delete-cache'), 'debloat_delete_cache'); ?>"
class="button button-secondary" style="margin-top: 10px;">
<?php echo esc_html('Empty All Cache', 'debloat'); ?>
</a>
</p>
</div>
</div>
</div>
<?php
}
}

View File

@ -0,0 +1,183 @@
<?php
namespace Sphere\Debloat\Admin;
use Sphere\Debloat\Plugin;
/**
* Cache clear, stats and similar for admin area.
*
* @author asadkn
* @since 1.0.0
*/
class Cache
{
protected $deleting = false;
public function init()
{
$this->register_clear_hooks();
}
/**
* Register hooks to clear cache.
*
* @return void
*/
public function register_clear_hooks()
{
/**
* Use this hook to clear caches externally.
*/
add_action('debloat/empty_caches', [$this, 'empty']);
/**
* Other plugin hooks to empty cache on.
*/
$hooks = [
// WP Rocket.
'after_rocket_clean_domain',
// W3 Total Cache.
'w3tc_flush_all',
// SGF plugin font cache delete.
'sgf/after_delete_cache',
];
foreach ($hooks as $hook) {
add_action($hook, [$this, 'empty']);
}
}
/**
* Get all the debloat cache transients.
*
* @return array
*/
public function get_transients()
{
global $wpdb;
return (array) $wpdb->get_results(
"SELECT `option_name` FROM {$wpdb->options} WHERE `option_name` LIKE '_transient_debloat_sheet_cache_%'",
ARRAY_A
);
}
/**
* Delete all the debloat cache transients.
*
* @return void
*/
protected function delete_transients()
{
foreach ($this->get_transients() as $transient) {
$transient = str_replace('_transient_', '', $transient['option_name']);
\delete_transient($transient);
}
}
/**
* Delete all types of caches.
*
* @return void
*/
public function empty()
{
// Already clearing the caches. Needed as we hook into plugins clear methods,
// but also call them ourselves on cache clear.
if ($this->deleting) {
return;
}
// One of the hooks set via register_clear_hooks() may fire.
$this->deleting = true;
$this->delete_transients();
// Delete files cache.
Plugin::file_cache()->delete_cache('js');
Plugin::file_cache()->delete_cache('css');
// Clear cache plugins.
$this->empty_cache_plugins();
$this->deleting = false;
}
/**
* Empty external caches from plugins and hosts.
*
* @return void
*/
protected function empty_cache_plugins()
{
// WP Super Cache.
if (function_exists('wp_cache_clear_cache')) {
wp_cache_clear_cache(
is_multisite() ? get_current_blog_id() : 0
);
}
// W3 Total Cache.
if (function_exists('w3tc_pgcache_flush')) {
w3tc_pgcache_flush();
}
// WP Rocket.
if (function_exists('rocket_clean_domain')) {
rocket_clean_domain();
}
// WP Fastest Cache.
if (function_exists('wpfc_clear_all_cache')) {
wpfc_clear_all_cache();
}
// Swift Performance plugin.
if (class_exists('\Swift_Performance_Cache') && is_callable(['\Swift_Performance_Cache', 'clear_all_cache'])) {
\Swift_Performance_Cache::clear_all_cache();
}
// LiteSpeed Cache.
if (class_exists('\LiteSpeed_Cache_API') && is_callable(['\LiteSpeed_Cache_API', 'purge_all'])) {
\LiteSpeed_Cache_API::purge_all();
}
// Cache Enabler.
if (class_exists('\Cache_Enabler') && is_callable(['\Cache_Enabler', 'clear_total_cache'])) {
\Cache_Enabler::clear_total_cache();
}
// Comet cache.
if (class_exists('\comet_cache') && is_callable(['\comet_cache', 'clear'])) {
\comet_cache::clear();
}
// RT Nginx Helper plugin.
if (defined('NGINX_HELPER_BASENAME')) {
do_action('rt_nginx_helper_purge_all');
}
// Hummingbird
if (class_exists('\Hummingbird\WP_Hummingbird') && is_callable(['\Hummingbird\WP_Hummingbird', 'flush_cache'])) {
\Hummingbird\WP_Hummingbird::flush_cache();
}
// Pagely.
if (class_exists('\PagelyCachePurge') && is_callable(['\PagelyCachePurge', 'purgeAll'])) {
\PagelyCachePurge::purgeAll();
}
// WPEngine
if (class_exists('\WpeCommon')) {
is_callable(['\WpeCommon', 'purge_memcached']) && \WpeCommon::purge_memcached();
is_callable(['\WpeCommon', 'purge_varnish_cache']) && \WpeCommon::purge_varnish_cache();
}
// SiteGround.
if (function_exists('sg_cachepress_purge_cache')) {
sg_cachepress_purge_cache();
}
}
}

View File

@ -0,0 +1,446 @@
<?php
namespace Sphere\Debloat\Admin;
/**
* Options data.
*/
class OptionsData
{
/**
* Common shared data and options.
*
* @param string $key
* @return array
*/
public static function get_common($key = '')
{
$_common = [];
$_common['enable_on'] = [
'all' => esc_html__('All Pages', 'debloat'),
'single' => esc_html__('Single Post/Article', 'debloat'),
'pages' => esc_html__('Pages', 'delobat'),
'home' => esc_html__('Homepage', 'delobat'),
'archives' => esc_html__('Archives', 'delobat'),
'categories' => esc_html__('Categories', 'delobat'),
'search' => esc_html__('Search', 'delobat'),
];
return $key ? $_common[$key] : $_common;
}
public static function get_css()
{
$options = [];
$options[] = [
'name' => esc_html__('Optimize CSS', 'debloat'),
// 'description' => 'foo',
'type' => 'title',
'id' => '_optimize_css',
];
$options[] = [
'id' => 'optimize_css',
'name' => esc_html__('Fix Render-Blocking CSS', 'debloat'),
'desc' => esc_html__('Enable CSS Optimizations to fix Render-blocking CSS.', 'debloat'),
'type' => 'checkbox',
'default' => 0,
];
$options[] = [
'id' => 'optimize_css_to_inline',
'name' => esc_html__('Inline Optimized CSS', 'debloat'),
'desc' => esc_html__('Inline the CSS to prevent flash of unstyled content. Highly recommended.', 'debloat'),
'type' => 'checkbox',
'default' => 1,
'attributes' => ['data-conditional-id' => 'optimize_css'],
];
$options[] = [
'id' => 'optimize_gfonts_inline',
'name' => esc_html__('Inline Google Fonts CSS', 'debloat'),
'desc' => esc_html__('Inline the Google Fonts CSS for a big boost on FCP and slight on LCP on mobile. Highly recommended.', 'debloat'),
'type' => 'checkbox',
'default' => 1,
'attributes' => ['data-conditional-id' => 'optimize_css'],
];
$options[] = [
'id' => 'optimize_css_minify',
'name' => esc_html__('Minify CSS', 'debloat'),
'desc' => esc_html__('Minify CSS to reduced the CSS size.', 'debloat'),
'type' => 'checkbox',
'default' => 1,
'attributes' => ['data-conditional-id' => 'optimize_css'],
];
$options[] = [
'id' => 'optimize_css_excludes',
'name' => esc_html__('Exclude Styles', 'debloat'),
'desc' =>
esc_html__('Enter one per line to exclude certain CSS files from this optimizations. Examples:', 'debloat')
. ' <code>id:my-css-id</code>
<br /><code>wp-content/themes/my-theme/style.css</code>
<br /><code>wp-content/themes/my-theme*</code>
',
'type' => 'textarea_small',
'default' => '',
'attributes' => ['data-conditional-id' => 'optimize_css'],
];
$options[] = [
'id' => 'integrations_css',
'name' => esc_html__('Enable Plugin Integrations', 'debloat'),
'desc' => esc_html__('Special pre-made rules for CSS, specific to plugins, are applied if enabled.', 'debloat'),
'type' => 'multicheck_inline',
'options' => [
'elementor' => 'Elementor',
'wpbakery' => 'WPBakery Page Builder',
],
'default' => ['elementor', 'wpbakery'],
'select_all_button' => false,
];
$options[] = [
'id' => 'optimize_gfonts',
'name' => esc_html__('Optimize Google Fonts', 'debloat'),
'desc' => esc_html__('Add preconnect hints and add display swap for Google Fonts.', 'debloat'),
'type' => 'checkbox',
'default' => 1,
];
$options[] = [
'name' => esc_html__('Optimize CSS: Remove Unused', 'debloat'),
// 'description' => 'foo',
'type' => 'title',
'id' => '_remove_unused',
];
$options[] = [
'id' => 'remove_css',
'name' => esc_html__('Remove Unused CSS', 'debloat'),
'desc' => esc_html__('This is an expensive process. DO NOT use without a cache plugin.', 'debloat'),
'type' => 'checkbox',
'default' => 0,
];
$options[] = [
'id' => 'remove_css_all',
'name' => esc_html__('Remove from All Stylesheets', 'debloat'),
'desc' => esc_html__('WARNING: Only use if you are sure your plugins and themes dont add classes using JS. May also be enabled when delay loading all the original CSS.', 'debloat'),
'type' => 'checkbox',
'default' => 0,
'attributes' => ['data-conditional-id' => 'remove_css'],
];
$options[] = [
'id' => 'remove_css_plugins',
'name' => esc_html__('Enable for Plugins CSS', 'debloat'),
'desc' => esc_html__('Removed unused CSS on all plugins CSS files.', 'debloat'),
'type' => 'checkbox',
'default' => 0,
'attributes' => [
'data-conditional-id' => [
['key' => 'remove_css'],
['key' => 'remove_css_all', 'value' => 'off'],
]
],
];
$options[] = [
'id' => 'remove_css_theme',
'name' => esc_html__('Enable for Theme CSS', 'debloat'),
'desc' => esc_html__('Removed unused CSS from all theme CSS files.', 'debloat'),
'type' => 'checkbox',
'default' => 0,
'attributes' => [
'data-conditional-id' => [
['key' => 'remove_css'],
['key' => 'remove_css_all', 'value' => 'off'],
]
],
];
$options[] = [
'id' => 'remove_css_includes',
'name' => esc_html__('Target Stylesheets', 'debloat'),
'desc' =>
esc_html__('Will remove unused CSS from these targets. You may use an ID or the part of the URL. Examples:', 'debloat')
. ' <code>id:my-css-id</code>
<br /><code>wp-content/themes/my-theme/style.css</code>
<br /><code>wp-content/themes/my-theme*</code>: All theme stylesheets.
<br /><code>plugins/plugin-slug/*</code>: All stylesheets for plugin-slug.
',
'type' => 'textarea_small',
'default' => 'id:wp-block-library',
'attributes' => [
'data-conditional-id' => [
['key' => 'remove_css'],
['key' => 'remove_css_all', 'value' => 'off'],
]
],
];
$options[] = [
'id' => 'remove_css_excludes',
'name' => esc_html__('Exclude Stylesheets', 'debloat'),
'desc' =>
esc_html__('Enter one per line to exclude certain CSS files from this optimizations. Examples:', 'debloat')
. ' <code>id:my-css-id</code>
<br /><code>wp-content/themes/my-theme/style.css</code>
<br /><code>wp-content/themes/my-theme*</code>
',
'type' => 'textarea_small',
'default' => '',
'attributes' => ['data-conditional-id' => 'remove_css'],
];
$options[] = [
'id' => 'allow_css_selectors',
'name' => esc_html__('Always Keep Selectors', 'debloat'),
'desc' =>
esc_html__('Enter one per line. Partial or full matches for selectors (if any of these keywords found, the selector will be kept). Examples:', 'debloat')
. ' <code>.myclass</code>
<br /><code>.myclass*</code>: Will match selectors starting with .myclass, .myclass-xyz, .myclass_xyz etc.
<br /><code>.myclass *</code>: Selectors starting with .myclass, .myclass .sub-class and so on.
<br /><code>*.myclass *</code>: For matching .xyz .myclass, .myclass, .xyz .myclass .xyz and so on.
',
'type' => 'textarea_small',
'default' => '',
'attributes' => ['data-conditional-id' => 'remove_css'],
];
$options[] = [
'id' => 'allow_css_conditionals',
'name' => esc_html__('Advanced: Conditionally Keep Selectors', 'debloat'),
'desc' => 'Add advanced conditions.',
'type' => 'checkbox',
'default' => 0,
'attributes' => ['data-conditional-id' => 'remove_css'],
];
$options[] = [
'id' => 'allow_conditionals_data',
'name' => '',
'desc' => 'Keep selector if a certain condition is true. For example, condition type class with match <code>.mfp-lightbox</code> can be used to search for <code>.mfp-</code> to keep all the CSS selectors that have .mfp- in selector.',
'type' => 'group',
'default' => [],
'attributes' => ['data-conditional-id' => 'remove_css'],
'options' => [
'group_title' => 'Condition {#}',
'add_button' => esc_html__('Add Condition', 'debloat'),
'remove_button' => esc_html__('Remove', 'debloat'),
'closed' => true,
]
];
$options[] = [
'id' => 'remove_css_on',
'name' => esc_html__('Remove CSS On', 'debloat'),
'desc' => esc_html__('Pages where unused CSS should be removed.', 'debloat'),
'type' => 'multicheck',
'options' => self::get_common('enable_on'),
'default' => ['all'],
'select_all_button' => false,
'attributes' => ['data-conditional-id' => 'remove_css'],
];
$options[] = [
'id' => 'delay_css_load',
'name' => esc_html__('Delay load Original CSS', 'debloat'),
'desc' => esc_html__('Delay-loading all of the original CSS might be needed in situations where there are too many JS-based CSS classes that are added later such as sliders, that you cannot track down and add to exclusions right now. Or on pages that may have Auto-load Next Post.', 'debloat'),
'type' => 'checkbox',
'default' => 0,
'attributes' => ['data-conditional-id' => 'remove_css'],
];
$options[] = [
'id' => 'delay_css_on',
'name' => esc_html__('Delay load Original On', 'debloat'),
'desc' => esc_html__('Pages where original CSS should be delayed load.', 'debloat'),
'type' => 'multicheck',
'options' => self::get_common('enable_on'),
'default' => ['all'],
'select_all_button' => false,
'attributes' => ['data-conditional-id' => 'delay_css_load'],
];
return $options;
}
public static function get_js()
{
$options = [];
/**
* Optimize JS
*/
$options[] = [
'name' => esc_html__('Optimize JS', 'debloat'),
// 'description' => 'foo',
'type' => 'title',
'id' => '_defer_js',
];
$options[] = [
'id' => 'defer_js',
'name' => esc_html__('Defer Javascript', 'debloat'),
'desc' => esc_html__('Delay JS execution till HTML is loaded to fix Render-Blocking JS issues.', 'debloat'),
'type' => 'checkbox',
'default' => 0,
];
$options[] = [
'id' => 'defer_js_excludes',
'name' => esc_html__('Exclude Scripts', 'debloat'),
'desc' => esc_html__('Enter one per line to exclude certain JS files from being deferred.', 'debloat'),
'type' => 'textarea_small',
'default' => '',
'attributes' => ['data-conditional-id' => 'defer_js'],
];
$options[] = [
'id' => 'defer_js_inline',
'name' => esc_html__('Defer Inline JS', 'debloat'),
'desc' => sprintf(
'%s<p><strong>%s</strong> %s</p>',
esc_html__('Defer all inline JS.', 'debloat'),
esc_html__('Note:', 'debloat'),
esc_html__('Normally not needed. All correct dependent inline scripts are deferred by default. Enable if inline JS not enqueued using WordPress enqueue functions.', 'debloat')
),
'type' => 'checkbox',
'default' => 0,
'attributes' => ['data-conditional-id' => 'defer_js'],
];
$options[] = [
'id' => 'minify_js',
'name' => esc_html__('Minify Javascript', 'debloat'),
'desc' => esc_html__('Minify all the deferred or delayed JS files.', 'debloat'),
'type' => 'checkbox',
'default' => 0,
];
$options[] = [
'id' => 'integrations_js',
'name' => esc_html__('Enable Plugin Integrations', 'debloat'),
'desc' => esc_html__('Special pre-made rules for javascript, specific to plugins, are applied if enabled.', 'debloat'),
'type' => 'multicheck_inline',
'options' => [
'elementor' => 'Elementor',
'wpbakery' => 'WPBakery Page Builder',
],
'default' => ['elementor', 'wpbakery'],
'select_all_button' => false,
];
/**
* Delay JS
*/
$options[] = [
'name' => esc_html__('Delay Load JS', 'debloat'),
// 'description' => 'foo',
'type' => 'title',
'id' => '_delay_js',
];
$options[] = [
'id' => 'delay_js',
'name' => esc_html__('Delay Javascript', 'debloat'),
'desc' => esc_html__('Delay execution of the targeted JS files until user interaction.', 'debloat'),
'type' => 'checkbox',
'default' => 0,
];
$options[] = [
'id' => 'delay_js_max',
'name' => esc_html__('Maximum Delay (in seconds)', 'debloat'),
'desc' => esc_html__('Max seconds to wait for interaction until delayed JS is loaded anyways.', 'debloat'),
'type' => 'text_small',
'default' => '',
'attributes' => [
'type' => 'number',
'min' => 0,
'data-conditional-id' => 'delay_js'
],
];
$options[] = [
'id' => 'delay_js_all',
'name' => esc_html__('Delay All Scripts', 'debloat'),
'desc' => esc_html__('CAREFUL. Delays all JS files. Its better to target scripts manually below. If there are scripts that setup sliders/carousels, animations, or other similar things, these won\'t be setup until the first user interaction.', 'debloat'),
'type' => 'checkbox',
'default' => 0,
'attributes' => ['data-conditional-id' => 'delay_js'],
];
$options[] = [
'id' => 'delay_js_includes',
'name' => esc_html__('Target Scripts', 'debloat'),
'desc' =>
esc_html__('Will delay from these scripts. You may use an ID, part of the URL, or any code for inline scripts. One per line. Examples:', 'debloat')
. ' <code>id:my-js-id</code>
<br /><code>my-theme/js-file.js</code>
<br /><code>wp-content/themes/my-theme/*</code>: All theme JS files.
<br /><code>plugins/plugin-slug/*</code>: All JS files for plugin-slug.
',
'type' => 'textarea_small',
'default' => implode("\n", [
'twitter.com/widgets.js',
'gtm.js',
'id:google_gtagjs'
]),
'attributes' => [
'data-conditional-id' => [
['key' => 'delay_js'],
['key' => 'delay_js_all', 'value' => 'off'],
]
],
];
$options[] = [
'id' => 'delay_js_excludes',
'name' => esc_html__('Exclude Scripts', 'debloat'),
'desc' =>
esc_html__('Enter one per line to exclude certain scripts from this optimizations. Examples:', 'debloat')
. '<code>id:my-js-id</code>
<br /><code>my-theme/js-file.js</code>
<br /><code>wp-content/themes/my-theme/*</code>: All theme JS files.
<br /><code>someStringInJs</code>: Exclude by some text in inline JS tag.
',
'type' => 'textarea_small',
'default' => '',
'attributes' => ['data-conditional-id' => 'delay_js'],
];
$options[] = [
'id' => 'delay_js_adsense',
'name' => esc_html__('Delay Google Ads', 'debloat'),
'desc' => esc_html__('Delay Google Adsense until first interaction. Note: This may not be ideal if you have ads that are in header.', 'debloat'),
'type' => 'checkbox',
'default' => 1,
'attributes' => ['data-conditional-id' => 'delay_js'],
];
return $options;
}
public static function get_general()
{
$options = [];
$options[] = [
'name' => esc_html__('Disable for Admins', 'debloat'),
'desc' => esc_html__('Disable processing for logged in admin users or any user with capability "manage_options". (Useful if using a pagebuilder that conflicts)', 'debloat'),
'id' => 'disable_for_admins',
'type' => 'checkbox',
'default' => 0,
];
return $options;
}
public static function get_all()
{
return array_merge(
self::get_css(),
self::get_js()
);
}
}