Files
homeproz/wp-content/plugins/mls-by-hansonxyz/includes/class-mls-media-handler.php
T
Hanson.xyz Dev b9cddd2f64 Refactor MLS sync to Active/Pending only with on-demand media
Major changes to sync strategy following MLS Grid best practices:

- Initial sync now fetches only Active/Pending properties (~30K vs 1.3M)
- Replication (incremental) fetches all changes, deletes non-Active/Pending
- On-demand media fetching replaces background queue (avoids rate limits)
- Media downloaded and cached when first viewed, not during sync
- Updated CLI commands: wp mls media status/fetch/clear
- Comprehensive documentation with troubleshooting guide

This fixes the "Value out of range" API error caused by high $skip values.

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-15 08:25:37 -06:00

599 lines
18 KiB
PHP

<?php
/**
* MLS Media Handler
*
* Handles on-demand fetching and caching of media files from MLS listings.
* Images are downloaded when first requested and cached locally.
*
* Per MLS Grid rules:
* - MediaURLs must NOT be used directly on websites
* - Images must be downloaded and served from our own server
*/
if (!defined('ABSPATH')) {
exit;
}
class MLS_Media_Handler {
/**
* Upload subdirectory for MLS media
*/
const UPLOAD_SUBDIR = 'mls-listings';
/**
* Database instance
*/
private $db;
/**
* Logger instance
*/
private $logger;
/**
* Constructor
*/
public function __construct(MLS_DB $db, MLS_Logger $logger) {
$this->db = $db;
$this->logger = $logger;
}
/**
* Get base upload directory for MLS media
*
* @return string Absolute path
*/
public function get_upload_dir() {
$upload_dir = wp_upload_dir();
return $upload_dir['basedir'] . '/' . self::UPLOAD_SUBDIR;
}
/**
* Get base upload URL for MLS media
*
* @return string URL
*/
public function get_upload_url() {
$upload_dir = wp_upload_dir();
return $upload_dir['baseurl'] . '/' . self::UPLOAD_SUBDIR;
}
/**
* Get storage directory for a specific listing
*
* @param string $listing_key Listing key
* @return string Absolute path
*/
public function get_listing_dir($listing_key) {
$prefix = substr($listing_key, 0, 2);
return $this->get_upload_dir() . '/' . $prefix . '/' . $listing_key;
}
/**
* Store media metadata from API sync (no download)
*
* @param string $listing_key Listing key
* @param array $media_array Media array from API
* @param callable|null $progress_callback Callback for progress updates
* @return array Stats
*/
public function sync_property_media($listing_key, $media_array, $force = false, $progress_callback = null) {
global $wpdb;
if (empty($media_array)) {
return array('stored' => 0, 'skipped' => 0);
}
$received_keys = array();
$stored = 0;
$skipped = 0;
foreach ($media_array as $media) {
$media_key = $media['MediaKey'] ?? null;
if (!$media_key) {
continue;
}
$received_keys[] = $media_key;
// Check if media record exists
$existing = $wpdb->get_row($wpdb->prepare(
"SELECT * FROM {$this->db->media_table()}
WHERE listing_key = %s AND media_key = %s",
$listing_key,
$media_key
));
$data = array(
'listing_key' => $listing_key,
'media_key' => $media_key,
'media_type' => $media['MediaType'] ?? 'Photo',
'media_order' => $media['Order'] ?? 0,
'media_url' => $media['MediaURL'] ?? null,
'image_width' => $media['ImageWidth'] ?? null,
'image_height' => $media['ImageHeight'] ?? null,
'media_modification_timestamp' => isset($media['MediaModificationTimestamp'])
? date('Y-m-d H:i:s', strtotime($media['MediaModificationTimestamp']))
: null,
'updated_at' => current_time('mysql'),
);
if ($existing) {
// Check if URL changed - if so, clear cached file
if ($existing->media_url !== ($media['MediaURL'] ?? null) && $existing->local_path) {
$file_path = $this->get_upload_dir() . '/' . $existing->local_path;
if (file_exists($file_path)) {
unlink($file_path);
}
$data['local_path'] = null;
$data['local_url'] = null;
$data['downloaded_at'] = null;
}
$wpdb->update(
$this->db->media_table(),
$data,
array('id' => $existing->id)
);
$skipped++;
} else {
$data['created_at'] = current_time('mysql');
$wpdb->insert($this->db->media_table(), $data);
$stored++;
}
if ($progress_callback) {
call_user_func($progress_callback, 'media_stored', array('media_key' => $media_key));
}
}
// Delete orphaned media records
if (!empty($received_keys)) {
$placeholders = implode(',', array_fill(0, count($received_keys), '%s'));
$values = array_merge(array($listing_key), $received_keys);
$orphaned = $wpdb->get_results($wpdb->prepare(
"SELECT id, local_path FROM {$this->db->media_table()}
WHERE listing_key = %s AND media_key NOT IN ({$placeholders})",
$values
));
foreach ($orphaned as $record) {
if ($record->local_path) {
$file_path = $this->get_upload_dir() . '/' . $record->local_path;
if (file_exists($file_path)) {
unlink($file_path);
}
}
$wpdb->delete($this->db->media_table(), array('id' => $record->id));
}
}
return array('stored' => $stored, 'skipped' => $skipped);
}
/**
* Get image URL for a media record, fetching on-demand if needed
*
* @param int|object $media Media ID or media record object
* @param bool $fetch_if_missing Whether to fetch if not cached
* @return string|null Local URL or null
*/
public function get_image_url($media, $fetch_if_missing = true) {
global $wpdb;
// Get media record if ID passed
if (is_numeric($media)) {
$media = $wpdb->get_row($wpdb->prepare(
"SELECT * FROM {$this->db->media_table()} WHERE id = %d",
$media
));
}
if (!$media) {
return null;
}
// Already cached
if ($media->local_url && $media->local_path) {
$file_path = $this->get_upload_dir() . '/' . $media->local_path;
if (file_exists($file_path)) {
return $media->local_url;
}
}
// Fetch on demand
if ($fetch_if_missing && $media->media_url) {
$result = $this->fetch_and_cache($media);
if ($result) {
return $result;
}
}
return null;
}
/**
* Get primary image URL for a listing (on-demand)
*
* @param string $listing_key Listing key
* @param bool $fetch_if_missing Whether to fetch if not cached
* @return string|null Image URL
*/
public function get_primary_image($listing_key, $fetch_if_missing = true) {
global $wpdb;
// First check for already-cached image
$cached = $wpdb->get_row($wpdb->prepare(
"SELECT * FROM {$this->db->media_table()}
WHERE listing_key = %s AND local_url IS NOT NULL AND local_path IS NOT NULL
ORDER BY media_order ASC
LIMIT 1",
$listing_key
));
if ($cached) {
$file_path = $this->get_upload_dir() . '/' . $cached->local_path;
if (file_exists($file_path)) {
return $cached->local_url;
}
}
// Get first media record (may not be cached)
$media = $wpdb->get_row($wpdb->prepare(
"SELECT * FROM {$this->db->media_table()}
WHERE listing_key = %s AND media_url IS NOT NULL
ORDER BY media_order ASC
LIMIT 1",
$listing_key
));
if (!$media) {
return null;
}
// If already cached and file exists, return it
if ($media->local_url && $media->local_path) {
$file_path = $this->get_upload_dir() . '/' . $media->local_path;
if (file_exists($file_path)) {
return $media->local_url;
}
}
// Fetch on demand
if ($fetch_if_missing) {
return $this->fetch_and_cache($media);
}
return null;
}
/**
* Get all images for a listing (on-demand for first N)
*
* @param string $listing_key Listing key
* @param int $fetch_limit Max images to fetch on-demand (0 = none)
* @return array Media records with local_url populated where available
*/
public function get_listing_images($listing_key, $fetch_limit = 1) {
global $wpdb;
$media = $wpdb->get_results($wpdb->prepare(
"SELECT * FROM {$this->db->media_table()}
WHERE listing_key = %s
ORDER BY media_order ASC",
$listing_key
));
if (empty($media)) {
return array();
}
$fetched = 0;
foreach ($media as &$item) {
// Check if cached and file exists
if ($item->local_url && $item->local_path) {
$file_path = $this->get_upload_dir() . '/' . $item->local_path;
if (file_exists($file_path)) {
continue;
}
}
// Fetch on demand up to limit
if ($fetched < $fetch_limit && $item->media_url) {
$url = $this->fetch_and_cache($item);
if ($url) {
$item->local_url = $url;
$fetched++;
}
}
}
return $media;
}
/**
* Fetch image from MLS Grid and cache locally
*
* @param object $media Media record
* @return string|null Local URL on success, null on failure
*/
private function fetch_and_cache($media) {
global $wpdb;
if (empty($media->media_url)) {
return null;
}
// Download the image
$response = wp_remote_get($media->media_url, array(
'timeout' => 30,
));
if (is_wp_error($response)) {
$this->logger->warning('Media fetch failed', array(
'listing_key' => $media->listing_key,
'media_key' => $media->media_key,
'error' => $response->get_error_message(),
));
return null;
}
$status_code = wp_remote_retrieve_response_code($response);
if ($status_code !== 200) {
$this->logger->warning('Media fetch HTTP error', array(
'listing_key' => $media->listing_key,
'media_key' => $media->media_key,
'status' => $status_code,
));
return null;
}
$body = wp_remote_retrieve_body($response);
if (empty($body)) {
return null;
}
// Determine extension
$content_type = wp_remote_retrieve_header($response, 'content-type');
$extension = $this->get_extension_from_content_type($content_type, $media->media_url);
// Create directory
$listing_dir = $this->get_listing_dir($media->listing_key);
if (!file_exists($listing_dir)) {
wp_mkdir_p($listing_dir);
}
// Save file
$filename = $media->media_order . '.' . $extension;
$file_path = $listing_dir . '/' . $filename;
if (file_put_contents($file_path, $body) === false) {
$this->logger->error('Failed to write media file', array(
'path' => $file_path,
));
return null;
}
// Update database
$prefix = substr($media->listing_key, 0, 2);
$relative_path = $prefix . '/' . $media->listing_key . '/' . $filename;
$local_url = $this->get_upload_url() . '/' . $relative_path;
$wpdb->update(
$this->db->media_table(),
array(
'local_path' => $relative_path,
'local_url' => $local_url,
'file_size' => strlen($body),
'mime_type' => $content_type,
'downloaded_at' => current_time('mysql'),
),
array('id' => $media->id)
);
$this->logger->debug('Media fetched and cached', array(
'listing_key' => $media->listing_key,
'media_key' => $media->media_key,
'size' => strlen($body),
));
return $local_url;
}
/**
* Get file extension from content type
*
* @param string $content_type Content type header
* @param string $url Original URL as fallback
* @return string File extension
*/
private function get_extension_from_content_type($content_type, $url) {
$content_type = strtolower(explode(';', $content_type)[0]);
$map = array(
'image/jpeg' => 'jpg',
'image/jpg' => 'jpg',
'image/png' => 'png',
'image/gif' => 'gif',
'image/webp' => 'webp',
);
if (isset($map[$content_type])) {
return $map[$content_type];
}
// Fallback to URL extension
$path = parse_url($url, PHP_URL_PATH);
$ext = pathinfo($path, PATHINFO_EXTENSION);
return $ext ?: 'jpg';
}
/**
* Delete all media for a property
*
* @param string $listing_key Listing key
*/
public function delete_property_media($listing_key) {
global $wpdb;
// Delete files
$listing_dir = $this->get_listing_dir($listing_key);
if (file_exists($listing_dir)) {
$this->recursive_delete($listing_dir);
}
// Delete records
$wpdb->delete(
$this->db->media_table(),
array('listing_key' => $listing_key)
);
}
/**
* Recursively delete a directory
*
* @param string $dir Directory path
*/
private function recursive_delete($dir) {
if (!is_dir($dir)) {
return;
}
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
$path = $dir . '/' . $file;
if (is_dir($path)) {
$this->recursive_delete($path);
} else {
unlink($path);
}
}
rmdir($dir);
}
/**
* Get media for a listing (legacy compatibility)
*
* @param string $listing_key Listing key
* @return array Media records
*/
public function get_listing_media($listing_key) {
global $wpdb;
return $wpdb->get_results($wpdb->prepare(
"SELECT * FROM {$this->db->media_table()}
WHERE listing_key = %s
ORDER BY media_order ASC",
$listing_key
));
}
/**
* Clean up orphaned media files (files without database records)
*
* @return int Number of directories deleted
*/
public function cleanup_orphaned_files() {
global $wpdb;
$deleted = 0;
$base_dir = $this->get_upload_dir();
if (!is_dir($base_dir)) {
return 0;
}
foreach (scandir($base_dir) as $prefix) {
if ($prefix === '.' || $prefix === '..' || !is_dir($base_dir . '/' . $prefix)) {
continue;
}
$prefix_dir = $base_dir . '/' . $prefix;
foreach (scandir($prefix_dir) as $listing_key) {
if ($listing_key === '.' || $listing_key === '..') {
continue;
}
$listing_dir = $prefix_dir . '/' . $listing_key;
if (!is_dir($listing_dir)) {
continue;
}
$exists = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$this->db->properties_table()} WHERE listing_key = %s",
$listing_key
));
if (!$exists) {
$this->recursive_delete($listing_dir);
$deleted++;
}
}
}
return $deleted;
}
/**
* Get cache statistics
*
* @return array Cache stats
*/
public function get_cache_stats() {
global $wpdb;
return array(
'total_media' => (int) $wpdb->get_var(
"SELECT COUNT(*) FROM {$this->db->media_table()}"
),
'cached' => (int) $wpdb->get_var(
"SELECT COUNT(*) FROM {$this->db->media_table()} WHERE local_url IS NOT NULL"
),
'uncached' => (int) $wpdb->get_var(
"SELECT COUNT(*) FROM {$this->db->media_table()} WHERE local_url IS NULL"
),
);
}
/**
* Get path to missing media log file (legacy compatibility)
*
* @return string File path
*/
public function get_missing_log_path() {
$upload_dir = wp_upload_dir();
return $upload_dir['basedir'] . '/mls-missing-media.log';
}
/**
* Get missing media count (legacy compatibility)
*
* @return int
*/
public function get_missing_count() {
$log_file = $this->get_missing_log_path();
if (!file_exists($log_file)) {
return 0;
}
$content = file_get_contents($log_file);
return substr_count($content, "\n");
}
/**
* Clear missing log (legacy compatibility)
*/
public function clear_missing_log() {
$log_file = $this->get_missing_log_path();
if (file_exists($log_file)) {
unlink($log_file);
}
}
}