settingslink = 'options-general.php?page=' . WP_FFPC_OPTIONS_PAGE;
/* check if plugin is network-activated */
$this->check_for_network();
/* register options */
$this->get_options();
/* check is backend is available */
$alive = wp_ffpc_init( $this->options );
/* don't register hooks if backend is dead */
if (!$alive)
{
/* 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);
}
$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' ) );
/* add admin styling */
if( is_admin() )
{
wp_enqueue_script ( "jquery-ui-tabs" );
wp_enqueue_style( WP_FFPC_PARAM . '.admin.css' , WP_FFPC_URL . '/' . WP_FFPC_PARAM .'.admin.css', false, '0.1');
}
/* 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 */
if ( $this->network )
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') );
if ( $this->all_options['version'] < WP_FFPC_VERSION ) {
$this->save_settings ();
if ( @file_exists( WP_FFPC_ACACHE_MAIN_FILE )) {
$this->generate_config();
}
}
}
/**
* activation hook: save default settings in order to eliminate bugs.
*
*/
function activate ( ) {
/* register options for first time */
add_site_option( WP_FFPC_PARAM, $this->options , '' , 'yes');
$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;
header( "Location: ". $this->settingslink ."&saved=true" );
}
/* 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" );
}
if ( $this->network )
$optionspage = 'settings.php';
else
$optionspage = 'options-general.php';
/* we use settings menu, no need for highest level menu */
add_submenu_page( $optionspage, 'Edit WP-FFPC options', __('WP-FFPC', WP_FFPC_PARAM ), 10, WP_FFPC_OPTIONS_PAGE , array ( $this , 'admin_panel' ) );
}
/**
* 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
*/
if ($_GET['saved']=='true' || $this->status == 1) : ?>
status == 2) : ?>
options['cache_type'] == 'memcached' && !class_exists('Memcached') ) : ?>
options['cache_type'] == 'memcache' && !class_exists('Memcache') ) : ?>
options['cache_type'] == 'memcache' && $memcached_protocol == 'binary' ) : ?>
options['cache_type'] == 'memcached' || $this->options['cache_type'] == 'memcache' ) : ?>
options['cache_type'];
?>
Backend status: ', WP_FFPC_PARAM );
$init = wp_ffpc_init( $this->options );
/* we need to go through all servers */
foreach ( $this->options['servers'] as $server_string => $server ) {
echo $server['host'] . ":" . $server['port'] ." => ";
if ( is_array($init) && $init[$server_string] === false )
_e ( 'down ', WP_FFPC_PARAM );
elseif ( is_array($init) && $init[$server_string] === true )
_e ( 'up & running ', WP_FFPC_PARAM );
else
_e ( 'unknown, please try re-saving settings! ', WP_FFPC_PARAM );
}
?>
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 );
}
/**
* see if we are using network-wide setup or not
*
*/
function check_for_network( ) {
$this->options_key = $_SERVER['HTTP_HOST'];
if ( is_plugin_active_for_network ( WP_FFPC_PLUGIN ) )
{
/* 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';
}
}
/**
* deactivation hook: clear advanced-cache config file
*
*/
function deactivate ( ) {
//if (@file_exists (WP_FFPC_ACACHE_MAIN_FILE))
// @unlink (WP_FFPC_ACACHE_MAIN_FILE);
$this->save_settings( false, true );
//$this->generate_config ( true );
}
/**
* delete options from database
*
*/
function delete_options () {
delete_site_option( WP_FFPC_PARAM );
wp_ffpc_log ( "plugin options deleted ");
}
/**
* invalidate cache
*
* @param $post_id
* id of post to be removed from cache entries
*/
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
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 );
}
/**
* generates main advanced-cache config file
*
* @param $delete_current
* deletes current config settings on deactivation
*
*/
function generate_config( ) {
$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;
/* if no config left, don't create empty config */
if ( empty ( $this->all_options ) )
return false;
//if ( is_plugin_active_for_network ( WP_FFPC_PLUGIN ) && !is_plugin_active ( WP_FFPC_PLUGIN ) )
// return false;
$string = 'all_options , true ) . ';';
$string .= "\n\ninclude_once ('" . WP_FFPC_ACACHE_COMMON_FILE . "');\ninclude_once ('" . WP_FFPC_ACACHE_INC_FILE . "');\n";
file_put_contents($acache, $string);
return true;
}
/**
* parameters array with default values;
*
*/
function get_options ( ) {
$defaults = array (
'hosts'=>'127.0.0.1:11211',
'expire'=>300,
'invalidation_method'=>0,
'prefix_meta' =>'meta-',
'prefix_data' =>'data-',
'charset' => 'utf-8',
'pingback_status'=> false,
'debug' => false,
'syslog' => false,
'cache_type' => 'memcached',
'cache_loggedin' => false,
'nocache_home' => false,
'nocache_feed' => false,
'nocache_archive' => false,
'nocache_single' => false,
'nocache_page' => false,
'apc_compress' => false,
'sync_protocols' => false,
'persistent' => false,
'servers' => array (
'host' => '127.0.0.1',
'port' => 11211
),
);
$this->defaults = $defaults;
/* maps saved options and defaults */
//$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;
/* 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] );
}
}
}
}
/**
* 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 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 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)
{
?>
>
all_options[ $this->options_key ] );
else
$this->update_settings( $firstrun );
$this->all_options['version'] = WP_FFPC_VERSION;
/* save options */
update_site_option( WP_FFPC_PARAM , $this->all_options );
/* invalidate cache, this is neccessary */
$this->invalidate('system_flush');
/* if it's not for the first run, generate the config file */
if ( ! $firstrun )
$this->generate_config( );
}
/**
*
*
*/
function settings_link ( $links ) {
$settings_link = '' . __( 'Settings', WP_FFPC_PARAM ) . ' ';
array_unshift( $links, $settings_link );
return $links;
}
/**
* splits config parameter "hosts" into an array of server string, host and port
* to be used in later config
*
*/
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;
}
/**
* clean up at uninstall
*
*/
function uninstall ( ) {
delete_site_option( WP_FFPC_PARAM );
wp_ffpc_log ( "plugin uninstalled ");
}
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;
}
}
}
/**
* instantiate the class
*/
$wp_nmc = new WPFFPC();
?>