Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wp-rocket-smart-preload",
"version": "1.1.0",
"version": "1.2.0",
"description": "Smart preload for WP Rocket",
"type": "module",
"scripts": {
Expand Down
17 changes: 16 additions & 1 deletion src/settings-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,27 @@ function wp_rocket_smart_preload_settings_page()
$urls_to_always_include = get_option('rsp_pages_to_always_include', []);
$sitemap_page_limit = get_option('rsp_sitemap_page_limit', RSP_SITEMAP_PAGE_DEFAULT_LIMIT);
$deactivate_ip_protection = get_option('rsp_deactivate_ip_protection', 0);
$apply_rucss = get_option('rsp_apply_rucss', 0);
?>
<div class="wrap">
<h2>WP Rocket - Smart Preload Settings</h2>
<form id="plugin-form" method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
<?php wp_nonce_field('save_settings', 'wp_rocket_nonce'); ?>

<table class="form-table">
<tr valign="top">
<th scope="row">Apply to Remove Unused CSS</th>
<td>
<label class="switch">
<input type="checkbox" id="apply-rucss" name="apply-rucss" <?php checked($apply_rucss, '1'); ?>>
<span class="slider round"></span>
</label>
<p class="description">Enable to also limit Remove Unused CSS to be applied to the most visited pages.
<br>
<strong>Note:</strong> Home page URL is always included.
</p>
</td>
</tr>
<tr valign="top">
<th scope="row">Always Preload These URLs</th>
<td>
Expand Down Expand Up @@ -118,11 +132,12 @@ function save_wp_rocket_smart_preload_settings()

// Sanitize IP Protection
$ip_protection = isset($_POST['ip-protection']) ? '0' : '1';

$apply_rucss = isset($_POST['apply-rucss']) ? '1' : '0';
// Save settings
update_option('rsp_pages_to_always_include', $urls);
update_option('rsp_sitemap_page_limit', $url_limit);
update_option('rsp_deactivate_ip_protection', $ip_protection);
update_option('rsp_apply_rucss', $apply_rucss);

// Redirect back to the settings page
$redirect_url = add_query_arg('page', 'wp-rocket-smart-preload', admin_url('options-general.php'));
Expand Down
33 changes: 33 additions & 0 deletions src/wp-rocket-smart-preload.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
add_filter('rsp_deactivate_ip_protection', function ($deactivate_ip_protection) {
return $deactivate_ip_protection; // Edit this (return true) to deactivate the IP protection feature. (This feature prevents counting fake visits due to multiple page refreshes from the same IP address)
}, 0);
add_filter('rsp_apply_rucss', function ($apply_rucss) {
return $apply_rucss; // Edit this (return true) to apply the Remove Unused CSS feature to the most visited pages.
}, 0);
// STOP EDITING


Expand Down Expand Up @@ -265,6 +268,7 @@ function rsp_remove_database_options()
delete_option('rsp_pages_to_always_include');
delete_option('rsp_sitemap_page_limit');
delete_option('rsp_deactivate_ip_protection');
delete_option('rsp_apply_rucss');
}
/**
* Removes transients related to WP Rocket - Smart Preload.
Expand Down Expand Up @@ -663,6 +667,35 @@ function vaidate_accepted_frequencies($value)
$accepted_values = ['hourly', 'twicedaily', 'daily', 'weekly'];
return in_array($value, $accepted_values, true) ? $value : 'daily';
}

/**
* Applies Smart Preload to the Remove Unused CSS (RUCSS) process.
*
* Prevents the URL to be added to RUCSS table if not a part of the most visited pages.
*
* @since 1.2.0
*
* @return int
*/
function rsp_apply_to_rucss($value)
{
if (!is_admin() || !wp_doing_ajax() || !is_404()) {
return $value;
}
$apply_rucss = (bool) apply_filters('rsp_apply_rucss', get_option('rsp_apply_rucss', 0));
if (!$apply_rucss) {
return $value;
}
$url = untrailingslashit("{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}");
$sitemap_page_limit = apply_filters('rsp_sitemap_page_limit', get_option('rsp_sitemap_page_limit', RSP_SITEMAP_PAGE_DEFAULT_LIMIT));
$sitemap_page_limit = validate_positive_integer($sitemap_page_limit, RSP_SITEMAP_PAGE_DEFAULT_LIMIT);
$urls_to_preload = rsp_get_urls_to_preload($sitemap_page_limit);
if (! in_array($url, $urls_to_preload, true)) {
return 0;
}
return $value;
}
add_filter('pre_get_rocket_option_remove_unused_css', 'rsp_apply_to_rucss');
/**
* TODO
* - Implement:
Expand Down