plugin_url = plugin_dir_url( __FILE__ ); $this->plugin_dir = plugin_dir_path( __FILE__ ); $this->admin_css_handle = $this->plugin_constant . '-admin-css'; $this->admin_css_url = $this->plugin_url . 'wp-admin.css'; } /** * init hook function runs before admin panel hook, themeing and options read */ public function plugin_pre_init() { static::debug ( __CLASS__, '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 . '-engine.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; } } if (!isset($_SERVER['HTTP_HOST'])) $_SERVER['HTTP_HOST'] = '127.0.0.1'; /* 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 fix the incorrect one, otherwise the plugin may not work as expected.", 'wp-ffpc'), $sitedomain, $_SERVER['HTTP_HOST'] ); } $this->global_config_key = $_SERVER['HTTP_HOST']; } /* cache type possible values array */ $this->select_cache_type = array ( 'apc' => __( 'APC' , 'wp-ffpc'), 'apcu' => __( 'APCu' , 'wp-ffpc'), 'memcache' => __( 'PHP Memcache' , 'wp-ffpc'), 'memcached' => __( 'PHP Memcached' , 'wp-ffpc'), ); /* check for required functions / classes for the cache types */ $this->valid_cache_type = array ( 'apc' => (function_exists( 'apc_cache_info' ) && version_compare(PHP_VERSION, '5.3.0') <= 0 ) ? true : false, 'apcu' => function_exists( 'apcu_cache_info' ) ? true : false, 'memcache' => class_exists ( 'Memcache') ? true : false, 'memcached' => class_exists ( 'Memcached') ? true : false, ); /* invalidation method possible values array */ $this->select_invalidation_method = array ( 0 => __( 'flush cache' , 'wp-ffpc'), 1 => __( 'only modified post' , 'wp-ffpc'), 2 => __( 'modified post and all related taxonomies' , 'wp-ffpc'), ); /* map of possible key masks */ $this->list_uri_vars = array ( '$scheme' => __('The HTTP scheme (i.e. http, https).', 'wp-ffpc'), '$host' => __('Host in the header of request or name of the server processing the request if the Host header is not available.', 'wp-ffpc'), '$request_uri' => __('The *original* request URI as received from the client including the args', 'wp-ffpc'), '$remote_user' => __('Name of user, authenticated by the Auth Basic Module', 'wp-ffpc'), '$cookie_PHPSESSID' => __('PHP Session Cookie ID, if set ( empty if not )', 'wp-ffpc'), '$accept_lang' => __('First HTTP Accept Lang set in the HTTP request', 'wp-ffpc'), //'$cookie_COOKnginy IE' => __('Value of COOKIE', 'wp-ffpc'), //'$http_HEADER' => __('Value of HTTP request header HEADER ( lowercase, dashes converted to underscore )', 'wp-ffpc'), //'$query_string' => __('Full request URI after rewrites', 'wp-ffpc'), //'' => __('', 'wp-ffpc'), ); /* 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 */ $backend_class = 'WP_FFPC_Backend_' . $this->options['cache_type']; $this->backend = new $backend_class ( $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', 'wp-ffpc') . ''; /* check & collect errors */ /* look for WP_CACHE */ if ( ! WP_CACHE ) $this->errors['no_wp_cache'] = __("WP_CACHE is disabled. Without that, cache plugins, like this, will not work. Please add `define ( 'WP_CACHE', true );` to the beginning of wp-config.php.", 'wp-ffpc'); /* look for global settings array */ if ( ! $this->global_saved ) $this->errors['no_global_saved'] = sprintf( __('This site was reached as %s ( according to PHP HTTP_HOST ) and there are no settings present for this domain in the WP-FFPC configuration yet. Please save the %s for the domain or fix the webserver configuration!', 'wp-ffpc'), $_SERVER['HTTP_HOST'], $settings_link); /* look for writable acache file */ if ( file_exists ( $this->acache ) && ! is_writable ( $this->acache ) ) $this->errors['no_acache_write'] = sprintf(__('Advanced cache file (%s) is not writeable!
Please change the permissions on the file.', 'wp-ffpc'), $this->acache); /* look for acache file */ if ( ! file_exists ( $this->acache ) ) $this->errors['no_acache_saved'] = sprintf (__('Advanced cache file is yet to be generated, please save %s', 'wp-ffpc'), $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!', 'wp-ffpc'), $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' && isset($memcache_settings['memcache.protocol']) ) { $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.', 'wp-ffpc'); } } } $filtered_errors = apply_filters('wp_ffpc_post_init_errors_array', $this->errors); if ($filtered_errors) { if ( php_sapi_name() != "cli" ) { foreach ( $this->errors as $e => $msg ) { static::alert ( $msg, LOG_WARNING, $this->network ); } } } add_filter('contextual_help', array( &$this, 'plugin_admin_nginx_help' ), 10, 2); } /** * 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(); static::alert ( __('WP-FFPC settings were upgraded; please double check if everything is still working correctly.', 'wp-ffpc'), 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 ( 'wp-ffpc') ) { /* remove precache log entry */ static::_delete_option( self::precache_log ); /* remove precache timestamp entry */ static::_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 ( 'wp-ffpc') ) { /* 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.

', 'wp-ffpc'); /* [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

E#
' ) ) ); */ } return $contextual_help; } /** * admin help panel */ public function plugin_admin_nginx_help($contextual_help, $screen_id ) { /* add our page only if the screenid is correct */ if ( strpos( $screen_id, $this->plugin_settings_page ) ) { $content = __('

Sample config for nginx to utilize the data entries

', 'wp-ffpc'); $content .= __('
This is not meant to be a copy-paste configuration; you most probably have to tailor it to your needs.
', 'wp-ffpc'); $content .= __('
In case you are about to use nginx to fetch memcached entries directly and to use SHA1 hash keys, you will need an nginx version compiled with HttpSetMiscModule. Otherwise set_sha1 function is not available in nginx.
', 'wp-ffpc'); $content .= '
' . $this->nginx_example() . '
'; get_current_screen()->add_help_tab( array( 'id' => 'wp-ffpc-nginx-help', 'title' => __( 'nginx example', 'wp-ffpc' ), 'content' => $content, ) ); } return $contextual_help; } /** * admin panel, the admin page displayed for plugin settings */ public function plugin_admin_panel() { /** * security, if somehow we're running without WordPress security functions */ if( ! function_exists( 'current_user_can' ) || ! current_user_can( 'manage_options' ) ){ die( ); } /* woo_commenrce page url */ if ( class_exists( 'WooCommerce' ) ) { $page_wc_checkout=str_replace( home_url(), '', wc_get_page_permalink( 'checkout' ) ); $page_wc_myaccount=str_replace( home_url(), '', wc_get_page_permalink( 'myaccount' ) ); $page_wc_cart=str_replace( home_url(), '', wc_get_page_permalink( 'cart' ) ); $wcapi='^/wc-api|^/\?wc-api='; $this->options['nocache_woocommerce_url'] = '^'.$page_wc_checkout.'|^'.$page_wc_myaccount.'|^'.$page_wc_cart.'|'.$wcapi; } else { $this->options['nocache_woocommerce_url'] = ''; } ?>
plugin_donation_form(); /** * if options were saved, display saved message */ if (isset($_GET[ self::key_save ]) && $_GET[ self::key_save ]=='true' || $this->status == 1) { ?>

status == 2) { ?>

status == 3) { ?>

status == 4) { ?>

plugin_name ; _e( ' settings', 'wp-ffpc') ; ?>

options['cache_type']; ?>

options['cache_type'], 'memcache') ) { ?>

Backend status:
', 'wp-ffpc'); /* we need to go through all servers */ $servers = $this->backend->status(); error_log(__CLASS__ . ':' .json_encode($servers)); if ( is_array( $servers ) && !empty ( $servers ) ) { foreach ( $servers as $server_string => $status ) { echo $server_string ." => "; if ( $status == 0 ) _e ( 'down
', 'wp-ffpc'); elseif ( ( $this->options['cache_type'] == 'memcache' && $status > 0 ) || $status == 1 ) _e ( 'up & running
', 'wp-ffpc'); else _e ( 'unknown, please try re-saving settings!
', 'wp-ffpc'); } } ?>

plugin_admin_panel_get_tabs(); ?>
    $tab_label): ?>
    including values set by other applications', 'clear only the modified posts entry, everything else remains in cache', 'unvalidates post and the taxonomy related to the post', ); foreach ($this->select_invalidation_method AS $current_key => $current_invalidation_method) { printf('
  1. %1$s - %2$s
  2. ', $current_invalidation_method, $invalidation_method_description[$current_key]); } ?>
options['comments_invalidate'],true); ?> />
WARNING: changing this will result the previous cache to becomes invalid!
If you are caching with nginx, you should update your nginx configuration and reload nginx after changing this value.', 'wp-ffpc'); ?>
WARNING: changing this will result the previous cache to becomes invalid!', 'wp-ffpc'); ?>
use the guide below to change it.
WARNING: changing this will result the previous cache to becomes invalid!
If you are caching with nginx, you should update your nginx configuration and reload nginx after changing this value.', 'wp-ffpc'); ?>
list_uri_vars as $uri => $desc ) { echo '
'. $uri .'
'. $desc .'
'; } ?>
options['hashkey'],true); ?> /> module in nginx if you want nginx to fetch the data directly; for details, please see the nginx example tab.', 'wp-ffpc'); ?>

WP_DEBUG and the WP_FFPC__DEBUG_MODE constants `true` in wp-config.php.
This will enable NOTICE level messages apart from the WARNING level ones which are always displayed.', 'wp-ffpc'); ?>

options['pingback_header'],true); ?> />
options['response_header'],true); ?> />
options['generate_time'],true); ?> /> tag.', 'wp-ffpc'); ?>
options['cache_loggedin'],true); ?> />
options['nocache_home'],true); ?> /> options['nocache_feed'],true); ?> /> options['nocache_archive'],true); ?> /> options['nocache_page'],true); ?> /> options['nocache_single'],true); ?> /> options['nocache_dyn'],true); ?> /> options['nocache_woocommerce'],true); ?> /> options['nocache_woocommerce_url'] ) ) echo "
Url:".$this->options['nocache_woocommerce_url']; ?>
If you are caching with nginx, you should update your nginx configuration and reload nginx after changing this value.', 'wp-ffpc'); ?>
pattern1|pattern2|etc', 'wp-ffpc'); ?>
. Regular expressions use you must! e.g. pattern1|pattern2|etc
WARNING: be careful where you display this, because it will apply to any content, including archives, collection pages, singles, anything. If empty, this setting will be ignored.', 'wp-ffpc'); ?>
- in case of TCP based connections, list the servers as host1:port1,host2:port2,... . Do not add trailing , and always separate host and port with : .
- for a unix socket enter: unix://[socket_path]', 'wp-ffpc'); ?>

options['authuser'] ) || !empty( $this->options['authpass'] ) ) ) { ?>

options['memcached_binary'],true); ?> />
network ); $log = static::_get_option( self::precache_log, $this->network ); if ( @file_exists ( $this->precache_logfile ) ) { $logtime = filemtime ( $this->precache_logfile ); /* update precache log in DB if needed */ if ( $logtime > $gentime ) { $log = file ( $this->precache_logfile ); static::_update_option( self::precache_log , $log, $this->network ); static::_update_option( self::precache_timestamp , $logtime, $this->network ); } } if ( empty ( $log ) ) { _e('No precache log was found!', 'wp-ffpc'); } else { ?>

status == 5 || $this->shell_function == false ) { ?> Since precaching may take a very long time, it's done through a background CLI process in order not to run out of max execution time of PHP. Please enable one of the following functions if you whish to use precaching: " , 'wp-ffpc') ?>shell_possibilities ); ?>
The plugin tries to visit links of taxonomy terms without the taxonomy name as well. This may generate 404 hits, please be prepared for these in your logfiles if you plan to pre-cache.', 'wp-ffpc'); ?>
__( 'Cache type', 'wp-ffpc'), 'debug' => __( 'Debug & in-depth', 'wp-ffpc'), 'exceptions' => __( 'Cache exceptions', 'wp-ffpc'), 'servers' => __( 'Backend settings', 'wp-ffpc'), 'precache' => __( 'Precache & precache log', 'wp-ffpc') ); return apply_filters('wp_ffpc_admin_panel_tabs', $default_tabs); } /** * extending options_save * */ public function plugin_extend_options_save( $activating ) { /* schedule cron if posted */ $schedule = wp_get_schedule( self::precache_id ); if ( $this->options['precache_schedule'] != 'null' ) { /* clear all other schedules before adding a new in order to replace */ wp_clear_scheduled_hook ( self::precache_id ); static::debug ( $this->plugin_constant, __( 'Scheduling WP-CRON event', 'wp-ffpc') ); $this->scheduled = wp_schedule_event( time(), $this->options['precache_schedule'] , self::precache_id ); } elseif ( ( !isset($this->options['precache_schedule']) || $this->options['precache_schedule'] == 'null' ) && !empty( $schedule ) ) { static::debug ( $this->plugin_constant, __('Clearing WP-CRON scheduled hook ' , 'wp-ffpc') ); wp_clear_scheduled_hook ( self::precache_id ); } /* flush the cache when new options are saved, not needed on activation */ if ( !$activating ) $this->backend->clear(null, true); /* create the to-be-included configuration for advanced-cache.php */ $this->update_global_config(); /* create advanced cache file, needed only once or on activation, because there could be lefover advanced-cache.php from different plugins */ if ( !$activating ) $this->deploy_advanced_cache(); } /** * read hook; needs to be implemented */ public function plugin_extend_options_read( &$options ) { /*if ( strstr( $this->options['nocache_url']), '^wp-' )wp_login_url() $this->options['nocache_url'] = */ /* read the global options, network compatibility */ $this->global_config = get_site_option( $this->global_option ); /* check if current site present in global config */ if ( !empty ( $this->global_config[ $this->global_config_key ] ) ) $this->global_saved = true; $this->global_config[ $this->global_config_key ] = $options; } /** * options delete hook; needs to be implemented */ public function plugin_extend_options_delete( ) { delete_site_option ( $this->global_option ); } /** * need to do migrations from previous versions of the plugin * */ public function plugin_options_migrate( &$options ) { if ( version_compare ( $options['version'] , $this->plugin_version, '<' ) ) { /* cleanup possible leftover files from previous versions */ $check = array ( 'advanced-cache.php', 'nginx-sample.conf', 'wp-ffpc.admin.css', 'wp-ffpc-common.php' ); foreach ( $check as $fname ) { $fname = $this->plugin_dir . $fname; if ( file_exists ( $fname ) ) unlink ( $fname ); } /* look for previous config leftovers */ $try = get_site_option( 'wp-ffpc'); /* network option key changed, remove & migrate the leftovers if there's any */ if ( !empty ( $try ) && $this->network ) { /* clean it up, we don't use it anymore */ delete_site_option ( 'wp-ffpc'); if ( empty ( $options ) && array_key_exists ( $this->global_config_key, $try ) ) { $options = $try [ $this->global_config_key ]; } elseif ( empty ( $options ) && array_key_exists ( 'host', $try ) ) { $options = $try; } } /* updating from version <= 0.4.x */ if ( !empty ( $options['host'] ) ) { $options['hosts'] = $options['host'] . ':' . $options['port']; } /* migrating from version 0.6.x */ elseif ( is_array ( $options ) && array_key_exists ( $this->global_config_key , $options ) ) { $options = $options[ $this->global_config_key ]; } /* renamed options */ if ( isset ( $options['syslog'] ) ) $options['log'] = $options['syslog']; if ( isset ( $options['debug'] ) ) $options['response_header'] = $options['debug']; } } /** * advanced-cache.php creator function * */ private function deploy_advanced_cache( ) { if (!touch($this->acache)) { error_log('Generating advanced-cache.php failed: '.$this->acache.' is not writable'); return false; } /* if no active site left no need for advanced cache :( */ if ( empty ( $this->global_config ) ) { error_log('Generating advanced-cache.php failed: Global config is empty'); return false; } /* add the required includes and generate the needed code */ $string[] = "global_config, true ) . ';' ; //$string[] = "include_once ('" . $this->acache_backend . "');"; $string[] = "include_once ('" . $this->acache_worker . "');"; /* write the file and start caching from this point */ return file_put_contents( $this->acache, join( "\n" , $string ) ); } /** * function to generate working example from the nginx sample file * * @return string nginx config file * */ private function nginx_example () { /* read the sample file */ $nginx = file_get_contents ( $this->nginx_sample ); if ( isset($this->options['hashkey']) && $this->options['hashkey'] == true ) $mckeys = ' set_sha1 $memcached_sha1_key $memcached_raw_key; set $memcached_key DATAPREFIX$memcached_sha1_key;'; else $mckeys = ' set $memcached_key DATAPREFIX$memcached_raw_key;'; $nginx = str_replace ( 'HASHEDORNOT' , $mckeys , $nginx ); /* replace the data prefix with the configured one */ $to_replace = array ( 'DATAPREFIX' , 'KEYFORMAT', 'SERVERROOT', 'SERVERLOG' ); $replace_with = array ( $this->options['prefix_data'], $this->options['key'] , ABSPATH, $_SERVER['SERVER_NAME'] ); $nginx = str_replace ( $to_replace , $replace_with , $nginx ); /* set upstream servers from configured servers, best to get from the actual backend */ $servers = $this->backend->get_servers(); $nginx_servers = ''; if ( is_array ( $servers )) { foreach ( array_keys( $servers ) as $server ) { $nginx_servers .= " server ". $server .";\n"; } } else { $nginx_servers .= " server ". $servers .";\n"; } $nginx = str_replace ( 'MEMCACHED_SERVERS' , $nginx_servers , $nginx ); $loggedincookies = join('|', $this->backend->cookies ); /* this part is not used when the cache is turned on for logged in users */ $loggedin = ' if ($http_cookie ~* "'. $loggedincookies .'" ) { set $memcached_request 0; }'; /* add logged in cache, if valid */ if ( ! $this->options['cache_loggedin']) $nginx = str_replace ( 'LOGGEDIN_EXCEPTION' , $loggedin , $nginx ); else $nginx = str_replace ( 'LOGGEDIN_EXCEPTION' , '' , $nginx ); /* nginx can skip caching for visitors with certain cookies specified in the options */ if( $this->options['nocache_cookies'] ) { $cookies = str_replace( ",","|", $this->options['nocache_cookies'] ); $cookies = str_replace( " ","", $cookies ); $cookie_exception = '# avoid cache for cookies specified if ($http_cookie ~* ' . $cookies . ' ) { set $memcached_request 0; }'; $nginx = str_replace ( 'COOKIES_EXCEPTION' , $cookie_exception , $nginx ); } else { $nginx = str_replace ( 'COOKIES_EXCEPTION' , '' , $nginx ); } /* add custom response header if specified in the options */ if( $this->options['response_header'] && strstr ( $this->options['cache_type'], 'memcached') ) { $response_header = 'add_header X-Cache-Engine "WP-FFPC with ' . $this->options['cache_type'] .' via nginx";'; $nginx = str_replace ( 'RESPONSE_HEADER' , $response_header , $nginx ); } else { $nginx = str_replace ( 'RESPONSE_HEADER' , '' , $nginx ); } return htmlspecialchars($nginx); } /** * function to update global configuration * * @param boolean $remove_site Bool to remove or add current config to global * */ private function update_global_config ( $remove_site = false ) { /* remove or add current config to global config */ if ( $remove_site ) { unset ( $this->global_config[ $this->global_config_key ] ); } else { $this->global_config[ $this->global_config_key ] = $this->options; } /* deploy advanced-cache.php */ $this->deploy_advanced_cache (); /* save options to database */ update_site_option( $this->global_option , $this->global_config ); } /** * generate cache entry for every available permalink, might be very-very slow, * therefore it starts a background process * */ private function precache ( &$links ) { /* double check if we do have any links to pre-cache */ if ( !empty ( $links ) && !$this->precache_running() ) { $out = ' $dummy ) { $starttime = explode ( " ", microtime() ); $starttime = $starttime[1] + $starttime[0]; $page = file_get_contents( $permalink ); $size = round ( ( strlen ( $page ) / 1024 ), 2 ); $endtime = explode ( " ", microtime() ); $endtime = round( ( $endtime[1] + $endtime[0] ) - $starttime, 2 ); echo $permalink . "\t" . $endtime . "\t" . $size . "\n"; unset ( $page, $size, $starttime, $endtime ); sleep( 1 ); } unlink ( "'. $this->precache_phpfile .'" ); ?>'; file_put_contents ( $this->precache_phpfile, $out ); /* call the precache worker file in the background */ $shellfunction = $this->shell_function; $shellfunction( 'php '. $this->precache_phpfile .' >'. $this->precache_logfile .' 2>&1 &' ); } } /** * check is precache is still ongoing * */ private function precache_running () { $return = false; /* if the precache file exists, it did not finish running as it should delete itself on finish */ if ( file_exists ( $this->precache_phpfile )) { $return = true; } /* [TODO] cross-platform process check; this is *nix only else { $shellfunction = $this->shell_function; $running = $shellfunction( "ps aux | grep \"". $this->precache_phpfile ."\" | grep -v grep | awk '{print $2}'" ); if ( is_int( $running ) && $running != 0 ) { $return = true; } } */ return $return; } /** * run full-site precache */ public function precache_coldrun () { /* container for links to precache, well be accessed by reference */ $links = array(); /* when plugin is network wide active, we need to pre-cache for all link of all blogs */ if ( $this->network ) { /* list all blogs */ global $wpdb; $pfix = empty ( $wpdb->base_prefix ) ? 'wp_' : $wpdb->base_prefix; $blog_list = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM ". $pfix ."blogs ORDER BY blog_id", '' ) ); foreach ($blog_list as $blog) { if ( $blog->archived != 1 && $blog->spam != 1 && $blog->deleted != 1) { /* get permalinks for this blog */ $this->precache_list_permalinks ( $links, $blog->blog_id ); } } } else { /* no network, better */ $this->precache_list_permalinks ( $links, false ); } /* double check if we do have any links to pre-cache */ if ( !empty ( $links ) ) { $this->precache ( $links ); } } /** * gets all post-like entry permalinks for a site, returns values in passed-by-reference array * */ private function precache_list_permalinks ( &$links, $site = false ) { /* $post will be populated when running throught the posts */ global $post; include_once ( ABSPATH . "wp-load.php" ); /* if a site id was provided, save current blog and change to the other site */ if ( $site !== false ) { $current_blog = get_current_blog_id(); switch_to_blog( $site ); $url = $this->_site_url( $site ); //$url = get_blog_option ( $site, 'siteurl' ); if ( substr( $url, -1) !== '/' ) $url = $url . '/'; $links[ $url ] = true; } /* get all published posts */ $args = array ( 'post_type' => 'any', 'posts_per_page' => -1, 'post_status' => 'publish', ); $posts = new WP_Query( $args ); /* get all the posts, one by one */ while ( $posts->have_posts() ) { $posts->the_post(); /* get the permalink for currently selected post */ switch ($post->post_type) { case 'revision': case 'nav_menu_item': break; case 'page': $permalink = get_page_link( $post->ID ); break; /* * case 'post': $permalink = get_permalink( $post->ID ); break; */ case 'attachment': $permalink = get_attachment_link( $post->ID ); break; default: $permalink = get_permalink( $post->ID ); break; } /* in case the bloglinks are relative links add the base url, site specific */ $baseurl = empty( $url ) ? static::_site_url() : $url; if ( !strstr( $permalink, $baseurl ) ) { $permalink = $baseurl . $permalink; } /* collect permalinks */ $links[ $permalink ] = true; } $this->backend->taxonomy_links ( $links ); /* just in case, reset $post */ wp_reset_postdata(); /* switch back to original site if we navigated away */ if ( $site !== false ) { switch_to_blog( $current_blog ); } } public function getBackend() { return $this->backend; } } endif;