Commit 8487a3d9 authored by Benjamin Adams's avatar Benjamin Adams
Browse files

Plugin and index page

parent 71667cd6
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
```
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index-wp-redis\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index-wp-redis\.php [L]
</IfModule>
# END WordPress
```
\ No newline at end of file
<?php
$seconds_cache_redis = 60*60*12; // 12 hours by default
$ip_of_your_website = '64.90.38.145'; //You must set this to the IP of your website
$secret_string = "changeme"; /*This must be the same as in the wp-admin section if you want to manually refresh the cache
ex: http://example.com/sample-post?refresh=changeme */
// so we don't confuse the cloudflare server
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
}
define('WP_USE_THEMES', true);
// Start the timer so we can track the page load time
$start = microtime();
include("wp-content/plugins/wp-redis-cache/predis5.2.php"); //we need this to use Redis inside of PHP
$redis = new Predis_Client();
$current_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$current_url = str_replace("?refresh=$secret_string", '', $current_url); //clean up the URL
$current_url = str_replace("&refresh=$secret_string", '', $current_url);
$redis_key = md5($current_url);
//Either manual refresh cache by adding ?refresh=secret_string after the URL or somebody posting a comment
if (isset($_GET['refresh']) || $_GET['refresh']==$secret_string || ($_SERVER['HTTP_REFERER'] == $current_url && $_SERVER['REQUEST_URI'] != '/' && $_SERVER['REMOTE_ADDR'] != $ip_of_your_website)) {
$redis->del($redis_key);
require('./wp-blog-header.php');
// This page is cached, lets display it
} else if ($redis->exists($redis_key)) {
$html_of_page = $redis->get($redis_key);
echo $html_of_page;
// If the cache does not exist lets display the user the normal page without cache, and then fetch a new cache page
} else if ($_SERVER['REMOTE_ADDR'] != $ip_of_your_website && strstr($current_url, 'preview=true') == false) {
require('./wp-blog-header.php');
$html_of_page = @file_get_contents($current_url);
$usr_seconds=get_option('wp-redis-cache-seconds'); //if the user has the seconds defined in the admin section use it
if(isset($usr_seconds) && is_numeric($usr_seconds))
{
$seconds_cache_redis=$usr_seconds;
}
if(strlen($html_of_page) > 5000)
{
$redis->setex($redis_key, $seconds_cache_redis, $html_of_page);
}
// This is what your server should get if no cache exists
} else {
require('./wp-blog-header.php');
}
if ($_SERVER['REMOTE_ADDR'] != $ip_of_your_website) {
// How long did it take to load the page? (CloudFlare may strip out comments)
$end = microtime();
$time = (@getMicroTime($end) - @getMicroTime($start));
echo "<!-- Cache system by Benjamin Adams. Page generated in ".round($time,5)." seconds. -->";
}
function getMicroTime($t) {
list($usec, $sec) = explode(" ",$t);
return ((float)$usec + (float)$sec);
}
?>
\ No newline at end of file
<?php
add_action('transition_post_status', 'refresh_wp_redis_cache',10,3);
//clears the cache after you update a post
function refresh_wp_redis_cache( $new, $old, $post )
{
if($new == "publish")
{
$permalink = get_permalink( $post->ID );
include("predis5.2.php"); //we need this to use Redis inside of PHP
$redis = new Predis_Client();
$redis_key = md5($permalink);
$redis->del($redis_key);
}
}
\ No newline at end of file
<?php
class wctest{
public function __construct(){
if(is_admin()){
add_action('admin_menu', array($this, 'add_plugin_page'));
add_action('admin_init', array($this, 'page_init'));
}
}
public function add_plugin_page(){
// This page will be under "Settings"
add_options_page('Settings Admin', 'Settings', 'manage_options', 'test-setting-admin', array($this, 'create_admin_page'));
}
public function create_admin_page(){
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>Settings</h2>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields('test_option_group');
do_settings_sections('test-setting-admin');
?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
public function page_init(){
register_setting('test_option_group', 'array_key', array($this, 'check_ID'));
add_settings_section(
'setting_section_id',
'Setting',
array($this, 'print_section_info'),
'test-setting-admin'
);
add_settings_field(
'some_id',
'Some ID(Title)',
array($this, 'create_an_id_field'),
'test-setting-admin',
'setting_section_id'
);
}
public function check_ID($input){
if(is_numeric($input['some_id'])){
$mid = $input['some_id'];
if(get_option('test_some_id') === FALSE){
add_option('test_some_id', $mid);
}else{
update_option('test_some_id', $mid);
}
}else{
$mid = '';
}
return $mid;
}
public function print_section_info(){
print 'Enter your setting below:';
}
public function create_an_id_field(){
?><input type="text" id="input_whatever_unique_id_I_want" name="array_key[some_id]" value="<?=get_option('test_some_id');?>" /><?php
}
}
$wctest = new wctest();
\ No newline at end of file
<?php
/*
Plugin Name: Wp Redis Cache
Plugin URI: https://github.com/BenjaminAdams/wp-redis-cache
Description: Cache Wordpress with redis
Version: 1.0
Author: Benjamin Adams
Author URI: http://dudelol.com
/* Copyright 2013 Benjamin Adams (email : ben@dudelol.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
//Custom Theme Settings
add_action('admin_menu', 'add_redis_interface');
function add_redis_interface() {
add_options_page('Wp Redis', 'Wp Redis', '8', 'functions', 'edit_redis_options');
}
function edit_redis_options() {
?>
<div class='wrap'>
<h2>Wp-Redis Options</h2>
<form method="post" action="options.php">
<?php wp_nonce_field('update-options') ?>
<p><strong>Seconds of Caching:</strong><br />
How many seconds would you like to cache? *Recommended 12 hours or 43200 seconds <br />
<input type="text" name="wp-redis-cache-seconds" size="45" value="<?php echo get_option('wp-redis-cache-seconds'); ?>" /></p>
<p><strong>Secret String:</strong><br />
To refresh the cache of your post manually you will need to set a secret string so do can refresh manually like so:
<br /> http://example.com/post_name?refresh=secret_string. <br />
<br />Important! You must change this in the index-wp-redis.php file<br />
<input type="text" name="wp-redis-secret" size="45" value="<?php echo get_option('wp-redis-secret'); ?>" /></p>
<p><input type="submit" name="Submit" value="Update Options" /></p>
<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="wp-redis-cache-seconds,wp-redis-secret" />
</form>
</div>
<?php
}
include('cache.php');
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment