| 1 |
<?php |
| 2 |
/** |
| 3 |
* Field-mapping connection scope resolver (per-connection field mapping UI). |
| 4 |
* |
| 5 |
* WHY THIS FILE EXISTS |
| 6 |
* -------------------- |
| 7 |
* The Field Configuration storage went per-connection with #275 |
| 8 |
* (mlsimport_admin_fields_select_{mls_id}), but every UI surface stayed |
| 9 |
* hard-bound to the CURRENT connection: the field_options tab, its mutation |
| 10 |
* AJAX (mlsimport_change_field_configuration), and the metadata-gather AJAX |
| 11 |
* all resolved mls_id 0. With 2+ connections a user could only ever see and |
| 12 |
* edit the current connection's mapping. |
| 13 |
* |
| 14 |
* This file holds the ONE rule that opens those surfaces to a chosen |
| 15 |
* connection: a request may name a scope ($_GET['mls'] on the tab, |
| 16 |
* $_POST['mls_id'] on the endpoints), and it resolves the same single way |
| 17 |
* everywhere: |
| 18 |
* |
| 19 |
* requested id is a registered connection => that connection, |
| 20 |
* anything else (absent, garbage, unknown) => the current connection. |
| 21 |
* |
| 22 |
* The fallback keeps every legacy caller and single-connection install on |
| 23 |
* exactly the pre-multi-MLS path (current resolves to 0 => flat options on an |
| 24 |
* unconfigured install, per mlsimport-connection-options.php). |
| 25 |
* |
| 26 |
* Pure function — no WordPress calls — so the rule is unit-testable |
| 27 |
* (tests/field-mapping/FieldMappingStage1FieldScopeTest.php). |
| 28 |
* |
| 29 |
* @since 7.3.0 |
| 30 |
* @package Mlsimport |
| 31 |
*/ |
| 32 |
|
| 33 |
if ( ! defined( 'ABSPATH' ) ) { |
| 34 |
exit; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Resolve which connection a field-mapping surface addresses. |
| 39 |
* |
| 40 |
* Step by step: |
| 41 |
* 1. Cast the raw request value (query/post string) to int; non-numeric |
| 42 |
* garbage becomes 0 and can never match a registered id. |
| 43 |
* 2. A positive id that is one of the registered connections wins. |
| 44 |
* 3. Everything else resolves to the current connection — the legacy scope. |
| 45 |
* |
| 46 |
* @param mixed $requested Raw request value ($_GET['mls'] / $_POST['mls_id']). |
| 47 |
* @param array $registered_ids mls_ids of the registered connections. |
| 48 |
* @param int $current_id The current connection's mls_id (0 = unconfigured). |
| 49 |
* @return int The mls_id to scope reads/writes to. |
| 50 |
*/ |
| 51 |
function mlsimport_field_mapping_scope( $requested, array $registered_ids, int $current_id ): int { |
| 52 |
// Step 1: request values arrive as strings; garbage casts to 0. |
| 53 |
$requested = is_scalar( $requested ) ? (int) $requested : 0; |
| 54 |
|
| 55 |
// Step 2: only a registered connection can be addressed. |
| 56 |
if ( $requested > 0 && in_array( $requested, array_map( 'intval', $registered_ids ), true ) ) { |
| 57 |
return $requested; |
| 58 |
} |
| 59 |
|
| 60 |
// Step 3: the legacy scope — current connection. |
| 61 |
return $current_id; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Resolve a request's field-mapping scope against the live registry. |
| 66 |
* |
| 67 |
* Thin WordPress wrapper so the three call sites (tab partial, mutation AJAX, |
| 68 |
* gather AJAX) resolve identically without repeating the registry/current |
| 69 |
* lookups. Requires WordPress; the rule itself lives in the pure function. |
| 70 |
* |
| 71 |
* @param mixed $requested Raw request value ($_GET['mls'] / $_POST['mls_id']). |
| 72 |
* @return int The mls_id to scope reads/writes to. |
| 73 |
*/ |
| 74 |
function mlsimport_field_mapping_request_scope( $requested ): int { |
| 75 |
return mlsimport_field_mapping_scope( |
| 76 |
$requested, |
| 77 |
array_keys( Mlsimport_Connections::all() ), |
| 78 |
mlsimport_current_mls_id() |
| 79 |
); |
| 80 |
} |
| 81 |
|