Version Description
- The far future expiry is written using days instead of hours so GT metrix can read it better.
Download this release
Release Info
Developer | mra13 |
Plugin | Far Future Expiry Header |
Version | 1.4 |
Comparing to | |
See all releases |
Version 1.4
- far-future-expiration-settings.php +167 -0
- far-future-expiration.php +335 -0
- readme.txt +80 -0
far-future-expiration-settings.php
ADDED
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
function displayFarFutureExpirationSettings()
|
3 |
+
{
|
4 |
+
$errors = '';
|
5 |
+
|
6 |
+
$enable_ffe = '';
|
7 |
+
$enable_gif = '';
|
8 |
+
$enable_jpeg = '';
|
9 |
+
$enable_jpg = '';
|
10 |
+
$enable_png = '';
|
11 |
+
$enable_ico = '';
|
12 |
+
$enable_js = '';
|
13 |
+
$enable_css = '';
|
14 |
+
$enable_swf = '';
|
15 |
+
$num_expiry_days = '';
|
16 |
+
$enable_gzip = '';
|
17 |
+
|
18 |
+
global $wpdb, $ffe_plugin;
|
19 |
+
if (isset($_POST['ffe_save_settings']))
|
20 |
+
{
|
21 |
+
if (!empty($_POST['num-expiry-days'])) {
|
22 |
+
if (is_numeric($_POST['num-expiry-days']) && ($_POST['num-expiry-days'] >= 0)){
|
23 |
+
$num_expiry_days = esc_sql($_POST['num-expiry-days']);
|
24 |
+
} else {
|
25 |
+
$errors .= 'Please enter an integer amount greater than or equal to zero for the "Number of Days" field.<br/>';
|
26 |
+
}
|
27 |
+
}
|
28 |
+
|
29 |
+
if(!isset($_POST["ffe-gif"]) && !isset($_POST["ffe-jpeg"]) && !isset($_POST["ffe-jpg"]) && !isset($_POST["ffe-png"]) && !isset($_POST["ffe-ico"])
|
30 |
+
&& !isset($_POST["ffe-js"]) && !isset($_POST["ffe-css"]) && !isset($_POST["ffe-swf"])) {
|
31 |
+
$errors .= 'You must choose at least one "File Type" by enabling one of the checkboxes!<br/>';
|
32 |
+
}
|
33 |
+
if (strlen($errors)> 0){
|
34 |
+
echo '<div id="message" class="error"><p>' . $errors . '</p></div>';
|
35 |
+
}
|
36 |
+
else{
|
37 |
+
$options = array(
|
38 |
+
'enable_ffe' => isset($_POST["enable-ffe"])?1:0,
|
39 |
+
'num_expiry_days' => $num_expiry_days,
|
40 |
+
'enable_gif' => isset($_POST["ffe-gif"])?1:0,
|
41 |
+
'enable_jpeg' => isset($_POST["ffe-jpeg"])?1:0,
|
42 |
+
'enable_jpg' => isset($_POST["ffe-jpg"])?1:0,
|
43 |
+
'enable_png' => isset($_POST["ffe-png"])?1:0,
|
44 |
+
'enable_ico' => isset($_POST["ffe-ico"])?1:0,
|
45 |
+
'enable_js' => isset($_POST["ffe-js"])?1:0,
|
46 |
+
'enable_css' => isset($_POST["ffe-css"])?1:0,
|
47 |
+
'enable_swf' => isset($_POST["ffe-swf"])?1:0,
|
48 |
+
'enable_gzip' => isset($_POST["enable-gzip"])?1:0
|
49 |
+
);
|
50 |
+
update_option('far_future_expiration_settings', $options); //store the results in WP options table
|
51 |
+
echo '<div id="message" class="updated fade">';
|
52 |
+
echo '<p>Settings Saved</p>';
|
53 |
+
echo '</div>';
|
54 |
+
//Now let's modify the .htaccess file
|
55 |
+
$write_result = $ffe_plugin->write_to_htaccess();
|
56 |
+
if ($write_result){
|
57 |
+
echo '<div id="message" class="updated fade">';
|
58 |
+
echo '<p>Your .htaccess file was successfully modified</p>';
|
59 |
+
echo '</div>';
|
60 |
+
} else {
|
61 |
+
echo '<div id="message" class="error"><p>Unable to modify the .htaccess file. Please check the file permissions!</p></div>';
|
62 |
+
}
|
63 |
+
}
|
64 |
+
}
|
65 |
+
|
66 |
+
$far_future_expiration_settings = get_option('far_future_expiration_settings');
|
67 |
+
if ($far_future_expiration_settings)
|
68 |
+
{
|
69 |
+
$enable_ffe = $far_future_expiration_settings['enable_ffe'];
|
70 |
+
$num_expiry_days = $far_future_expiration_settings['num_expiry_days'];
|
71 |
+
$enable_gif = $far_future_expiration_settings['enable_gif'];
|
72 |
+
$enable_jpeg = $far_future_expiration_settings['enable_jpeg'];
|
73 |
+
$enable_jpg = $far_future_expiration_settings['enable_jpg'];
|
74 |
+
$enable_png = $far_future_expiration_settings['enable_png'];
|
75 |
+
$enable_ico = $far_future_expiration_settings['enable_ico'];
|
76 |
+
$enable_js = $far_future_expiration_settings['enable_js'];
|
77 |
+
$enable_css = $far_future_expiration_settings['enable_css'];
|
78 |
+
$enable_swf = $far_future_expiration_settings['enable_swf'];
|
79 |
+
$enable_gzip = $far_future_expiration_settings['enable_gzip'];
|
80 |
+
}
|
81 |
+
?>
|
82 |
+
<div id="poststuff"><div id="post-body">
|
83 |
+
<div class="postbox">
|
84 |
+
<h3 class="hndle"><label for="title">Using The Far Future Expiration Plugin</label></h3>
|
85 |
+
<div class="inside">
|
86 |
+
|
87 |
+
<p>You can read the usage instruction on the <a href="https://www.tipsandtricks-hq.com/wordpress-far-future-expiration-plugin-5980" target="_blank">far future expiration plugin</a> page.</p>
|
88 |
+
|
89 |
+
<p>This plugin will modify your .htaccess file by inserting code which will add expires headers for common file types.</p>
|
90 |
+
<p>Expires header specifies a time far enough in the future so that browsers won't try to re-fetch images, CSS, javascript etc files
|
91 |
+
that haven't changed (this reduces the number of HTTP requests) and hence the performance improvement on subsequent page views.</p>
|
92 |
+
<p>To use this plugin do the following:</p>
|
93 |
+
<ol>
|
94 |
+
<li>Ensure that the "mod_expires" module is enabled from your host's main configuration file.
|
95 |
+
<br />Check with your hosting provider or if you have access to the httpd.conf file the following line should be uncommented:
|
96 |
+
<br /><i>LoadModule expires_module modules/mod_expires.so</i>
|
97 |
+
</li>
|
98 |
+
<li>Enable the "Far Future Expiration" checkbox</li>
|
99 |
+
<li>Set the number of days till expiry</li>
|
100 |
+
<li>Select the file types you wish to enable the "far future expiration" feature for by using the checkboxes in the "File Types" section</li>
|
101 |
+
</ol>
|
102 |
+
<p><strong>NOTE: When you use this plugin, the file selected file types are cached in the browser until they expire. Therefore you should not use this on files that change frequently.</strong></p>
|
103 |
+
|
104 |
+
</div></div>
|
105 |
+
|
106 |
+
<form action="" method="POST">
|
107 |
+
|
108 |
+
<div class="postbox">
|
109 |
+
<h3 class="hndle"><label for="title">Far Future Expiration Settings</label></h3>
|
110 |
+
<div class="inside">
|
111 |
+
<table class="form-table">
|
112 |
+
<tr valign="top">
|
113 |
+
<th scope="row"><label for="Enableffa"> Enable Far Future Expiration : </label></th>
|
114 |
+
<td>
|
115 |
+
<input type="checkbox" name="enable-ffe" <?php if($enable_ffe) echo ' checked="checked"'; ?> />
|
116 |
+
</td>
|
117 |
+
</tr>
|
118 |
+
<tr valign="top">
|
119 |
+
<th scope="row"><label for="NumDays"> Number of Days:</label>
|
120 |
+
</th>
|
121 |
+
<td>
|
122 |
+
<input type="text" size="10" name="num-expiry-days" value="<?php echo $num_expiry_days; ?>" /> <span>(Days)</span>
|
123 |
+
<span class="description"> This value sets the expiry date of the selected file types to x days into the future.</span>
|
124 |
+
</td>
|
125 |
+
</tr>
|
126 |
+
<tr valign="top">
|
127 |
+
<th scope="row"><label for="FileTypes"> File Types : </label></th>
|
128 |
+
<td>
|
129 |
+
<div><input type="checkbox" name="ffe-gif" <?php if($enable_gif) echo ' checked="checked"'; ?> /> GIF</div>
|
130 |
+
<div><input type="checkbox" name="ffe-jpeg" <?php if($enable_jpeg) echo ' checked="checked"'; ?> /> JPEG</div>
|
131 |
+
<div><input type="checkbox" name="ffe-jpg" <?php if($enable_jpg) echo ' checked="checked"'; ?> /> JPG</div>
|
132 |
+
<div><input type="checkbox" name="ffe-png" <?php if($enable_png) echo ' checked="checked"'; ?> /> PNG</div>
|
133 |
+
<div><input type="checkbox" name="ffe-ico" <?php if($enable_ico) echo ' checked="checked"'; ?> /> ICO</div>
|
134 |
+
<div><input type="checkbox" name="ffe-js" <?php if($enable_js) echo ' checked="checked"'; ?> /> JS</div>
|
135 |
+
<div><input type="checkbox" name="ffe-css" <?php if($enable_css) echo ' checked="checked"'; ?> /> CSS</div>
|
136 |
+
<div><input type="checkbox" name="ffe-swf" <?php if($enable_swf) echo ' checked="checked"'; ?> /> SWF</div>
|
137 |
+
</td>
|
138 |
+
</tr>
|
139 |
+
</table>
|
140 |
+
<div style="border-bottom: 1px solid #dedede; height: 10px"></div>
|
141 |
+
<br />
|
142 |
+
|
143 |
+
</div></div>
|
144 |
+
|
145 |
+
<div class="postbox">
|
146 |
+
<h3 class="hndle"><label for="title">Gzip Compression Settings</label></h3>
|
147 |
+
<div class="inside">
|
148 |
+
<table class="form-table">
|
149 |
+
<tr valign="top">
|
150 |
+
<th scope="row"><label for="Enable-gzip"> Enable Gzip Compression : </label></th>
|
151 |
+
<td>
|
152 |
+
<input type="checkbox" name="enable-gzip" <?php if($enable_gzip) echo ' checked="checked"'; ?> />
|
153 |
+
</td>
|
154 |
+
</tr>
|
155 |
+
</table>
|
156 |
+
</div></div>
|
157 |
+
|
158 |
+
<p><input type="submit" name="ffe_save_settings" value="Save" class="button-primary" /></p>
|
159 |
+
</form>
|
160 |
+
|
161 |
+
<div style="background: #D7E7F5; border: 1px solid #1166BB; color: #333333; margin: 20px 0; padding: 10px;">
|
162 |
+
Check out our other <a href="https://www.tipsandtricks-hq.com/development-center" target="_blank">WordPress plugins</a> and <a href="https://www.appthemes.com/" target="_blank">WordPress themes</a>
|
163 |
+
</div>
|
164 |
+
|
165 |
+
</div></div><!-- end of poststuff -->
|
166 |
+
<?php
|
167 |
+
}
|
far-future-expiration.php
ADDED
@@ -0,0 +1,335 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/*
|
3 |
+
Plugin Name: Far Future Expiration Plugin
|
4 |
+
Plugin URI: http://www.tipsandtricks-hq.com/wordpress-far-future-expiration-plugin-5980
|
5 |
+
Description: This plugin will add a "far future expiration" date for various file types to improve site performance.
|
6 |
+
Author: Tips and Tricks HQ
|
7 |
+
Version: 1.4
|
8 |
+
Author URI: https://www.tipsandtricks-hq.com/
|
9 |
+
License: GPLv2 or later
|
10 |
+
*/
|
11 |
+
|
12 |
+
/*
|
13 |
+
* The main plugin class,
|
14 |
+
* initialized right after declaration
|
15 |
+
*/
|
16 |
+
if(!class_exists('farFutureExpiration'))
|
17 |
+
{
|
18 |
+
class farFutureExpiration
|
19 |
+
{
|
20 |
+
/*
|
21 |
+
* Declare keys here as well as our tabs array which
|
22 |
+
* is populated when registering settings
|
23 |
+
*/
|
24 |
+
private $edit_ffe_settings_page_key = 'edit_ffe_settings_page';
|
25 |
+
private $ffe_options_key = 'ffe_plugin_options';
|
26 |
+
private $ffe_settings_tabs = array();
|
27 |
+
|
28 |
+
function __construct() {
|
29 |
+
$this->define_constants();
|
30 |
+
$this->loader_operations();
|
31 |
+
}
|
32 |
+
|
33 |
+
function define_constants() {
|
34 |
+
global $wpdb;
|
35 |
+
define('FFE_PLUGIN_DB_VERSION', '1.0');
|
36 |
+
define('FFE_PLUGIN_PATH', dirname(__FILE__));
|
37 |
+
define('FFE_PLUGIN_URL', plugins_url('',__FILE__));
|
38 |
+
}
|
39 |
+
|
40 |
+
function loader_operations(){
|
41 |
+
add_action('plugins_loaded', array( &$this, 'ffe_execute_plugins_loaded_operations'));
|
42 |
+
}
|
43 |
+
|
44 |
+
function ffe_execute_plugins_loaded_operations()
|
45 |
+
{
|
46 |
+
add_action( 'init', array( &$this, 'load_settings' ) );
|
47 |
+
add_action( 'init', array( &$this, 'do_init_time_tasks' ) );
|
48 |
+
add_action( 'admin_init', array( &$this, 'register_ffe_settings_page' ) );
|
49 |
+
add_action( 'admin_menu', array( &$this, 'add_admin_menus' ) );
|
50 |
+
}
|
51 |
+
|
52 |
+
/*
|
53 |
+
* Loads tab settings from
|
54 |
+
* the database into their respective arrays. Uses
|
55 |
+
* array_merge to merge with default values if they're
|
56 |
+
* missing.
|
57 |
+
*/
|
58 |
+
function load_settings() {
|
59 |
+
$this->edit_ffe_settings_page = (array) get_option( $this->edit_ffe_settings_page_key );
|
60 |
+
|
61 |
+
// Merge with defaults
|
62 |
+
$this->edit_ffe_settings_page = array_merge( array(
|
63 |
+
'edit_ffe_option' => 'Floating Cart Settings Page'
|
64 |
+
), $this->edit_ffe_settings_page );
|
65 |
+
|
66 |
+
}
|
67 |
+
|
68 |
+
function do_init_time_tasks()
|
69 |
+
{
|
70 |
+
if(!is_admin()){//Front end init time tasks
|
71 |
+
$ffe_settings = get_option('far_future_expiration_settings');
|
72 |
+
if($ffe_settings['enable_gzip']){//Gzip compression is enabled
|
73 |
+
ob_start('ob_gzhandler');
|
74 |
+
}
|
75 |
+
}
|
76 |
+
}
|
77 |
+
|
78 |
+
|
79 |
+
/*
|
80 |
+
* Registers the display templates page via the Settings API,
|
81 |
+
* appends the setting to the tabs array of the object.
|
82 |
+
*/
|
83 |
+
function register_ffe_settings_page() {
|
84 |
+
$this->ffe_settings_tabs[$this->edit_ffe_settings_page_key] = 'Far Future Expiration Settings';
|
85 |
+
register_setting( $this->edit_ffe_settings_page_key, $this->edit_ffe_settings_page_key );
|
86 |
+
}
|
87 |
+
|
88 |
+
/******************************************************************************
|
89 |
+
* Now we just need to define an admin page.
|
90 |
+
******************************************************************************/
|
91 |
+
|
92 |
+
/*
|
93 |
+
* Called during admin_menu, adds an options
|
94 |
+
* page under Settings
|
95 |
+
*/
|
96 |
+
|
97 |
+
function add_admin_menus(){
|
98 |
+
add_options_page('FarFutureExpiry', 'FarFutureExpiry', 'manage_options', $this->ffe_options_key, array(&$this, 'ffe_plugin_option_page'));
|
99 |
+
}
|
100 |
+
|
101 |
+
/*
|
102 |
+
* Plugin Options page rendering goes here, checks
|
103 |
+
* for active tab and replaces key with the related
|
104 |
+
* settings key. Uses the plugin_options_tabs method
|
105 |
+
* to render the tabs.
|
106 |
+
*/
|
107 |
+
function ffe_plugin_option_page() {
|
108 |
+
$tab = isset( $_GET['tab'] ) ? $_GET['tab'] : $this->edit_ffe_settings_page_key;
|
109 |
+
?>
|
110 |
+
<div class="wrap">
|
111 |
+
<?php
|
112 |
+
$this->plugin_options_tabs();
|
113 |
+
if ($tab == $this->edit_ffe_settings_page_key)
|
114 |
+
{
|
115 |
+
include_once('far-future-expiration-settings.php');
|
116 |
+
displayFarFutureExpirationSettings();
|
117 |
+
}
|
118 |
+
?>
|
119 |
+
</div>
|
120 |
+
<?php
|
121 |
+
}
|
122 |
+
|
123 |
+
function current_tab() {
|
124 |
+
$tab = isset( $_GET['tab'] ) ? $_GET['tab'] : $this->edit_ffe_settings_page_key;
|
125 |
+
return $tab;
|
126 |
+
}
|
127 |
+
|
128 |
+
/*
|
129 |
+
* Renders our tabs in the plugin options page,
|
130 |
+
* walks through the object's tabs array and prints
|
131 |
+
* them one by one. Provides the heading for the
|
132 |
+
* plugin_options_page method.
|
133 |
+
*/
|
134 |
+
function plugin_options_tabs() {
|
135 |
+
$current_tab = isset( $_GET['tab'] ) ? $_GET['tab'] : $this->edit_ffe_settings_page_key;
|
136 |
+
|
137 |
+
echo '<h2 class="nav-tab-wrapper">';
|
138 |
+
foreach ( $this->ffe_settings_tabs as $tab_key => $tab_caption ) {
|
139 |
+
$active = $current_tab == $tab_key ? 'nav-tab-active' : '';
|
140 |
+
echo '<a class="nav-tab ' . $active . '" href="?page=' . $this->ffe_options_key . '&tab=' . $tab_key . '">' . $tab_caption . '</a>';
|
141 |
+
}
|
142 |
+
echo '</h2>';
|
143 |
+
}
|
144 |
+
|
145 |
+
|
146 |
+
function write_to_htaccess()
|
147 |
+
{
|
148 |
+
//clean up old rules first
|
149 |
+
if ($this->delete_from_htaccess() == -1)
|
150 |
+
{
|
151 |
+
return -1; //unable to write to the file
|
152 |
+
}
|
153 |
+
|
154 |
+
$htaccess = ABSPATH . '.htaccess';
|
155 |
+
//get the subdirectory if it is installed in one
|
156 |
+
$siteurl = explode( '/', get_option( 'siteurl' ) );
|
157 |
+
if (isset($siteurl[3]))
|
158 |
+
{
|
159 |
+
$dir = '/' . $siteurl[3] . '/';
|
160 |
+
}
|
161 |
+
else
|
162 |
+
{
|
163 |
+
$dir = '/';
|
164 |
+
}
|
165 |
+
|
166 |
+
if (!$f = @fopen($htaccess, 'a+'))
|
167 |
+
{
|
168 |
+
@chmod( $htaccess, 0644 );
|
169 |
+
if (!$f = @fopen( $htaccess, 'a+'))
|
170 |
+
{
|
171 |
+
return -1;
|
172 |
+
}
|
173 |
+
}
|
174 |
+
//backup_a_file($htaccess); //should we back up htaccess file??
|
175 |
+
@ini_set( 'auto_detect_line_endings', true );
|
176 |
+
$ht = explode( PHP_EOL, implode( '', file( $htaccess ) ) ); //parse each line of file into array
|
177 |
+
|
178 |
+
$rules = $this->getrules();
|
179 |
+
if ($rules == -1)
|
180 |
+
{
|
181 |
+
return -1;
|
182 |
+
}
|
183 |
+
|
184 |
+
$rulesarray = explode( PHP_EOL, $rules );
|
185 |
+
$contents = array_merge( $rulesarray, $ht );
|
186 |
+
|
187 |
+
if (!$f = @fopen($htaccess, 'w+'))
|
188 |
+
{
|
189 |
+
return -1; //we can't write to the file
|
190 |
+
}
|
191 |
+
|
192 |
+
$blank = false;
|
193 |
+
|
194 |
+
//write each line to file
|
195 |
+
foreach ( $contents as $insertline )
|
196 |
+
{
|
197 |
+
if ( trim( $insertline ) == '' )
|
198 |
+
{
|
199 |
+
if ( $blank == false )
|
200 |
+
{
|
201 |
+
fwrite( $f, PHP_EOL . trim( $insertline ) );
|
202 |
+
}
|
203 |
+
$blank = true;
|
204 |
+
}
|
205 |
+
else
|
206 |
+
{
|
207 |
+
$blank = false;
|
208 |
+
fwrite( $f, PHP_EOL . trim( $insertline ) );
|
209 |
+
}
|
210 |
+
}
|
211 |
+
@fclose( $f );
|
212 |
+
return 1; //success
|
213 |
+
}
|
214 |
+
|
215 |
+
function getrules()
|
216 |
+
{
|
217 |
+
@ini_set( 'auto_detect_line_endings', true );
|
218 |
+
|
219 |
+
//figure out what server they're using
|
220 |
+
if (strstr(strtolower(filter_var($_SERVER['SERVER_SOFTWARE'], FILTER_SANITIZE_STRING)), 'apache'))
|
221 |
+
{
|
222 |
+
$aiowps_server = 'apache';
|
223 |
+
}
|
224 |
+
else if (strstr(strtolower(filter_var($_SERVER['SERVER_SOFTWARE'], FILTER_SANITIZE_STRING)), 'nginx'))
|
225 |
+
{
|
226 |
+
$aiowps_server = 'nginx';
|
227 |
+
}
|
228 |
+
else if (strstr(strtolower(filter_var($_SERVER['SERVER_SOFTWARE'], FILTER_SANITIZE_STRING)), 'litespeed'))
|
229 |
+
{
|
230 |
+
$aiowps_server = 'litespeed';
|
231 |
+
}
|
232 |
+
else
|
233 |
+
{ //unsupported server
|
234 |
+
return -1;
|
235 |
+
}
|
236 |
+
|
237 |
+
$rules = '';
|
238 |
+
$far_future_expiration_settings = get_option('far_future_expiration_settings');
|
239 |
+
$expire_time = $far_future_expiration_settings['num_expiry_days'];
|
240 |
+
if($far_future_expiration_settings['enable_ffe']=='1'){
|
241 |
+
//write the rules
|
242 |
+
$rules .= '<IfModule mod_expires.c>' . PHP_EOL;
|
243 |
+
$rules .= 'ExpiresActive on' . PHP_EOL;
|
244 |
+
$rules .= '<FilesMatch "\.(';
|
245 |
+
if($far_future_expiration_settings['enable_gif']=='1'){
|
246 |
+
$rules .= 'gif|';
|
247 |
+
}
|
248 |
+
if($far_future_expiration_settings['enable_jpeg']=='1'){
|
249 |
+
$rules .= 'jpeg|';
|
250 |
+
}
|
251 |
+
if($far_future_expiration_settings['enable_jpg']=='1'){
|
252 |
+
$rules .= 'jpg|';
|
253 |
+
}
|
254 |
+
if($far_future_expiration_settings['enable_png']=='1'){
|
255 |
+
$rules .= 'png|';
|
256 |
+
}
|
257 |
+
if($far_future_expiration_settings['enable_ico']=='1'){
|
258 |
+
$rules .= 'ico|';
|
259 |
+
}
|
260 |
+
if($far_future_expiration_settings['enable_js']=='1'){
|
261 |
+
$rules .= 'js|';
|
262 |
+
}
|
263 |
+
if($far_future_expiration_settings['enable_css']=='1'){
|
264 |
+
$rules .= 'css|';
|
265 |
+
}
|
266 |
+
if($far_future_expiration_settings['enable_swf']=='1'){
|
267 |
+
$rules .= 'swf|';
|
268 |
+
}
|
269 |
+
//$expire_time_in_hours = $expire_time * 24;//Better to use the value in days so (the tools read it better)
|
270 |
+
|
271 |
+
//Let's remove any trailing "|" if necessary
|
272 |
+
$rules = rtrim($rules, "|");
|
273 |
+
$rules .= ')$">' . PHP_EOL;
|
274 |
+
$rules .= 'ExpiresDefault "access plus '.$expire_time.' days"'. PHP_EOL;
|
275 |
+
$rules .= '</FilesMatch>'. PHP_EOL;
|
276 |
+
$rules .= '</IfModule>' . PHP_EOL;
|
277 |
+
}
|
278 |
+
|
279 |
+
//Add outer markers if we have rules
|
280 |
+
if ($rules != '')
|
281 |
+
{
|
282 |
+
$rules = "# BEGIN Far Future Expiration Plugin" . PHP_EOL . $rules . "# END Far Future Expiration Plugin" . PHP_EOL;
|
283 |
+
}
|
284 |
+
|
285 |
+
return $rules;
|
286 |
+
}
|
287 |
+
|
288 |
+
|
289 |
+
function delete_from_htaccess($section = 'Far Future Expiration Plugin')
|
290 |
+
{
|
291 |
+
$htaccess = ABSPATH . '.htaccess';
|
292 |
+
|
293 |
+
@ini_set('auto_detect_line_endings', true);
|
294 |
+
if (!file_exists($htaccess))
|
295 |
+
{
|
296 |
+
$ht = @fopen($htaccess, 'a+');
|
297 |
+
@fclose($ht);
|
298 |
+
}
|
299 |
+
$ht_contents = explode(PHP_EOL, implode('', file($htaccess))); //parse each line of file into array
|
300 |
+
if ($ht_contents)
|
301 |
+
{ //as long as there are lines in the file
|
302 |
+
$state = true;
|
303 |
+
if (!$f = @fopen($htaccess, 'w+'))
|
304 |
+
{
|
305 |
+
@chmod( $htaccess, 0644 );
|
306 |
+
if (!$f = @fopen( $htaccess, 'w+'))
|
307 |
+
{
|
308 |
+
return -1;
|
309 |
+
}
|
310 |
+
}
|
311 |
+
|
312 |
+
foreach ( $ht_contents as $n => $markerline )
|
313 |
+
{ //for each line in the file
|
314 |
+
if (strpos($markerline, '# BEGIN ' . $section) !== false)
|
315 |
+
{ //if we're at the beginning of the section
|
316 |
+
$state = false;
|
317 |
+
}
|
318 |
+
if ($state == true)
|
319 |
+
{ //as long as we're not in the section keep writing
|
320 |
+
fwrite($f, trim($markerline) . PHP_EOL);
|
321 |
+
}
|
322 |
+
if (strpos($markerline, '# END ' . $section) !== false)
|
323 |
+
{ //see if we're at the end of the section
|
324 |
+
$state = true;
|
325 |
+
}
|
326 |
+
}
|
327 |
+
@fclose($f);
|
328 |
+
return 1;
|
329 |
+
}
|
330 |
+
return 1;
|
331 |
+
}
|
332 |
+
|
333 |
+
} //end class
|
334 |
+
}
|
335 |
+
$GLOBALS['ffe_plugin'] = new farFutureExpiration();
|
readme.txt
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
=== Far Future Expiry Header ===
|
2 |
+
Contributors: Tips and Tricks HQ
|
3 |
+
Donate link: https://www.tipsandtricks-hq.com/wordpress-far-future-expiration-plugin-5980
|
4 |
+
Tags: cache, expiry, expiry header, far future expiration, expires header, wp-cache, minify, gzip, javascript, css, compression, speed,
|
5 |
+
Requires at least: 3.5
|
6 |
+
Tested up to: 5.8
|
7 |
+
Stable tag: 1.4
|
8 |
+
License: GPLv2 or later
|
9 |
+
|
10 |
+
This plugin will add a far future expiry header for various file types to improve page load speed of your site
|
11 |
+
|
12 |
+
== Description ==
|
13 |
+
|
14 |
+
This plugin offers a few lightweight features to speed up your WordPress site without much hassle.
|
15 |
+
|
16 |
+
= Far Future Expiry =
|
17 |
+
|
18 |
+
When the feature is enabled, this plugin will modify your .htaccess file by inserting code which will add expires headers for common static file types.
|
19 |
+
|
20 |
+
Expiry header specifies a time far enough in the future so that browsers won't try to re-fetch images, CSS, javascript etc files that haven't changed (this reduces the number of HTTP requests) and hence the performance improvement on subsequent page views.
|
21 |
+
|
22 |
+
= Gzip Compression =
|
23 |
+
|
24 |
+
You can also enable Gzip compression on your site using this plugin. Gzip compression will speed up your WordPress site by compressing the page output and sending it to your visitors browser.
|
25 |
+
|
26 |
+
When enabled, the plugin will do gzip compression if the visitor's browser can handle it.
|
27 |
+
|
28 |
+
This feature may conflict with a few other plugins. So disable this feature if you need to use a plugin which doesn't work with gzip compression.
|
29 |
+
|
30 |
+
== Installation ==
|
31 |
+
|
32 |
+
1. Upload the far-future-expiration.zip file from the Plugins -> Add New page in the WordPress administration panel.
|
33 |
+
2. Activate the plugin through the "Plugins" menu in the WordPress administration panel.
|
34 |
+
|
35 |
+
== Usage ==
|
36 |
+
|
37 |
+
To use this plugin do the following:
|
38 |
+
|
39 |
+
1) Ensure that the "mod_expires" module is enabled from your host's main configuration file
|
40 |
+
|
41 |
+
2) Check with your hosting provider or if you have access to the httpd.conf file the following line should be uncommented:
|
42 |
+
LoadModule expires_module modules/mod_expires.so
|
43 |
+
|
44 |
+
3) Enable the "Far Future Expiration" checkbox
|
45 |
+
|
46 |
+
4) Set the number of days till expiry
|
47 |
+
|
48 |
+
5) Select the file types you wish to enable the "far future expiration" feature for by using the checkboxes in the "File Types" section
|
49 |
+
|
50 |
+
NOTE: When you use this plugin, the file selected file types are cached in the browser until they expire. Therefore you should not use this on files that change frequently.
|
51 |
+
|
52 |
+
More details can be found on the [plugin's documentation page](https://www.tipsandtricks-hq.com/wordpress-far-future-expiration-plugin-5980)
|
53 |
+
|
54 |
+
== Frequently Asked Questions ==
|
55 |
+
|
56 |
+
= Can I set a far future expiry header with this plugin? =
|
57 |
+
Yes
|
58 |
+
|
59 |
+
= Can I eanble gzip compression on my site using this plugin? =
|
60 |
+
yes
|
61 |
+
|
62 |
+
== Changelog ==
|
63 |
+
|
64 |
+
= 1.4 =
|
65 |
+
* The far future expiry is written using days instead of hours so GT metrix can read it better.
|
66 |
+
|
67 |
+
= 1.3 =
|
68 |
+
* Added a new feature to enable gzip compression on the site
|
69 |
+
|
70 |
+
= 1.2 =
|
71 |
+
* Fixed a minor bug with the htacess rules.
|
72 |
+
|
73 |
+
= 1.1 =
|
74 |
+
* First commit to wp repo
|
75 |
+
|
76 |
+
== Upgrade Notice ==
|
77 |
+
None
|
78 |
+
|
79 |
+
== Screenshots ==
|
80 |
+
Visit the plugin site at http://www.tipsandtricks-hq.com/wordpress-far-future-expiration-plugin-5980 for screenshots and more info.
|