| 1 |
<?php |
| 2 |
/** |
| 3 |
* Live mode: 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 |
* back to the mls_id ranges the admin class uses. |
| 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: fall back to the mls_id range mapping 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 |
// Copy only the whitelisted, scalar mld_details keys into the stored config. |
| 60 |
$config = array(); |
| 61 |
foreach ( array( 'api_import_url', 'api_token_url', 'api_media_url', 'type', 'expand', 'field_corellation', 'mls_filter_params', 'mls_id' ) as $key ) { |
| 62 |
if ( isset( $answer['mls_data'][ $key ] ) && is_scalar( $answer['mls_data'][ $key ] ) ) { |
| 63 |
$config[ $key ] = (string) $answer['mls_data'][ $key ]; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
// Persist the fetched config and hand it back to the caller. |
| 68 |
update_option( 'mlsimport_live_mls_config', $config ); |
| 69 |
return $config; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Provider type derived from the configured mls_id, mirroring the admin |
| 74 |
* class ranges: 900–3000 trestle, 5000–6000 rapattoni, 6000–7000 paragon, |
| 75 |
* 7000–8000 realtorca, 8000–9000 connectmls (8001 brightmls); bridge is the |
| 76 |
* default everywhere else (the Lambda's own default). |
| 77 |
* |
| 78 |
* @return string |
| 79 |
*/ |
| 80 |
function mlsimport_live_provider_type_from_mls_id(): string { |
| 81 |
// The selected MLS id drives the range test below (0 when unset). |
| 82 |
$options = get_option( 'mlsimport_admin_options' ); |
| 83 |
$mls_id = is_array( $options ) && isset( $options['mlsimport_mls_name'] ) ? (int) $options['mlsimport_mls_name'] : 0; |
| 84 |
|
| 85 |
// 8001 is the single BrightMLS id — checked before the 8000–9000 band it sits in. |
| 86 |
if ( 8001 === $mls_id ) { |
| 87 |
return 'brightmls'; |
| 88 |
} |
| 89 |
// 900–3000: Trestle (CoreLogic). |
| 90 |
if ( $mls_id >= 900 && $mls_id < 3000 ) { |
| 91 |
return 'trestle'; |
| 92 |
} |
| 93 |
// 5000–6000: Rapattoni. |
| 94 |
if ( $mls_id >= 5000 && $mls_id < 6000 ) { |
| 95 |
return 'rapattoni'; |
| 96 |
} |
| 97 |
// 6000–7000: Paragon. |
| 98 |
if ( $mls_id >= 6000 && $mls_id < 7000 ) { |
| 99 |
return 'paragon'; |
| 100 |
} |
| 101 |
// 7000–8000: Realtor.ca (CREA). |
| 102 |
if ( $mls_id >= 7000 && $mls_id < 8000 ) { |
| 103 |
return 'realtorca'; |
| 104 |
} |
| 105 |
// 8000–9000: ConnectMLS (BrightMLS 8001 already peeled off above). |
| 106 |
if ( $mls_id >= 8000 && $mls_id < 9000 ) { |
| 107 |
return 'connectmls'; |
| 108 |
} |
| 109 |
// Everything else: bridge — the Lambda's own default. |
| 110 |
return 'bridge'; |
| 111 |
} |
| 112 |
|