plugin_url = plugin_dir_url( __FILE__ );
$this->plugin_dir = plugin_dir_path( __FILE__ );
$this->common_url = $this->plugin_url . self::common_slug;
$this->common_dir = $this->plugin_dir . self::common_slug;
$this->admin_css_handle = $this->plugin_constant . '-admin-css';
$this->admin_css_url = $this->common_url . 'wp-admin.css';
}
/**
* init hook function runs before admin panel hook, themeing and options read
*/
public function plugin_pre_init() {
/* advanced cache "worker" file */
$this->acache_worker = $this->plugin_dir . $this->plugin_constant . '-acache.php';
/* WordPress advanced-cache.php file location */
$this->acache = WP_CONTENT_DIR . '/advanced-cache.php';
/* nginx sample config file */
$this->nginx_sample = $this->plugin_dir . $this->plugin_constant . '-nginx-sample.conf';
/* backend driver file */
$this->acache_backend = $this->plugin_dir . $this->plugin_constant . '-backend.php';
/* flush button identifier */
$this->button_flush = $this->plugin_constant . '-flush';
/* precache button identifier */
$this->button_precache = $this->plugin_constant . '-precache';
/* global options identifier */
$this->global_option = $this->plugin_constant . '-global';
/* precache log */
$this->precache_logfile = sys_get_temp_dir() . '/' . self::precache_log;
/* this is the precacher php worker file */
$this->precache_phpfile = sys_get_temp_dir() . '/' . self::precache_php;
/* search for a system function */
$this->shell_possibilities = array ( 'shell_exec', 'exec', 'system', 'passthru' );
/* get disabled functions list */
$disabled_functions = array_map('trim', explode(',', ini_get('disable_functions') ) );
foreach ( $this->shell_possibilities as $possible ) {
if ( function_exists ($possible) && ! ( ini_get('safe_mode') || in_array( $possible, $disabled_functions ) ) ) {
/* set shell function */
$this->shell_function = $possible;
break;
}
}
/* set global config key; here, because it's needed for migration */
if ( $this->network ) {
$this->global_config_key = 'network';
}
else {
$sitedomain = parse_url( get_option('siteurl') , PHP_URL_HOST);
if ( $_SERVER['HTTP_HOST'] != $sitedomain ) {
$this->errors['domain_mismatch'] = sprintf( __("Domain mismatch: the site domain configuration (%s) does not match the HTTP_HOST (%s) variable in PHP. Please update the settings to match the two, otherwise the plugin may not work as expected.", $this->plugin_constant ), $sitedomain, $_SERVER['HTTP_HOST'] );
}
$this->global_config_key = $_SERVER['HTTP_HOST'];
}
/* cache type possible values array */
$this->select_cache_type = array (
'apc' => __( 'APC' , $this->plugin_constant ),
'apcu' => __( 'APCu' , $this->plugin_constant ),
'memcache' => __( 'PHP Memcache' , $this->plugin_constant ),
'memcached' => __( 'PHP Memcached' , $this->plugin_constant ),
'redis' => __( 'Redis (experimental, it will break!)' , $this->plugin_constant ),
);
/* check for required functions / classes for the cache types */
$this->valid_cache_type = array (
'apc' => function_exists( 'apc_cache_info' ) ? true : false,
'apcu' => function_exists( 'apcu_cache_info' ) ? true : false,
'memcache' => class_exists ( 'Memcache') ? true : false,
'memcached' => class_exists ( 'Memcached') ? true : false,
'redis' => class_exists( 'Redis' ) ? true : false,
);
/* invalidation method possible values array */
$this->select_invalidation_method = array (
0 => __( 'flush cache' , $this->plugin_constant ),
1 => __( 'only modified post' , $this->plugin_constant ),
2 => __( 'modified post and all taxonomies' , $this->plugin_constant ),
3 => __( 'modified post and posts index page' , $this->plugin_constant ),
);
/* map of possible key masks */
$this->list_uri_vars = array (
'$scheme' => __('The HTTP scheme (i.e. http, https).', $this->plugin_constant ),
'$host' => __('Host in the header of request or name of the server processing the request if the Host header is not available.', $this->plugin_constant ),
'$request_uri' => __('The *original* request URI as received from the client including the args', $this->plugin_constant ),
'$remote_user' => __('Name of user, authenticated by the Auth Basic Module', $this->plugin_constant ),
'$cookie_PHPSESSID' => __('PHP Session Cookie ID, if set ( empty if not )', $this->plugin_constant ),
//'$cookie_COOKnginy IE' => __('Value of COOKIE', $this->plugin_constant ),
//'$http_HEADER' => __('Value of HTTP request header HEADER ( lowercase, dashes converted to underscore )', $this->plugin_constant ),
//'$query_string' => __('Full request URI after rewrites', $this->plugin_constant ),
//'' => __('', $this->plugin_constant ),
);
/* get current wp_cron schedules */
$wp_schedules = wp_get_schedules();
/* add 'null' to switch off timed precache */
$schedules['null'] = __( 'do not use timed precache' );
foreach ( $wp_schedules as $interval=>$details ) {
$schedules[ $interval ] = $details['display'];
}
$this->select_schedules = $schedules;
}
/**
* additional init, steps that needs the plugin options
*
*/
public function plugin_post_init () {
/* initiate backend */
$this->backend = new WP_FFPC_Backend ( $this->options );
/* re-save settings after update */
add_action( 'upgrader_process_complete', array ( &$this->plugin_upgrade ), 10, 2 );
/* cache invalidation hooks */
add_action( 'transition_post_status', array( &$this->backend , 'clear_ng' ), 10, 3 );
/* comments invalidation hooks */
if ( $this->options['comments_invalidate'] ) {
add_action( 'comment_post', array( &$this->backend , 'clear' ), 0 );
add_action( 'edit_comment', array( &$this->backend , 'clear' ), 0 );
add_action( 'trashed_comment', array( &$this->backend , 'clear' ), 0 );
add_action( 'pingback_post', array( &$this->backend , 'clear' ), 0 );
add_action( 'trackback_post', array( &$this->backend , 'clear' ), 0 );
add_action( 'wp_insert_comment', array( &$this->backend , 'clear' ), 0 );
}
/* invalidation on some other ocasions as well */
add_action( 'switch_theme', array( &$this->backend , 'clear' ), 0 );
add_action( 'deleted_post', array( &$this->backend , 'clear' ), 0 );
add_action( 'edit_post', array( &$this->backend , 'clear' ), 0 );
/* add filter for catching canonical redirects */
if ( WP_CACHE )
add_filter('redirect_canonical', 'wp_ffpc_redirect_callback', 10, 2);
/* add precache coldrun action */
add_action( self::precache_id , array( &$this, 'precache_coldrun' ) );
/* link on to settings for plugins page */
$settings_link = ' » ' . __( 'WP-FFPC Settings', $this->plugin_constant ) . '';
/* check & collect errors */
/* look for WP_CACHE */
if ( ! WP_CACHE )
$this->errors['no_wp_cache'] = __("WP_CACHE is disabled, plugin will not work that way. Please add `define ( 'WP_CACHE', true );` to wp-config.php", $this->plugin_constant ) . $settings_link;
/* look for global settings array */
if ( ! $this->global_saved )
$this->errors['no_global_saved'] = __("Plugin settings are not yet saved for the site, please save settings!", $this->plugin_constant) . $settings_link;
/* look for writable acache file */
if ( file_exists ( $this->acache ) && ! is_writable ( $this->acache ) )
$this->errors['no_acache_write'] = __("Advanced cache file is not writeable!
Please change the permissions on the file: ", $this->plugin_constant) . $this->acache;
/* look for acache file */
if ( ! file_exists ( $this->acache ) )
$this->errors['no_acache_saved'] = __("Advanced cache file is yet to be generated, please save settings!", $this->plugin_constant). $settings_link;
/* look for extensions that should be available */
foreach ( $this->valid_cache_type as $backend => $status ) {
if ( $this->options['cache_type'] == $backend && ! $status ) {
$this->errors['no_backend'] = sprintf ( __('%s cache backend activated but no PHP %s extension was found.
Please either use different backend or activate the module!', $this->plugin_constant), $backend, $backend );
}
}
/* get the current runtime configuration for memcache in PHP because Memcache in binary mode is really problematic */
if ( extension_loaded ( 'memcache' ) ) {
$memcache_settings = ini_get_all( 'memcache' );
if ( !empty ( $memcache_settings ) && $this->options['cache_type'] == 'memcache' )
{
$memcache_protocol = strtolower($memcache_settings['memcache.protocol']['local_value']);
if ( $memcache_protocol == 'binary' ) {
$this->errors['memcached_binary'] = __('WARNING: Memcache extension is configured to use binary mode. This is very buggy and the plugin will most probably not work correctly.
Please consider to change either to ASCII mode or to Memcached extension.', $this->plugin_constant );
}
}
}
$filtered_errors = apply_filters('wp_ffpc_post_init_errors_array', $this->errors);
if ($filtered_errors) {
foreach ( $this->errors as $e => $msg ) {
$this->utils->alert ( $msg, LOG_WARNING, $this->network );
}
}
}
/**
* activation hook function, to be extended
*/
public function plugin_activate() {
/* we leave this empty to avoid not detecting WP network correctly */
}
/**
* deactivation hook function, to be extended
*/
public function plugin_deactivate () {
/* remove current site config from global config */
$this->update_global_config( true );
}
/**
* uninstall hook function, to be extended
*/
public function plugin_uninstall( $delete_options = true ) {
/* delete advanced-cache.php file */
unlink ( $this->acache );
/* delete site settings */
if ( $delete_options ) {
$this->plugin_options_delete ();
}
}
/**
* once upgrade is finished, deploy advanced cache and save the new settings, just in case
*/
public function plugin_upgrade ( $upgrader_object, $hook_extra ) {
if (is_plugin_active( $this->plugin_constant . DIRECTORY_SEPARATOR . $this->plugin_constant . '.php' )) {
$this->update_global_config();
$this->plugin_options_save();
$this->deploy_advanced_cache();
$this->utils->alert ( __('WP-FFPC settings were upgraded; please double check if everything is still working correctly.', $this->plugin_constant ), LOG_NOTICE );
}
}
/**
* extending admin init
*
*/
public function plugin_extend_admin_init () {
/* save parameter updates, if there are any */
if ( isset( $_POST[ $this->button_flush ] ) && check_admin_referer ( $this->plugin_constant ) ) {
/* remove precache log entry */
$this->utils->_delete_option( self::precache_log );
/* remove precache timestamp entry */
$this->utils->_delete_option( self::precache_timestamp );
/* remove precache logfile */
if ( @file_exists ( $this->precache_logfile ) ) {
unlink ( $this->precache_logfile );
}
/* remove precache PHP worker */
if ( @file_exists ( $this->precache_phpfile ) ) {
unlink ( $this->precache_phpfile );
}
/* flush backend */
$this->backend->clear( false, true );
$this->status = 3;
header( "Location: ". $this->settings_link . self::slug_flush );
}
/* save parameter updates, if there are any */
if ( isset( $_POST[ $this->button_precache ] ) && check_admin_referer ( $this->plugin_constant ) ) {
/* is no shell function is possible, fail */
if ( $this->shell_function == false ) {
$this->status = 5;
header( "Location: ". $this->settings_link . self::slug_precache_disabled );
}
/* otherwise start full precache */
else {
$this->precache_message = $this->precache_coldrun();
$this->status = 4;
header( "Location: ". $this->settings_link . self::slug_precache );
}
}
}
/**
* admin help panel
*/
public function plugin_admin_help($contextual_help, $screen_id ) {
/* add our page only if the screenid is correct */
if ( strpos( $screen_id, $this->plugin_settings_page ) ) {
$contextual_help = __('
Please visit the official support forum of the plugin for help.
', $this->plugin_constant ); /* [TODO] give detailed information on errors & troubleshooting get_current_screen()->add_help_tab( array( 'id' => $this->plugin_constant . '-issues', 'title' => __( 'Troubleshooting' ), 'content' => __( 'List of errors, possible reasons and solutions
plugin_constant ) ?>
plugin_constant ) ?>
plugin_constant ); ?>
plugin_constant ) ?>
plugin_constant); echo $this->options['cache_type']; ?>
options['cache_type'], 'memcache') ) { ?>Backend status:
', $this->plugin_constant );
/* we need to go through all servers */
$servers = $this->backend->status();
if ( is_array( $servers ) && !empty ( $servers ) ) {
foreach ( $servers as $server_string => $status ) {
echo $server_string ." => ";
if ( $status == 0 )
_e ( 'down
', $this->plugin_constant );
elseif ( ( $this->options['cache_type'] == 'memcache' && $status > 0 ) || $status == 1 )
_e ( 'up & running
', $this->plugin_constant );
else
_e ( 'unknown, please try re-saving settings!
', $this->plugin_constant );
}
}
?>