| 1 |
<?php |
| 2 |
/** |
| 3 |
* Live mode: the on/off flag, cache TTL and manual config overrides. |
| 4 |
* |
| 5 |
* Options only + a small procedural admin screen under the Standalone Design |
| 6 |
* menu. The React settings app is untouched. The overrides exist so a site |
| 7 |
* can run live mode before (or without) the SaaS config handshake; any |
| 8 |
* override left empty defers to the fetched config. |
| 9 |
* |
| 10 |
* @package Mlsimport |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Whether the live-mode switch is on (the flag only — not the full gate). |
| 19 |
* |
| 20 |
* @return bool |
| 21 |
*/ |
| 22 |
function mlsimport_live_enabled(): bool { |
| 23 |
return '1' === get_option( 'mlsimport_live_enabled', '' ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Cache TTL in seconds for live reads. Option-driven, default 1 hour. |
| 28 |
* |
| 29 |
* @return int |
| 30 |
*/ |
| 31 |
function mlsimport_live_cache_ttl(): int { |
| 32 |
$ttl = (int) get_option( 'mlsimport_live_cache_ttl', 3600 ); |
| 33 |
if ( $ttl < 60 ) { |
| 34 |
$ttl = 3600; |
| 35 |
} |
| 36 |
/** Filter the live-mode cache TTL (seconds). @since 6.4 */ |
| 37 |
return (int) apply_filters( 'mlsimport_live_cache_ttl', $ttl ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Manual per-MLS config overrides (api_import_url, type, expand, |
| 42 |
* field_corellation). Empty values are dropped so they defer to the config |
| 43 |
* fetched from the SaaS at setup. |
| 44 |
* |
| 45 |
* @return array |
| 46 |
*/ |
| 47 |
function mlsimport_live_config_overrides(): array { |
| 48 |
$overrides = get_option( 'mlsimport_live_overrides', array() ); |
| 49 |
if ( ! is_array( $overrides ) ) { |
| 50 |
return array(); |
| 51 |
} |
| 52 |
return array_filter( |
| 53 |
$overrides, |
| 54 |
static function ( $value ) { |
| 55 |
return is_string( $value ) && '' !== trim( $value ); |
| 56 |
} |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Register the Live Mode screen under Standalone Design. |
| 62 |
* |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
function mlsimport_live_settings_menu(): void { |
| 66 |
add_submenu_page( |
| 67 |
'mlsimport_standalone_settings', |
| 68 |
esc_html__( 'Live Mode', 'mlsimport' ), |
| 69 |
esc_html__( 'Live Mode', 'mlsimport' ), |
| 70 |
'manage_options', |
| 71 |
'mlsimport_live_settings', |
| 72 |
'mlsimport_live_settings_page' |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Render + save the Live Mode settings screen. |
| 78 |
* |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
function mlsimport_live_settings_page(): void { |
| 82 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
$notice = ''; |
| 87 |
if ( isset( $_POST['mlsimport_live_settings_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['mlsimport_live_settings_nonce'] ), 'mlsimport_live_settings' ) ) { |
| 88 |
if ( isset( $_POST['mlsimport_live_clear_cache'] ) ) { |
| 89 |
mlsimport_live_cache_clear(); |
| 90 |
$notice = esc_html__( 'Live cache cleared.', 'mlsimport' ); |
| 91 |
} elseif ( isset( $_POST['mlsimport_live_refresh_config'] ) ) { |
| 92 |
$config = mlsimport_live_config_refresh(); |
| 93 |
$notice = false === $config |
| 94 |
? esc_html__( 'Could not fetch the MLS configuration. Check the MLSImport connection.', 'mlsimport' ) |
| 95 |
: esc_html__( 'MLS configuration refreshed.', 'mlsimport' ); |
| 96 |
} else { |
| 97 |
$was_enabled = mlsimport_live_enabled(); |
| 98 |
update_option( 'mlsimport_live_enabled', isset( $_POST['mlsimport_live_enabled'] ) ? '1' : '' ); |
| 99 |
$ttl = isset( $_POST['mlsimport_live_cache_ttl'] ) ? absint( wp_unslash( $_POST['mlsimport_live_cache_ttl'] ) ) : 3600; |
| 100 |
update_option( 'mlsimport_live_cache_ttl', max( 60, $ttl ) ); |
| 101 |
|
| 102 |
$overrides = array(); |
| 103 |
foreach ( array( 'api_import_url', 'api_token_url', 'api_media_url', 'type', 'expand', 'field_corellation' ) as $key ) { |
| 104 |
$raw = isset( $_POST[ 'mlsimport_live_override_' . $key ] ) ? sanitize_text_field( wp_unslash( $_POST[ 'mlsimport_live_override_' . $key ] ) ) : ''; |
| 105 |
if ( '' !== trim( $raw ) ) { |
| 106 |
$overrides[ $key ] = trim( $raw ); |
| 107 |
} |
| 108 |
} |
| 109 |
update_option( 'mlsimport_live_overrides', $overrides ); |
| 110 |
|
| 111 |
if ( mlsimport_live_enabled() !== $was_enabled ) { |
| 112 |
// Deferred flush: the toggle changed after init, so this |
| 113 |
// request's in-memory rules still reflect the OLD gate state. |
| 114 |
// The next request re-registers routes with the new one. |
| 115 |
delete_option( 'rewrite_rules' ); |
| 116 |
} |
| 117 |
$notice = esc_html__( 'Live mode settings saved.', 'mlsimport' ); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
$config = function_exists( 'mlsimport_live_config' ) ? mlsimport_live_config() : array(); |
| 122 |
$overrides = get_option( 'mlsimport_live_overrides', array() ); |
| 123 |
$overrides = is_array( $overrides ) ? $overrides : array(); |
| 124 |
?> |
| 125 |
<div class="wrap"> |
| 126 |
<h1><?php esc_html_e( 'Live MLS Mode', 'mlsimport' ); ?></h1> |
| 127 |
<?php if ( '' !== $notice ) : ?> |
| 128 |
<div class="notice notice-info"><p><?php echo esc_html( $notice ); ?></p></div> |
| 129 |
<?php endif; ?> |
| 130 |
<p><?php esc_html_e( 'Live mode reads listings directly from your MLS on demand and stores nothing on this site. Recommended for shared/weak hosting.', 'mlsimport' ); ?></p> |
| 131 |
<form method="post"> |
| 132 |
<?php wp_nonce_field( 'mlsimport_live_settings', 'mlsimport_live_settings_nonce' ); ?> |
| 133 |
<table class="form-table" role="presentation"> |
| 134 |
<tr> |
| 135 |
<th scope="row"><?php esc_html_e( 'Enable live mode', 'mlsimport' ); ?></th> |
| 136 |
<td><label><input type="checkbox" name="mlsimport_live_enabled" value="1" <?php checked( mlsimport_live_enabled() ); ?>> <?php esc_html_e( 'Read listings live from the MLS (no local storage)', 'mlsimport' ); ?></label></td> |
| 137 |
</tr> |
| 138 |
<tr> |
| 139 |
<th scope="row"><label for="mlsimport_live_cache_ttl"><?php esc_html_e( 'Cache lifetime (seconds)', 'mlsimport' ); ?></label></th> |
| 140 |
<td><input type="number" min="60" id="mlsimport_live_cache_ttl" name="mlsimport_live_cache_ttl" value="<?php echo esc_attr( (string) mlsimport_live_cache_ttl() ); ?>"></td> |
| 141 |
</tr> |
| 142 |
<?php |
| 143 |
foreach ( array( |
| 144 |
'api_import_url' => __( 'API import URL override', 'mlsimport' ), |
| 145 |
'api_token_url' => __( 'API token URL override', 'mlsimport' ), |
| 146 |
'api_media_url' => __( 'API media URL override', 'mlsimport' ), |
| 147 |
'type' => __( 'Provider type override', 'mlsimport' ), |
| 148 |
'expand' => __( 'Expand override', 'mlsimport' ), |
| 149 |
'field_corellation' => __( 'Field correlation override (JSON)', 'mlsimport' ), |
| 150 |
) as $key => $label ) : |
| 151 |
$fetched = isset( $config[ $key ] ) && is_string( $config[ $key ] ) ? $config[ $key ] : ''; |
| 152 |
?> |
| 153 |
<tr> |
| 154 |
<th scope="row"><label for="mlsimport_live_override_<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $label ); ?></label></th> |
| 155 |
<td> |
| 156 |
<input type="text" class="regular-text" id="mlsimport_live_override_<?php echo esc_attr( $key ); ?>" name="mlsimport_live_override_<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( $overrides[ $key ] ?? '' ); ?>" placeholder="<?php echo esc_attr( $fetched ); ?>"> |
| 157 |
</td> |
| 158 |
</tr> |
| 159 |
<?php endforeach; ?> |
| 160 |
</table> |
| 161 |
<p class="submit"> |
| 162 |
<button type="submit" class="button button-primary"><?php esc_html_e( 'Save', 'mlsimport' ); ?></button> |
| 163 |
<button type="submit" class="button" name="mlsimport_live_refresh_config" value="1"><?php esc_html_e( 'Refresh MLS config', 'mlsimport' ); ?></button> |
| 164 |
<button type="submit" class="button" name="mlsimport_live_clear_cache" value="1"><?php esc_html_e( 'Clear live cache', 'mlsimport' ); ?></button> |
| 165 |
</p> |
| 166 |
</form> |
| 167 |
</div> |
| 168 |
<?php |
| 169 |
} |
| 170 |
|