Add on-demand media URL refresh for expired MLS Grid tokens

MLS Grid media URLs expire after ~24 hours. Instead of running
scheduled syncs, this adds on-demand refresh when images are requested:

- Add is_url_expired() to parse expires timestamp from media URLs
- Add refresh_media_urls() to fetch fresh URLs from API for one listing
- Add get_property_media() API method using ListingId filter
- Image endpoint checks URL expiration before fetching
- If expired, refreshes URLs from API then proceeds with fetch

This is more efficient than scheduled full syncs because:
- Only refreshes URLs for listings actually being viewed
- Zero overhead for unviewed listings
- Scales naturally with traffic

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Hanson.xyz Dev
2025-12-15 23:45:44 -06:00
parent 4db53b607c
commit 72b932b25e
3 changed files with 146 additions and 0 deletions
@@ -137,6 +137,11 @@ class MLS_Image_Endpoint {
/**
* Get source image path, fetching from MLS if needed
*
* This method handles:
* 1. Returning cached local images if available
* 2. Checking if media URL has expired and refreshing if needed
* 3. Fetching images from MLS Grid on demand
*/
private function get_source_image($listing_key, $index) {
global $wpdb;
@@ -165,6 +170,30 @@ class MLS_Image_Endpoint {
}
}
// Check if the media URL has expired before trying to fetch
if ($this->media_handler->is_url_expired($media->media_url)) {
$this->logger->debug('Media URL expired, refreshing', array(
'listing_key' => $listing_key,
'index' => $index,
));
// Refresh media URLs from API
if ($this->media_handler->refresh_media_urls($listing_key)) {
// Re-fetch the record with fresh URL
$media = $wpdb->get_row($wpdb->prepare(
"SELECT * FROM {$db->media_table()}
WHERE listing_key = %s AND media_order = %d
LIMIT 1",
$listing_key,
$index
));
if (!$media) {
return null;
}
}
}
// Fetch from MLS on demand
$url = $this->media_handler->get_image_url($media, true);
if (!$url) {