web/wp-content/plugins/wp-super-cache/wp-cache-phase2.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
     1 <?php
     1 <?php
     2 
     2 
     3 function wp_cache_phase2() {
     3 function wp_cache_phase2() {
     4 	global $cache_filename, $cache_acceptable_files, $wp_cache_gzip_encoding, $super_cache_enabled, $cache_rebuild_files, $wp_cache_gmt_offset, $wp_cache_blog_charset, $wp_cache_last_gc;
     4 	global $cache_filename, $cache_acceptable_files, $wp_cache_gzip_encoding, $super_cache_enabled, $cache_rebuild_files, $wp_cache_last_gc;
     5 	global $cache_max_time, $wp_cache_not_logged_in, $wp_cache_request_uri, $super_cache_enabled;
     5 	global $cache_max_time, $wp_cache_request_uri, $super_cache_enabled, $wp_cache_object_cache, $cache_time_interval, $wp_cache_no_cache_for_get;
       
     6 	global $cache_enabled, $wp_cache_gmt_offset, $wp_cache_blog_charset, $cache_schedule_type, $cache_scheduled_time, $cache_schedule_interval;
       
     7 
       
     8 	if ( $cache_enabled == false ) {
       
     9 		if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Caching disabled! quiting!", 1 );
       
    10 		return false;
       
    11 	}
       
    12 
     6 	if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'In WP Cache Phase 2', 5 );
    13 	if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'In WP Cache Phase 2', 5 );
     7 
    14 
     8 	$wp_cache_gmt_offset   = get_option( 'gmt_offset' ); // caching for later use when wpdb is gone. http://wordpress.org/support/topic/224349
    15 	$wp_cache_gmt_offset = get_option( 'gmt_offset' ); // caching for later use when wpdb is gone. http://wordpress.org/support/topic/224349
     9 	$wp_cache_blog_charset = get_option( 'blog_charset' );
    16 	$wp_cache_blog_charset = get_option( 'blog_charset' );
    10 
    17 
    11 	wp_cache_mutex_init();
    18 	wp_cache_mutex_init();
    12 	if(function_exists('add_action') && ( !defined( 'WPLOCKDOWN' ) || ( defined( 'WPLOCKDOWN' ) && constant( 'WPLOCKDOWN' ) == '0' ) ) ) {
    19 	if(function_exists('add_action') && ( !defined( 'WPLOCKDOWN' ) || ( defined( 'WPLOCKDOWN' ) && constant( 'WPLOCKDOWN' ) == '0' ) ) ) {
    13 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Setting up WordPress actions', 5 );
    20 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Setting up WordPress actions', 5 );
       
    21 
       
    22 		add_action( 'template_redirect', 'wp_super_cache_query_vars' );
       
    23 
    14 		// Post ID is received
    24 		// Post ID is received
       
    25 		add_action('wp_trash_post', 'wp_cache_post_edit', 0);
    15 		add_action('publish_post', 'wp_cache_post_edit', 0);
    26 		add_action('publish_post', 'wp_cache_post_edit', 0);
    16 		add_action('edit_post', 'wp_cache_post_change', 0); // leaving a comment called edit_post
    27 		add_action('edit_post', 'wp_cache_post_change', 0); // leaving a comment called edit_post
    17 		add_action('delete_post', 'wp_cache_post_edit', 0);
    28 		add_action('delete_post', 'wp_cache_post_edit', 0);
    18 		add_action('publish_phone', 'wp_cache_post_edit', 0);
    29 		add_action('publish_phone', 'wp_cache_post_edit', 0);
    19 		// Coment ID is received
    30 		// Coment ID is received
    23 		add_action('edit_comment', 'wp_cache_get_postid_from_comment', 99);
    34 		add_action('edit_comment', 'wp_cache_get_postid_from_comment', 99);
    24 		add_action('wp_set_comment_status', 'wp_cache_get_postid_from_comment', 99, 2);
    35 		add_action('wp_set_comment_status', 'wp_cache_get_postid_from_comment', 99, 2);
    25 		// No post_id is available
    36 		// No post_id is available
    26 		add_action('switch_theme', 'wp_cache_no_postid', 99); 
    37 		add_action('switch_theme', 'wp_cache_no_postid', 99); 
    27 		add_action('edit_user_profile_update', 'wp_cache_no_postid', 99); 
    38 		add_action('edit_user_profile_update', 'wp_cache_no_postid', 99); 
    28 
    39 		add_action( 'wp_update_nav_menu', 'wp_cache_clear_cache_on_menu' );
    29 		add_action('wp_cache_gc','wp_cache_gc_cron');
    40 		add_action('wp_cache_gc','wp_cache_gc_cron');
       
    41 		add_action( 'clean_post_cache', 'wp_cache_post_edit' );
       
    42 		add_filter( 'supercache_filename_str', 'wp_cache_check_mobile' );
       
    43 		add_action( 'wp_cache_gc_watcher', 'wp_cache_gc_watcher' );
    30 
    44 
    31 		do_cacheaction( 'add_cacheaction' );
    45 		do_cacheaction( 'add_cacheaction' );
       
    46 	}
       
    47 
       
    48 	if ( $wp_cache_no_cache_for_get && false == empty( $_GET ) && false == defined( 'DOING_CRON' ) ) {
       
    49 		if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Non empty GET request. Caching disabled on settings page.", 1 );
       
    50 		return false;
    32 	}
    51 	}
    33 
    52 
    34 	if ( is_admin() ) {
    53 	if ( is_admin() ) {
    35 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching wp-admin requests.', 5 );
    54 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching wp-admin requests.', 5 );
    36 		return false;
    55 		return false;
    39 	if ( $_SERVER["REQUEST_METHOD"] == 'POST' || !empty( $_POST ) || get_option( 'gzipcompression' ) ) {
    58 	if ( $_SERVER["REQUEST_METHOD"] == 'POST' || !empty( $_POST ) || get_option( 'gzipcompression' ) ) {
    40 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching POST request.', 5 );
    59 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching POST request.', 5 );
    41 		return false;
    60 		return false;
    42 	}
    61 	}
    43 
    62 
       
    63 	if ( $wp_cache_object_cache && !empty( $_GET ) ) {
       
    64 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching GET request while object cache storage enabled.', 5 );
       
    65 		return false;
       
    66 	}
       
    67 
    44 	if ( isset( $_GET[ 'preview' ] ) ) {
    68 	if ( isset( $_GET[ 'preview' ] ) ) {
    45 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching preview post.', 2 );
    69 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching preview post.', 2 );
    46 		return false;
    70 		return false;
    47 	}
    71 	}
    48 
    72 
    49 	if ( !empty( $_GET ) ) {
    73 	if ( !empty( $_GET ) ) {
    50 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Supercache caching disabled. Non empty GET request.', 5 );
    74 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Supercache caching disabled. Only using wp-cache. Non empty GET request.', 5 );
    51 		$super_cache_enabled = false;
    75 		$super_cache_enabled = false;
    52 	}
       
    53 
       
    54 	if ( $wp_cache_not_logged_in && is_user_logged_in() && !is_feed() && !is_admin() ) {
       
    55 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) {
       
    56 			wp_cache_debug( 'not caching for logged in user', 5 ); 
       
    57 			register_shutdown_function( 'wpcache_logged_in_message' );
       
    58 		}
       
    59 		return false;
       
    60 	}
    76 	}
    61 
    77 
    62 	$script = basename($_SERVER['PHP_SELF']);
    78 	$script = basename($_SERVER['PHP_SELF']);
    63 	if (!in_array($script, $cache_acceptable_files) && wp_cache_is_rejected($wp_cache_request_uri)) {
    79 	if (!in_array($script, $cache_acceptable_files) && wp_cache_is_rejected($wp_cache_request_uri)) {
    64 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'URI rejected. Not Caching', 2 );
    80 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'URI rejected. Not Caching', 2 );
    77 
    93 
    78 	// restore old supercache file temporarily
    94 	// restore old supercache file temporarily
    79 	if( $super_cache_enabled && $cache_rebuild_files ) {
    95 	if( $super_cache_enabled && $cache_rebuild_files ) {
    80 		$user_info = wp_cache_get_cookies_values();
    96 		$user_info = wp_cache_get_cookies_values();
    81 		$do_cache = apply_filters( 'do_createsupercache', $user_info );
    97 		$do_cache = apply_filters( 'do_createsupercache', $user_info );
    82 		if( $user_info == '' || $do_cache === true ) {
    98 		if( $user_info == '' || $do_cache === true )
    83 			$dir = get_current_url_supercache_dir();
    99 			wpcache_do_rebuild( get_current_url_supercache_dir() );
    84 			$files_to_check = array( $dir . 'index.html', $dir . 'index.html.gz' );
   100 	}
    85 			foreach( $files_to_check as $cache_file ) {
   101 
    86 				if( !@file_exists( $cache_file . '.needs-rebuild' ) )
   102 	schedule_wp_gc();
    87 					continue;
   103 }
    88 				$mtime = @filemtime($cache_file . '.needs-rebuild');
   104 
    89 				if( $mtime && (time() - $mtime) < 30 ) {
   105 function wpcache_do_rebuild( $dir ) {
    90 					if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Rebuild file renamed to cache file temporarily", 3 );
   106 	$dir = trailingslashit( $dir );
    91 					@rename( $cache_file . '.needs-rebuild', $cache_file );
   107 	$files_to_check = get_all_supercache_filenames( $dir );
    92 				}
   108 	foreach( $files_to_check as $cache_file ) {
    93 				// cleanup old files or if rename fails
   109 		$cache_file = $dir . $cache_file;
    94 				if( @file_exists( $cache_file . '.needs-rebuild' ) ) {
   110 		if( !@file_exists( $cache_file . '.needs-rebuild' ) )
    95 					if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Rebuild file deleted", 3 );
   111 			continue;
    96 					@unlink( $cache_file . '.needs-rebuild' );
   112 		$mtime = @filemtime($cache_file . '.needs-rebuild');
    97 				}
   113 		if( $mtime && (time() - $mtime) < 30 ) {
    98 			}
   114 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Rebuild file renamed to cache file temporarily: $cache_file", 3 );
    99 		}
   115 			@rename( $cache_file . '.needs-rebuild', $cache_file );
   100 	}
   116 		}
   101 
   117 		// cleanup old files or if rename fails
   102 	if( !isset( $cache_max_time ) )
   118 		if( @file_exists( $cache_file . '.needs-rebuild' ) ) {
   103 		$cache_max_time = 600;
   119 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Rebuild file deleted: {$cache_file}.needs-rebuild", 3 );
   104 	$last_gc = get_option( "wpsupercache_gc_time" );
   120 			@unlink( $cache_file . '.needs-rebuild' );
   105 
       
   106 	if( !$last_gc ) {
       
   107 		update_option( 'wpsupercache_gc_time', time() );
       
   108 	}
       
   109 	$next_gc = $cache_max_time < 1800 ? $cache_max_time : 600;
       
   110 	if( $last_gc < ( time() - $next_gc ) ) {
       
   111 		update_option( 'wpsupercache_gc_time', time() );
       
   112 
       
   113 		global $wp_cache_shutdown_gc;
       
   114 		if( !isset( $wp_cache_shutdown_gc ) || $wp_cache_shutdown_gc == 0 ) {
       
   115 			if(!wp_next_scheduled('wp_cache_gc')) {
       
   116 				wp_schedule_single_event(time() + 10 , 'wp_cache_gc');
       
   117 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'scheduled wp_cache_gc for 10 seconds time.', 5 );
       
   118 			}
       
   119 		} else {
       
   120 			global $time_to_gc_cache;
       
   121 			$time_to_gc_cache = 1; // tell the "shutdown gc" to run!
       
   122 		}
   121 		}
   123 	}
   122 	}
   124 }
   123 }
   125 
   124 
   126 function wpcache_logged_in_message() {
   125 function wpcache_logged_in_message() {
   132 		global $cache_rejected_user_agent;
   131 		global $cache_rejected_user_agent;
   133 
   132 
   134 		if (!function_exists('apache_request_headers')) return false;
   133 		if (!function_exists('apache_request_headers')) return false;
   135 		$headers = apache_request_headers();
   134 		$headers = apache_request_headers();
   136 		if (!isset($headers["User-Agent"])) return false;
   135 		if (!isset($headers["User-Agent"])) return false;
       
   136 		if ( false == is_array( $cache_rejected_user_agent ) )
       
   137 			return false;
   137 		foreach ($cache_rejected_user_agent as $expr) {
   138 		foreach ($cache_rejected_user_agent as $expr) {
   138 			if (strlen($expr) > 0 && stristr($headers["User-Agent"], $expr))
   139 			if (strlen($expr) > 0 && stristr($headers["User-Agent"], $expr))
   139 				return true;
   140 				return true;
   140 		}
   141 		}
   141 		return false;
   142 		return false;
   164 	$auto_rejected = array( '/wp-admin/', 'xmlrpc.php', 'wp-app.php' );
   165 	$auto_rejected = array( '/wp-admin/', 'xmlrpc.php', 'wp-app.php' );
   165 	foreach( $auto_rejected as $u ) {
   166 	foreach( $auto_rejected as $u ) {
   166 		if( strstr( $uri, $u ) )
   167 		if( strstr( $uri, $u ) )
   167 			return true; // we don't allow caching of wp-admin for security reasons
   168 			return true; // we don't allow caching of wp-admin for security reasons
   168 	}
   169 	}
   169 	foreach ($cache_rejected_uri as $expr) {
   170 	if ( false == is_array( $cache_rejected_uri ) )
   170 		if( $expr != '' && preg_match( "~$expr~", $uri ) )
   171 		return false;
       
   172 	foreach ( $cache_rejected_uri as $expr ) {
       
   173 		if( $expr != '' && @preg_match( "~$expr~", $uri ) )
   171 			return true;
   174 			return true;
   172 	}
   175 	}
   173 	return false;
   176 	return false;
   174 }
   177 }
   175 
   178 
   176 function wp_cache_mutex_init() {
   179 function wp_cache_mutex_init() {
   177 	global $use_flock, $mutex, $cache_path, $mutex_filename, $sem_id, $blog_cache_dir, $wp_cache_mutex_disabled;
   180 	global $mutex, $wp_cache_mutex_disabled, $use_flock, $blog_cache_dir, $mutex_filename, $sem_id;
   178 
   181 
   179 	if( isset( $wp_cache_mutex_disabled ) && $wp_cache_mutex_disabled )
   182 	if( isset( $wp_cache_mutex_disabled) && $wp_cache_mutex_disabled )
   180 		return true;
   183 		return true;
   181 
   184 
   182 	if(!is_bool($use_flock)) {
   185 	if( !is_bool( $use_flock ) ) {
   183 		if(function_exists('sem_get')) 
   186 		if(function_exists('sem_get')) 
   184 			$use_flock = false;
   187 			$use_flock = false;
   185 		else
   188 		else
   186 			$use_flock = true;
   189 			$use_flock = true;
   187 	}
   190 	}
   188 
   191 
   189 	$mutex = false;
   192 	$mutex = false;
   190 	if ($use_flock)  {
   193 	if ($use_flock )  {
       
   194 		setup_blog_cache_dir();
   191 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Created mutex lock on filename: {$blog_cache_dir}{$mutex_filename}", 5 );
   195 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Created mutex lock on filename: {$blog_cache_dir}{$mutex_filename}", 5 );
   192 		$mutex = @fopen($blog_cache_dir . $mutex_filename, 'w');
   196 		$mutex = @fopen( $blog_cache_dir . $mutex_filename, 'w' );
   193 	} else {
   197 	} else {
   194 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Created mutex lock on semaphore: $sem_id", 5 );
   198 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Created mutex lock on semaphore: {$sem_id}", 5 );
   195 		$mutex = @sem_get($sem_id, 1, 0644 | IPC_CREAT, 1);
   199 		$mutex = @sem_get( $sem_id, 1, 0644 | IPC_CREAT, 1 );
   196 	}
   200 	}
   197 }
   201 }
   198 
   202 
   199 function wp_cache_writers_entry() {
   203 function wp_cache_writers_entry() {
   200 	global $use_flock, $mutex, $cache_path, $mutex_filename, $wp_cache_mutex_disabled;
   204 	global $mutex, $wp_cache_mutex_disabled, $use_flock;
   201 
   205 
   202 	if( isset( $wp_cache_mutex_disabled ) && $wp_cache_mutex_disabled )
   206 	if( isset( $wp_cache_mutex_disabled ) && $wp_cache_mutex_disabled )
   203 		return true;
   207 		return true;
   204 
   208 
   205 	if( !$mutex ) {
   209 	if( !$mutex ) {
   206 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "(writers entry) mutex lock not created. not caching.", 2 );
   210 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "(writers entry) mutex lock not created. not caching.", 2 );
   207 		return false;
   211 		return false;
   208 	}
   212 	}
   209 
   213 
   210 	if ($use_flock) {
   214 	if ( $use_flock ) {
   211 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "grabbing lock using flock()", 5 );
   215 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "grabbing lock using flock()", 5 );
   212 		flock($mutex,  LOCK_EX);
   216 		flock($mutex,  LOCK_EX);
   213 	} else {
   217 	} else {
   214 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "grabbing lock using sem_acquire()", 5 );
   218 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "grabbing lock using sem_acquire()", 5 );
   215 		sem_acquire($mutex);
   219 		sem_acquire($mutex);
   217 
   221 
   218 	return true;
   222 	return true;
   219 }
   223 }
   220 
   224 
   221 function wp_cache_writers_exit() {
   225 function wp_cache_writers_exit() {
   222 	global $use_flock, $mutex, $cache_path, $mutex_filename, $wp_cache_mutex_disabled;
   226 	global $mutex, $wp_cache_mutex_disabled, $use_flock;
   223 
   227 
   224 	if( isset( $wp_cache_mutex_disabled ) && $wp_cache_mutex_disabled )
   228 	if( isset( $wp_cache_mutex_disabled ) && $wp_cache_mutex_disabled )
   225 		return true;
   229 		return true;
   226 
   230 
   227 	if( !$mutex ) {
   231 	if( !$mutex ) {
   228 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "(writers exit) mutex lock not created. not caching.", 2 );
   232 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "(writers exit) mutex lock not created. not caching.", 2 );
   229 		return false;
   233 		return false;
   230 	}
   234 	}
   231 
   235 
   232 	if ($use_flock) {
   236 	if ( $use_flock ) {
   233 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "releasing lock using flock()", 5 );
   237 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "releasing lock using flock()", 5 );
   234 		flock($mutex,  LOCK_UN);
   238 		flock($mutex,  LOCK_UN);
   235 	} else {
   239 	} else {
   236 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "releasing lock using sem_release()", 5 );
   240 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "releasing lock using sem_release()", 5 );
   237 		sem_release($mutex);
   241 		sem_release($mutex);
   238 	}
   242 	}
   239 }
   243 }
   240 
   244 
   241 function get_current_url_supercache_dir() {
   245 function wp_super_cache_query_vars() {
   242 	global $cached_direct_pages, $cache_path, $wp_cache_request_uri;
   246 	global $wp_super_cache_query;
   243 	$uri = preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', str_replace( '/index.php', '/', str_replace( '..', '', preg_replace("/(\?.*)?$/", '', $wp_cache_request_uri ) ) ) );
   247 	if ( is_search() )
   244 	$uri = str_replace( '\\', '', $uri );
   248 		$wp_super_cache_query[ 'is_search' ] = 1;
   245 	$dir = strtolower(preg_replace('/:.*$/', '',  $_SERVER["HTTP_HOST"])) . $uri; // To avoid XSS attacks
   249 	if ( is_page() )
   246 	$dir = apply_filters( 'supercache_dir', $dir );
   250 		$wp_super_cache_query[ 'is_page' ] = 1;
   247 	$dir = trailingslashit( $cache_path . 'supercache/' . $dir );
   251 	if ( is_archive() )
   248 	if( is_array( $cached_direct_pages ) && in_array( $_SERVER[ 'REQUEST_URI' ], $cached_direct_pages ) ) {
   252 		$wp_super_cache_query[ 'is_archive' ] = 1;
   249 		$dir = trailingslashit( ABSPATH . $uri );
   253 	if ( is_tag() )
   250 	}
   254 		$wp_super_cache_query[ 'is_tag' ] = 1;
   251 	$dir = str_replace( '//', '/', $dir );
   255 	if ( is_single() )
   252 	if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "supercache dir: $dir", 5 );
   256 		$wp_super_cache_query[ 'is_single' ] = 1;
   253 	return $dir;
   257 	if ( is_category() )
       
   258 		$wp_super_cache_query[ 'is_category' ] = 1;
       
   259 	if ( is_front_page() )
       
   260 		$wp_super_cache_query[ 'is_front_page' ] = 1;
       
   261 	if ( is_home() )
       
   262 		$wp_super_cache_query[ 'is_home' ] = 1;
       
   263 	if ( is_author() )
       
   264 		$wp_super_cache_query[ 'is_author' ] = 1;
       
   265 	if ( is_feed() )
       
   266 		$wp_super_cache_query[ 'is_feed' ] = 1;
   254 }
   267 }
   255 
   268 
   256 function wp_cache_ob_callback( $buffer ) {
   269 function wp_cache_ob_callback( $buffer ) {
   257 	global $wp_cache_pages;
   270 	global $wp_cache_pages, $wp_query, $wp_super_cache_query;
   258 	if( defined( 'DONOTCACHEPAGE' ) )
   271 	$buffer = apply_filters( 'wp_cache_ob_callback_filter', $buffer );
       
   272 	if( defined( 'DONOTCACHEPAGE' ) ) {
       
   273 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'DONOTCACHEPAGE defined. Caching disabled.', 2 );
   259 		return $buffer;
   274 		return $buffer;
       
   275 	}
       
   276 
       
   277 	if ( isset( $wp_cache_pages[ 'single' ] ) && $wp_cache_pages[ 'single' ] == 1 && isset( $wp_super_cache_query[ 'is_single' ] ) ) {
       
   278 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching single post.', 2 );
       
   279 		return $buffer;
       
   280 	} elseif ( isset( $wp_cache_pages[ 'pages' ] ) && $wp_cache_pages[ 'pages' ] == 1 && isset( $wp_super_cache_query[ 'is_page' ] ) ) {
       
   281 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching single page.', 2 );
       
   282 		return $buffer;
       
   283 	} elseif ( isset( $wp_cache_pages[ 'archives' ] ) && $wp_cache_pages[ 'archives' ] == 1 && isset( $wp_super_cache_query[ 'is_archive' ] ) ) {
       
   284 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching archive page.', 2 );
       
   285 		return $buffer;
       
   286 	} elseif ( isset( $wp_cache_pages[ 'tag' ] ) && $wp_cache_pages[ 'tag' ] == 1 && isset( $wp_super_cache_query[ 'is_tag' ] ) ) {
       
   287 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching tag page.', 2 );
       
   288 		return $buffer;
       
   289 	} elseif ( isset( $wp_cache_pages[ 'category' ] ) && $wp_cache_pages[ 'category' ] == 1 && isset( $wp_super_cache_query[ 'is_category' ] ) ) {
       
   290 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching category page.', 2 );
       
   291 		return $buffer;
       
   292 	} elseif ( isset( $wp_cache_pages[ 'frontpage' ] ) && $wp_cache_pages[ 'frontpage' ] == 1 && isset( $wp_super_cache_query[ 'is_front_page' ] ) ) {
       
   293 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching front page.', 2 );
       
   294 		return $buffer;
       
   295 	} elseif ( isset( $wp_cache_pages[ 'home' ] ) && $wp_cache_pages[ 'home' ] == 1 && isset( $wp_super_cache_query[ 'is_home' ] ) ) {
       
   296 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching home page.', 2 );
       
   297 		return $buffer;
       
   298 	} elseif ( isset( $wp_cache_pages[ 'search' ] ) && $wp_cache_pages[ 'search' ] == 1 && isset( $wp_super_cache_query[ 'is_search' ] ) ) {
       
   299 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching search page.', 2 );
       
   300 		return $buffer;
       
   301 	} elseif ( isset( $wp_cache_pages[ 'author' ] ) && $wp_cache_pages[ 'author' ] == 1 && isset( $wp_super_cache_query[ 'is_author' ] ) ) {
       
   302 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching author page.', 2 );
       
   303 		return $buffer;
       
   304 	} elseif ( isset( $wp_cache_pages[ 'feed' ] ) && $wp_cache_pages[ 'feed' ] == 1 && isset( $wp_super_cache_query[ 'is_feed' ] ) ) {
       
   305 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching feed.', 2 );
       
   306 		return $buffer;
       
   307 	}
       
   308 
       
   309 	if ( !isset( $wp_query ) )
       
   310 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'wp_cache_ob_callback: WARNING! $query not defined but the plugin has worked around that problem.', 4 );
       
   311 
   260 	if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Output buffer callback', 4 );
   312 	if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Output buffer callback', 4 );
   261 
   313 
   262 	if ( isset( $wp_cache_pages[ 'single' ] ) && $wp_cache_pages[ 'single' ] == 1 && is_single() ) {
       
   263 		return $buffer;
       
   264 	} elseif ( isset( $wp_cache_pages[ 'pages' ] ) && $wp_cache_pages[ 'pages' ] == 1 && is_page() ) {
       
   265 		return $buffer;
       
   266 	} elseif ( isset( $wp_cache_pages[ 'archives' ] ) && $wp_cache_pages[ 'archives' ] == 1 && is_archive() ) {
       
   267 		return $buffer;
       
   268 	} elseif ( isset( $wp_cache_pages[ 'tag' ] ) && $wp_cache_pages[ 'tag' ] == 1 && is_tag() ) {
       
   269 		return $buffer;
       
   270 	} elseif ( isset( $wp_cache_pages[ 'category' ] ) && $wp_cache_pages[ 'category' ] == 1 && is_category() ) {
       
   271 		return $buffer;
       
   272 	} elseif ( isset( $wp_cache_pages[ 'frontpage' ] ) && $wp_cache_pages[ 'frontpage' ] == 1 && is_front_page() ) {
       
   273 		return $buffer;
       
   274 	} elseif ( isset( $wp_cache_pages[ 'home' ] ) && $wp_cache_pages[ 'home' ] == 1 && is_home() ) {
       
   275 		return $buffer;
       
   276 	} elseif ( isset( $wp_cache_pages[ 'search' ] ) && $wp_cache_pages[ 'search' ] == 1 && is_search() ) {
       
   277 		return $buffer;
       
   278 	} elseif ( isset( $wp_cache_pages[ 'feed' ] ) && $wp_cache_pages[ 'feed' ] == 1 && is_feed() ) {
       
   279 		return $buffer;
       
   280 	}
       
   281 	$buffer = &wp_cache_get_ob( $buffer );
   314 	$buffer = &wp_cache_get_ob( $buffer );
   282 	wp_cache_shutdown_callback();
   315 	wp_cache_shutdown_callback();
   283 	return $buffer;
   316 	return $buffer;
   284 }
   317 }
   285 
   318 
       
   319 function wp_cache_append_tag( &$buffer ) {
       
   320 	global $wp_cache_gmt_offset, $wp_super_cache_comments;
       
   321 	global $cache_enabled, $super_cache_enabled;
       
   322 
       
   323 	if ( false == isset( $wp_super_cache_comments ) )
       
   324 		$wp_super_cache_comments = 1;
       
   325 
       
   326 	if ( $wp_super_cache_comments == 0 )
       
   327 		return false;
       
   328 
       
   329 	$timestamp = gmdate('Y-m-d H:i:s', (time() + ( $wp_cache_gmt_offset * 3600)));
       
   330 	if ( $cache_enabled || $super_cache_enabled ) {
       
   331 		$buffer .= "\n<!-- Cached page generated by WP-Super-Cache on $timestamp -->\n";
       
   332 	} else {
       
   333 		$buffer .= "\n<!-- Live page served on $timestamp -->\n";
       
   334 	}
       
   335 }
       
   336 
       
   337 function wp_cache_add_to_buffer( &$buffer, $text ) {
       
   338 	global $wp_super_cache_comments;
       
   339 
       
   340 	if ( false == isset( $wp_super_cache_comments ) )
       
   341 		$wp_super_cache_comments = 1;
       
   342 
       
   343 	if ( $wp_super_cache_comments == 0 )
       
   344 		return false;
       
   345 
       
   346 	$buffer .= "\n<!-- $text -->";
       
   347 }
   286 
   348 
   287 function wp_cache_get_ob(&$buffer) {
   349 function wp_cache_get_ob(&$buffer) {
   288 	global $cache_path, $cache_filename, $meta_file, $wp_start_time, $supercachedir;
   350 	global $cache_enabled, $cache_path, $cache_filename, $meta_file, $wp_start_time, $supercachedir;
   289 	global $new_cache, $wp_cache_meta, $file_expired, $blog_id, $cache_compression;
   351 	global $new_cache, $wp_cache_meta, $file_expired, $blog_id, $cache_compression;
   290 	global $wp_cache_gzip_encoding, $super_cache_enabled, $cached_direct_pages;
   352 	global $wp_cache_gzip_encoding, $super_cache_enabled, $cached_direct_pages;
   291 	global $wp_cache_404, $gzsize, $supercacheonly, $wp_cache_gzip_first, $wp_cache_gmt_offset;
   353 	global $wp_cache_404, $gzsize, $supercacheonly;
   292 	global $blog_cache_dir, $wp_cache_request_uri, $wp_supercache_cache_list;
   354 	global $blog_cache_dir, $wp_cache_request_uri, $wp_supercache_cache_list;
       
   355 	global $wp_cache_not_logged_in, $wp_cache_object_cache, $cache_max_time;
       
   356 	global $wp_cache_is_home, $wp_cache_front_page_checks;
   293 
   357 
   294 	$new_cache = true;
   358 	$new_cache = true;
   295 	$wp_cache_meta = '';
   359 	$wp_cache_meta = '';
   296 
   360 
   297 	/* Mode paranoic, check for closing tags 
   361 	/* Mode paranoic, check for closing tags 
   298 	 * we avoid caching incomplete files */
   362 	 * we avoid caching incomplete files */
   299 	if ( $buffer == '' ) {
   363 	if ( $buffer == '' ) {
   300 		$new_cache = false;
   364 		$new_cache = false;
   301 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) {
   365 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) {
   302 			wp_cache_debug( "Buffer is blank. Output buffer may have been corrupted by another plugin or this is a redirected URL. Look for text 'ob_start' in the files of your plugins directory.", 2 );
   366 			wp_cache_debug( "Buffer is blank. Output buffer may have been corrupted by another plugin or this is a redirected URL. Look for text 'ob_start' in the files of your plugins directory.", 2 );
   303 			$buffer .= "\n<!-- Page not cached by WP Super Cache. Blank Page. Check output buffer usage by plugins. -->\n";
   367 			wp_cache_add_to_buffer( $buffer, "Page not cached by WP Super Cache. Blank Page. Check output buffer usage by plugins." );
   304 		}
   368 		}
   305 	}
   369 	}
   306 
   370 
   307 	if ( $wp_cache_404 && false == apply_filters( 'wpsupercache_404', false ) ) {
   371 	if ( $wp_cache_404 && false == apply_filters( 'wpsupercache_404', false ) ) {
   308 		$new_cache = false;
   372 		$new_cache = false;
   309 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) {
   373 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) {
   310 			wp_cache_debug( "404 file not found not cached", 2 );
   374 			wp_cache_debug( "404 file not found not cached", 2 );
   311 			$buffer .= "\n<!-- Page not cached by WP Super Cache. 404. -->\n";
   375 			wp_cache_add_to_buffer( $buffer, "Page not cached by WP Super Cache. 404." );
   312 		}
   376 		}
   313 	}
   377 	}
   314 
   378 
   315 	if (!preg_match('/(<\/html>|<\/rss>|<\/feed>)/i',$buffer) ) {
   379 	if ( !preg_match( apply_filters( 'wp_cache_eof_tags', '/(<\/html>|<\/rss>|<\/feed>|<\/urlset|<\?xml)/i' ), $buffer ) ) {
   316 		$new_cache = false;
   380 		$new_cache = false;
   317 		if( false === strpos( $_SERVER[ 'REQUEST_URI' ], 'robots.txt' ) ) {
   381 		if( false === strpos( $_SERVER[ 'REQUEST_URI' ], 'robots.txt' ) ) {
   318 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) {
   382 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) {
   319 				wp_cache_debug( "No closing html tag. Not caching.", 2 );
   383 				wp_cache_debug( "No closing html tag. Not caching.", 2 );
   320 				$buffer .= "\n<!-- Page not cached by WP Super Cache. No closing HTML tag. Check your theme. -->\n";
   384 				wp_cache_add_to_buffer( $buffer, "Page not cached by WP Super Cache. No closing HTML tag. Check your theme." );
   321 			}
   385 			}
   322 		} else {
   386 		} else {
   323 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "robots.txt detected. Not caching.", 2 );
   387 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "robots.txt detected. Not caching.", 2 );
   324 		}
   388 		}
   325 	}
   389 	}
   327 	if( !$new_cache )
   391 	if( !$new_cache )
   328 		return $buffer;
   392 		return $buffer;
   329 
   393 
   330 	$duration = wp_cache_microtime_diff($wp_start_time, microtime());
   394 	$duration = wp_cache_microtime_diff($wp_start_time, microtime());
   331 	$duration = sprintf("%0.3f", $duration);
   395 	$duration = sprintf("%0.3f", $duration);
   332 	$buffer .= "\n<!-- Dynamic page generated in $duration seconds. -->\n";
   396 	wp_cache_add_to_buffer( $buffer, "Dynamic page generated in $duration seconds." );
   333 
   397 
   334 	if( !wp_cache_writers_entry() ) {
   398 	if( !wp_cache_writers_entry() ) {
   335 		$buffer .= "\n<!-- Page not cached by WP Super Cache. Could not get mutex lock. -->\n";
   399 		wp_cache_add_to_buffer( $buffer, "Page not cached by WP Super Cache. Could not get mutex lock." );
   336 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Could not get mutex lock. Not caching.", 1 );
   400 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Could not get mutex lock. Not caching.", 1 );
   337 		return $buffer;
   401 		return $buffer;
   338 	}
   402 	}
   339 
   403 
       
   404 	if ( $wp_cache_not_logged_in && is_feed() ) {
       
   405 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Feed detected. Writing legacy cache files.", 5 );
       
   406 		$wp_cache_not_logged_in = false;
       
   407 	}
       
   408 
       
   409 	$home_url = parse_url( trailingslashit( get_bloginfo( 'url' ) ) );
       
   410 
   340 	$dir = get_current_url_supercache_dir();
   411 	$dir = get_current_url_supercache_dir();
   341 	$supercachedir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '',  $_SERVER["HTTP_HOST"]);
   412 	$supercachedir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '',  $home_url[ 'host' ]);
   342 	if( !empty( $_GET ) || is_feed() || ( $super_cache_enabled == true && is_dir( substr( $supercachedir, 0, -1 ) . '.disabled' ) ) ) {
   413 	if( !empty( $_GET ) || is_feed() || ( $super_cache_enabled == true && is_dir( substr( $supercachedir, 0, -1 ) . '.disabled' ) ) ) {
   343 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Supercache disabled: GET or feed detected or disabled by config.", 2 );
   414 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Supercache disabled: GET or feed detected or disabled by config.", 2 );
   344 		$super_cache_enabled = false;
   415 		$super_cache_enabled = false;
   345 	}
   416 	}
   346 
   417 
   347 	$tmp_wpcache_filename = $cache_path . uniqid( mt_rand(), true ) . '.tmp';
   418 	$tmp_wpcache_filename = $cache_path . uniqid( mt_rand(), true ) . '.tmp';
   348 
   419 
   349 	// Don't create wp-cache files for anon users
       
   350 	$supercacheonly = false;
   420 	$supercacheonly = false;
   351 	if( $super_cache_enabled && wp_cache_get_cookies_values() == '' ) {
   421 	if( $super_cache_enabled ) {
   352 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Anonymous user detected. Only creating Supercache file.", 3 );
   422 		if ( wp_cache_get_cookies_values() == '' && empty( $_GET ) ) {
   353 		$supercacheonly = true;
   423 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Anonymous user detected. Only creating Supercache file.", 3 );
   354 	}
   424 			$supercacheonly = true;
   355 
   425 		}
   356 	if( !$supercacheonly ) {
   426 	}
   357 		if ( !@file_exists( $blog_cache_dir . $cache_filename ) || ( @file_exists( $blog_cache_dir . $cache_filename ) && ( time() - @filemtime( $blog_cache_dir . $cache_filename ) ) > 5 ) ) {
   427 
       
   428 	$cache_error = '';
       
   429 	if ( $wp_cache_not_logged_in && wp_cache_get_cookies_values() != '' ) {
       
   430 		$super_cache_enabled = false;
       
   431 		$cache_enabled = false;
       
   432 		$cache_error = 'Not caching requests by known users. (See Advanced Settings page)';
       
   433 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching for known user.', 5 );
       
   434 	}
       
   435 
       
   436 	if ( $wp_cache_object_cache ) { // half on mode when using the object cache
       
   437 		if ( wp_cache_get_cookies_values() != '' ) {
       
   438 			$cache_enabled = false;
       
   439 			$cache_error = 'Known User and using object. Only anonymous users cached.';
       
   440 		}
       
   441 		$super_cache_enabled = false;
       
   442 		$supercacheonly = false;
       
   443 		wp_cache_init(); // PHP5 destroys objects during shutdown
       
   444 	}
       
   445 
       
   446 	if ( !$cache_enabled ) {
       
   447 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Cache is not enabled. Sending buffer to browser.', 5 );
       
   448 		wp_cache_writers_exit();
       
   449 		wp_cache_add_to_buffer( $buffer, "Page not cached by WP Super Cache. Check your settings page. $cache_error" );
       
   450 			return $buffer;
       
   451 	}
       
   452 
       
   453 	if( @is_dir( $dir ) == false )
       
   454 		@wp_mkdir_p( $dir );
       
   455 
       
   456 	$fr = $fr2 = $gz = false;
       
   457 	// Open wp-cache cache file
       
   458 	if ( !$supercacheonly ) {
       
   459 		if ( false == $wp_cache_object_cache ) {
   358 			$fr = @fopen($tmp_wpcache_filename, 'w');
   460 			$fr = @fopen($tmp_wpcache_filename, 'w');
   359 			if (!$fr) {
   461 			if (!$fr) {
   360 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Error. Supercache could not write to " . str_replace( ABSPATH, '', $cache_path ) . $cache_filename, 1 );
   462 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Error. Supercache could not write to " . str_replace( ABSPATH, '', $cache_path ) . $cache_filename, 1 );
   361 				$buffer .= "<!-- File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $cache_path ) . $cache_filename . " -->\n";
   463 				wp_cache_add_to_buffer( $buffer, "File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $cache_path ) . $cache_filename );
       
   464 				wp_cache_writers_exit();
   362 				return $buffer;
   465 				return $buffer;
   363 			}
   466 			}
   364 		}
   467 		}
   365 	}
   468 	} else {
   366 	if( $super_cache_enabled ) {
       
   367 		$user_info = wp_cache_get_cookies_values();
   469 		$user_info = wp_cache_get_cookies_values();
   368 		$do_cache = apply_filters( 'do_createsupercache', $user_info );
   470 		$do_cache = apply_filters( 'do_createsupercache', $user_info );
   369 		if( $user_info == '' || $do_cache === true ) {
   471 		if ( $super_cache_enabled && ( $user_info == '' || $do_cache === true ) ) {
   370 
   472 
   371 			if( @is_dir( $dir ) == false )
   473 			$cache_fname = $dir . supercache_filename();
   372 				@wp_mkdir_p( $dir );
       
   373 
       
   374 			$cache_fname = "{$dir}index.html";
       
   375 			$tmp_cache_filename = $dir . uniqid( mt_rand(), true ) . '.tmp';
   474 			$tmp_cache_filename = $dir . uniqid( mt_rand(), true ) . '.tmp';
   376 			if ( !@file_exists( $cache_fname ) || ( @file_exists( $cache_fname ) && ( time() - @filemtime( $cache_fname ) ) > 5 ) ) {
   475 			$fr2 = @fopen( $tmp_cache_filename, 'w' );
   377 				$fr2 = @fopen( $tmp_cache_filename, 'w' );
   476 			if ( !$fr2 ) {
   378 				if (!$fr2) {
   477 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Error. Supercache could not write to " . str_replace( ABSPATH, '', $tmp_cache_filename ), 1 );
   379 					if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Error. Supercache could not write to " . str_replace( ABSPATH, '', $tmp_cache_filename ), 1 );
   478 				wp_cache_add_to_buffer( $buffer, "File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $tmp_cache_filename ) );
   380 					$buffer .= "<!-- File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $tmp_cache_filename ) . " -->\n";
   479 				@fclose( $fr );
       
   480 				@unlink( $tmp_wpcache_filename );
       
   481 				wp_cache_writers_exit();
       
   482 				return $buffer;
       
   483 			} elseif ( $cache_compression ) {
       
   484 				$gz = @fopen( $tmp_cache_filename . ".gz", 'w');
       
   485 				if (!$gz) {
       
   486 					if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Error. Supercache could not write to " . str_replace( ABSPATH, '', $tmp_cache_filename ) . ".gz", 1 );
       
   487 					wp_cache_add_to_buffer( $buffer, "File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $tmp_cache_filename ) . ".gz" );
   381 					@fclose( $fr );
   488 					@fclose( $fr );
   382 					@unlink( $tmp_wpcache_filename );
   489 					@unlink( $tmp_wpcache_filename );
       
   490 					@fclose( $fr2 );
       
   491 					@unlink( $tmp_cache_filename );
       
   492 					wp_cache_writers_exit();
   383 					return $buffer;
   493 					return $buffer;
   384 				} elseif( $cache_compression ) {
   494 				}
   385 					$gz = @fopen( $tmp_cache_filename . ".gz", 'w');
   495 			}
   386 					if (!$gz) {
   496 		}
   387 						if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Error. Supercache could not write to " . str_replace( ABSPATH, '', $tmp_cache_filename ) . ".gz", 1 );
   497 	}
   388 						$buffer .= "<!-- File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $tmp_cache_filename ) . ".gz -->\n";
   498 
   389 						@fclose( $fr );
   499 	$added_cache = 0;
   390 						@unlink( $tmp_wpcache_filename );
   500 	$oc_key = get_oc_key();
   391 						@fclose( $fr2 );
   501 	if ( preg_match( '/<!--mclude|<!--mfunc|<!--dynamic-cached-content-->/', $buffer ) ) { //Dynamic content
   392 						@unlink( $tmp_cache_filename );
       
   393 						return $buffer;
       
   394 					}
       
   395 				}
       
   396 			}
       
   397 		}
       
   398 	}
       
   399 
       
   400 	if (preg_match('/<!--mclude|<!--mfunc/', $buffer)) { //Dynamic content
       
   401 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Dynamic content found in buffer.", 4 );
   502 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Dynamic content found in buffer.", 4 );
   402 		$store = preg_replace('|<!--mclude (.*?)-->(.*?)<!--/mclude-->|is', 
   503 		$store = preg_replace('|<!--mclude (.*?)-->(.*?)<!--/mclude-->|is',
   403 				"<!--mclude-->\n<?php include_once('" . ABSPATH . "$1'); ?>\n<!--/mclude-->", $buffer);
   504 				"<!--mclude-->\n<?php include_once('" . ABSPATH . "$1'); ?>\n<!--/mclude-->", $buffer);
   404 		$store = preg_replace('|<!--mfunc (.*?)-->(.*?)<!--/mfunc-->|is', 
   505 		$store = preg_replace('|<!--mfunc (.*?)-->(.*?)<!--/mfunc-->|is',
   405 				"<!--mfunc-->\n<?php $1 ;?>\n<!--/mfunc-->", $store);
   506 				"<!--mfunc-->\n<?php $1 ;?>\n<!--/mfunc-->", $store);
   406 		$store = apply_filters( 'wpsupercache_buffer', $store );
   507 		$store = preg_replace('|<!--dynamic-cached-content-->(.*?)<!--(.*?)--><!--/dynamic-cached-content-->|is',
       
   508 				"<!--dynamic-cached-content-->\n<?php$2?>\n<!--/dynamic-cached-content-->", $store);
   407 		$wp_cache_meta[ 'dynamic' ] = true;
   509 		$wp_cache_meta[ 'dynamic' ] = true;
   408 		/* Clean function calls in tag */
   510 		/* Clean function calls in tag */
   409 		$buffer = preg_replace('|<!--mclude (.*?)-->|is', '<!--mclude-->', $buffer);
   511 		$buffer = preg_replace('|<!--mclude (.*?)-->|is', '<!--mclude-->', $buffer);
   410 		$buffer = preg_replace('|<!--mfunc (.*?)-->|is', '<!--mfunc-->', $buffer);
   512 		$buffer = preg_replace('|<!--mfunc (.*?)-->|is', '<!--mfunc-->', $buffer);
   411 		if( $fr )
   513 		$buffer = preg_replace('|<!--dynamic-cached-content-->(.*?)<!--(.*?)--><!--/dynamic-cached-content-->|is',
   412 			fputs($fr, $store);
   514 				"<!--dynamic-cached-content-->$1<!--/dynamic-cached-content-->", $buffer);
   413 		if( $fr2 )
   515 		$store = apply_filters( 'wpsupercache_buffer', $store );
   414 			fputs($fr2, $store . '<!-- super cache -->' );
   516 		// Append WP Super Cache or Live page comment tag
   415 		if( $gz )
   517 		wp_cache_append_tag($buffer);
   416 			fputs($gz, gzencode( $store . '<!-- super cache gz -->', 1, FORCE_GZIP ) );
   518 		wp_cache_append_tag($store);
       
   519 		global $wp_super_cache_late_init;
       
   520 		if ( false == isset( $wp_super_cache_late_init ) || ( isset( $wp_super_cache_late_init ) && $wp_super_cache_late_init == 0 ) )
       
   521 			wp_cache_add_to_buffer( $buffer, 'Super Cache dynamic page detected but $wp_super_cache_late_init not set. See the readme.txt for further details.' );
       
   522 
       
   523 		if ( false == $wp_cache_object_cache ) {
       
   524 			if( $fr ) { // legacy caching
       
   525 				fputs($fr, $store);
       
   526 			} elseif ( isset( $fr2 ) ) { // supercache active
       
   527 				$php_fname = "{$dir}" . supercache_filename() . ".php";
       
   528 				$tmp_php_filename = $dir . uniqid( mt_rand(), true ) . '.tmp';
       
   529 				$php_fd = @fopen( $tmp_php_filename, 'w' );
       
   530 				if ( !$php_fd ) {
       
   531 					if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Error. Supercache could not write to " . str_replace( ABSPATH, '', $tmp_php_filename ), 1 );
       
   532 					wp_cache_add_to_buffer( $buffer, "File not cached! Super Cache couldn't write to: " . str_replace( ABSPATH, '', $tmp_php_filename ) );
       
   533 					@fclose( $php_fd );
       
   534 					@unlink( $tmp_php_filename );
       
   535 					wp_cache_writers_exit();
       
   536 					return $buffer;
       
   537 				}
       
   538 				fputs( $php_fd, $store );
       
   539 			}
       
   540 		} else {
       
   541 			wp_cache_set( $oc_key, $store, 'supercache', $cache_max_time );
       
   542 		}
       
   543 		if ( $cache_compression && $wp_cache_gzip_encoding ) {
       
   544 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Gzipping dynamic buffer.", 5 );
       
   545 			wp_cache_add_to_buffer( $buffer, "Compression = gzip" );
       
   546 			$gzdata = gzencode( $buffer, 6, FORCE_GZIP );
       
   547 			$gzsize = function_exists( 'mb_strlen' ) ? mb_strlen( $gzdata, '8bit' ) : strlen( $gzdata );
       
   548 		}
   417 	} else {
   549 	} else {
   418 		$buffer = apply_filters( 'wpsupercache_buffer', $buffer );
   550 		$buffer = apply_filters( 'wpsupercache_buffer', $buffer );
   419 		$buffer .= "<!-- Cached page generated by WP-Super-Cache on " . gmdate('Y-m-d H:i:s', (time() + ( $wp_cache_gmt_offset * 3600)))  . " -->\n";
   551 		// Append WP Super Cache or Live page comment tag
   420 
   552 		wp_cache_append_tag($buffer);
   421 		if( $gz || $wp_cache_gzip_encoding ) {
   553 		if( $gz || $wp_cache_gzip_encoding ) {
   422 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Gzipping buffer.", 5 );
   554 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Gzipping buffer.", 5 );
   423 			$gzdata = gzencode( $buffer . "<!-- Compression = gzip -->", 3, FORCE_GZIP );
   555 			wp_cache_add_to_buffer( $buffer, "Compression = gzip" );
   424 			$gzsize = strlen($gzdata);
   556 			$gzdata = gzencode( $buffer, 6, FORCE_GZIP );
       
   557 			$gzsize = function_exists( 'mb_strlen' ) ? mb_strlen( $gzdata, '8bit' ) : strlen( $gzdata );
   425 		}
   558 		}
   426 		if ($wp_cache_gzip_encoding) {
   559 		if ($wp_cache_gzip_encoding) {
   427 			$wp_cache_meta[ 'headers' ][ 'Content-Encoding' ] = 'Content-Encoding: ' . $wp_cache_gzip_encoding;
   560 			$wp_cache_meta[ 'headers' ][ 'Content-Encoding' ] = 'Content-Encoding: ' . $wp_cache_gzip_encoding;
   428 			$wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Accept-Encoding, Cookie';
   561 			$wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Accept-Encoding, Cookie';
   429 			// Return uncompressed data & store compressed for later use
   562 			// Return uncompressed data & store compressed for later use
   430 			if( $fr ) {
   563 			if ( false == $wp_cache_object_cache ) {
   431 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing gzipped buffer to wp-cache cache file.", 5 );
   564 				if( $fr ) {
   432 				fputs($fr, $gzdata);
   565 					if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing gzipped buffer to wp-cache cache file.", 5 );
       
   566 					fputs($fr, $gzdata);
       
   567 				}
       
   568 			} elseif ( $cache_enabled ) {
       
   569 				wp_cache_set( $oc_key . ".gz", $gzdata, 'supercache', $cache_max_time ); 
       
   570 				$added_cache = 1;
   433 			}
   571 			}
   434 		} else { // no compression
   572 		} else { // no compression
   435 			$wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Cookie';
   573 			$wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Cookie';
   436 			if( $fr ) {
   574 			if ( false == $wp_cache_object_cache ) {
   437 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing non-gzipped buffer to wp-cache cache file.", 5 );
   575 				if( $fr ) {
   438 				fputs($fr, $buffer);
   576 					if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing non-gzipped buffer to wp-cache cache file.", 5 );
       
   577 					fputs($fr, $buffer);
       
   578 				}
       
   579 			} elseif ( $cache_enabled ) {
       
   580 				wp_cache_set( $oc_key, $buffer, 'supercache', $cache_max_time ); 
       
   581 				$added_cache = 1;
       
   582 			}
       
   583 		}
       
   584 		if ( false == $wp_cache_object_cache ) {
       
   585 			if( $fr2 ) {
       
   586 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing non-gzipped buffer to supercache file.", 5 );
       
   587 				wp_cache_add_to_buffer( $buffer, "super cache" );
       
   588 				fputs($fr2, $buffer );
       
   589 			}
       
   590 			if( $gz ) {
       
   591 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing gzipped buffer to supercache file.", 5 );
       
   592 				fwrite($gz, $gzdata );
       
   593 			}
       
   594 		}
       
   595 	}
       
   596 	$new_cache = true;
       
   597 	if ( false == $wp_cache_object_cache ) {
       
   598 		if( $fr ) {
       
   599 			$supercacheonly = false;
       
   600 			fclose($fr);
       
   601 			if ( filesize( $tmp_wpcache_filename ) == 0 ) {
       
   602 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Warning! The file $tmp_wpcache_filename was empty. Did not rename to {$blog_cache_dir}{$cache_filename}", 5 );
       
   603 				@unlink( $tmp_wpcache_filename );
       
   604 			} else {
       
   605 				if ( !rename( $tmp_wpcache_filename, $blog_cache_dir . $cache_filename ) ) {
       
   606 					if ( false == is_dir( $blog_cache_dir ) )
       
   607 						@wp_mkdir_p( $blog_cache_dir );
       
   608 					unlink( $blog_cache_dir . $cache_filename );
       
   609 					rename( $tmp_wpcache_filename, $blog_cache_dir . $cache_filename );
       
   610 				}
       
   611 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Renamed temp wp-cache file to {$blog_cache_dir}$cache_filename", 5 );
       
   612 				$added_cache = 1;
   439 			}
   613 			}
   440 		}
   614 		}
   441 		if( $fr2 ) {
   615 		if( $fr2 ) {
   442 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing non-gzipped buffer to supercache file.", 5 );
   616 			fclose($fr2);
   443 			fputs($fr2, $buffer . '<!-- super cache -->' );
   617 			if ( $wp_cache_front_page_checks && $cache_fname == $supercachedir . $home_url[ 'path' ] . supercache_filename() && !( $wp_cache_is_home ) ) {
       
   618 				wp_cache_writers_exit();
       
   619 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Warning! Not writing another page to front page cache.", 1 );
       
   620 				return $buffer;
       
   621 			} elseif ( filesize( $tmp_cache_filename ) == 0 ) {
       
   622 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Warning! The file $tmp_cache_filename was empty. Did not rename to {$cache_fname}", 5 );
       
   623 				@unlink( $tmp_cache_filename );
       
   624 			} else {
       
   625 				if ( !@rename( $tmp_cache_filename, $cache_fname ) ) {
       
   626 					@unlink( $cache_fname );
       
   627 					@rename( $tmp_cache_filename, $cache_fname );
       
   628 				}
       
   629 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Renamed temp supercache file to $cache_fname", 5 );
       
   630 				$added_cache = 1;
       
   631 			}
       
   632 		}
       
   633 		if ( isset( $php_fd ) ) {
       
   634 			fclose( $php_fd );
       
   635 			if ( $php_fname == $supercachedir . $home_url[ 'path' ] . supercache_filename() . '.php' && !( $wp_cache_is_home ) ) {
       
   636 				wp_cache_writers_exit();
       
   637 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Warning! Not writing another page to front page cache.", 1 );
       
   638 				return $buffer;
       
   639 			} elseif ( filesize( $tmp_php_filename ) == 0 ) {
       
   640 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Warning! The file $tmp_php_filename was empty. Did not rename to {$php_fname}", 5 );
       
   641 				@unlink( $tmp_php_filename );
       
   642 			} else {
       
   643 				if ( !@rename( $tmp_php_filename, $php_fname ) ) {
       
   644 					@unlink( $php_fname );
       
   645 					@rename( $tmp_php_filename, $php_fname );
       
   646 				}
       
   647 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Renamed temp supercache file to $php_fname", 5 );
       
   648 				$added_cache = 1;
       
   649 			}
   444 		}
   650 		}
   445 		if( $gz ) {
   651 		if( $gz ) {
   446 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing gzipped buffer to supercache file.", 5 );
   652 			fclose($gz);
   447 			fwrite($gz, $gzdata );
   653 			if ( filesize( $tmp_cache_filename . '.gz' ) == 0 ) {
   448 		}
   654 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Warning! The file {$tmp_cache_filename}.gz was empty. Did not rename to {$cache_fname}.gz", 5 );
   449 		$buffer .= $log;
   655 				@unlink( $tmp_cache_filename . '.gz' );
   450 	}
   656 			} else {
   451 	$new_cache = true;
   657 				if ( !@rename( $tmp_cache_filename . '.gz', $cache_fname . '.gz' ) ) {
   452 	$added_cache = 0;
   658 					@unlink( $cache_fname . '.gz' );
   453 	if( $fr ) {
   659 					@rename( $tmp_cache_filename . '.gz', $cache_fname . '.gz' );
   454 		$supercacheonly = false;
   660 				}
   455 		fclose($fr);
   661 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Renamed temp supercache gz file to {$cache_fname}.gz", 5 );
   456 		if( !rename( $tmp_wpcache_filename, $blog_cache_dir . $cache_filename ) ) {
   662 				$added_cache = 1;
   457 			unlink( $blog_cache_dir . $cache_filename );
   663 			}
   458 			rename( $tmp_wpcache_filename, $blog_cache_dir . $cache_filename );
   664 		}
   459 		}
       
   460 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Renamed temp wp-cache file to {$blog_cache_dir}$cache_filename", 5 );
       
   461 		$added_cache = 1;
       
   462 	}
       
   463 	if( $fr2 ) {
       
   464 		fclose($fr2);
       
   465 		if( !@rename( $tmp_cache_filename, $cache_fname ) ) {
       
   466 			@unlink( $cache_fname );
       
   467 			@rename( $tmp_cache_filename, $cache_fname );
       
   468 		}
       
   469 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Renamed temp supercache file to $cache_fname", 5 );
       
   470 		$added_cache = 1;
       
   471 	}
       
   472 	if( $gz ) {
       
   473 		fclose($gz);
       
   474 		if( !@rename( $tmp_cache_filename . '.gz', $cache_fname . '.gz' ) ) {
       
   475 			@unlink( $cache_fname . '.gz' );
       
   476 			@rename( $tmp_cache_filename . '.gz', $cache_fname . '.gz' );
       
   477 		}
       
   478 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Renamed temp supercache gz file to {$cache_fname}.gz", 5 );
       
   479 		$added_cache = 1;
       
   480 	}
   665 	}
   481 	if ( $added_cache && isset( $wp_supercache_cache_list ) && $wp_supercache_cache_list ) {
   666 	if ( $added_cache && isset( $wp_supercache_cache_list ) && $wp_supercache_cache_list ) {
   482 		update_option( 'wpsupercache_count', ( get_option( 'wpsupercache_count' ) + 1 ) );
   667 		update_option( 'wpsupercache_count', ( get_option( 'wpsupercache_count' ) + 1 ) );
   483 		$last_urls = (array)get_option( 'supercache_last_cached' );
   668 		$last_urls = (array)get_option( 'supercache_last_cached' );
   484 		if ( count( $last_urls ) >= 10 )
   669 		if ( count( $last_urls ) >= 10 )
   485 			$last_urls = array_slice( $last_urls, 1, 9 );
   670 			$last_urls = array_slice( $last_urls, 1, 9 );
   486 		$last_urls[] = array( 'url' => $_SERVER[ 'REQUEST_URI' ], 'date' => date( 'Y-m-d H:i:s' ) );
   671 		$last_urls[] = array( 'url' => $_SERVER[ 'REQUEST_URI' ], 'date' => date( 'Y-m-d H:i:s' ) );
   487 		update_option( 'supercache_last_cached', $last_urls );
   672 		update_option( 'supercache_last_cached', $last_urls );
   488 	}
   673 	}
   489 	wp_cache_writers_exit();
   674 	wp_cache_writers_exit();
   490 	if ( !headers_sent() && isset( $wp_cache_gzip_first ) && 1 == $wp_cache_gzip_first && $wp_cache_gzip_encoding && $gzdata) {
   675 	if ( !headers_sent() && $wp_cache_gzip_encoding && $gzdata) {
   491 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing gzip content headers. Sending buffer to browser", 5 );
   676 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing gzip content headers. Sending buffer to browser", 5 );
   492 		header( 'Content-Encoding: ' . $wp_cache_gzip_encoding );
   677 		header( 'Content-Encoding: ' . $wp_cache_gzip_encoding );
   493 		header( 'Vary: Accept-Encoding, Cookie' );
   678 		header( 'Vary: Accept-Encoding, Cookie' );
   494 		header( 'Content-Length: ' . $gzsize );
   679 		header( 'Content-Length: ' . $gzsize );
   495 		return $gzdata;
   680 		return $gzdata;
   505 	if( !wp_cache_writers_entry() )
   690 	if( !wp_cache_writers_entry() )
   506 		return false;
   691 		return false;
   507 	if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Cleaning cache in $blog_cache_dir", 3 );
   692 	if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Cleaning cache in $blog_cache_dir", 3 );
   508 	if ( ( $handle = @opendir( $blog_cache_dir ) ) ) { 
   693 	if ( ( $handle = @opendir( $blog_cache_dir ) ) ) { 
   509 		while ( false !== ($file = @readdir($handle))) {
   694 		while ( false !== ($file = @readdir($handle))) {
   510 			if ( preg_match("/^$file_prefix/", $file) )
   695 			if ( preg_match("/^$file_prefix/", $file) ) {
   511 				@unlink( $blog_cache_dir . $file );
   696 				$meta = unserialize( file_get_contents( $blog_cache_dir . 'meta/' . $file ) );
       
   697 				if ( $meta[ 'blog_id' ] == $wpdb->blogid ) {
       
   698 					@unlink( $blog_cache_dir . $file );
       
   699 					@unlink( $blog_cache_dir . 'meta/' . str_replace( '.html', '.meta', $file ) );
       
   700 				}
       
   701 			}
   512 		}
   702 		}
   513 		closedir($handle);
   703 		closedir($handle);
   514 	}
   704 	}
   515 	wp_cache_writers_exit();
   705 	wp_cache_writers_exit();
   516 }
   706 }
   517 
   707 
   518 function prune_super_cache( $directory, $force = false, $rename = false ) {
   708 function prune_super_cache( $directory, $force = false, $rename = false ) {
   519 	global $cache_max_time, $cache_path, $super_cache_enabled, $cache_rebuild_files, $blog_cache_dir;
   709 	global $cache_max_time, $cache_path, $cache_rebuild_files, $blog_cache_dir;
   520 
   710 	static $log = 0;
   521 	if( !is_admin() && $super_cache_enabled == 0 )
       
   522 		return false;
       
   523 
   711 
   524 	if( !isset( $cache_max_time ) )
   712 	if( !isset( $cache_max_time ) )
   525 		$cache_max_time = 3600;
   713 		$cache_max_time = 3600;
   526 
   714 
   527 	$now = time();
   715 	$now = time();
   544 						$donotdelete = false;
   732 						$donotdelete = false;
   545 						while( !$donotdelete && ( $file = @readdir( $handle ) ) !== false ) {
   733 						while( !$donotdelete && ( $file = @readdir( $handle ) ) !== false ) {
   546 							if ($file == '.' || $file == '..')
   734 							if ($file == '.' || $file == '..')
   547 								continue;
   735 								continue;
   548 							$donotdelete = true;
   736 							$donotdelete = true;
       
   737 							if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "gc: could not delete $entry as it's not empty: $file", 2 );
   549 						}
   738 						}
   550 						closedir($handle);
   739 						closedir($handle);
   551 					}
   740 					}
   552 					if( $donotdelete )
   741 					if( $donotdelete )
   553 						continue;
   742 						continue;
   554 					if( !$rename ) {
   743 					if( !$rename ) {
   555 						@rmdir( $entry );
   744 						@rmdir( $entry );
   556 						if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "gc: deleted $entry", 2 );
   745 						$log++;
       
   746 						if ( $force ) {
       
   747 							if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "gc: deleted $entry, forced delete", 2 );
       
   748 						} else {
       
   749 							if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "gc: deleted $entry, older than $cache_max_time seconds", 2 );
       
   750 						}
   557 					}
   751 					}
   558 				}
   752 				}
   559 			}
   753 			}
   560 			closedir($dh);
   754 			closedir($dh);
   561 		}
   755 		}
   562 	} elseif( is_file($directory) && ($force || @filemtime( $directory ) + $cache_max_time <= $now ) ) {
   756 	} elseif( is_file($directory) && ($force || @filemtime( $directory ) + $cache_max_time <= $now ) ) {
   563 		$oktodelete = true;
   757 		$oktodelete = true;
   564 		if( in_array( $directory, $protected_directories ) )
   758 		if( in_array( $directory, $protected_directories ) )
   565 			$oktodelete = false;
   759 			$oktodelete = false;
   566 		if( $oktodelete && !$rename ) {
   760 		if( $oktodelete && !$rename ) {
       
   761 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "prune_super_cache: deleted $directory", 5 );
   567 			@unlink( $directory );
   762 			@unlink( $directory );
       
   763 			$log++;
   568 		} elseif( $oktodelete && $rename ) {
   764 		} elseif( $oktodelete && $rename ) {
       
   765 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "prune_super_cache: wp_cache_rebuild_or_delete( $directory )", 5 );
   569 			wp_cache_rebuild_or_delete( $directory );
   766 			wp_cache_rebuild_or_delete( $directory );
   570 		}
   767 			$log++;
   571 	}
   768 		}
       
   769 	}
       
   770 	return $log;
   572 }
   771 }
   573 
   772 
   574 function wp_cache_rebuild_or_delete( $file ) {
   773 function wp_cache_rebuild_or_delete( $file ) {
   575 	global $cache_rebuild_files;
   774 	global $cache_rebuild_files;
   576 	if( strpos( $file, '?' ) !== false )
   775 	if( strpos( $file, '?' ) !== false )
   587 		@unlink( $file );
   786 		@unlink( $file );
   588 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "rebuild_or_gc: deleted $file", 2 );
   787 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "rebuild_or_gc: deleted $file", 2 );
   589 	}
   788 	}
   590 }
   789 }
   591 
   790 
   592 function wp_cache_phase2_clean_expired($file_prefix) {
   791 function wp_cache_phase2_clean_expired( $file_prefix, $force = false ) {
   593 	global $cache_path, $cache_max_time, $blog_cache_dir;
   792 	global $cache_path, $cache_max_time, $blog_cache_dir, $wp_cache_preload_on;
   594 
   793 
   595 	clearstatcache();
   794 	clearstatcache();
   596 	if( !wp_cache_writers_entry() )
   795 	if( !wp_cache_writers_entry() )
   597 		return false;
   796 		return false;
   598 	$now = time();
   797 	$now = time();
   608 			}
   807 			}
   609 			if($file != '.' && $file != '..') {
   808 			if($file != '.' && $file != '..') {
   610 				if( is_dir( $blog_cache_dir . $file ) == false && (@filemtime($blog_cache_dir . $file) + $cache_max_time) <= $now  ) {
   809 				if( is_dir( $blog_cache_dir . $file ) == false && (@filemtime($blog_cache_dir . $file) + $cache_max_time) <= $now  ) {
   611 					if( substr( $file, -9 ) != '.htaccess' ) {
   810 					if( substr( $file, -9 ) != '.htaccess' ) {
   612 						@unlink($blog_cache_dir . $file);
   811 						@unlink($blog_cache_dir . $file);
   613 						if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Deleting $blog_cache_dir{$file}", 5 );
   812 						if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Deleting $blog_cache_dir{$file}, older than $cache_max_time seconds", 5 );
   614 					}
   813 					}
   615 				}
   814 				}
   616 			}
   815 			}
   617 		}
   816 		}
   618 		closedir($handle);
   817 		closedir($handle);
   619 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Doing GC on supercache dir: {$cache_path}supercache", 2 );
   818 		if ( false == $wp_cache_preload_on || true == $force ) {
   620 		prune_super_cache( $cache_path . 'supercache' );
   819 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Doing GC on supercache dir: {$cache_path}supercache", 2 );
       
   820 			$deleted = prune_super_cache( $cache_path . 'supercache', false, false );
       
   821 		}
   621 	}
   822 	}
   622 
   823 
   623 	wp_cache_writers_exit();
   824 	wp_cache_writers_exit();
   624 	return true;
   825 	return $deleted;
   625 }
   826 }
   626 
   827 
   627 function wp_cache_shutdown_callback() {
   828 function wp_cache_shutdown_callback() {
   628 	global $cache_path, $cache_max_time, $file_expired, $file_prefix, $meta_file, $new_cache, $wp_cache_meta, $known_headers, $blog_id, $wp_cache_gzip_encoding, $gzsize, $cache_filename, $supercacheonly, $blog_cache_dir;
   829 	global $cache_path, $cache_max_time, $file_expired, $file_prefix, $meta_file, $new_cache, $wp_cache_meta, $known_headers, $blog_id, $wp_cache_gzip_encoding, $gzsize, $cache_filename, $supercacheonly, $blog_cache_dir;
   629 	global $wp_cache_blog_charset, $wp_cache_request_uri, $wp_cache_key;
   830 	global $wp_cache_request_uri, $wp_cache_key, $wp_cache_object_cache, $cache_enabled, $wp_cache_blog_charset, $wp_cache_not_logged_in;
   630 
   831 	global $WPSC_HTTP_HOST;
   631 	$wp_cache_meta[ 'uri' ] = $_SERVER["SERVER_NAME"].preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', $wp_cache_request_uri); // To avoid XSS attacks
   832 
       
   833 	$wp_cache_meta[ 'uri' ] = $WPSC_HTTP_HOST . preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', $wp_cache_request_uri); // To avoid XSS attacks
   632 	$wp_cache_meta[ 'blog_id' ] = $blog_id;
   834 	$wp_cache_meta[ 'blog_id' ] = $blog_id;
   633 	$wp_cache_meta[ 'post' ] = wp_cache_post_id();
   835 	$wp_cache_meta[ 'post' ] = wp_cache_post_id();
   634 	$wp_cache_meta[ 'key' ] = $wp_cache_key;
   836 	$wp_cache_meta[ 'key' ] = $wp_cache_key;
   635 	$wp_cache_meta = apply_filters( 'wp_cache_meta', $wp_cache_meta );
   837 	$wp_cache_meta = apply_filters( 'wp_cache_meta', $wp_cache_meta );
   636 
   838 
   638 	foreach ($known_headers as $key) {
   840 	foreach ($known_headers as $key) {
   639 		if(isset($response[$key])) {
   841 		if(isset($response[$key])) {
   640 			$wp_cache_meta[ 'headers' ][ $key ] = "$key: " . $response[$key];
   842 			$wp_cache_meta[ 'headers' ][ $key ] = "$key: " . $response[$key];
   641 		}
   843 		}
   642 	}
   844 	}
       
   845 
       
   846 	if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_shutdown_callback: collecting meta data.", 2 );
       
   847 
   643 	if (!isset( $response['Last-Modified'] )) {
   848 	if (!isset( $response['Last-Modified'] )) {
   644 		$value = gmdate('D, d M Y H:i:s') . ' GMT';
   849 		$value = gmdate('D, d M Y H:i:s') . ' GMT';
   645 		/* Dont send this the first time */
   850 		/* Dont send this the first time */
   646 		/* @header('Last-Modified: ' . $value); */
   851 		/* @header('Last-Modified: ' . $value); */
   647 		$wp_cache_meta[ 'headers' ][ 'Last-Modified' ] = "Last-Modified: $value";
   852 		$wp_cache_meta[ 'headers' ][ 'Last-Modified' ] = "Last-Modified: $value";
   648 	}
   853 	}
   649 	if (!$response['Content-Type'] && !$response['Content-type']) {
   854 	if ( !isset( $response[ 'Content-Type' ] ) && !isset( $response[ 'Content-type' ] ) ) {
   650 		// On some systems, headers set by PHP can't be fetched from
   855 		// On some systems, headers set by PHP can't be fetched from
   651 		// the output buffer. This is a last ditch effort to set the
   856 		// the output buffer. This is a last ditch effort to set the
   652 		// correct Content-Type header for feeds, if we didn't see
   857 		// correct Content-Type header for feeds, if we didn't see
   653 		// it in the response headers already. -- dougal
   858 		// it in the response headers already. -- dougal
   654 		if (is_feed()) {
   859 		if (is_feed()) {
   676 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Sending 'Content-Type: $value' header.", 2 );
   881 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Sending 'Content-Type: $value' header.", 2 );
   677 		@header("Content-Type: $value");
   882 		@header("Content-Type: $value");
   678 		$wp_cache_meta[ 'headers' ][ 'Content-Type' ] = "Content-Type: $value";
   883 		$wp_cache_meta[ 'headers' ][ 'Content-Type' ] = "Content-Type: $value";
   679 	}
   884 	}
   680 
   885 
   681 	if ( ! $supercacheonly && $new_cache ) {
   886 	if ( $cache_enabled && !$supercacheonly && $new_cache ) {
   682 		if( $wp_cache_gzip_encoding && !in_array( 'Content-Encoding: ' . $wp_cache_gzip_encoding, $wp_cache_meta[ 'headers' ] ) ) {
   887 		if( !isset( $wp_cache_meta[ 'dynamic' ] ) && $wp_cache_gzip_encoding && !in_array( 'Content-Encoding: ' . $wp_cache_gzip_encoding, $wp_cache_meta[ 'headers' ] ) ) {
   683 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Sending gzip headers.", 2 );
   888 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Sending gzip headers.", 2 );
   684 			$wp_cache_meta[ 'headers' ][ 'Content-Encoding' ] = 'Content-Encoding: ' . $wp_cache_gzip_encoding;
   889 			$wp_cache_meta[ 'headers' ][ 'Content-Encoding' ] = 'Content-Encoding: ' . $wp_cache_gzip_encoding;
   685 			$wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Accept-Encoding, Cookie';
   890 			$wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Accept-Encoding, Cookie';
   686 		}
   891 		}
   687 
   892 
   688 		$serial = serialize($wp_cache_meta);
   893 		$serial = serialize($wp_cache_meta);
   689 		if( wp_cache_writers_entry() ) {
   894 		if( wp_cache_writers_entry() ) {
   690 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing meta file: {$blog_cache_dir}meta/{$meta_file}", 2 );
   895 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing meta file: {$blog_cache_dir}meta/{$meta_file}", 2 );
   691 			$tmp_meta_filename = $blog_cache_dir . 'meta/' . uniqid( mt_rand(), true ) . '.tmp';
   896 			if ( false == $wp_cache_object_cache ) {
   692 			$fr = @fopen( $tmp_meta_filename, 'w');
   897 				$tmp_meta_filename = $blog_cache_dir . 'meta/' . uniqid( mt_rand(), true ) . '.tmp';
   693 			if( !$fr )
   898 				$fr = @fopen( $tmp_meta_filename, 'w');
   694 				@mkdir( $blog_cache_dir . 'meta' );
   899 				if( !$fr )
   695 			$fr = fopen( $tmp_meta_filename, 'w');
   900 					@wp_mkdir_p( $blog_cache_dir . 'meta' );
   696 			fputs($fr, $serial);
   901 				$fr = @fopen( $tmp_meta_filename, 'w');
   697 			fclose($fr);
   902 				if ( $fr ) {
   698 			@chmod( $tmp_meta_filename, 0666 & ~umask());
   903 					fputs($fr, $serial);
   699 			if( !@rename( $tmp_meta_filename, $blog_cache_dir . 'meta/' . $meta_file ) ) {
   904 					fclose($fr);
   700 				unlink( $blog_cache_dir . 'meta/' . $meta_file );
   905 					@chmod( $tmp_meta_filename, 0666 & ~umask());
   701 				rename( $tmp_meta_filename, $blog_cache_dir . 'meta/' . $meta_file );
   906 					if( !@rename( $tmp_meta_filename, $blog_cache_dir . 'meta/' . $meta_file ) ) {
       
   907 						@unlink( $blog_cache_dir . 'meta/' . $meta_file );
       
   908 						@rename( $tmp_meta_filename, $blog_cache_dir . 'meta/' . $meta_file );
       
   909 					}
       
   910 				} else {
       
   911 					if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Problem writing meta file: {$blog_cache_dir}meta/{$meta_file}", 2 );
       
   912 				}
       
   913 			} elseif ( $cache_enabled ) {
       
   914 				$oc_key = get_oc_key() . ".meta";
       
   915 				if ( gzip_accepted() )
       
   916 					$oc_key .= ".gz";
       
   917 				wp_cache_set( $oc_key, $serial, 'supercache', $cache_max_time );
   702 			}
   918 			}
   703 			wp_cache_writers_exit();
   919 			wp_cache_writers_exit();
   704 		}
   920 		}
       
   921 	} else {
       
   922 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Did not write meta file: $meta_file *$supercacheonly* *$wp_cache_not_logged_in* *$new_cache*", 2 );
   705 	}
   923 	}
   706 	global $time_to_gc_cache;
   924 	global $time_to_gc_cache;
   707 	if( isset( $time_to_gc_cache ) && $time_to_gc_cache == 1 ) {
   925 	if( isset( $time_to_gc_cache ) && $time_to_gc_cache == 1 ) {
   708 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Executing wp_cache_gc action.", 3 );
   926 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Executing wp_cache_gc action.", 3 );
   709 		do_action( 'wp_cache_gc' );
   927 		do_action( 'wp_cache_gc' );
   719 	$comment = get_comment($comment_id, ARRAY_A);
   937 	$comment = get_comment($comment_id, ARRAY_A);
   720 	if ( $status != 'NA' ) {
   938 	if ( $status != 'NA' ) {
   721 		$comment[ 'old_comment_approved' ] = $comment[ 'comment_approved' ];
   939 		$comment[ 'old_comment_approved' ] = $comment[ 'comment_approved' ];
   722 		$comment[ 'comment_approved' ] = $status;
   940 		$comment[ 'comment_approved' ] = $status;
   723 	}
   941 	}
       
   942 
       
   943 	if ( ( $status == 'trash' || $status == 'spam' ) && $comment[ 'old_comment_approved' ] != 1 ) {
       
   944 		// don't modify cache if moderated comments are trashed or spammed
       
   945 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Moderated comment deleted or spammed. Don't delete any cache files.", 4 );
       
   946 		define( 'DONOTDELETECACHE', 1 );
       
   947 		return wp_cache_post_id();
       
   948 	}
   724 	$postid = $comment['comment_post_ID'];
   949 	$postid = $comment['comment_post_ID'];
   725 	// Do nothing if comment is not moderated
   950 	// Do nothing if comment is not moderated
   726 	// http://ocaoimh.ie/2006/12/05/caching-wordpress-with-wp-cache-in-a-spam-filled-world
   951 	// http://ocaoimh.ie/2006/12/05/caching-wordpress-with-wp-cache-in-a-spam-filled-world
   727 	if ( !preg_match('/wp-admin\//', $wp_cache_request_uri) ) {
   952 	if ( !preg_match('/wp-admin\//', $wp_cache_request_uri) ) {
   728 		if ( $comment['comment_approved'] == 'delete' && ( isset( $comment[ 'old_comment_approved' ] ) && $comment[ 'old_comment_approved' ] == 0 ) ) { // do nothing if moderated comments are deleted
   953 		if ( $comment['comment_approved'] == 'delete' && ( isset( $comment[ 'old_comment_approved' ] ) && $comment[ 'old_comment_approved' ] == 0 ) ) { // do nothing if moderated comments are deleted
   729 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Moderated comment deleted. Don't delete any cache files.", 4 );
   954 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Moderated comment deleted. Don't delete any cache files.", 4 );
       
   955 			define( 'DONOTDELETECACHE', 1 );
   730 			return $postid;
   956 			return $postid;
   731 		} elseif ( $comment['comment_approved'] == 'spam' ) {
   957 		} elseif ( $comment['comment_approved'] == 'spam' ) {
   732 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Spam comment. Don't delete any cache files.", 4 );
   958 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Spam comment. Don't delete any cache files.", 4 );
       
   959 			define( 'DONOTDELETECACHE', 1 );
   733 			return $postid;
   960 			return $postid;
   734 		} elseif( $comment['comment_approved'] == '0' ) {
   961 		} elseif( $comment['comment_approved'] == '0' ) {
   735 			if ( $comment[ 'content_type' ] == '' ) {
   962 			if ( $comment[ 'content_type' ] == '' ) {
   736 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Moderated comment. Don't delete supercache file until comment approved.", 4 );
   963 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Moderated comment. Don't delete supercache file until comment approved.", 4 );
   737 				$super_cache_enabled = 0; // don't remove the super cache static file until comment is approved
   964 				$super_cache_enabled = 0; // don't remove the super cache static file until comment is approved
       
   965 				define( 'DONOTDELETECACHE', 1 );
   738 			} else {
   966 			} else {
   739 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Moderated ping or trackback. Not deleting cache files..", 4 );
   967 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Moderated ping or trackback. Not deleting cache files..", 4 );
       
   968 				define( 'DONOTDELETECACHE', 1 );
   740 				return $postid;
   969 				return $postid;
   741 			}
   970 			}
   742 		}
   971 		}
   743 	}
   972 	}
   744 	// We must check it up again due to WP bugs calling two different actions
   973 	// We must check it up again due to WP bugs calling two different actions
   745 	// for delete, for example both wp_set_comment_status and delete_comment 
   974 	// for delete, for example both wp_set_comment_status and delete_comment 
   746 	// are called when deleting a comment
   975 	// are called when deleting a comment
   747 	if ($postid > 0)  {
   976 	if ($postid > 0)  {
   748 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post $postid changed. Update cache.", 4 );
   977 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post $postid changed. Update cache.", 4 );
   749 		return wp_cache_post_change($postid);
   978 		return wp_cache_post_change( $postid );
       
   979 	} elseif ( $_GET[ 'delete_all' ] != 'Empty Trash' && $_GET[ 'delete_all2' ] != 'Empty Spam' ) {
       
   980 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Unknown post changed. Update cache.", 4 );
       
   981 		return wp_cache_post_change( wp_cache_post_id() );
       
   982 	}
       
   983 }
       
   984 
       
   985 /* Used by wp_update_nav_menu action to clear current blog's cache files when navigation menu is modified */
       
   986 function wp_cache_clear_cache_on_menu() {
       
   987 	global $wpdb;
       
   988 	wp_cache_clear_cache( $wpdb->blogid );
       
   989 }
       
   990 
       
   991 /* Clear out the cache directory. */
       
   992 function wp_cache_clear_cache( $blog_id = 0 ) {
       
   993 	global $cache_path, $wp_cache_object_cache;
       
   994 	if ( $wp_cache_object_cache ) {
       
   995 		reset_oc_version();
   750 	} else {
   996 	} else {
   751 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Unknown post changed. Update cache.", 4 );
   997 		if ( $blog_id == 0 ) {
   752 		return wp_cache_post_change(wp_cache_post_id());
   998 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Clearing all cached files in wp_cache_clear_cache()", 4 );
   753 	}
   999 			prune_super_cache( $cache_path . 'supercache/', true );
   754 }
  1000 			prune_super_cache( $cache_path, true );
   755 
  1001 		} else {
   756 /* Clear out the cache directory. */
  1002 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Clearing all cached files for blog $blog_id in wp_cache_clear_cache()", 4 );
   757 function wp_cache_clear_cache() {
  1003 			prune_super_cache( get_supercache_dir( $blog_id ), true );
   758 	global $cache_path;
  1004 			prune_super_cache( $cache_path . 'blogs/', true );
   759 	prune_super_cache( $cache_path . 'supercache/', true );
  1005 		}
   760 	prune_super_cache( $cache_path, true );
  1006 	}
   761 }
  1007 }
   762 
  1008 
       
  1009 /* check if we want to clear out all cached files on post updates, otherwise call standard wp_cache_post_change() */
   763 function wp_cache_post_edit($post_id) {
  1010 function wp_cache_post_edit($post_id) {
   764 	global $wp_cache_clear_on_post_edit, $cache_path, $blog_cache_dir;
  1011 	global $wp_cache_clear_on_post_edit, $cache_path, $blog_cache_dir;
       
  1012 	static $last_post_edited = -1;
       
  1013 
       
  1014 	if ( $post_id == $last_post_edited ) {
       
  1015 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_edit: Already processed post $post_id.", 4 );
       
  1016 		return $post_id;
       
  1017 	}
       
  1018 
       
  1019 	$post = get_post( $post_id );
       
  1020 	// Some users are inexplicibly seeing this error on scheduled posts. 
       
  1021 	// define this constant to disable the post status check.
       
  1022 	if ( false == defined( 'WPSCFORCEUPDATE' ) && $post->post_status != 'publish' ) {
       
  1023 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_edit: draft post, not deleting any cache files.", 4 );
       
  1024 		return $post_id;
       
  1025 	}
       
  1026 
       
  1027 	// we want to process the post again just in case it becomes published before the second time this function is called.
       
  1028 	$last_post_edited = $post_id;
   765 	if( $wp_cache_clear_on_post_edit ) {
  1029 	if( $wp_cache_clear_on_post_edit ) {
   766 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Clearing cache $blog_cache_dir and {$cache_path}supercache/ on post edit per config.", 2 );
  1030 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_edit: Clearing cache $blog_cache_dir and {$cache_path}supercache/ on post edit per config.", 2 );
   767 		prune_super_cache( $blog_cache_dir, true );
  1031 		if ( $wp_cache_object_cache ) {
   768 		prune_super_cache( $cache_path . 'supercache/', true );
  1032 			reset_oc_version();
       
  1033 		} else {
       
  1034 			prune_super_cache( $blog_cache_dir, true );
       
  1035 			prune_super_cache( get_supercache_dir(), true );
       
  1036 		}
   769 	} else {
  1037 	} else {
   770 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Clearing cache for post $post_id on post edit.", 2 );
  1038 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_edit: Clearing cache for post $post_id on post edit.", 2 );
   771 		wp_cache_post_change( $post_id );
  1039 		wp_cache_post_change( $post_id );
   772 	}
  1040 	}
   773 }
  1041 }
   774 
  1042 
   775 function wp_cache_post_id_gc( $siteurl, $post_id ) {
  1043 function wp_cache_post_id_gc( $siteurl, $post_id, $all = 'all' ) {
   776 	global $cache_path;
  1044 	global $cache_path, $wp_cache_object_cache, $wp_cache_refresh_single_only;
   777 	
  1045 	
       
  1046 	if ( $wp_cache_object_cache )
       
  1047 		reset_oc_version();
       
  1048 
   778 	$post_id = intval( $post_id );
  1049 	$post_id = intval( $post_id );
   779 	if( $post_id == 0 )
  1050 	if( $post_id == 0 )
   780 		return;
  1051 		return;
   781 
  1052 
   782 	$permalink = trailingslashit( str_replace( get_option( 'home' ), '', post_permalink( $post_id ) ) );
  1053 	$permalink = trailingslashit( str_replace( get_option( 'home' ), '', post_permalink( $post_id ) ) );
   783 	$dir = $cache_path . 'supercache/' . $siteurl;
  1054 	$dir = get_current_url_supercache_dir( $post_id );
   784 	if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_id_gc clearing cache in $dir{$permalink}.", 4 );
  1055 	if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_id_gc post_id: $post_id " . post_permalink( $post_id ) . " clearing cache in $dir.", 4 );
   785 	prune_super_cache( $dir . $permalink, true, true );
  1056 	if ( $all == 'all' ) {
   786 	@rmdir( $dir . $permalink );
  1057 		prune_super_cache( $dir, true, true );
   787 	if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_id_gc clearing cache in {$dir}page/.", 4 );
  1058 		do_action( 'gc_cache', 'prune', $permalink );
   788 	prune_super_cache( $dir . 'page/', true );
  1059 		@rmdir( $dir );
   789 }
  1060 	} else {
   790 
  1061 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_id_gc clearing cached index files in $dir.", 4 );
   791 function wp_cache_post_change($post_id) {
  1062 		prune_super_cache( $dir, true, true ); 
   792 	global $file_prefix, $cache_path, $blog_id, $super_cache_enabled, $blog_cache_dir, $blogcacheid;
  1063 		do_action( 'gc_cache', 'prune', $permalink );
       
  1064 	}
       
  1065 	if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_id_gc clearing cache in {$dir}/page/.", 4 );
       
  1066 	prune_super_cache( $dir . '/page/', true );
       
  1067 	$supercache_home = get_supercache_dir();
       
  1068 	if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_id_gc clearing cache in {$supercache_home}/page/.", 4 );
       
  1069 	prune_super_cache( $supercache_home . '/page/', true );
       
  1070 	do_action( 'gc_cache', 'prune', '/page/' );
       
  1071 }
       
  1072 
       
  1073 function wp_cache_post_change( $post_id ) {
       
  1074 	global $file_prefix, $cache_path, $blog_id, $super_cache_enabled, $blog_cache_dir, $blogcacheid, $wp_cache_refresh_single_only;
   793 	static $last_processed = -1;
  1075 	static $last_processed = -1;
   794 
  1076 
   795 	if ($post_id == $last_processed) return $post_id;
  1077 	if ( $post_id == $last_processed ) {
       
  1078 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_change: Already processed post $post_id.", 4 );
       
  1079 		return $post_id;
       
  1080 	}
       
  1081 	$post = get_post( $post_id );
       
  1082 	// Some users are inexplicibly seeing this error on scheduled posts. 
       
  1083 	// define this constant to disable the post status check.
       
  1084 	if ( false == defined( 'WPSCFORCEUPDATE' ) && $post->post_status != 'publish' ) {
       
  1085 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_change: draft post, not deleting any cache files.", 4 );
       
  1086 		return $post_id;
       
  1087 	}
   796 	$last_processed = $post_id;
  1088 	$last_processed = $post_id;
       
  1089 
   797 	if( !wp_cache_writers_entry() )
  1090 	if( !wp_cache_writers_entry() )
   798 		return $post_id;
  1091 		return $post_id;
   799 
  1092 
       
  1093 	if ( isset( $wp_cache_refresh_single_only ) && $wp_cache_refresh_single_only && ( strpos( $_SERVER[ 'HTTP_REFERER' ], 'edit-comments.php' ) || strpos( $_SERVER[ 'REQUEST_URI' ], 'wp-comments-post.php' ) ) ) {
       
  1094 		if ( defined( 'DONOTDELETECACHE' ) ) {
       
  1095 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_change: comment detected and it's moderated or spam. Not deleting cached files.", 4 );
       
  1096 			return $post_id;
       
  1097 		} else {
       
  1098 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_change: comment detected. only deleting post page.", 4 );
       
  1099 			$all = false;
       
  1100 		}
       
  1101 	} else {
       
  1102 		$all = true;
       
  1103 	}
       
  1104 
       
  1105 	if ( $wp_cache_object_cache )
       
  1106 		reset_oc_version();
       
  1107 
   800 	$permalink = trailingslashit( str_replace( get_option( 'siteurl' ), '', post_permalink( $post_id ) ) );
  1108 	$permalink = trailingslashit( str_replace( get_option( 'siteurl' ), '', post_permalink( $post_id ) ) );
   801 	if( $super_cache_enabled ) {
  1109 	if( $super_cache_enabled ) {
       
  1110 		$dir = get_supercache_dir();
   802 		$siteurl = trailingslashit( strtolower( preg_replace( '/:.*$/', '', str_replace( 'http://', '', get_option( 'home' ) ) ) ) );
  1111 		$siteurl = trailingslashit( strtolower( preg_replace( '/:.*$/', '', str_replace( 'http://', '', get_option( 'home' ) ) ) ) );
   803 		// make sure the front page has a rebuild file
  1112 		// make sure the front page has a rebuild file
   804 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post change: deleting cache files in " . $cache_path . 'supercache/' . $siteurl, 4 );
       
   805 		prune_super_cache( $cache_path . 'supercache/' . $siteurl . 'index.html', true, true ); 
       
   806 		prune_super_cache( $cache_path . 'supercache/' . $siteurl . 'index.html.gz', true, true );
       
   807 		wp_cache_post_id_gc( $siteurl, $post_id );
  1113 		wp_cache_post_id_gc( $siteurl, $post_id );
   808 		if( get_option( 'show_on_front' ) == 'page' ) {
  1114 		if ( $all == true ) {
   809 			wp_cache_post_id_gc( $siteurl, get_option( 'page_on_front' ) );
  1115 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post change: deleting cache files in " . $cache_path . 'supercache/' . $siteurl, 4 );
   810 			wp_cache_post_id_gc( $siteurl, get_option( 'page_for_posts' ) );
  1116 			$files_to_check = get_all_supercache_filenames( $dir );
       
  1117 			foreach( $files_to_check as $cache_file ) {
       
  1118 				prune_super_cache( $dir . $cache_file, true, true ); 
       
  1119 			}
       
  1120 			do_action( 'gc_cache', 'prune', 'homepage' );
       
  1121 		}
       
  1122 		if( $all == true && get_option( 'show_on_front' ) == 'page' ) {
       
  1123 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post change: deleting page_on_front and page_for_posts pages.", 4 );
       
  1124 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post change: page_on_front " . get_option( 'page_on_front' ), 4 );
       
  1125 			wp_cache_post_id_gc( $siteurl, get_option( 'page_on_front' ), 'single' );
       
  1126 			$permalink = trailingslashit( str_replace( get_option( 'home' ), '', post_permalink( get_option( 'page_for_posts' ) ) ) );
       
  1127 			$files_to_check = get_all_supercache_filenames( $dir . $permalink );
       
  1128 			foreach( $files_to_check as $cache_file ) {
       
  1129 				prune_super_cache( $dir . $permalink . $cache_file, true, true ); 
       
  1130 			}
       
  1131 			do_action( 'gc_cache', 'prune', $permalink );
   811 		}
  1132 		}
   812 	}
  1133 	}
   813 
  1134 
   814 	$matches = array();
  1135 	$matches = array();
   815 	if ( ($handle = @opendir( $blog_cache_dir . 'meta/' )) ) { 
  1136 	if ( ($handle = @opendir( $blog_cache_dir . 'meta/' )) ) { 
   817 			if ( preg_match("/^({$file_prefix}{$blogcacheid}.*)\.meta/", $file, $matches) ) {
  1138 			if ( preg_match("/^({$file_prefix}{$blogcacheid}.*)\.meta/", $file, $matches) ) {
   818 				$meta_pathname = $blog_cache_dir . 'meta/' . $file;
  1139 				$meta_pathname = $blog_cache_dir . 'meta/' . $file;
   819 				$content_pathname = $blog_cache_dir . $matches[1] . ".html";
  1140 				$content_pathname = $blog_cache_dir . $matches[1] . ".html";
   820 				$meta = unserialize(@file_get_contents($meta_pathname));
  1141 				$meta = unserialize(@file_get_contents($meta_pathname));
   821 				if( false == is_array( $meta ) ) {
  1142 				if( false == is_array( $meta ) ) {
       
  1143 					if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post change cleaning up stray file: $content_pathname", 4 );
   822 					@unlink($meta_pathname);
  1144 					@unlink($meta_pathname);
   823 					@unlink($content_pathname);
  1145 					@unlink($content_pathname);
   824 					continue;
  1146 					continue;
   825 				}
  1147 				}
   826 				if ($post_id > 0 && $meta) {
  1148 				if ($post_id > 0 && $meta) {
   827 					if ($meta[ 'blog_id' ] == $blog_id  && (!$meta[ 'post' ] || $meta[ 'post' ] == $post_id) ) {
  1149 					$permalink = trailingslashit( str_replace( get_option( 'home' ), '', post_permalink( $post_id ) ) );
       
  1150 					if ( $meta[ 'blog_id' ] == $blog_id  && ( ( $all == true && !$meta[ 'post' ] ) || $meta[ 'post' ] == $post_id) ) {
       
  1151 						if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post change: deleting post cache files for {$meta[ 'uri' ]}: $content_pathname", 4 );
   828 						@unlink($meta_pathname);
  1152 						@unlink($meta_pathname);
   829 						@unlink($content_pathname);
  1153 						@unlink($content_pathname);
   830 						@wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html');
  1154 						if ( $super_cache_enabled == true ) {
   831 						@wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html.gz');
  1155 							@prune_super_cache( $dir . $permalink, true, true );
       
  1156 							do_action( 'gc_cache', 'rebuild', $permalink );
       
  1157 						}
   832 					}
  1158 					}
   833 				} elseif ($meta[ 'blog_id' ] == $blog_id) {
  1159 				} elseif ($meta[ 'blog_id' ] == $blog_id) {
       
  1160 					if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post change: deleting cache files for {$meta[ 'uri' ]}: $content_pathname", 4 );
   834 					@unlink($meta_pathname);
  1161 					@unlink($meta_pathname);
   835 					@unlink($content_pathname);
  1162 					@unlink($content_pathname);
   836 					@wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html');
  1163 					if ( $super_cache_enabled == true ) {
   837 					@wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html.gz');
  1164 						$files_to_check = get_all_supercache_filenames( $dir . $meta[ 'uri' ] );
       
  1165 						foreach( $files_to_check as $cache_file ) {
       
  1166 							@wp_cache_rebuild_or_delete( $dir . trailingslashit( $meta[ 'uri' ] ) . $cache_file );
       
  1167 						}
       
  1168 						do_action( 'gc_cache', 'rebuild', trailingslashit( $meta[ 'uri' ] ) );
       
  1169 					}
   838 				}
  1170 				}
   839 
  1171 
   840 			}
  1172 			}
   841 		}
  1173 		}
   842 		closedir($handle);
  1174 		closedir($handle);
   860 	if (isset( $_GET[ 'p' ] ) && $_GET['p'] > 0) return $_GET['p'];
  1192 	if (isset( $_GET[ 'p' ] ) && $_GET['p'] > 0) return $_GET['p'];
   861 	if (isset( $_POST[ 'p' ] ) && $_POST['p'] > 0) return $_POST['p'];
  1193 	if (isset( $_POST[ 'p' ] ) && $_POST['p'] > 0) return $_POST['p'];
   862 	return 0;
  1194 	return 0;
   863 }
  1195 }
   864 
  1196 
       
  1197 function maybe_stop_gc( $flag ) {
       
  1198 
       
  1199 	if ( @file_exists( $flag ) ) {
       
  1200 		if ( time() - filemtime( $flag ) > 3600 ) {
       
  1201 			@unlink( $flag );
       
  1202 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "maybe_stop_gc: GC flag found but deleted because it's older than 3600 seconds.", 5 );
       
  1203 			return false;
       
  1204 		} else {
       
  1205 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'maybe_stop_gc: GC flag found. GC cancelled.', 5 );
       
  1206 			return true;
       
  1207 		}
       
  1208 	} else {
       
  1209 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'maybe_stop_gc: GC flag not found. GC will go ahead..', 5 );
       
  1210 		return false;
       
  1211 	}
       
  1212 }
       
  1213 function get_gc_flag() {
       
  1214 	global $cache_path;
       
  1215 	return $cache_path . strtolower( preg_replace( '!/:.*$!', '', str_replace( 'http://', '', str_replace( 'https://', '', get_option( 'home' ) ) ) ) ) . "_wp_cache_gc.txt";
       
  1216 }
       
  1217 
   865 function wp_cache_gc_cron() {
  1218 function wp_cache_gc_cron() {
   866 	global $file_prefix, $cache_max_time;
  1219 	global $file_prefix, $cache_max_time, $cache_gc_email_me, $cache_time_interval;
       
  1220 
       
  1221 	$msg = '';
       
  1222 	if ( $cache_max_time == 0 ) {
       
  1223 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Cache garbage collection disabled because cache expiry time is zero.', 5 );
       
  1224 		return false;
       
  1225 	}
       
  1226 
       
  1227 	$gc_flag = get_gc_flag();
       
  1228 	if ( maybe_stop_gc( $gc_flag ) ) {
       
  1229 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'GC flag found. GC cancelled.', 5 );
       
  1230 		return false;
       
  1231 	}
       
  1232 
       
  1233 	update_option( 'wpsupercache_gc_time', time() ); 
       
  1234 	if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_gc_cron: Set GC Flag. ($gc_flag)", 5 );
       
  1235 	$fp = @fopen( $gc_flag, 'w' );
       
  1236 	@fclose( $fp );
       
  1237 
   867 	if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Cache garbage collection.', 5 );
  1238 	if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Cache garbage collection.', 5 );
   868 
  1239 
   869 	if( !isset( $cache_max_time ) )
  1240 	if( !isset( $cache_max_time ) )
   870 		$cache_max_time = 600;
  1241 		$cache_max_time = 600;
   871 
  1242 
   872 	$start = time();
  1243 	$start = time();
   873 	if( !wp_cache_phase2_clean_expired($file_prefix ) ) {
  1244 	$num = 0;
       
  1245 	if( false === ( $num = wp_cache_phase2_clean_expired( $file_prefix ) ) ) {
   874 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Cache Expiry cron job failed. Probably mutex locked.', 1 );
  1246 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Cache Expiry cron job failed. Probably mutex locked.', 1 );
   875 		update_option( 'wpsupercache_gc_time', time() - ( $cache_max_time - 10 ) ); // if GC failed then run it again in one minute
  1247 		update_option( 'wpsupercache_gc_time', time() - ( $cache_time_interval - 10 ) ); // if GC failed then run it again in one minute
   876 	}
  1248 		$msg .= __( 'Cache expiry cron job failed. Job will run again in 10 seconds.', 'wp-super-cache' ) . "\n";
   877 	if( time() - $start > 30 )
  1249 	}
       
  1250 	if( time() - $start > 30 ) {
   878 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Cache Expiry cron job took more than 30 seconds to execute.\nYou should reduce the Expiry Time in the WP Super Cache admin page\nas you probably have more cache files than your server can handle efficiently.", 1 );
  1251 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Cache Expiry cron job took more than 30 seconds to execute.\nYou should reduce the Expiry Time in the WP Super Cache admin page\nas you probably have more cache files than your server can handle efficiently.", 1 );
       
  1252 		$msg .= __( 'Cache expiry cron job took more than 30 seconds. You should probably run the garbage collector more often.', 'wp-super-cache' ) . "\n";
       
  1253 	}
       
  1254 
       
  1255 	if ( $cache_gc_email_me ) {
       
  1256 		if ( $msg != '' )
       
  1257 			$msg = "The following warnings were generated by the WP Super Cache Garbage Collector:\n" . $msg;
       
  1258 
       
  1259 		$msg = "Hi,\n\nThe WP Super Cache Garbage Collector has now run, deleting " . (int)$num . " files and directories.\nIf you want to switch off these emails please see the WP Super Cache Advanced Settings\npage on your blog.\n\n{$msg}\nRegards,\nThe Garbage Collector.";
       
  1260 
       
  1261 		wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] WP Super Cache GC Report', 'wp-super-cache' ), site_url() ), $msg );
       
  1262 	}
       
  1263 	@unlink( $gc_flag );
       
  1264 	if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'GC completed. GC flag deleted.', 5 );
       
  1265 	schedule_wp_gc( 1 );
       
  1266 }
       
  1267 
       
  1268 function schedule_wp_gc( $forced = 0 ) {
       
  1269 	global $cache_schedule_type, $cache_max_time, $cache_time_interval, $cache_scheduled_time, $cache_schedule_interval;
       
  1270 
       
  1271 	if ( false == isset( $cache_time_interval ) )
       
  1272 		$cache_time_interval = 3600;
       
  1273 
       
  1274 	if ( false == isset( $cache_schedule_type ) ) {
       
  1275 		$cache_schedule_type = 'interval';
       
  1276 		$cache_schedule_interval = $cache_max_time;
       
  1277 	}
       
  1278 	if ( $cache_schedule_type == 'interval' ) {
       
  1279 		if ( !isset( $cache_max_time ) )
       
  1280 			$cache_max_time = 600;
       
  1281 		if ( $cache_max_time == 0 )
       
  1282 			return false;
       
  1283 		$last_gc = get_option( "wpsupercache_gc_time" );
       
  1284 
       
  1285 		if ( !$last_gc ) {
       
  1286 			update_option( 'wpsupercache_gc_time', time() );
       
  1287 			$last_gc = get_option( "wpsupercache_gc_time" );
       
  1288 		}
       
  1289 		if ( $forced || ( $last_gc < ( time() - 60 ) ) ) { // Allow up to 60 seconds for the previous job to run
       
  1290 			global $wp_cache_shutdown_gc;
       
  1291 			if ( !isset( $wp_cache_shutdown_gc ) || $wp_cache_shutdown_gc == 0 ) {
       
  1292 				if ( !($t = wp_next_scheduled( 'wp_cache_gc' ) ) ) {
       
  1293 					wp_clear_scheduled_hook( 'wp_cache_gc' );
       
  1294 					wp_schedule_single_event( time() + $cache_time_interval, 'wp_cache_gc' );
       
  1295 					if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'scheduled wp_cache_gc for 10 seconds time.', 5 );
       
  1296 				}
       
  1297 			} else {
       
  1298 				global $time_to_gc_cache;
       
  1299 				$time_to_gc_cache = 1; // tell the "shutdown gc" to run!
       
  1300 			}
       
  1301 		}
       
  1302 	} elseif ( $cache_schedule_type == 'time' && !wp_next_scheduled( 'wp_cache_gc' ) ) {
       
  1303 		wp_schedule_event( strtotime( $cache_scheduled_time ), $cache_schedule_interval, 'wp_cache_gc' );
       
  1304 	}
       
  1305 	return true;
       
  1306 }
       
  1307 
       
  1308 function wp_cache_gc_watcher() {
       
  1309 	if ( false == wp_next_scheduled( 'wp_cache_gc' ) ) {
       
  1310 		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'GC Watcher: scheduled new gc cron.', 5 );
       
  1311 		schedule_wp_gc();
       
  1312 	}
   879 }
  1313 }
   880 
  1314 
   881 ?>
  1315 ?>