Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using an AJAX method to generate cache files #2

Merged
merged 16 commits into from
Jun 2, 2024
22 changes: 14 additions & 8 deletions admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function guide_callback(){ ?>
</div>
</br>
<div style="background-color: yellow; padding: 20px;">
<b>Note that, the Static Cache can only be regenerated by using the buttons in the admin toolbar, or in the Settings page. After the cache is Flushed, the cache is NOT regenerated when someone visits the pages. This is done so that none of the personalized content gets saved in the HTML caches.</b>
<b>Note that, the Static Cache can only be regenerated by using the buttons in the admin toolbar, or in the Cache Regenerate page. After the cache is Flushed, the cache is NOT regenerated when someone visits the pages. This is done so that none of the personalized content gets saved in the HTML caches.</b>
</div>
</br>
<div style="background-color: green; color: white; padding: 20px; text-align: center;">
Expand All @@ -44,16 +44,17 @@ public function guide_callback(){ ?>
<li><a href="#enter-urls">Enter URLs manually to make them static</a></li>
<li><a href="#minification">Minification</a></li>
<li><a href="#always-exclude-urls">Always Exclude URLs</a></li>
<li><a href="#interval-per-requests">Cache Warmup Requests Interval</a></li>
<li><a href="#fallback-method">Fallback Method</a></li>
<li><a href="#cron-time">Cron Time</a></li>
<li><a href="#flush-warm-on-save">Flush & Warm on Save</a></li>
</ul>

<section id="general">
<h2>General Functionality</h2>
<p>This plugin creates static HTML versions of your pages/posts, or literally any custom post types, and serves them to your non-logged-in visitors. This is an awesome way to make your website blazing fast, and not even one request is made to PHP to request your pages.
</br></br>Anytime a Static page/post/any custom post type is updated, the cache of that specific page is automatically cleared, and regenerated. So, you do not have to worry about regenerating the cache eac time after making changes to your content.
</br></br>This plugin is also well-integrated with frontend post rating plugins as well, so that when any rating is added, the cache gets regenerated automatically. If you are using any rating plugins that are not working with this plugin, kindly let me know in the support forum.
</br></br>This plugin heavily relies on CRON to process it's functionalities. So please make sure either your server-level cron or WordPress Cron is running and working. If not, this plugin will show errors in the Admin Notices to help you direct to the problem. If there are still many issues, kindly share in the plugin support thread.</br></br>By default, this plugin automatically works with Apache and Litespeed servers, and everywhere .htaccess rules is functional. But to make this plugin work with NGINX, you will be needing to add some rules to your nginx.conf or site.conf file. An appropiate admin notice will be shown to you accordingly, kindly follow those instructions.
</br></br><b>This plugin generates Static HTML cache files, and serves them to your users. So get started, simply set your options in <a target="_blank" href="<?php echo esc_url(admin_url('admin.php?page=serve_static_settings')) ?>">Settings</a>, and then navigate to <a target="_blank" href="<?php echo esc_url(admin_url('admin.php?page=serve_static_warmer')) ?>">Cache Generator</a>, and click on "Create Cache Files". If there are still any issues, kindly share in the plugin support thread.</b></br></br>By default, this plugin automatically works with Apache and Litespeed servers, and everywhere .htaccess rules is functional. But to make this plugin work with NGINX, you will be needing to add some rules to your nginx.conf or site.conf file. An appropiate admin notice will be shown to you accordingly, kindly follow those instructions.
</br></br>This plugin is supposed to work with all the form builder plugins like WP Forms, Ninja Forms. If you face any issues while using any form plugin, kindly let me know in the thread, and I will try to make it compitable.
</p>
</section>
Expand Down Expand Up @@ -146,15 +147,20 @@ public function guide_callback(){ ?>
Enter the full URLs you want to exclude from making static. One in one line. eg: "<?php echo esc_url( get_site_url() ) ?>/my-page"</p>
</section>

<section id="interval-per-requests">
<h2>Cache Warmup Requests Interval</h2>
<p>The number of seconds you want to delay each WarmUp requests. Warmup requests use server resources, so one with a low config server might choose to go with a higher value, so that the server is not overloaded by reqesusts. Or else, it is okay to keep the value at 1, or just empty.</p>
</section>

<section id="fallback-method">
<h2>Fallback method</h2>
<p>By default, this plugin uses <i> .htaccess </i> modifications to serve the static HTMl files. This is the most recommended way, as this doesn't need to load WordPress core or files at all. But in any case, if in your server the <i> .htaccess </i> modifications are not working, or the static cache is not working properly on your frontend, you can enable this Fallback option. Enabling this option will result in using PHP to serve the Static cache, which means, WordPress files and Core will be loaded before the Static files are served. This may result in less performance optimization, still you will notice an positive impact on your loading speed. Yet, for the best performance, please get in touch with your hosting provider to check why the custom <i> .htaccess </i> rules are not working, or open a support thread in the Support Forum of this plugin. Please only use this as the last resort. </p>
</section>

<section id="cron-time">
<h2>Cron Time</h2>
<p>Select cron time to Flush and regenerate cache on a regular basis. The Static cache copies will be regenerated automatically after the interval you selected. When the cache copies are not available, your visitors will be served from the normal PHP processed pages. </p>
</section>

<section id="flush-warm-on-save">
<h2>Flush & Warm on Save</h2>
<p>Check this option if you want to Flush and Save the Cache each time you Save the Settings of the plugin. Turn this option off if you are making frequent changes and testing the features. After you are done, simply Turn this on, and Save Changes. This will regenerate the cache based on your new settings.</p>
</section>
</div>
<?php
}
Expand Down
108 changes: 108 additions & 0 deletions admin/Warmer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace ServeStatic\Class;

if (!defined('ABSPATH')) {
exit; // Exit if accessed directly.
}

class WarmerAdmin {

public function warmer_sub_menu(){
add_submenu_page( 'serve_static_settings', 'Cache Generator', 'Cache Generator', 'manage_options', 'serve_static_warmer', array( $this, 'warmer_callback'), 0);
}

public function warmer_callback(){
$master_key = get_option('serve_static_master_key', false);
$is_disabled = $master_key == 1 ? '' : 'disabled';
?>
<div class="wrap serve-static-wrap">
<h1 class="wp-heading-inline serve-static-wp-heading-inline">Static Cache Generator</h1>
</br>
<?php if( $master_key == 0 || $master_key === false ) { ?>
</br>
<div style="background-color: #900000; color: white; text-align: center; padding: 20px;">
<b>Kindly enable the Plugin Functionality from Serve Static > Settings > Enable Plugin Functionality</b>
</div>
</br>
<?php } ?>
<button id="serve-static-send-requests-button" class="action-button" <?php echo esc_attr($is_disabled); ?> >Create Cache Files</button>
</br>
<div id="serve-static-request-progress-container" class="serve-static-notification" style="display: none;">
<div id="serve-static-request-progress-bar-container">
<div id="serve-static-request-progress-bar" style="width: 0%;"></div>
</div>
<div id="serve-static-request-progress-text">In Progress.... Done: 0/0</div>
</div>
<div id="serve-static-request-success" class="serve-static-notification serve-static-success">
<?php
$warmup_ajax = new WarmUpAjax;
$logs = $warmup_ajax->GetLogs(); // Get logs from custom DB table.
$all_done = get_option('serve_static_log_all_done');
$failed_count = get_option('serve_statis_failed_requests_count', 0);
if ( $all_done && $all_done == 1 ) { ?>
<p class="serve-static-success"><b>All Done...</b></p>
<p class="serve-static-error">Failed requests: <?php echo (int) $failed_count; ?></p>
<?php } elseif ( ! empty( $logs ) ) { ?>
<p class="serve-static-error"><b>Last Warmup was incomplete...</b></p>
<?php }
?>
</div>
<div id="serve-static-request-status" class="request-status">
<table id="serve-static-request-table">
<thead>
<tr>
<th>URL</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
if (!empty($logs)) {
foreach ($logs as $log) {
?>
<tr>
<td><?php echo esc_attr( $log->log_url ) ?></td>
<td><?php echo wp_kses( $log->log_status, array( 'b' => array( 'style' => array(), ), ) ) ?></td>
</tr>
<?php }
}
?>
<!-- Table rows will be dynamically populated here -->
</tbody>
</table>
</div>
</div>
<?php
// Warm Cache from Admin Toolbar.
if (isset($_GET['action']) && $_GET['action'] === 'warm_cache' && isset($_GET['_wpnonce'])) {
$nonce = sanitize_text_field(wp_unslash($_GET['_wpnonce']));

if (wp_verify_nonce($nonce, 'serve_static_warm_cache')) {
// Inject JavaScript to auto-click the button
?>
<script type="text/javascript">
setTimeout(() => {
document.getElementById("serve-static-send-requests-button").click();
}, 500 );
</script>
<?php
}
}
}

public function warmer_remove_admin_notices() {

$current_url = 'http' . ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on' ? 's' : '' ) . '://' . sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) . esc_url( wp_unslash( $_SERVER['REQUEST_URI'] ) );
$base_url = admin_url('admin.php?page=serve_static_warmer');

if ( false !== strpos( $current_url, $base_url ) ) {
remove_all_actions( 'admin_notices' );
remove_all_actions( 'all_admin_notices' );
}
}
}

$warmer_admin = new WarmerAdmin();
add_action('admin_menu', array($warmer_admin, 'warmer_sub_menu'));
add_action( 'in_admin_header', array( $warmer_admin, 'warmer_remove_admin_notices' ), 20 );
109 changes: 109 additions & 0 deletions assets/css/ajax-style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
.serve-static-wrap {
max-width: 800px;
margin: auto;
padding: 20px;
}

.serve-static-wp-heading-inline {
font-size: 24px;
margin-bottom: 20px;
}

.serve-static-notification {
margin-bottom: 20px;
padding: 10px;
border-radius: 5px;
text-align: center;
}

.serve-static-notification p {
margin: 0;
font-size: 18px;
}

.serve-static-success {
color: green;
background-color: #e6ffed;
border: 1px solid #b6f0c1;
}

.serve-static-error {
color: red;
background-color: #ffe6e6;
border: 1px solid #f0b6b6;
}

#serve-static-request-table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}

#serve-static-request-table th,
#serve-static-request-table td {
width: 1000px;
border: 1px solid #ddd;
padding: 12px;
text-align: left;
}

#serve-static-request-table th {
background-color: #f2f2f2;
font-weight: bold;
}

#serve-static-request-status {
max-height: 300px;
overflow-y: auto;
margin-top: 20px;
padding: 20px;
border: 1px solid #ddd;
border-radius: 5px;
background-color: #f9f9f9;
}

.action-button {
display: block;
margin: auto;
padding: 10px 20px;
font-size: 16px;
color: #fff;
background-color: #0073aa;
border: none;
border-radius: 5px;
cursor: pointer;
text-align: center;
text-decoration: none;
}

.action-button:hover {
background-color: #005177;
}

.action-button:disabled {
background-color: #cccccc;
color: #666666;
cursor: not-allowed;
border: 1px solid #999999;
}

/* Progress Bar */
#serve-static-request-progress-bar-container {
width: 100%;
background-color: #e0e0e0;
border-radius: 5px;
overflow: hidden;
height: 20px;
margin-bottom: 10px;
}

#serve-static-request-progress-bar {
height: 100%;
background-color: #76c7c0; /* Adjust this to your desired progress bar color */
width: 0;
transition: width 0.4s ease;
}

#serve-static-request-progress-text {
font-size: 18px;
}
Loading
Loading