<?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;
|
}
|