Jake Vanderwerf
2025-09-30 2143fc670fa245750ac7f774fcb38ea8cfbf7edb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
 
if (!defined('ABSPATH')) {
    exit;
}
 
/**
 * Cache Stuffs
 */
/**
 * Update cache timestamps when content is modified
 * This helps us reduce database requests and only update a content cache when necessary
 */
/**
 * @param string $type
 * @param object|null $item
 *
 * @return array
 */
function jvbUpdateCacheTimestamp(string $type, object|null $item = null):array
{
    $timestamps = jvbGetCache();
    $current_time = time();
 
    // Handle different types of updates
    switch ($type) {
        case 'term':
            // Get term and update its taxonomy timestamp
            $taxonomy = str_replace(BASE, '', $item->taxonomy);
            $timestamps[ $taxonomy ] = $current_time;
            JVBase\managers\CacheManager::invalidateGroup($taxonomy);
            JVBase\managers\CacheManager::invalidateGroup('terms');
            break;
 
        case 'post':
            // Update specific post type timestamp
            $post_type = str_replace(BASE, '', $item->postType);
            $timestamps[$post_type] = $current_time;
            JVBase\managers\CacheManager::invalidateGroup($post_type);
            break;
        default:
            $timestamps[$type] = $current_time;
            JVBase\managers\CacheManager::invalidateGroup($type);
            break;
    }
    update_option('jvb_cache', $timestamps);
    return $timestamps;
}
 
// Hook into WordPress content changes
add_action('save_post', function ($post_id, $post, $update) {
    if (jvbNoSaveIt($post_id, $post)) {
        return;
    }
    if (!in_array($post->post_type, jvbBasedFeedContent())) {
        return;
    }
    jvbUpdateCacheTimestamp('post', $post);
}, 10, 3);
 
add_action('edited_term', function ($term_id, $tt_id, $taxonomy) {
    jvbUpdateCacheTimestamp('term', get_term($term_id, $taxonomy));
}, 10, 3);
 
add_action('created_term', function ($term_id, $tt_id, $taxonomy) {
    jvbUpdateCacheTimestamp('term', get_term($term_id, $taxonomy));
}, 10, 3);
 
add_action('rest_api_init', function () {
    register_rest_route('jvb/v1', '/cachedContent', [
        'methods' => 'GET',
        'callback' => 'jvbCacheCheck',
        'permission_callback' => '__return_true' // Public endpoint
    ]);
});
 
function jvbCacheCheck(WP_REST_Request $request):WP_REST_Response
{
    // Get the cache timestamps
    $timestamps = jvbGetCache();
 
    $data = $request->get_params();
    error_log('Cache request: '.print_r($data, true));
 
    // Set Last-Modified header for conditional requests
    $last_update = 0;
    foreach ($timestamps as $type) {
        if ($type > $last_update) {
            $last_update = $type;
        }
    }
    if ($last_update > 0) {
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $last_update) . ' GMT');
    }
 
    // Check If-Modified-Since header
    $if_modified_since = $request->get_header('if-modified-since');
    if ($if_modified_since) {
        $if_modified_since = strtotime($if_modified_since);
        if ($last_update <= $if_modified_since) {
            // Return 304 Not Modified if nothing has changed
            return new WP_REST_Response(null, 304);
        }
    }
 
    // Add cache control headers
    header('Cache-Control: max-age=60, must-revalidate');
 
    return new WP_REST_Response($timestamps);
}
 
function jvbGetCache():array
{
    $timestamps = get_option('jvb_cache');
    if (!$timestamps) {
        $timestamps = [];
        $keys = JVB()->registeredContent();
        $currentTime = time();
        foreach ($keys as $key) {
            $timestamps[$key] = $currentTime;
        }
        update_option('jvb_cache', $timestamps);
    }
    return $timestamps;
}