| 1 |
<?php |
| 2 |
/** |
| 3 |
* Direct MLS access: the per-MLS provider configuration. |
| 4 |
* |
| 5 |
* The GET /clients SaaS call already returns the client's mld_details record |
| 6 |
* (api_import_url, api_token_url, api_media_url, type, expand, |
| 7 |
* field_corellation, mls_filter_params) — mlsimport_live_config_refresh() |
| 8 |
* stores the whitelisted keys as one option. Manual overrides from the |
| 9 |
* settings screen win over fetched values; provider type additionally falls |
| 10 |
* falls back through the Provider Family module for older saved settings. |
| 11 |
* |
| 12 |
* @package Mlsimport |
| 13 |
*/ |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* The per-MLS config: fetched values overlaid with manual overrides. |
| 21 |
* |
| 22 |
* @return array Empty array when no usable config exists (no api_import_url). |
| 23 |
*/ |
| 24 |
function mlsimport_live_config(): array { |
| 25 |
// Start from the fetched mld_details record (guard against a corrupt option). |
| 26 |
$stored = get_option( 'mlsimport_live_mls_config', array() ); |
| 27 |
$config = is_array( $stored ) ? $stored : array(); |
| 28 |
// Manual settings-screen overrides win over fetched values. |
| 29 |
$config = array_merge( $config, mlsimport_live_config_overrides() ); |
| 30 |
|
| 31 |
// Provider type: use the Provider Family compatibility map when unset. |
| 32 |
if ( empty( $config['type'] ) ) { |
| 33 |
$config['type'] = mlsimport_live_provider_type_from_mls_id(); |
| 34 |
} |
| 35 |
// Normalize type to lower-case for the dialect dispatch downstream. |
| 36 |
$config['type'] = strtolower( trim( (string) $config['type'] ) ); |
| 37 |
|
| 38 |
// No base URL means no usable config — report empty so the gate stays off. |
| 39 |
if ( empty( $config['api_import_url'] ) ) { |
| 40 |
return array(); |
| 41 |
} |
| 42 |
|
| 43 |
return $config; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Fetch the mld_details config from the SaaS (GET clients) and store it. |
| 48 |
* |
| 49 |
* @return array|false The stored config, or false when the call failed. |
| 50 |
*/ |
| 51 |
function mlsimport_live_config_refresh() { |
| 52 |
// Ask the SaaS GET /clients endpoint for this client's record. |
| 53 |
$answer = ThemeImport::globalApiRequestSaas( 'clients', array(), 'GET' ); |
| 54 |
// No mls_data block means the call failed or the client isn't configured. |
| 55 |
if ( ! is_array( $answer ) || empty( $answer['mls_data'] ) || ! is_array( $answer['mls_data'] ) ) { |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
// Refresh the connection registry from mls_entitlements when the SaaS |
| 60 |
// sends it (#276); a legacy response without it changes nothing. |
| 61 |
mlsimport_apply_entitlements( $answer ); |
| 62 |
|
| 63 |
// Copy only the whitelisted, scalar mld_details keys into the stored config. |
| 64 |
$config = array(); |
| 65 |
foreach ( array( 'api_import_url', 'api_token_url', 'api_media_url', 'type', 'expand', 'field_corellation', 'mls_filter_params', 'mls_id' ) as $key ) { |
| 66 |
if ( isset( $answer['mls_data'][ $key ] ) && is_scalar( $answer['mls_data'][ $key ] ) ) { |
| 67 |
$config[ $key ] = (string) $answer['mls_data'][ $key ]; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// Persist the fetched config and hand it back to the caller. |
| 72 |
update_option( 'mlsimport_live_mls_config', $config ); |
| 73 |
if ( ! empty( $config['type'] ) ) { |
| 74 |
$options = get_option( 'mlsimport_admin_options', array() ); |
| 75 |
$mls_id = ! empty( $config['mls_id'] ) |
| 76 |
? $config['mls_id'] |
| 77 |
: ( is_array( $options ) && isset( $options['mlsimport_mls_name'] ) ? $options['mlsimport_mls_name'] : '' ); |
| 78 |
Mlsimport_Provider_Family::remember_type( $config['type'], $mls_id ); |
| 79 |
} |
| 80 |
return $config; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Return the selected MLS provider type through the public Provider Family |
| 85 |
* module. Saved provider data wins and numeric ranges are only a fallback. |
| 86 |
* |
| 87 |
* @return string |
| 88 |
*/ |
| 89 |
function mlsimport_live_provider_type_from_mls_id(): string { |
| 90 |
// Read the selected MLS once and let the Provider Family module choose it. |
| 91 |
$options = get_option( 'mlsimport_admin_options' ); |
| 92 |
$mls_id = is_array( $options ) && isset( $options['mlsimport_mls_name'] ) ? (int) $options['mlsimport_mls_name'] : 0; |
| 93 |
$saved = Mlsimport_Provider_Family::saved_type( $mls_id ); |
| 94 |
|
| 95 |
return Mlsimport_Provider_Family::adapter( $saved, $mls_id )->type(); |
| 96 |
} |
| 97 |
|