2012-02-16 14:07:00 +00:00
< ? php
/*
Plugin Name : WP - FFPC
2013-03-08 11:19:34 +00:00
Version : 0.6 . 1
2012-02-16 14:07:00 +00:00
Plugin URI : http :// petermolnar . eu / wordpress / wp - ffpc
Description : Fast Full Page Cache , backend can be memcached or APC
Author : Peter Molnar
Author URI : http :// petermolnar . eu /
License : GPL2
*/
2013-03-06 18:31:28 +00:00
/* Copyright 2010 - 2013 Peter Molnar ( email : hello @ petermolnar . eu )
2012-02-16 14:07:00 +00:00
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
*/
2012-08-06 13:17:44 +01:00
/**
2012-08-16 11:36:20 +01:00
* checks for SSL connection
2012-08-06 13:17:44 +01:00
*/
if ( ! function_exists ( 'replace_if_ssl' ) ) {
function replace_if_ssl ( $string ) {
if ( isset ( $_SERVER [ 'HTTPS' ]) && ( ( strtolower ( $_SERVER [ 'HTTPS' ]) == 'on' ) || ( $_SERVER [ 'HTTPS' ] == '1' ) ) )
return str_replace ( 'http://' , 'https://' , $string );
else
return $string ;
}
}
2012-02-16 14:07:00 +00:00
/* fix */
2012-08-06 13:17:44 +01:00
if ( ! defined ( 'WP_PLUGIN_URL_' ) )
{
if ( defined ( 'WP_PLUGIN_URL' ) )
define ( 'WP_PLUGIN_URL_' , replace_if_ssl ( WP_PLUGIN_URL ) );
else
define ( 'WP_PLUGIN_URL_' , replace_if_ssl ( get_option ( 'siteurl' ) ) . '/wp-content/plugins' );
}
2012-02-16 14:07:00 +00:00
if ( ! defined ( 'WP_PLUGIN_DIR' ) )
define ( 'WP_PLUGIN_DIR' , ABSPATH . 'wp-content/plugins' );
/* constants */
define ( 'WP_FFPC_PARAM' , 'wp-ffpc' );
define ( 'WP_FFPC_OPTION_GROUP' , 'wp-ffpcparams' );
define ( 'WP_FFPC_OPTIONS_PAGE' , 'wp-ffpcoptions' );
2012-08-06 13:17:44 +01:00
define ( 'WP_FFPC_URL' , WP_PLUGIN_URL_ . '/' . WP_FFPC_PARAM );
2012-02-16 14:07:00 +00:00
define ( 'WP_FFPC_DIR' , WP_PLUGIN_DIR . '/' . WP_FFPC_PARAM );
define ( 'WP_FFPC_CONF_DIR' , WP_PLUGIN_DIR . '/' . WP_FFPC_PARAM . '/config' );
define ( 'WP_FFPC_ACACHE_MAIN_FILE' , ABSPATH . 'wp-content/advanced-cache.php' );
define ( 'WP_FFPC_ACACHE_INC_FILE' , WP_FFPC_DIR . '/advanced-cache.php' );
define ( 'WP_FFPC_ACACHE_COMMON_FILE' , WP_FFPC_DIR . '/wp-ffpc-common.php' );
define ( 'WP_FFPC_CONFIG_VAR' , '$wp_ffpc_config' );
2013-03-06 12:57:29 +00:00
define ( 'WP_FFPC_SERVER_LIST_SEPARATOR' , ',' );
define ( 'WP_FFPC_SERVER_SEPARATOR' , ':' );
2013-03-06 18:31:28 +00:00
define ( 'WP_FFPC_DONATION_LINK' , 'https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=XU3DG7LLA76WC' );
2013-03-07 12:09:59 +00:00
define ( 'WP_FFPC_FILE' , plugin_basename ( __FILE__ ) );
2013-03-07 15:07:56 +00:00
define ( 'WP_FFPC_PLUGIN' , 'wp-ffpc/wp-ffpc.php' );
2013-03-08 11:19:34 +00:00
define ( 'WP_FFPC_VERSION' , '0.6.1' );
2013-03-07 15:07:56 +00:00
if ( ! function_exists ( 'is_plugin_active_for_network' ) )
require_once ( ABSPATH . 'wp-admin/includes/plugin.php' );
2012-02-16 14:07:00 +00:00
2013-03-06 19:03:30 +00:00
/* get the common functions */
2012-02-21 05:49:57 +00:00
include_once ( WP_FFPC_DIR . '/wp-ffpc-common.php' );
2012-02-16 14:07:00 +00:00
if ( ! class_exists ( 'WPFFPC' )) {
/**
* main class
*
*/
class WPFFPC {
/* for options array */
var $options = array ();
2013-03-07 15:07:56 +00:00
/* stores options for all sites & all data */
var $all_options = array ();
/* key for current options array */
var $options_key = '' ;
2012-02-16 14:07:00 +00:00
/* for default options array */
var $defaults = array ();
/* memcached server object */
var $memcached = NULL ;
var $memcached_string = '' ;
/* status, 0 = nothing happened*/
var $status = 0 ;
2013-03-06 18:31:28 +00:00
/* stores information if plugin is network active or not */
2013-03-05 13:23:34 +00:00
var $network = false ;
2013-03-07 12:09:59 +00:00
var $settingslink = '' ;
2013-03-05 13:23:34 +00:00
2012-02-16 14:07:00 +00:00
/**
* constructor
*
*/
function __construct () {
2013-03-07 12:09:59 +00:00
/* set settings page link */
$this -> settingslink = 'options-general.php?page=' . WP_FFPC_OPTIONS_PAGE ;
/* check if plugin is network-activated */
2013-03-05 13:23:34 +00:00
$this -> check_for_network ();
2012-02-16 14:07:00 +00:00
/* register options */
$this -> get_options ();
/* check is backend is available */
2012-02-21 06:39:19 +00:00
$alive = wp_ffpc_init ( $this -> options );
2012-02-16 14:07:00 +00:00
/* don't register hooks if backend is dead */
2013-03-06 18:31:28 +00:00
if ( ! $alive )
2012-02-16 14:07:00 +00:00
{
/* init inactivation hooks */
add_action ( 'switch_theme' , array ( $this , 'invalidate' ), 0 );
add_action ( 'edit_post' , array ( $this , 'invalidate' ), 0 );
add_action ( 'publish_post' , array ( $this , 'invalidate' ), 0 );
add_action ( 'delete_post' , array ( $this , 'invalidate' ), 0 );
/* Capture and register if a redirect is sent back from WP , so the cache
can cache ( or ignore ) it . Redirects were source of problems for blogs
with more than one host name ( eg . domain . com and www . domain . com ) comined
with the use of Hyper Cache .*/
add_filter ( 'redirect_canonical' , array ( $this , 'redirect_canonical' ) , 10 , 2 );
}
2013-03-07 12:09:59 +00:00
$plugin = plugin_basename ( __FILE__ );
if ( $this -> network )
add_filter ( " network_admin_plugin_action_links_ $plugin " , array ( $this , 'settings_link' ) );
else
add_filter ( " plugin_action_links_ $plugin " , array ( $this , 'settings_link' ) );
2012-02-16 14:07:00 +00:00
/* add admin styling */
if ( is_admin () )
{
2013-03-05 13:23:34 +00:00
wp_enqueue_script ( " jquery-ui-tabs " );
2013-03-06 17:37:48 +00:00
wp_enqueue_style ( WP_FFPC_PARAM . '.admin.css' , WP_FFPC_URL . '/' . WP_FFPC_PARAM . '.admin.css' , false , '0.1' );
2012-02-16 14:07:00 +00:00
}
/* on activation */
register_activation_hook ( __FILE__ , array ( $this , 'activate' ) );
/* on deactivation */
register_deactivation_hook ( __FILE__ , array ( $this , 'deactivate' ) );
/* on uninstall */
register_uninstall_hook ( __FILE__ , array ( $this , 'uninstall' ) );
/* init plugin in the admin section */
/* if multisite, admin page will be on network admin section */
2013-03-05 13:23:34 +00:00
if ( $this -> network )
2012-02-16 14:07:00 +00:00
add_action ( 'network_admin_menu' , array ( $this , 'admin_init' ) );
/* not network, will be in simple admin menu */
else
add_action ( 'admin_menu' , array ( $this , 'admin_init' ) );
2013-03-08 11:19:34 +00:00
if ( $this -> all_options [ 'version' ] < WP_FFPC_VERSION ) {
$this -> save_settings ();
if ( @ file_exists ( WP_FFPC_ACACHE_MAIN_FILE )) {
$this -> generate_config ();
}
}
2012-02-16 14:07:00 +00:00
}
/**
* activation hook : save default settings in order to eliminate bugs .
*
*/
function activate ( ) {
2013-03-06 18:31:28 +00:00
/* register options for first time */
add_site_option ( WP_FFPC_PARAM , $this -> options , '' , 'yes' );
2012-02-16 14:07:00 +00:00
$this -> save_settings ( true );
}
/**
* init function for admin section
*
*/
function admin_init () {
/* save parameter updates, if there are any */
if ( isset ( $_POST [ WP_FFPC_PARAM . '-save' ]) )
{
$this -> save_settings ();
$this -> status = 1 ;
2013-03-07 12:09:59 +00:00
header ( " Location: " . $this -> settingslink . " &saved=true " );
2012-02-16 14:07:00 +00:00
}
2013-03-08 09:43:17 +00:00
2013-03-08 09:35:08 +00:00
/* save parameter updates, if there are any */
if ( isset ( $_POST [ WP_FFPC_PARAM . '-delete' ]) )
{
$this -> delete_options ();
$this -> status = 2 ;
header ( " Location: " . $this -> settingslink . " &deleted=true " );
}
2012-02-16 14:07:00 +00:00
2013-03-07 12:09:59 +00:00
if ( $this -> network )
$optionspage = 'settings.php' ;
else
$optionspage = 'options-general.php' ;
2013-03-06 18:31:28 +00:00
/* we use settings menu, no need for highest level menu */
2013-03-07 12:09:59 +00:00
add_submenu_page ( $optionspage , 'Edit WP-FFPC options' , __ ( 'WP-FFPC' , WP_FFPC_PARAM ), 10 , WP_FFPC_OPTIONS_PAGE , array ( $this , 'admin_panel' ) );
2012-02-16 14:07:00 +00:00
}
/**
* settings panel at admin section
*
*/
function admin_panel ( ) {
/**
* security
*/
if ( ! function_exists ( 'current_user_can' ) || ! current_user_can ( 'manage_options' ) ){
die ( );
}
/**
* if options were saved
*/
2012-02-21 13:45:48 +00:00
if ( $_GET [ 'saved' ] == 'true' || $this -> status == 1 ) : ?>
2012-02-16 14:07:00 +00:00
< div id = 'setting-error-settings_updated' class = 'updated settings-error' >< p >< strong > Settings saved .</ strong ></ p ></ div >
2012-02-21 13:45:48 +00:00
< ? php endif ;
2012-02-16 14:07:00 +00:00
2013-03-08 09:35:08 +00:00
/**
* if options were saved
*/
if ( $_GET [ 'deleted' ] == 'true' || $this -> status == 2 ) : ?>
< div id = 'setting-error-options_deleted' class = 'error' >< p >< strong > Plugin options deleted .</ strong ></ p ></ div >
< ? php endif ;
2012-02-16 14:07:00 +00:00
/**
* the admin panel itself
*/
?>
2012-12-07 16:57:18 +00:00
2013-03-05 13:23:34 +00:00
< script >
jQuery ( document ) . ready ( function ( $ ) {
jQuery ( " #wp-ffpc-settings " ) . tabs ();
});
</ script >
2012-12-07 16:57:18 +00:00
< div class = " wrap " >
2013-03-06 18:31:28 +00:00
< h4 > This plugin helped your business ? < a href = " <?php echo WP_FFPC_DONATION_LINK; ?> " > Buy me a coffee for having it , please : ) </ a ></ h4 >
2013-03-06 12:57:29 +00:00
2012-02-21 13:45:48 +00:00
< ? php if ( ! WP_CACHE ) : ?>
2013-03-06 19:03:30 +00:00
< div class = " error " >< p >< strong >< ? php _e ( " WARNING: WP_CACHE is disabled, plugin will not work that way. Please add define( 'WP_CACHE', true ); into the beginning of wp-config.php " , WP_FFPC_PARAM ); ?> </strong></p></div>
2012-12-07 16:57:18 +00:00
< ? php endif ; ?>
2013-03-06 18:31:28 +00:00
< ? php if ( ! file_exists ( WP_FFPC_ACACHE_MAIN_FILE ) ) : ?>
2013-03-06 19:03:30 +00:00
< div class = " error " >< p >< strong >< ? php _e ( " WARNING: advanced cache file is yet to be generated, please save settings! " , WP_FFPC_PARAM ); ?> </strong></p></div>
2013-03-06 18:31:28 +00:00
< ? php endif ; ?>
2013-03-06 19:03:30 +00:00
< ? php if ( $this -> options [ 'cache_type' ] == 'memcached' && ! class_exists ( 'Memcached' ) ) : ?>
< div class = " error " >< p >< strong >< ? php _e ( 'ERROR: Memcached cache backend activated but no PHP memcached extension was found.' , WP_FFPC_PARAM ); ?> </strong></p></div>
< ? php endif ; ?>
2013-03-06 18:31:28 +00:00
2013-03-06 19:03:30 +00:00
< ? php if ( $this -> options [ 'cache_type' ] == 'memcache' && ! class_exists ( 'Memcache' ) ) : ?>
< div class = " error " >< p >< strong >< ? php _e ( 'ERROR: Memcache cache backend activated but no PHP memcache extension was found.' , WP_FFPC_PARAM ); ?> </strong></p></div>
2012-12-07 16:57:18 +00:00
< ? php endif ; ?>
< ? php
2013-03-06 18:31:28 +00:00
/* get the current runtime configuration for memcache in PHP */
2012-12-07 16:57:18 +00:00
$memcached_settings = ini_get_all ( 'memcache' );
$memcached_protocol = strtolower ( $memcached_settings [ 'memcache.protocol' ][ 'local_value' ]);
?>
2013-03-06 19:03:30 +00:00
< ? php if ( $this -> options [ 'cache_type' ] == 'memcache' && $memcached_protocol == 'binary' ) : ?>
< div class = " error " >< p >< strong >< ? php _e ( 'WARNING: Memcache extension is configured to use binary mode. This is very buggy and the plugin will most probably not work. Please consider to change either to ascii mode or to Mecached extension.' , WP_FFPC_PARAM ); ?> </strong></p></div>
2012-12-07 16:57:18 +00:00
< ? php endif ; ?>
< ? php if ( $this -> options [ 'cache_type' ] == 'memcached' || $this -> options [ 'cache_type' ] == 'memcache' ) : ?>
2013-03-06 19:03:30 +00:00
< div class = " updated " >
2013-03-06 12:57:29 +00:00
< p >< strong >
2012-12-07 16:57:18 +00:00
< ? php
2013-03-06 12:57:29 +00:00
_e ( 'Driver: ' , WP_FFPC_PARAM );
echo $this -> options [ 'cache_type' ];
?>
</ strong ></ p >
< p >
< ? php
_e ( '<strong>Backend status:</strong><br />' , WP_FFPC_PARAM );
2013-03-07 15:07:56 +00:00
$init = wp_ffpc_init ( $this -> options );
2013-03-06 18:31:28 +00:00
/* we need to go through all servers */
2013-03-06 12:57:29 +00:00
foreach ( $this -> options [ 'servers' ] as $server_string => $server ) {
echo $server [ 'host' ] . " : " . $server [ 'port' ] . " => " ;
2013-03-08 11:19:34 +00:00
if ( is_array ( $init ) && $init [ $server_string ] === false )
_e ( '<span class="error-msg">down</span><br />' , WP_FFPC_PARAM );
elseif ( is_array ( $init ) && $init [ $server_string ] === true )
_e ( '<span class="ok-msg">up & running</span><br />' , WP_FFPC_PARAM );
else
_e ( '<span class="error-msg">unknown, please try re-saving settings!</span><br />' , WP_FFPC_PARAM );
2013-03-06 12:57:29 +00:00
}
2012-12-07 16:57:18 +00:00
?>
2013-03-06 12:57:29 +00:00
</ p >
</ div >
2012-02-21 13:45:48 +00:00
< ? php endif ; ?>
2012-02-16 14:07:00 +00:00
2012-08-06 13:17:44 +01:00
< h2 >< ? php _e ( 'WP-FFPC settings' , WP_FFPC_PARAM ) ; ?> </h2>
2013-03-05 16:32:09 +00:00
< form method = " post " action = " # " id = " wp-ffpc-settings " class = " wp-ffpc-panel " >
< ul class = " tabs " >
< li >< a href = " #wp-ffpc-type " class = " wp-switch-editor " >< ? php _e ( 'Cache type' , WP_FFPC_PARAM ); ?> </a></li>
< li >< a href = " #wp-ffpc-debug " class = " wp-switch-editor " >< ? php _e ( 'Debug & in-depth' , WP_FFPC_PARAM ); ?> </a></li>
< li >< a href = " #wp-ffpc-exceptions " class = " wp-switch-editor " >< ? php _e ( 'Cache exceptions' , WP_FFPC_PARAM ); ?> </a></li>
< li >< a href = " #wp-ffpc-apc " class = " wp-switch-editor " >< ? php _e ( 'APC' , WP_FFPC_PARAM ); ?> </a></li>
< li >< a href = " #wp-ffpc-memcached " class = " wp-switch-editor " >< ? php _e ( 'Memcache(d)' , WP_FFPC_PARAM ); ?> </a></li>
< li >< a href = " #wp-ffpc-nginx " class = " wp-switch-editor " >< ? php _e ( 'nginx' , WP_FFPC_PARAM ); ?> </a></li>
2013-03-05 13:23:34 +00:00
</ ul >
< fieldset id = " wp-ffpc-type " >
< legend >< ? php _e ( 'Set cache type' , WP_FFPC_PARAM ); ?> </legend>
2012-02-16 14:07:00 +00:00
< dl >
< dt >
2012-12-07 16:57:18 +00:00
< label for = " cache_type " >< ? php _e ( 'Select backend' , WP_FFPC_PARAM ); ?> </label>
2012-02-16 14:07:00 +00:00
</ dt >
< dd >
< select name = " cache_type " id = " cache_type " >
< ? php $this -> cache_type ( $this -> options [ 'cache_type' ] ) ?>
</ select >
2012-12-07 16:57:18 +00:00
< span class = " description " >< ? php _e ( 'Select cache driver: ' , WP_FFPC_PARAM ); ?> </span>
2012-02-16 14:07:00 +00:00
< span class = " default " >< ? php _e ( 'Default ' , WP_FFPC_PARAM ); ?> : <?php $this->cache_type( $this->defaults['cache_type'] , true ) ; ?></span>
</ dd >
< dt >
< label for = " expire " >< ? php _e ( 'Entry invalidation time' , WP_FFPC_PARAM ); ?> </label>
</ dt >
< dd >
< input type = " number " name = " expire " id = " expire " value = " <?php echo $this->options ['expire']; ?> " />
< span class = " description " >< ? php _e ( 'How long will an entry be valid' , WP_FFPC_PARAM ); ?> </span>
< span class = " default " >< ? php _e ( 'Default ' , WP_FFPC_PARAM ); ?> : <?php echo $this->defaults['expire']; ?></span>
</ dd >
< dt >
< label for = " charset " >< ? php _e ( 'Charset to send data with.' , WP_FFPC_PARAM ); ?> </label>
</ dt >
< dd >
< input type = " text " name = " charset " id = " charset " value = " <?php echo $this->options ['charset']; ?> " />
< span class = " description " >< ? php _e ( 'Charset of HTML and XML (pages and feeds) data.' , WP_FFPC_PARAM ); ?> </span>
< span class = " default " >< ? php _e ( 'Default ' , WP_FFPC_PARAM ); ?> : <?php echo $this->defaults['charset']; ?></span>
</ dd >
< dt >
< label for = " invalidation_method " >< ? php _e ( 'Cache invalidation method' , WP_FFPC_PARAM ); ?> </label>
</ dt >
< dd >
< select name = " invalidation_method " id = " invalidation_method " >
< ? php $this -> invalidation_method ( $this -> options [ 'invalidation_method' ] ) ?>
</ select >
< span class = " description " >< ? php _e ( 'Select cache invalidation method. <p><strong>WARNING! When selection "all", the cache will be fully flushed, including elements that were set by other applications.</strong></p>' , WP_FFPC_PARAM ); ?> </span>
< span class = " default " >< ? php _e ( 'Default ' , WP_FFPC_PARAM ); ?> : <?php $this->invalidation_method( $this->defaults['invalidation_method'] , true ) ; ?></span>
</ dd >
< dt >
< label for = " prefix_data " >< ? php _e ( 'Data prefix' , WP_FFPC_PARAM ); ?> </label>
</ dt >
< dd >
< input type = " text " name = " prefix_data " id = " prefix_data " value = " <?php echo $this->options ['prefix_data']; ?> " />
< span class = " description " >< ? php _e ( 'Prefix for HTML content keys, can be used in nginx.' , WP_FFPC_PARAM ); ?> </span>
< span class = " default " >< ? php _e ( 'Default ' , WP_FFPC_PARAM ); ?> : <?php echo $this->defaults['prefix_data']; ?></span>
</ dd >
< dt >
< label for = " prefix_meta " >< ? php _e ( 'Meta prefix' , WP_FFPC_PARAM ); ?> </label>
</ dt >
< dd >
< input type = " text " name = " prefix_meta " id = " prefix_meta " value = " <?php echo $this->options ['prefix_meta']; ?> " />
< span class = " description " >< ? php _e ( 'Prefix for meta content keys, used only with PHP processing.' , WP_FFPC_PARAM ); ?> </span>
< span class = " default " >< ? php _e ( 'Default ' , WP_FFPC_PARAM ); ?> : <?php echo $this->defaults['prefix_meta']; ?></span>
</ dd >
2013-03-05 13:23:34 +00:00
</ dl >
</ fieldset >
2012-02-16 14:07:00 +00:00
2013-03-05 13:23:34 +00:00
< fieldset id = " wp-ffpc-debug " >
< legend >< ? php _e ( 'Debug & in-depth settings' , WP_FFPC_PARAM ); ?> </legend>
< dl >
2012-02-16 14:07:00 +00:00
< dt >
< label for = " debug " >< ? php _e ( " Enable debug mode " , WP_FFPC_PARAM ); ?> </label>
</ dt >
< dd >
< input type = " checkbox " name = " debug " id = " debug " value = " 1 " < ? php checked ( $this -> options [ 'debug' ], true ); ?> />
< span class = " description " >< ? php _e ( 'An additional header, "X-Cache-Engine" will be added when pages are served through WP-FFPC.' , WP_FFPC_PARAM ); ?> </span>
< span class = " default " >< ? php _e ( 'Default ' , WP_FFPC_PARAM ); ?> : <?php $this->print_bool( $this->defaults['debug']); ?></span>
</ dd >
2012-02-21 07:01:46 +00:00
< dt >
< label for = " syslog " >< ? php _e ( " Enable syslog messages " , WP_FFPC_PARAM ); ?> </label>
</ dt >
< dd >
< input type = " checkbox " name = " syslog " id = " syslog " value = " 1 " < ? php checked ( $this -> options [ 'syslog' ], true ); ?> />
2012-02-21 13:45:48 +00:00
< span class = " description " >< ? php _e ( 'Writes sets, gets and flushes at INFO level into syslog, using "syslog" function of PHP.' , WP_FFPC_PARAM ); ?> </span>
2012-02-21 07:01:46 +00:00
< span class = " default " >< ? php _e ( 'Default ' , WP_FFPC_PARAM ); ?> : <?php $this->print_bool( $this->defaults['syslog']); ?></span>
</ dd >
2012-02-16 14:07:00 +00:00
< dt >
< label for = " pingback_status " >< ? php _e ( " Enable pingback links " , WP_FFPC_PARAM ); ?> </label>
</ dt >
< dd >
< input type = " checkbox " name = " pingback_status " id = " pingback_status " value = " 1 " < ? php checked ( $this -> options [ 'pingback_status' ], true ); ?> />
< span class = " description " >< ? php _e ( 'Enable "X-Pingback" headers in cached pages; will always use accessed hostname as host!' , WP_FFPC_PARAM ); ?> </span>
< span class = " default " >< ? php _e ( 'Default ' , WP_FFPC_PARAM ); ?> : <?php $this->print_bool( $this->defaults['pingback_status']); ?></span>
</ dd >
2012-12-07 16:57:18 +00:00
< dt >
< label for = " sync_protocols " >< ? php _e ( " Enable sync protocolls " , WP_FFPC_PARAM ); ?> </label>
</ dt >
< dd >
< input type = " checkbox " name = " sync_protocols " id = " sync_protocols " value = " 1 " < ? php checked ( $this -> options [ 'sync_protocols' ], true ); ?> />
< span class = " description " >< ? php _e ( 'Enable to replace every protocol to the same as in the request for site\'s domain' , WP_FFPC_PARAM ); ?> </span>
< span class = " default " >< ? php _e ( 'Default ' , WP_FFPC_PARAM ); ?> : <?php $this->print_bool( $this->defaults['sync_protocols']); ?></span>
</ dd >
2013-03-05 13:23:34 +00:00
</ dl >
</ fieldset >
2012-12-07 16:57:18 +00:00
2013-03-05 13:23:34 +00:00
< fieldset id = " wp-ffpc-exceptions " >
< legend >< ? php _e ( 'Set cache excepions' , WP_FFPC_PARAM ); ?> </legend>
< dl >
2012-02-16 14:07:00 +00:00
< dt >
< label for = " cache_loggedin " >< ? php _e ( 'Enable cache for logged in users' , WP_FFPC_PARAM ); ?> </label>
</ dt >
< dd >
< input type = " checkbox " name = " cache_loggedin " id = " cache_loggedin " value = " 1 " < ? php checked ( $this -> options [ 'cache_loggedin' ], true ); ?> />
< span class = " description " >< ? php _e ( 'Cache pages even if user is logged in.' , WP_FFPC_PARAM ); ?> </span>
< span class = " default " >< ? php _e ( 'Default ' , WP_FFPC_PARAM ); ?> : <?php $this->print_bool( $this->defaults['cache_loggedin']); ?></span>
</ dd >
< dt >
< label for = " nocache_home " >< ? php _e ( " Don't cache home " , WP_FFPC_PARAM ); ?> </label>
</ dt >
< dd >
< input type = " checkbox " name = " nocache_home " id = " nocache_home " value = " 1 " < ? php checked ( $this -> options [ 'nocache_home' ], true ); ?> />
< span class = " description " >< ? php _e ( 'Exclude home page from caching' , WP_FFPC_PARAM ); ?> </span>
< span class = " default " >< ? php _e ( 'Default ' , WP_FFPC_PARAM ); ?> : <?php $this->print_bool( $this->defaults['nocache_home']); ?></span>
</ dd >
< dt >
< label for = " nocache_feed " >< ? php _e ( " Don't cache feeds " , WP_FFPC_PARAM ); ?> </label>
</ dt >
< dd >
< input type = " checkbox " name = " nocache_feed " id = " nocache_feed " value = " 1 " < ? php checked ( $this -> options [ 'nocache_feed' ], true ); ?> />
< span class = " description " >< ? php _e ( 'Exclude feeds from caching.' , WP_FFPC_PARAM ); ?> </span>
< span class = " default " >< ? php _e ( 'Default ' , WP_FFPC_PARAM ); ?> : <?php $this->print_bool( $this->defaults['nocache_feed']); ?></span>
</ dd >
< dt >
< label for = " nocache_archive " >< ? php _e ( " Don't cache archives " , WP_FFPC_PARAM ); ?> </label>
</ dt >
< dd >
< input type = " checkbox " name = " nocache_archive " id = " nocache_archive " value = " 1 " < ? php checked ( $this -> options [ 'nocache_archive' ], true ); ?> />
< span class = " description " >< ? php _e ( 'Exclude archives from caching.' , WP_FFPC_PARAM ); ?> </span>
< span class = " default " >< ? php _e ( 'Default ' , WP_FFPC_PARAM ); ?> : <?php $this->print_bool( $this->defaults['nocache_archive']); ?></span>
</ dd >
< dt >
< label for = " nocache_single " >< ? php _e ( " Don't cache posts (and single-type entries) " , WP_FFPC_PARAM ); ?> </label>
</ dt >
< dd >
< input type = " checkbox " name = " nocache_single " id = " nocache_single " value = " 1 " < ? php checked ( $this -> options [ 'nocache_single' ], true ); ?> />
< span class = " description " >< ? php _e ( 'Exclude singles from caching.' , WP_FFPC_PARAM ); ?> </span>
< span class = " default " >< ? php _e ( 'Default ' , WP_FFPC_PARAM ); ?> : <?php $this->print_bool( $this->defaults['nocache_single']); ?></span>
</ dd >
< dt >
< label for = " nocache_page " >< ? php _e ( " Don't cache pages " , WP_FFPC_PARAM ); ?> </label>
</ dt >
< dd >
< input type = " checkbox " name = " nocache_page " id = " nocache_page " value = " 1 " < ? php checked ( $this -> options [ 'nocache_page' ], true ); ?> />
< span class = " description " >< ? php _e ( 'Exclude pages from caching.' , WP_FFPC_PARAM ); ?> </span>
< span class = " default " >< ? php _e ( 'Default ' , WP_FFPC_PARAM ); ?> : <?php $this->print_bool( $this->defaults['nocache_page']); ?></span>
</ dd >
</ dl >
</ fieldset >
2013-03-05 13:23:34 +00:00
< fieldset id = " wp-ffpc-apc " >
< legend >< ? php _e ( 'Settings for APC' , WP_FFPC_PARAM ); ?> </legend>
2012-02-16 14:07:00 +00:00
< dl >
2013-03-05 13:23:34 +00:00
< dt >
< label for = " apc_compress " >< ? php _e ( " Compress entries " , WP_FFPC_PARAM ); ?> </label>
</ dt >
< dd >
< input type = " checkbox " name = " apc_compress " id = " apc_compress " value = " 1 " < ? php checked ( $this -> options [ 'apc_compress' ], true ); ?> />
< span class = " description " >< ? php _e ( 'Try to compress APC entries. Requires PHP ZLIB.' , WP_FFPC_PARAM ); ?> </span>
< span class = " default " >< ? php _e ( 'Default ' , WP_FFPC_PARAM ); ?> : <?php $this->print_bool( $this->defaults['apc_compress']); ?></span>
</ dd >
</ dl >
</ fieldset >
< fieldset id = " wp-ffpc-memcached " >
< legend >< ? php _e ( 'Settings for memcached backend' , WP_FFPC_PARAM ); ?> </legend>
< dl >
2012-02-16 14:07:00 +00:00
< dt >
2013-03-06 12:57:29 +00:00
< label for = " host " >< ? php _e ( 'Host:Port' , WP_FFPC_PARAM ); ?> </label>
2012-02-16 14:07:00 +00:00
</ dt >
< dd >
2013-03-06 12:57:29 +00:00
< input type = " text " name = " hosts " id = " hosts " value = " <?php echo $this->options ['hosts']; ?> " />
< span class = " description " >< ? php _e ( 'List all valid Hostname:Port[,Hostname:Port] for memcached server(s)' , WP_FFPC_PARAM ); ?> </span>
2012-02-16 14:07:00 +00:00
< span class = " default " >< ? php _e ( 'Default ' , WP_FFPC_PARAM ); ?> : <?php echo $this->defaults['host']; ?></span>
</ dd >
< dt >
2013-03-06 12:57:29 +00:00
< label for = " persistent " >< ? php _e ( 'Persistent memcache connections' , WP_FFPC_PARAM ); ?> </label>
2012-02-16 14:07:00 +00:00
</ dt >
< dd >
2013-03-06 12:57:29 +00:00
< input type = " checkbox " name = " persistent " id = " persistent " value = " 1 " < ? php checked ( $this -> options [ 'persistent' ], true ); ?> />
< span class = " description " >< ? php _e ( 'Make all memcache connections persistent. Be carefull with this setting, always test the outcome.' , WP_FFPC_PARAM ); ?> </span>
< span class = " default " >< ? php _e ( 'Default ' , WP_FFPC_PARAM ); ?> : <?php echo $this->defaults['persistent']; ?></span>
2012-02-16 14:07:00 +00:00
</ dd >
</ dl >
</ fieldset >
2013-03-05 13:23:34 +00:00
< fieldset id = " wp-ffpc-nginx " >
2012-12-07 16:57:18 +00:00
< legend >< ? php _e ( 'Sample config for nginx to utilize the data entries' , WP_FFPC_PARAM ); ?> </legend>
< ? php
$search = array ( 'DATAPREFIX' , 'MEMCACHEDHOST' , 'MEMCACHEDPORT' );
$replace = array ( $this -> options [ 'prefix_data' ], $this -> options [ 'host' ], $this -> options [ 'port' ] );
$nginx = file_get_contents ( WP_FFPC_DIR . '/nginx-sample.conf' );
$nginx = str_replace ( $search , $replace , $nginx );
2013-03-06 14:13:26 +00:00
foreach ( array_keys ( $this -> options [ 'servers' ] ) as $server ) {
$nginx_servers .= " server " . $server . " ; \n " ;
}
$nginx = str_replace ( 'MEMCACHED_SERVERS' , $nginx_servers , $nginx );
2012-12-07 16:57:18 +00:00
?>
< pre >< ? php echo $nginx ; ?> </pre>
</ fieldset >
2013-03-08 09:35:08 +00:00
< p class = " clearcolumns " >
< input class = " button-primary " type = " submit " name = " <?php echo WP_FFPC_PARAM; ?>-save " id = " <?php echo WP_FFPC_PARAM; ?>-save " value = " Save Changes " />
< input class = " button-secondary " style = " float: right " type = " submit " name = " <?php echo WP_FFPC_PARAM; ?>-delete " id = " <?php echo WP_FFPC_PARAM; ?>-delete " value = " Delete options from DB " />
</ p >
2012-02-16 14:07:00 +00:00
</ form >
< ? php
}
/**
* generates cache type select box
*
* @ param $current
* the active or required size ' s identifier
*
* @ param $returntext
* boolean : is true , the description will be returned of $current size
*
* @ return
* prints either description of $current
* or option list for a < select > input field with $current set as active
*
*/
function cache_type ( $current , $returntext = false ) {
$e = array (
'apc' => 'use APC as store' ,
'memcache' => 'use memcached server with Memcache extension' ,
'memcached' => 'use memcached server with Memcached extension' ,
);
$this -> print_select_options ( $e , $current , $returntext );
}
2013-03-05 13:23:34 +00:00
/**
* see if we are using network - wide setup or not
*
*/
function check_for_network ( ) {
2013-03-07 15:07:56 +00:00
$this -> options_key = $_SERVER [ 'HTTP_HOST' ];
if ( is_plugin_active_for_network ( WP_FFPC_PLUGIN ) )
2013-03-07 12:09:59 +00:00
{
2013-03-07 15:07:56 +00:00
/* set active for network */
$this -> network = true ;
/* replace settings link */
$this -> settingslink = str_replace ( 'options-general.php' , 'settings.php' , $this -> settingslink );
/* set options key */
$this -> options_key = 'network' ;
2013-03-05 13:23:34 +00:00
}
2013-03-07 15:07:56 +00:00
2013-03-05 13:23:34 +00:00
}
2012-02-16 14:07:00 +00:00
/**
* deactivation hook : clear advanced - cache config file
*
*/
function deactivate ( ) {
2013-03-07 15:07:56 +00:00
//if (@file_exists (WP_FFPC_ACACHE_MAIN_FILE))
// @unlink (WP_FFPC_ACACHE_MAIN_FILE);
$this -> save_settings ( false , true );
//$this->generate_config ( true );
2012-02-16 14:07:00 +00:00
}
2013-03-08 09:43:17 +00:00
2013-03-08 09:35:08 +00:00
/**
* delete options from database
*
*/
function delete_options () {
delete_site_option ( WP_FFPC_PARAM );
wp_ffpc_log ( " plugin options deleted " );
}
2012-02-16 14:07:00 +00:00
/**
* invalidate cache
2013-03-06 18:31:28 +00:00
*
* @ param $post_id
* id of post to be removed from cache entries
2012-02-16 14:07:00 +00:00
*/
function invalidate ( $post_id ) {
wp_ffpc_clear ( $post_id );
}
/**
* generates invalidation method select box
*
* @ param $current
* the active or required size ' s identifier
*
* @ param $returntext
* boolean : is true , the description will be returned of $current size
*
* @ return
* prints either description of $current
* or option list for a < select > input field with $current set as active
*
*/
function invalidation_method ( $current , $returntext = false ) {
$e = array (
0 => 'all cached pages (WARNING! Flushes _all_ cached entrys! )' ,
1 => 'only modified post' ,
);
$this -> print_select_options ( $e , $current , $returntext );
}
/**
2013-03-07 15:07:56 +00:00
* generates main advanced - cache config file
*
* @ param $delete_current
* deletes current config settings on deactivation
2012-02-16 14:07:00 +00:00
*
*/
2013-03-07 15:07:56 +00:00
function generate_config ( ) {
2012-02-16 14:07:00 +00:00
$acache = WP_FFPC_ACACHE_MAIN_FILE ;
/* is file currently exists, delete it*/
if ( @ file_exists ( $acache ))
unlink ( $acache );
/* is deletion was unsuccessful, die, we have no rights to do that */
if ( @ file_exists ( $acache ))
return false ;
2013-03-07 15:07:56 +00:00
/* if no config left, don't create empty config */
if ( empty ( $this -> all_options ) )
return false ;
2012-02-16 14:07:00 +00:00
2013-03-07 15:07:56 +00:00
//if ( is_plugin_active_for_network ( WP_FFPC_PLUGIN ) && !is_plugin_active ( WP_FFPC_PLUGIN ) )
// return false;
2012-02-16 14:07:00 +00:00
2013-03-07 15:07:56 +00:00
$string = '<?php' . " \n " . 'global ' . WP_FFPC_CONFIG_VAR . " ; \n " ;
$string .= WP_FFPC_CONFIG_VAR . ' = ' . var_export ( $this -> all_options , true ) . ';' ;
2012-02-16 14:07:00 +00:00
$string .= " \n \n include_once (' " . WP_FFPC_ACACHE_COMMON_FILE . " '); \n include_once (' " . WP_FFPC_ACACHE_INC_FILE . " '); \n " ;
file_put_contents ( $acache , $string );
return true ;
}
2013-03-06 12:57:29 +00:00
2012-02-16 14:07:00 +00:00
/**
* parameters array with default values ;
*
*/
function get_options ( ) {
$defaults = array (
2013-03-06 12:57:29 +00:00
'hosts' => '127.0.0.1:11211' ,
2012-02-16 14:07:00 +00:00
'expire' => 300 ,
'invalidation_method' => 0 ,
'prefix_meta' => 'meta-' ,
'prefix_data' => 'data-' ,
'charset' => 'utf-8' ,
'pingback_status' => false ,
2013-03-08 11:19:34 +00:00
'debug' => false ,
2012-02-21 07:01:46 +00:00
'syslog' => false ,
2012-12-07 16:57:18 +00:00
'cache_type' => 'memcached' ,
2012-02-16 14:07:00 +00:00
'cache_loggedin' => false ,
'nocache_home' => false ,
'nocache_feed' => false ,
'nocache_archive' => false ,
'nocache_single' => false ,
'nocache_page' => false ,
2012-02-19 13:35:46 +00:00
'apc_compress' => false ,
2012-12-07 16:57:18 +00:00
'sync_protocols' => false ,
2013-03-06 12:57:29 +00:00
'persistent' => false ,
'servers' => array (
'host' => '127.0.0.1' ,
'port' => 11211
),
2012-02-16 14:07:00 +00:00
);
$this -> defaults = $defaults ;
2013-03-06 18:31:28 +00:00
/* maps saved options and defaults */
2013-03-07 15:07:56 +00:00
//$this->options = get_site_option( WP_FFPC_PARAM , $defaults, false );
$this -> all_options = get_site_option ( WP_FFPC_PARAM );
if ( ! empty ( $this -> all_options [ $this -> options_key ] ) )
$this -> options = array_merge ( $defaults , $this -> all_options [ $this -> options_key ] );
else
$this -> options = $defaults ;
2013-03-06 12:57:29 +00:00
2013-03-08 09:35:08 +00:00
/* check for version number , if none , we ' re upgrading from 0.5 , therefore we need to copy the settings and remove them after
this will run once only at all
*/
if ( empty ( $this -> all_options [ 'version' ] ) )
{
foreach ( array_keys ( $defaults ) as $key ) {
if ( isset ( $this -> all_options [ $key ] ) )
{
$this -> options [ $key ] = $this -> all_options [ $key ];
unset ( $this -> all_options [ $key ] );
}
}
}
2012-02-16 14:07:00 +00:00
}
/**
* prints `true` or `false` depending on a bool variable .
*
* @ param $val
* The boolen variable to print status of .
*
*/
function print_bool ( $val ) {
$bool = $val ? 'true' : 'false' ;
echo $bool ;
}
/**
* select field processor
*
* @ param sizes
* array to build < option > values of
*
* @ param $current
* the current resize type
*
* @ param $returntext
* boolean : is true , the description will be returned of $current type
*
* @ return
* prints either description of $current
* or option list for a < select > input field with $current set as active
*
*/
function print_select_options ( $sizes , $current , $returntext = false ) {
if ( $returntext )
{
_e ( $sizes [ $current ] , WP_FFPC_PARAM );
return ;
}
foreach ( $sizes as $ext => $name )
{
?>
< option value = " <?php echo $ext ?> " < ? php selected ( $ext , $current ); ?> >
< ? php _e ( $name , WP_FFPC_PARAM ); ?>
</ option >
< ? php
}
}
/**
* function to be able to store redirects
2013-03-06 18:31:28 +00:00
*
* @ param $redirect_url
* url of required wordpress redirect
*
* @ param $requested_url
* currently unused
*
2012-02-16 14:07:00 +00:00
*/
function redirect_canonical ( $redirect_url , $requested_url ) {
global $wp_nmc_redirect ;
$wp_nmc_redirect = $redirect_url ;
return $redirect_url ;
}
/**
* save settings function
*
2013-03-06 18:31:28 +00:00
* @ param firstrun
* boolean : true if the function is called on plugin activation
*
2012-02-16 14:07:00 +00:00
*/
2013-03-07 15:07:56 +00:00
function save_settings ( $firstrun = false , $delete_current = false ) {
2012-02-16 14:07:00 +00:00
2013-03-07 15:07:56 +00:00
if ( $delete_current )
unset ( $this -> all_options [ $this -> options_key ] );
else
$this -> update_settings ( $firstrun );
2013-03-06 12:57:29 +00:00
2013-03-08 09:35:08 +00:00
$this -> all_options [ 'version' ] = WP_FFPC_VERSION ;
2013-03-06 18:31:28 +00:00
/* save options */
2013-03-07 15:07:56 +00:00
update_site_option ( WP_FFPC_PARAM , $this -> all_options );
2012-02-16 14:07:00 +00:00
2013-03-06 18:31:28 +00:00
/* invalidate cache, this is neccessary */
2012-02-19 13:35:46 +00:00
$this -> invalidate ( 'system_flush' );
2013-03-06 18:31:28 +00:00
/* if it's not for the first run, generate the config file */
2012-02-16 14:07:00 +00:00
if ( ! $firstrun )
2013-03-07 15:07:56 +00:00
$this -> generate_config ( );
2012-02-19 13:35:46 +00:00
2012-02-16 14:07:00 +00:00
}
2013-03-07 12:09:59 +00:00
/**
*
*
*/
function settings_link ( $links ) {
$settings_link = '<a href="' . $this -> settingslink . '">' . __ ( 'Settings' , WP_FFPC_PARAM ) . '</a>' ;
array_unshift ( $links , $settings_link );
return $links ;
}
2013-03-06 18:31:28 +00:00
/**
* splits config parameter " hosts " into an array of server string , host and port
* to be used in later config
*
*/
2013-03-06 12:57:29 +00:00
function split_hosts ( ) {
$servers = explode ( WP_FFPC_SERVER_LIST_SEPARATOR , $this -> options [ 'hosts' ]);
$good_servers = array ();
foreach ( $servers as $server_num => $server_string ) {
$separator = strpos ( $server_string , WP_FFPC_SERVER_SEPARATOR );
$host = substr ( $server_string , 0 , $separator );
$port = substr ( $server_string , $separator + 1 );
if ( ! empty ( $host ) && ! empty ( $port ) && is_numeric ( $port ) ) {
$good_servers [ $server_string ] = array (
'host' => $host ,
'port' => $port
);
}
}
if ( ! empty ( $good_servers ))
$this -> options [ 'servers' ] = $good_servers ;
}
2012-02-16 14:07:00 +00:00
/**
* clean up at uninstall
*
*/
function uninstall ( ) {
delete_site_option ( WP_FFPC_PARAM );
2013-03-06 17:37:48 +00:00
wp_ffpc_log ( " plugin uninstalled " );
2012-02-16 14:07:00 +00:00
}
2013-03-07 15:07:56 +00:00
function update_settings ( $firstrun = false ) {
$options = $this -> defaults ;
/* only try to update defaults if it's not first run and $_POST is not empty */
if ( ! $firstrun && ! empty ( $_POST ) )
{
foreach ( $options as $key => $default )
{
/* $_POST element is available */
if ( ! empty ( $_POST [ $key ]))
{
$update = $_POST [ $key ];
/* get rid of slashed */
if ( strlen ( $update ) != 0 &&! is_numeric ( $update ) )
$update = stripslashes ( $update );
$options [ $key ] = $update ;
}
/* empty $_POST element : when HTML form posted , empty checkboxes a 0 values will not be
part of the $_POST array , thus we need to check if this is the situation by
checking the types of the elements , since a missing value could mean update from 1 to 0
*/
elseif ( empty ( $_POST [ $key ] ) && ( is_bool ( $default ) || is_int ( $default ) ) )
{
$options [ $key ] = 0 ;
}
}
}
$this -> options = $options ;
/* set up server array from hosts config var */
$this -> split_hosts ();
$this -> all_options [ $this -> options_key ] = $this -> options ;
}
2012-02-16 14:07:00 +00:00
}
}
/**
* instantiate the class
*/
$wp_nmc = new WPFFPC ();
?>