wp_cache_shutdown_callback()WPSCache 1.0

Возвращает

null. Ничего.

Использование

wp_cache_shutdown_callback();

Код wp_cache_shutdown_callback() WPSCache 1.8

<?php
function wp_cache_shutdown_callback() {
	global $cache_max_time, $meta_file, $new_cache, $wp_cache_meta, $known_headers, $blog_id, $wp_cache_gzip_encoding, $supercacheonly, $blog_cache_dir;
	global $wp_cache_request_uri, $wp_cache_key, $cache_enabled, $wp_cache_blog_charset, $wp_cache_not_logged_in;
	global $WPSC_HTTP_HOST, $wp_super_cache_query;

	if ( ! function_exists( 'wpsc_init' ) ) {
		/*
		 * If a server has multiple networks the plugin may not have been activated
		 * on all of them. Give feeds on those blogs a short TTL.
		 * ref: https://wordpress.org/support/topic/fatal-error-while-updating-post-or-publishing-new-one/
		 */
		$wpsc_feed_ttl = 1;
		wp_cache_debug( 'wp_cache_shutdown_callback: Plugin not loaded. Setting feed ttl to 60 seconds.' );
	}

	if ( false == $new_cache ) {
		wp_cache_debug( 'wp_cache_shutdown_callback: No cache file created. Returning.' );
		return false;
	}

	$wp_cache_meta['uri']     = $WPSC_HTTP_HOST . preg_replace( '/[ <>\'\"\r\n\t\(\)]/', '', $wp_cache_request_uri ); // To avoid XSS attacks
	$wp_cache_meta['blog_id'] = $blog_id;
	$wp_cache_meta['post']    = wp_cache_post_id();
	$wp_cache_meta['key']     = $wp_cache_key;
	$wp_cache_meta            = apply_filters( 'wp_cache_meta', $wp_cache_meta );

	$response = wp_cache_get_response_headers();
	foreach ( $response as $key => $value ) {
		$wp_cache_meta['headers'][ $key ] = "$key: $value";
	}

	wp_cache_debug( 'wp_cache_shutdown_callback: collecting meta data.', 2 );

	if ( ! isset( $response['Last-Modified'] ) ) {
		$value = gmdate( 'D, d M Y H:i:s' ) . ' GMT';
		/*
		 Dont send this the first time */
		/* @header('Last-Modified: ' . $value); */
		$wp_cache_meta['headers']['Last-Modified'] = "Last-Modified: $value";
	}
	$is_feed = false;
	if ( ! isset( $response['Content-Type'] ) && ! isset( $response['Content-type'] ) ) {
		// On some systems, headers set by PHP can't be fetched from
		// the output buffer. This is a last ditch effort to set the
		// correct Content-Type header for feeds, if we didn't see
		// it in the response headers already. -- dougal
		if ( isset( $wp_super_cache_query['is_feed'] ) ) {
			if ( isset( $wp_super_cache_query['is_sitemap'] ) ) {
				$type  = 'sitemap';
				$value = 'text/xml';
			} else {
				$type = get_query_var( 'feed' );
				$type = str_replace( '/', '', $type );
				switch ( $type ) {
					case 'atom':
						$value = 'application/atom+xml';
						break;
					case 'rdf':
						$value = 'application/rdf+xml';
						break;
					case 'rss':
					case 'rss2':
					default:
						$value = 'application/rss+xml';
				}
			}
			$is_feed = true;

			if ( isset( $wpsc_feed_ttl ) && $wpsc_feed_ttl == 1 ) {
				$wp_cache_meta['ttl'] = 60;
			}
			$is_feed = true;

			wp_cache_debug( "wp_cache_shutdown_callback: feed is type: $type - $value" );
		} elseif ( isset( $wp_super_cache_query['is_rest'] ) ) { // json
			$value = 'application/json';
		} else { // not a feed
			$value = get_option( 'html_type' );
			if ( $value == '' ) {
				$value = 'text/html';
			}
		}
		$value .= '; charset="' . $wp_cache_blog_charset . '"';

		wp_cache_debug( "Sending 'Content-Type: $value' header.", 2 );
		@header( "Content-Type: $value" );
		$wp_cache_meta['headers']['Content-Type'] = "Content-Type: $value";
	}

	if ( $cache_enabled && ! $supercacheonly && $new_cache ) {
		if ( ! isset( $wp_cache_meta['dynamic'] ) && $wp_cache_gzip_encoding && ! in_array( 'Content-Encoding: ' . $wp_cache_gzip_encoding, $wp_cache_meta['headers'] ) ) {
			wp_cache_debug( 'Sending gzip headers.', 2 );
			$wp_cache_meta['headers']['Content-Encoding'] = 'Content-Encoding: ' . $wp_cache_gzip_encoding;
			if ( defined( 'WPSC_VARY_HEADER' ) ) {
				if ( WPSC_VARY_HEADER != '' ) {
					$vary_header = WPSC_VARY_HEADER;
				} else {
					$vary_header = '';
				}
			} else {
				$vary_header = 'Accept-Encoding, Cookie';
			}
			if ( $vary_header ) {
				$wp_cache_meta['headers']['Vary'] = 'Vary: ' . $vary_header;
			}
		}

		$serial = '<?php die(); ?>' . json_encode( $wp_cache_meta );
		$dir    = get_current_url_supercache_dir();
		if ( @is_dir( $dir ) == false ) {
			@wp_mkdir_p( $dir );
		}

		if ( wp_cache_writers_entry() ) {
			wp_cache_debug( "Writing meta file: {$dir}meta-{$meta_file}", 2 );

			$tmp_meta_filename   = $dir . uniqid( mt_rand(), true ) . '.tmp';
			$final_meta_filename = $dir . 'meta-' . $meta_file;
			$fr                  = @fopen( $tmp_meta_filename, 'w' );
			if ( $fr ) {
				fputs( $fr, $serial );
				fclose( $fr );
				@chmod( $tmp_meta_filename, 0666 & ~umask() );
				if ( ! @rename( $tmp_meta_filename, $final_meta_filename ) ) {
					@unlink( $dir . $final_meta_filename );
					@rename( $tmp_meta_filename, $final_meta_filename );
				}
			} else {
				wp_cache_debug( "Problem writing meta file: {$final_meta_filename}" );
			}
			wp_cache_writers_exit();

			// record locations of archive feeds to be updated when the site is updated.
			// Only record a maximum of 50 feeds to avoid bloating database.
			if ( ( isset( $wp_super_cache_query['is_feed'] ) || $is_feed ) && ! isset( $wp_super_cache_query['is_single'] ) ) {
				$wpsc_feed_list = (array) get_option( 'wpsc_feed_list' );
				if ( count( $wpsc_feed_list ) <= 50 ) {
					$wpsc_feed_list[] = $dir . $meta_file;
					update_option( 'wpsc_feed_list', $wpsc_feed_list );
				}
			}
		}
	} else {
		wp_cache_debug( "Did not write meta file: meta-{$meta_file}\nsupercacheonly: $supercacheonly\nwp_cache_not_logged_in: $wp_cache_not_logged_in\nnew_cache:$new_cache" );
	}
	global $time_to_gc_cache;
	if ( isset( $time_to_gc_cache ) && $time_to_gc_cache == 1 ) {
		wp_cache_debug( 'Executing wp_cache_gc action.', 3 );
		do_action( 'wp_cache_gc' );
	}
}

function wp_cache_no_postid( $id ) {
	return wp_cache_post_change( wp_cache_post_id() );
}

function wp_cache_get_postid_from_comment( $comment_id, $status = 'NA' ) {
	global $super_cache_enabled, $wp_cache_request_uri;

	if ( defined( 'DONOTDELETECACHE' ) ) {
		return;
	}

	// Check is it "Empty Spam" or "Empty Trash"
	if ( isset( $GLOBALS['pagenow'] ) && $GLOBALS['pagenow'] === 'edit-comments.php' &&
		( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) )
	) {
		wp_cache_debug( "Delete all SPAM or Trash comments. Don't delete any cache files.", 4 );
		define( 'DONOTDELETECACHE', 1 );
		return;
	}

	$comment = get_comment( $comment_id, ARRAY_A );
	if ( $status != 'NA' ) {
		$comment['old_comment_approved'] = $comment['comment_approved'];
		$comment['comment_approved']     = $status;
	}

	if ( ( $status == 'trash' || $status == 'spam' ) && $comment['old_comment_approved'] != 1 ) {
		// don't modify cache if moderated comments are trashed or spammed
		wp_cache_debug( "Moderated comment deleted or spammed. Don't delete any cache files.", 4 );
		define( 'DONOTDELETECACHE', 1 );
		return wp_cache_post_id();
	}
	$postid = isset( $comment['comment_post_ID'] ) ? (int) $comment['comment_post_ID'] : 0;
	// Do nothing if comment is not moderated
	// http://ocaoimh.ie/2006/12/05/caching-wordpress-with-wp-cache-in-a-spam-filled-world
	if ( ! preg_match( '/wp-admin\//', $wp_cache_request_uri ) ) {
		if ( $comment['comment_approved'] == 'delete' && ( isset( $comment['old_comment_approved'] ) && $comment['old_comment_approved'] == 0 ) ) { // do nothing if moderated comments are deleted
			wp_cache_debug( "Moderated comment deleted. Don't delete any cache files.", 4 );
			define( 'DONOTDELETECACHE', 1 );
			return $postid;
		} elseif ( $comment['comment_approved'] == 'spam' ) {
			wp_cache_debug( "Spam comment. Don't delete any cache files.", 4 );
			define( 'DONOTDELETECACHE', 1 );
			return $postid;
		} elseif ( $comment['comment_approved'] == '0' ) {
			if ( $comment['comment_type'] == '' ) {
				wp_cache_debug( "Moderated comment. Don't delete supercache file until comment approved.", 4 );
				$super_cache_enabled = 0; // don't remove the super cache static file until comment is approved
				define( 'DONOTDELETECACHE', 1 );
			} else {
				wp_cache_debug( 'Moderated ping or trackback. Not deleting cache files..', 4 );
				define( 'DONOTDELETECACHE', 1 );
				return $postid;
			}
		}
	}
	// We must check it up again due to WP bugs calling two different actions
	// for delete, for example both wp_set_comment_status and delete_comment
	// are called when deleting a comment
	if ( $postid > 0 ) {
		wp_cache_debug( "Post $postid changed. Update cache.", 4 );
		return wp_cache_post_change( $postid );
	} else {
		wp_cache_debug( 'Unknown post changed. Update cache.', 4 );
		return wp_cache_post_change( wp_cache_post_id() );
	}
}

/* Used by wp_update_nav_menu action to clear current blog's cache files when navigation menu is modified */
function wp_cache_clear_cache_on_menu() {
	global $wpdb;
	wp_cache_clear_cache( $wpdb->blogid );
}

/* Clear out the cache directory. */
function wp_cache_clear_cache( $blog_id = 0 ) {
	global $cache_path;

	if ( $blog_id == 0 ) {
		wp_cache_debug( 'Clearing all cached files in wp_cache_clear_cache()', 4 );
		prune_super_cache( $cache_path . 'supercache/', true );
		prune_super_cache( $cache_path, true );
	} else {
		wp_cache_debug( "Clearing all cached files for blog $blog_id in wp_cache_clear_cache()", 4 );
		prune_super_cache( get_supercache_dir( $blog_id ), true );
		prune_super_cache( $cache_path . 'blogs/', true );
	}

	do_action( 'wp_cache_cleared' );
}

function wpsc_delete_post_archives( $post ) {
	$post = @get_post( $post );
	if ( ! is_object( $post ) ) {
		return;
	}

	// Taxonomies - categories, tags, custom taxonomies
	foreach ( get_object_taxonomies( $post, 'objects' ) as $taxonomy ) {

		if ( ! $taxonomy->public || ! $taxonomy->rewrite ) {
			continue;
		}

		$terms = get_the_terms( $post->ID, $taxonomy->name );
		if ( empty( $terms ) ) {
			continue;
		}

		foreach ( $terms as $term ) {

			$term_url = get_term_link( $term, $taxonomy->name );
			if ( is_wp_error( $term_url ) ) {
				continue;
			}

			wpsc_delete_url_cache( $term_url );
			wp_cache_debug( 'wpsc_delete_post_archives: deleting cache of taxonomies: ' . $term_url );
		}
	}

	// Post type archive page
	if ( $post->post_type === 'page' ) {
		$archive_url = false;
	} elseif ( $post->post_type === 'post' && get_option( 'show_on_front' ) !== 'posts' && ! get_option( 'page_for_posts' ) ) {
		$archive_url = false;
	} else {
		$archive_url = get_post_type_archive_link( $post->post_type );
	}

	if ( $archive_url ) {
		wpsc_delete_url_cache( $archive_url );
		wp_cache_debug( 'wpsc_delete_post_archives: deleting cache of post type archive: ' . $archive_url );
	}

	// Author archive page
	$author_url = get_author_posts_url( $post->post_author );
	if ( $author_url ) {
		wpsc_delete_url_cache( $author_url );
		wp_cache_debug( 'wpsc_delete_post_archives: deleting cache of author archive: ' . $author_url );
	}
}

function wpsc_delete_cats_tags( $post ) {
	if ( function_exists( '_deprecated_function' ) ) {
		_deprecated_function( __FUNCTION__, 'WP Super Cache 1.6.3', 'wpsc_delete_post_archives' );
	}

	$post       = get_post( $post );
	$categories = get_the_category( $post->ID );
	if ( $categories ) {
		$category_base = get_option( 'category_base' );
		if ( $category_base == '' ) {
			$category_base = '/category/';
		}
		$category_base = trailingslashit( $category_base ); // paranoid much?
		foreach ( $categories as $cat ) {
			prune_super_cache( get_supercache_dir() . $category_base . $cat->slug . '/', true );
			wp_cache_debug( 'wpsc_post_transition: deleting category: ' . get_supercache_dir() . $category_base . $cat->slug . '/' );
		}
	}
	$posttags = get_the_tags( $post->ID );
	if ( $posttags ) {
		$tag_base = get_option( 'tag_base' );
		if ( $tag_base == '' ) {
			$tag_base = '/tag/';
		}
		$tag_base = trailingslashit( str_replace( '..', '', $tag_base ) ); // maybe!
		foreach ( $posttags as $tag ) {
			prune_super_cache( get_supercache_dir() . $tag_base . $tag->slug . '/', true );
			wp_cache_debug( 'wpsc_post_transition: deleting tag: ' . get_supercache_dir() . $tag_base . $tag->slug . '/' );
		}
	}
}

function wpsc_post_transition( $new_status, $old_status, $post ) {

	$ptype = is_object( $post ) ? get_post_type_object( $post->post_type ) : null;
	if ( empty( $ptype ) || ! $ptype->public ) {
		return;
	}

	if ( ( $old_status === 'private' || $old_status === 'publish' ) && $new_status !== 'publish' ) { // post unpublished
		if ( ! function_exists( 'get_sample_permalink' ) ) {
			require_once ABSPATH . 'wp-admin/includes/post.php';
		}
		list( $permalink, $post_name ) = get_sample_permalink( $post );
		$post_url                      = str_replace( array( '%postname%', '%pagename%' ), $post->post_name, $permalink );
	} elseif ( $old_status !== 'publish' && $new_status === 'publish' ) { // post published
		wp_cache_post_edit( $post->ID );
		return;
	}

	if ( ! empty( $post_url ) ) {
		wp_cache_debug( 'wpsc_post_transition: deleting cache of post: ' . $post_url );
		wpsc_delete_url_cache( $post_url );
		wpsc_delete_post_archives( $post );
	}
}

/* check if we want to clear out all cached files on post updates, otherwise call standard wp_cache_post_change() */
function wp_cache_post_edit( $post_id ) {
	global $wp_cache_clear_on_post_edit, $cache_path, $blog_cache_dir;
	static $last_post_edited = -1;

	if ( $post_id == $last_post_edited ) {
		$action = current_filter();
		wp_cache_debug( "wp_cache_post_edit(${action}): Already processed post $post_id.", 4 );
		return $post_id;
	}

	$post = get_post( $post_id );
	if ( ! is_object( $post ) || 'auto-draft' === $post->post_status ) {
		return $post_id;
	}

	// Some users are inexplicibly seeing this error on scheduled posts.
	// define this constant to disable the post status check.
	if ( ! defined( 'WPSCFORCEUPDATE' ) && ! in_array( get_post_status( $post ), array( 'publish', 'private' ), true ) ) {
		wp_cache_debug( 'wp_cache_post_edit: draft post, not deleting any cache files. status: ' . $post->post_status, 4 );
		return $post_id;
	}

	// we want to process the post again just in case it becomes published before the second time this function is called.
	$last_post_edited = $post_id;
	if ( $wp_cache_clear_on_post_edit ) {
		wp_cache_debug( "wp_cache_post_edit: Clearing cache $blog_cache_dir and {$cache_path}supercache/ on post edit per config.", 2 );
		prune_super_cache( $blog_cache_dir, true );
		prune_super_cache( get_supercache_dir(), true );
	} else {
		$action = current_filter();
		wp_cache_debug( "wp_cache_post_edit: Clearing cache for post $post_id on ${action}", 2 );
		wp_cache_post_change( $post_id );
		wpsc_delete_post_archives( $post_id ); // delete related archive pages.
	}
}

function wp_cache_post_id_gc( $post_id, $all = 'all' ) {

	$post_id = intval( $post_id );
	if ( $post_id == 0 ) {
		return true;
	}

	$permalink = trailingslashit( str_replace( get_option( 'home' ), '', get_permalink( $post_id ) ) );
	if ( false !== strpos( $permalink, '?' ) ) {
		wp_cache_debug( 'wp_cache_post_id_gc: NOT CLEARING CACHE. Permalink has a "?". ' . $permalink );
		return false;
	}
	$dir = get_current_url_supercache_dir( $post_id );
	wp_cache_debug( "wp_cache_post_id_gc post_id: $post_id " . get_permalink( $post_id ) . " clearing cache in $dir.", 4 );
	if ( $all ) {
		prune_super_cache( $dir, true, true );
		do_action( 'gc_cache', 'prune', $permalink );
		@rmdir( $dir );
		$supercache_home = get_supercache_dir();
		wp_cache_debug( "wp_cache_post_id_gc clearing cache in {$supercache_home}page/." );
		prune_super_cache( $supercache_home . 'page/', true );
		do_action( 'gc_cache', 'prune', 'page/' );
	} else {
		wp_cache_debug( "wp_cache_post_id_gc clearing cached index files in $dir.", 4 );
		prune_super_cache( $dir, true, true );
		do_action( 'gc_cache', 'prune', $permalink );
	}
	return true;
}

function wp_cache_post_change( $post_id ) {
	global $file_prefix, $cache_path, $blog_id, $super_cache_enabled, $blog_cache_dir, $wp_cache_refresh_single_only;
	static $last_processed = -1;

	if ( $post_id == $last_processed ) {
		$action = current_filter();
		wp_cache_debug( "wp_cache_post_change(${action}): Already processed post $post_id.", 4 );
		return $post_id;
	}

	$post  = get_post( $post_id );
	$ptype = is_object( $post ) ? get_post_type_object( $post->post_type ) : null;
	if ( empty( $ptype ) || ! $ptype->public ) {
		return $post_id;
	}

	// Some users are inexplicibly seeing this error on scheduled posts.
	// define this constant to disable the post status check.
	if ( ! defined( 'WPSCFORCEUPDATE' ) && ! in_array( get_post_status( $post ), array( 'publish', 'private' ), true ) ) {
		wp_cache_debug( 'wp_cache_post_change: draft post, not deleting any cache files.', 4 );
		return $post_id;
	}
	$last_processed = $post_id;

	if ( ! wp_cache_writers_entry() ) {
		return $post_id;
	}

	if (
		isset( $wp_cache_refresh_single_only ) &&
		$wp_cache_refresh_single_only &&
		(
			isset( $_SERVER['HTTP_REFERER'] ) &&
			strpos( $_SERVER['HTTP_REFERER'], 'edit-comments.php' ) ||
			strpos( $_SERVER['REQUEST_URI'], 'wp-comments-post.php' )
		)
	) {
		if ( defined( 'DONOTDELETECACHE' ) ) {
			wp_cache_debug( "wp_cache_post_change: comment detected and it's moderated or spam. Not deleting cached files.", 4 );
			return $post_id;
		} else {
			wp_cache_debug( 'wp_cache_post_change: comment detected. only deleting post page.', 4 );
			$all = false;
		}
	} else {
		$all = true;
	}

	$all_backup = $all;
	$all        = apply_filters( 'wpsc_delete_related_pages_on_edit', $all ); // return 0 to disable deleting homepage and other pages.
	if ( $all != $all_backup ) {
		wp_cache_debug( 'wp_cache_post_change: $all changed by wpsc_delete_related_pages_on_edit filter: ' . intval( $all ) );
	}

	// Delete supercache files whenever a post change event occurs, even if supercache is currently disabled.
	$dir = get_supercache_dir();
	// make sure the front page has a rebuild file
	if ( false == wp_cache_post_id_gc( $post_id, $all ) ) {
		wp_cache_debug( 'wp_cache_post_change: not deleting any cache files as GC of post returned false' );
		wp_cache_writers_exit();
		return false;
	}
	if ( $all == true ) {
		wp_cache_debug( 'Post change: supercache enabled: deleting cache files in ' . $dir );
		wpsc_rebuild_files( $dir );
		do_action( 'gc_cache', 'prune', 'homepage' );
		if ( get_option( 'show_on_front' ) == 'page' ) {
			wp_cache_debug( 'Post change: deleting page_on_front and page_for_posts pages.', 4 );
			wp_cache_debug( 'Post change: page_on_front ' . get_option( 'page_on_front' ), 4 );
			/**
			 * It's possible that page_for_posts is zero.
			 * Quick fix to reduce issues in debugging.
			 */
			wp_cache_debug( 'Post change: page_for_posts ' . get_option( 'page_for_posts' ), 4 );
			if ( get_option( 'page_for_posts' ) ) {
				$permalink = trailingslashit( str_replace( get_option( 'home' ), '', get_permalink( get_option( 'page_for_posts' ) ) ) );
				wp_cache_debug( 'Post change: Deleting files in: ' . str_replace( '//', '/', $dir . $permalink ) );
				wpsc_rebuild_files( $dir . $permalink );
				do_action( 'gc_cache', 'prune', $permalink );
			}
		}
	} else {
		wp_cache_debug( 'wp_cache_post_change: not deleting all pages.', 4 );
	}