| 1 |
<?php |
| 2 |
/** |
| 3 |
* Metadata auto-trigger decision for the credential surfaces. |
| 4 |
* |
| 5 |
* The Field Options tab saves the whole import-field configuration server-side |
| 6 |
* inside the mlsimport_saas_get_metadata_function AJAX handler (metadata fetch |
| 7 |
* + reconcile + mlsimport_admin_fields_select save). Historically that AJAX |
| 8 |
* only fired when the user opened the Field Options tab. This module lets the |
| 9 |
* credential surfaces (settings display_options tab, onboarding account step) |
| 10 |
* fire the very same AJAX in the background the moment BOTH connections are |
| 11 |
* confirmed, so the field configuration is already saved before the user ever |
| 12 |
* opens Field Options. |
| 13 |
* |
| 14 |
* Step by step: |
| 15 |
* 1. The partial computes the SaaS token and the MLS connection flag exactly |
| 16 |
* as it does for the two green status messages. |
| 17 |
* 2. It calls mlsimport_metadata_autotrigger_markup() with those values, the |
| 18 |
* current mlsimport_mls_metadata_populated flag, and a fresh nonce. |
| 19 |
* 3. When (and only when) token present + connected + not yet populated, the |
| 20 |
* returned markup carries the #mlsimport_saas_get_metadata nonce input the |
| 21 |
* AJAX needs plus a DOM-ready call to the existing JS function |
| 22 |
* mlsimport_saas_get_metadata() in mlsimport-admin.js. |
| 23 |
* 4. Any credentials/MLS save deletes mlsimport_mls_metadata_populated, so the |
| 24 |
* trigger re-arms automatically after a change and fires at most once per |
| 25 |
* validated configuration. |
| 26 |
* |
| 27 |
* Pure PHP on purpose: no WordPress calls, the caller passes every input |
| 28 |
* (nonce already escaped), so the rule is unit-testable without stubs. |
| 29 |
* |
| 30 |
* @package MLSImport |
| 31 |
*/ |
| 32 |
|
| 33 |
if ( ! defined( 'ABSPATH' ) ) { |
| 34 |
exit; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Build the background metadata-gather trigger markup, or nothing. |
| 39 |
* |
| 40 |
* @param string $token SaaS API token ('' = account not connected). |
| 41 |
* @param string $is_mls_connected mlsimport_connection_test flag ('yes' = connected). |
| 42 |
* @param string $metadata_populated mlsimport_mls_metadata_populated flag ('yes' = already gathered). |
| 43 |
* @param string $nonce Escaped nonce for the mlsimport_saas_get_metadata action. |
| 44 |
* @return string Trigger markup when the gather should fire, '' otherwise. |
| 45 |
*/ |
| 46 |
function mlsimport_metadata_autotrigger_markup( $token, $is_mls_connected, $metadata_populated, $nonce ) { |
| 47 |
// Fires at most once: only a credentials/MLS save clears this flag, |
| 48 |
// which re-arms the trigger for the new configuration. |
| 49 |
if ( 'yes' === $metadata_populated ) { |
| 50 |
return ''; |
| 51 |
} |
| 52 |
// BOTH connections must be confirmed: SaaS account (token) and MLS. |
| 53 |
if ( '' === trim( $token ) || 'yes' !== $is_mls_connected ) { |
| 54 |
return ''; |
| 55 |
} |
| 56 |
// Post the gather AJAX directly instead of calling the shared JS helper |
| 57 |
// mlsimport_saas_get_metadata(): that helper reloads the page on success, |
| 58 |
// which would yank the credentials page from under the user (and abort a |
| 59 |
// click to another admin page while the gather is in flight). Here the |
| 60 |
// gather is fire-and-forget; only the Field Options "Stand By" page needs |
| 61 |
// the reloading variant. |
| 62 |
return '<input type="hidden" id="mlsimport_saas_get_metadata" value="' . $nonce . '">' |
| 63 |
. '<script>jQuery(document).ready(function(){' |
| 64 |
. " jQuery.post(ajaxurl, { action: 'mlsimport_saas_get_metadata_function', security: '" . $nonce . "' });" |
| 65 |
. ' });</script>'; |
| 66 |
} |
| 67 |
|