/**
 * Sets headers on post object pages (posts, pages, attachments, custom
 * post types).
 *
 * In order to calculate the modified time, two time sources are used:
 *   1) the post object's modified time.
 *   2) the modified time of the most recent comment that is attached to the post object.
 * The most "recent" timestamp of the two is returned.
 */
function addh_set_headers_for_object($options)
{
    // Get current queried object.
    $post = get_queried_object();
    // Valid post types: post, page, attachment, public custom post types
    if (!is_object($post) || !isset($post->post_type) || !in_array(get_post_type($post), addh_get_supported_post_types_singular())) {
        return;
    }
    // Check for password protected posts
    if (post_password_required()) {
        return;
    }
    // Retrieve stored time of post object
    $post_mtime = $post->post_modified_gmt;
    $post_mtime_unix = strtotime($post_mtime);
    // Initially set the $mtime to the post mtime timestamp
    $mtime = $post_mtime_unix;
    // If there are comments attached to this post object, find the mtime of
    // the most recent comment.
    if (intval($post->comment_count) > 0) {
        // Retrieve the mtime of the most recent comment
        $comments = get_comments(array('status' => 'approve', 'orderby' => 'comment_date_gmt', 'number' => '1', 'post_id' => $post->ID));
        if (!empty($comments)) {
            $comment = $comments[0];
            $comment_mtime = $comment->comment_date_gmt;
            $comment_mtime_unix = strtotime($comment_mtime);
            // Compare the two mtimes and keep the most recent (higher) one.
            if ($comment_mtime_unix > $post_mtime_unix) {
                $mtime = $comment_mtime_unix;
            }
        }
    }
    addh_batch_generate_headers($post, $mtime, $options);
}
Esempio n. 2
0
/**
 * Sets headers on post object pages (posts, pages, attachments, custom
 * post types).
 *
 * In order to calculate the modified time, two time sources are used:
 *   1) the post object's modified time.
 *   2) the modified time of the most recent comment that is attached to the post object.
 * The most "recent" timestamp of the two is returned.
 */
function addh_set_headers_for_object($options)
{
    // Get current queried object.
    $post = get_queried_object();
    // Valid post types: post, page, attachment, public custom post types
    if (!is_object($post) || !isset($post->post_type) || !in_array(get_post_type($post), addh_get_supported_post_types_singular())) {
        return;
    }
    // Check for password protected posts
    if (post_password_required()) {
        return;
    }
    // Retrieve stored time of post object
    $post_mtime = $post->post_modified_gmt;
    $post_mtime_unix = strtotime($post_mtime);
    // Initially set the $mtime to the post mtime timestamp
    $mtime = $post_mtime_unix;
    // If there are comments attached to this post object, find the mtime of
    // the most recent comment.
    if (intval($post->comment_count) > 0) {
        // Retrieve the mtime of the most recent comment
        $comments = get_comments(array('status' => 'approve', 'orderby' => 'comment_date_gmt', 'number' => '1', 'post_id' => $post->ID));
        if (!empty($comments)) {
            $comment = $comments[0];
            $comment_mtime = $comment->comment_date_gmt;
            $comment_mtime_unix = strtotime($comment_mtime);
            // Compare the two mtimes and keep the most recent (higher) one.
            if ($comment_mtime_unix > $post_mtime_unix) {
                $mtime = $comment_mtime_unix;
            }
        }
    }
    // Check for old content (only if a threshold is set)
    if (absint($options['cache_old_content_threshold_seconds']) > 0) {
        // Find the time that determines whether a resource should be treated as 'old content'.
        // Basically this is the current GM time minus the 'old content threshold in seconds' as set in the option.
        $threshold_time_unix = absint(gmdate('U')) - absint($options['cache_old_content_threshold_seconds']);
        // Any content which has been last modified before that time should be treated as old
        // and the 'cache_old_content_max_age_seconds' option should be used as the caching
        // timeout, instead of 'cache_max_age_seconds'.
        // Here we check whether the mtime (last modified time) is older than the threshold time.
        if ($mtime < $threshold_time_unix) {
            // This content is old. Set the caching timeout to the caching timeout of old content.
            $options['cache_max_age_seconds'] = $options['cache_old_content_max_age_seconds'];
        }
    }
    addh_batch_generate_headers($post, $mtime, $options);
}