array( 'id' => 'front_page', 'name' => 'Front page', 'max_age' => 300, // 5 min 's_maxage' => 150 // 2 min 30 sec ), 'singles' => array( 'id' => 'singles', 'name' => 'Posts', 'max_age' => 600, // 10 min 's_maxage' => 60 // 1 min ), 'pages' => array( 'id' => 'pages', 'name' => 'Pages', 'max_age' => 1200, // 20 min 's_maxage' => 300 // 5 min ), 'home' => array( 'id' => 'home', 'name' => 'Main index', 'max_age' => 180, // 3 min 's_maxage' => 45, // 45 sec 'paged' => 5 // 5 sec ), 'categories' => array( 'id' => 'categories', 'name' => 'Categories', 'max_age' => 900, // 15 min 's_maxage' => 300, // 5 min 'paged' => 8 // 8 sec ), 'tags' => array( 'id' => 'tags', 'name' => 'Tags', 'max_age' => 900, // 15 min 's_maxage' => 300, // 5 min 'paged' => 10 // 8 sec ), 'authors' => array( 'id' => 'authors', 'name' => 'Authors', 'max_age' => 1800, // 30 min 's_maxage' => 600, // 10 min 'paged' => 10 // 10 sec ), 'dates' => array( 'id' => 'dates', 'name' => 'Dated archives', 'max_age' => 10800, // 3 hours 's_maxage' => 2700 // 45 min ), 'feeds' => array( 'id' => 'feeds', 'name' => 'Feeds', 'max_age' => 5400, // 1 hours 30 min 's_maxage' => 600 // 10 min ), 'attachment' => array( 'id' => 'attachment', 'name' => 'Attachments', 'max_age' => 10800, // 3 hours 's_maxage' => 2700 // 45 min ), 'search' => array( 'id' => 'search', 'name' => 'Search results', 'max_age' => 1800, // 30 min 's_maxage' => 600 // 10 min ), 'notfound' => array( 'id' => 'notfound', 'name' => '404 Not Found', 'max_age' => 900, // 15 min 's_maxage' => 300 // 5 min ) ); function cache_control_stale_factorer( $factor, $max_age ) { if ( is_paged() && is_int( $factor ) && $factor > 0 ) { $multiplier = get_query_var( 'paged' ) - 1; if ( $multiplier > 0 ) { $factored_max_age = $factor * $multiplier; if ( $factored_max_age >= ( $max_age * 10 ) ) return $max_age * 10; return $factored_max_age; } } return 0; } function cache_control_is_future_now_maxtime( $max_time_future ) { // trusting the database to cache this query $future_post = new WP_Query( array( 'post_status' => 'future', 'posts_per_page' => 1, 'orderby' => 'date', 'order' => 'ASC' ) ); if ( $future_post->have_posts() ) { $local_nowtime = intval( current_time( 'timestamp', 0 ) ); while ( $future_post->have_posts() ) { $future_post->the_post(); $local_futuretime = get_the_time( 'U' ); if ( ( $local_nowtime + $max_time_future ) > $local_futuretime ) $max_time_future = $local_futuretime - $local_nowtime + rand( 2, 32 ); } wp_reset_postdata(); } return $max_time_future; } function cache_control_build_directive_header( $max_age, $s_maxage ) { $directive = ""; if ( !empty( $max_age ) && is_int( $max_age ) && $max_age > 0) $directive = "max-age=$max_age"; if ( !empty( $s_maxage ) && is_int( $s_maxage ) && $s_maxage > 0 && $s_maxage != $max_age ) { if ( !$directive != "" ) $directive = "public"; $directive = "$directive, s-maxage=$s_maxage"; } if ( $directive != "" ) return $directive; return "no-cache, no-store, must-revalidate"; } function cache_control_build_directive_from_option( $option_name ) { global $cache_control_options; $option = $cache_control_options[$option_name]; $max_age = intval( get_option( 'cache_control_' . $option['id'] . '_max_age', $option['max_age'] ) ); $s_maxage = intval( get_option( 'cache_control_' . $option['id'] . '_s_maxage', $option['s_maxage'] ) ); // dynamically shorten caching time when a scheduled post is imminent if ( $option_name != 'attachment' && $option_name != 'dates' && $option_name != 'pages' && $option_name != 'singles' && $option_name != 'notfound' ) { $max_age = cache_control_is_future_now_maxtime( $max_age ); $s_maxage = cache_control_is_future_now_maxtime( $s_maxage ); } if ( is_paged() && isset( $option['paged'] ) ) { $page_factor = intval( get_option( 'cache_control_' . $option['id'] . '_paged', $option['paged'] ) ); $max_age += cache_control_stale_factorer( $page_factor, $max_age ); $s_maxage += cache_control_stale_factorer( $page_factor, $s_maxage ); } return cache_control_build_directive_header( $max_age, $s_maxage ); } function cache_control_select_directive() { if ( is_preview() || is_user_logged_in() || is_trackback() || is_admin() ) return cache_control_build_directive_header( false, false ); elseif ( is_feed() ) return cache_control_build_directive_from_option( 'feeds' ); elseif ( is_front_page() && !is_paged() ) return cache_control_build_directive_from_option( 'front_page' ); elseif ( is_single() ) return cache_control_build_directive_from_option( 'singles' ); elseif ( is_page() ) return cache_control_build_directive_from_option( 'pages' ); elseif ( is_home() ) return cache_control_build_directive_from_option( 'home' ); elseif ( is_category() ) return cache_control_build_directive_from_option( 'categories' ); elseif ( is_tag() ) return cache_control_build_directive_from_option( 'tags' ); elseif ( is_author() ) return cache_control_build_directive_from_option( 'authors' ); elseif ( is_attachment() ) return cache_control_build_directive_from_option( 'attachment' ); elseif ( is_search() ) return cache_control_build_directive_from_option( 'search' ); elseif ( is_404() ) return cache_control_build_directive_from_option( 'notfound' ); elseif ( is_date() ) { if ( ( is_year() && strcmp(get_the_time('Y'), date('Y')) < 0 ) || ( is_month() && strcmp(get_the_time('Y-m'), date('Y-m')) < 0 ) || ( ( is_day() || is_time() ) && strcmp(get_the_time('Y-m-d'), date('Y-m-d')) < 0 ) ) { return cache_control_build_directive_from_option( 'dates' ); } else return cache_control_build_directive_from_option( 'home' ); } return cache_control_build_directive_header( false, false ); } function cache_control_send_http_header( $directives ) { if ( !empty( $directives ) ) header ( "Cache-Control: $directives" ); } function cache_control_send_headers() { cache_control_send_http_header( cache_control_select_directive() ); } add_action( 'template_redirect', 'cache_control_send_headers' ); if ( is_admin() ) { require_once( dirname(__file__) . '/admin.php' ); } ?>