208 lines
4.9 KiB
PHP
Executable File
208 lines
4.9 KiB
PHP
Executable File
<?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);
|
|
}
|
|
}
|