| 1 |
<?php |
| 2 |
/** |
| 3 |
* Add-MLS drawer — the follow-up field-mapping seed (issue #325). |
| 4 |
* |
| 5 |
* WHY THIS FILE EXISTS |
| 6 |
* -------------------- |
| 7 |
* The drawer's add used to save the connection and THEN, inside the same |
| 8 |
* request, run the connection-scoped metadata gather that seeds the new |
| 9 |
* connection's field mapping from theme defaults (#264). That gather is a |
| 10 |
* second SaaS round trip carrying the full metadata + enum payload. On hosts |
| 11 |
* with a PHP or proxy time limit the request died after the record was |
| 12 |
* written: the browser got no answer, the drawer painted a failure, and the |
| 13 |
* user's retry was refused with "already one of your connections". |
| 14 |
* |
| 15 |
* The rule now: the add request answers the moment the connection is saved, |
| 16 |
* and the seed is its own request. This file owns that seed: |
| 17 |
* |
| 18 |
* - the flow core: refuse an unregistered id, otherwise run the scoped |
| 19 |
* gather and report whether the mapping was seeded, |
| 20 |
* - the AJAX handler the drawer posts right after a successful add. |
| 21 |
* |
| 22 |
* A failed seed never undoes the add — the connection works; the drawer tells |
| 23 |
* the user to open Field Options, which retries the gather on its own. |
| 24 |
* |
| 25 |
* The add flow lives in includes/mlsimport-connections-add.php; the drawer |
| 26 |
* behavior in admin/js/mlsimport-connections-drawer.js. |
| 27 |
* |
| 28 |
* @since 7.2.1 |
| 29 |
* @package Mlsimport |
| 30 |
*/ |
| 31 |
|
| 32 |
if ( ! defined( 'ABSPATH' ) ) { |
| 33 |
exit; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Seed one registered connection's field mapping. Never terminates. |
| 38 |
* |
| 39 |
* Step by step: |
| 40 |
* 1. Refuse an id that is not a registered connection — there is nothing to |
| 41 |
* seed and no reason to contact the SaaS. |
| 42 |
* 2. Run the connection-scoped metadata gather (#264/#281): it reconciles this |
| 43 |
* connection's Field Configuration and auto-enables the theme defaults. |
| 44 |
* 3. Report success either way — the connection exists regardless — with the |
| 45 |
* 'seeded' flag carrying the gather outcome, so the drawer can tell the |
| 46 |
* user to open Field Options when the seed is still pending. |
| 47 |
* |
| 48 |
* @param int $mls_id The registered connection to seed. |
| 49 |
* @return array{success:bool, code:string, message:string, seeded:bool} |
| 50 |
*/ |
| 51 |
function mlsimport_connections_seed_execute( int $mls_id ): array { |
| 52 |
// Step 1: only a registered connection can be seeded. |
| 53 |
if ( null === Mlsimport_Connections::get( $mls_id ) ) { |
| 54 |
return array( |
| 55 |
'success' => false, |
| 56 |
'code' => 'unknown_connection', |
| 57 |
'message' => esc_html__( 'This MLS is not one of your connections.', 'mlsimport' ), |
| 58 |
'seeded' => false, |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
// Step 2: the scoped gather seeds the mapping from theme defaults. |
| 63 |
$gather = mlsimport_gather_connection_metadata( $mls_id ); |
| 64 |
|
| 65 |
// Step 3: the seed request succeeded; 'seeded' says whether the mapping did. |
| 66 |
return array( |
| 67 |
'success' => true, |
| 68 |
'code' => (string) $gather['code'], |
| 69 |
'message' => (string) $gather['message'], |
| 70 |
'seeded' => (bool) $gather['success'], |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* AJAX: the Add-MLS drawer's follow-up seed after a saved connection. |
| 76 |
* |
| 77 |
* Step by step: |
| 78 |
* 1. Nonce (the Connections screen nonce) + administrator capability. |
| 79 |
* 2. Read the posted MLS id. |
| 80 |
* 3. Run the seed core; the drawer repaints from 'seeded'. |
| 81 |
* |
| 82 |
* @return void |
| 83 |
*/ |
| 84 |
function mlsimport_ajax_connections_seed() { |
| 85 |
// Step 1: security. |
| 86 |
check_ajax_referer( 'mlsimport_connections_screen', 'security' ); |
| 87 |
if ( ! current_user_can( 'administrator' ) ) { |
| 88 |
wp_send_json_error( array( 'message' => 'Unauthorized' ) ); |
| 89 |
} |
| 90 |
|
| 91 |
// Step 2: which connection. |
| 92 |
$mls_id = isset( $_POST['mls_id'] ) ? (int) $_POST['mls_id'] : 0; |
| 93 |
|
| 94 |
// Step 3: seed and answer. |
| 95 |
$outcome = mlsimport_connections_seed_execute( $mls_id ); |
| 96 |
if ( ! $outcome['success'] ) { |
| 97 |
wp_send_json_error( |
| 98 |
array( |
| 99 |
'code' => $outcome['code'], |
| 100 |
'message' => $outcome['message'], |
| 101 |
) |
| 102 |
); |
| 103 |
} |
| 104 |
wp_send_json_success( |
| 105 |
array( |
| 106 |
'mls_id' => $mls_id, |
| 107 |
'seeded' => $outcome['seeded'], |
| 108 |
) |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
add_action( 'wp_ajax_mlsimport_connections_seed', 'mlsimport_ajax_connections_seed' ); |
| 113 |
|