Files
homeproz/wp-content/plugins/mls-by-hansonxyz/includes/class-mls-options.php
T
Hanson.xyz Dev 6556479417 Add MLS by HansonXyz plugin for MLS Grid API integration
Features:
- Full sync of NorthStar MLS properties via MLS Grid API v2
- Incremental sync using ModificationTimestamp
- Local media download and storage
- Rate limit compliance (2 req/sec, 7200/hr, 40000/day)
- Sync state tracking with resume capability
- WP-CLI commands: test, sync, status, stats, cache
- Admin settings page with manual sync triggers
- Public API functions: mls_get_properties, mls_get_property, etc.

Database tables:
- mls_properties: Listing data with full field mapping
- mls_media: Downloaded images
- mls_sync_state: Sync progress tracking
- mls_rate_limits: API usage tracking
- mls_sync_log: Debug logging

Documentation:
- docs/CLAUDE.md: AI development guide
- docs/API.md: MLS Grid API reference
- docs/USAGE.md: User documentation

Tested: Connection, auth, sync 10 records, media download verified

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 21:24:38 -06:00

208 lines
4.9 KiB
PHP

<?php
/**
* Options handler class
*/
if (!defined('ABSPATH')) {
exit;
}
class MLS_Options {
/**
* Option key for storing all plugin options
*/
const OPTION_KEY = 'mls_plugin_options';
/**
* Default options
*/
private $defaults = array(
'api_url' => '',
'api_token' => '',
'originating_system' => 'northstar',
'auto_sync_enabled' => false,
'sync_interval' => 'hourly',
'sync_media' => true,
'last_full_sync' => null,
'last_incremental_sync' => null,
);
/**
* Get all options
*
* @return array
*/
public function get_all() {
$options = get_option(self::OPTION_KEY, array());
return wp_parse_args($options, $this->defaults);
}
/**
* Get a single option
*
* @param string $key Option key
* @param mixed $default Default value if not set
* @return mixed
*/
public function get($key, $default = null) {
$options = $this->get_all();
if (isset($options[$key])) {
return $options[$key];
}
if (null !== $default) {
return $default;
}
return isset($this->defaults[$key]) ? $this->defaults[$key] : null;
}
/**
* Set a single option
*
* @param string $key Option key
* @param mixed $value Option value
* @return bool
*/
public function set($key, $value) {
$options = $this->get_all();
$options[$key] = $value;
return update_option(self::OPTION_KEY, $options);
}
/**
* Set multiple options
*
* @param array $values Key-value pairs
* @return bool
*/
public function set_multiple($values) {
$options = $this->get_all();
foreach ($values as $key => $value) {
$options[$key] = $value;
}
return update_option(self::OPTION_KEY, $options);
}
/**
* Delete a single option
*
* @param string $key Option key
* @return bool
*/
public function delete($key) {
$options = $this->get_all();
if (isset($options[$key])) {
unset($options[$key]);
return update_option(self::OPTION_KEY, $options);
}
return true;
}
/**
* Delete all options
*
* @return bool
*/
public function delete_all() {
return delete_option(self::OPTION_KEY);
}
/**
* Get API URL from wp-config or options
*
* @return string
*/
public function get_api_url() {
// Check wp-config constant first
if (defined('MLSGRID_API_URL') && MLSGRID_API_URL) {
return MLSGRID_API_URL;
}
return $this->get('api_url', 'https://api.mlsgrid.com/v2');
}
/**
* Get API token from wp-config or options
*
* @return string
*/
public function get_api_token() {
// Check wp-config constant first
if (defined('MLSGRID_ACCESS_TOKEN') && MLSGRID_ACCESS_TOKEN) {
return MLSGRID_ACCESS_TOKEN;
}
return $this->get('api_token', '');
}
/**
* Get originating system name
*
* @return string
*/
public function get_originating_system() {
return $this->get('originating_system', 'northstar');
}
/**
* Check if API is configured
*
* @return bool
*/
public function is_configured() {
return !empty($this->get_api_url()) && !empty($this->get_api_token());
}
/**
* Check if auto sync is enabled
*
* @return bool
*/
public function is_auto_sync_enabled() {
return (bool) $this->get('auto_sync_enabled', false);
}
/**
* Get sync interval
*
* @return string WordPress cron interval name
*/
public function get_sync_interval() {
return $this->get('sync_interval', 'hourly');
}
/**
* Check if media sync is enabled
*
* @return bool
*/
public function is_media_sync_enabled() {
return (bool) $this->get('sync_media', true);
}
/**
* Update last sync timestamps
*
* @param string $type 'full' or 'incremental'
* @return bool
*/
public function update_last_sync($type = 'incremental') {
$key = 'full' === $type ? 'last_full_sync' : 'last_incremental_sync';
return $this->set($key, current_time('mysql'));
}
/**
* Get last sync time
*
* @param string $type 'full' or 'incremental'
* @return string|null MySQL datetime or null
*/
public function get_last_sync($type = 'incremental') {
$key = 'full' === $type ? 'last_full_sync' : 'last_incremental_sync';
return $this->get($key);
}
}