diff --git a/go-form-plugin/go-form-plugin.php b/go-form-plugin/go-form-plugin.php index 6f76d4d..5a9d9b7 100644 --- a/go-form-plugin/go-form-plugin.php +++ b/go-form-plugin/go-form-plugin.php @@ -414,6 +414,66 @@ function go_form_delete_custom_field() add_action('admin_post_go_form_delete_custom_field', 'go_form_delete_custom_field'); // ========== Entry Edit Admin Action ========== +function go_form_update_egd_data() +{ + go_form_admin_action('update_egd_data'); + + if (!isset($_POST['value_one'])) { + wp_redirect(admin_url('admin.php?page=go-form-settings')); + exit; + } + + global $wpdb, $ranks; + $form_id = intval($_POST['value_one']); + $entries = go_form_get_entries($form_id); + $updated_count = 0; + + foreach ($entries as $entry) { + if (empty($entry->egd_number)) { + continue; + } + + $pin = trim($entry->egd_number); + $url = 'https://europeangodatabase.eu/EGD/GetPlayerDataByPIN.php?pin=' . urlencode($pin); + + $response = wp_remote_get($url, ['timeout' => 10]); + + if (is_wp_error($response)) { + continue; + } + + $body = wp_remote_retrieve_body($response); + $data = json_decode($body, true); + + if ($data && isset($data['retcode']) && $data['retcode'] === 'Ok') { + // Map EGD Grade_n to our rank system + // EGD Grade_n: 0=30k, 1=29k, ... 25=5k, 30=1d, ... 39=9d, 40=1p, ... 47=9p + // Our ranks array: 0=30k, 1=29k, ... 25=5k, 30=1d, ... 39=9d, 40=1p, ... 47=9p + // So we can use Grade_n directly as our rank value + $new_rank = isset($data['Grade_n']) ? intval($data['Grade_n']) : $entry->rank; + $new_rating = isset($data['Gor']) ? intval($data['Gor']) : $entry->rating; + + // Only update if values changed + if ($new_rank !== $entry->rank || $new_rating !== $entry->rating) { + $wpdb->update( + "{$wpdb->prefix}go_form_entries", + ['rank' => $new_rank, 'rating' => $new_rating], + ['id' => $entry->id] + ); + $updated_count++; + } + } + } + + $redirect_url = add_query_arg( + array('page' => 'go-form-settings', 'form_id' => $form_id, 'egd_updated' => $updated_count), + admin_url('admin.php') + ); + wp_redirect($redirect_url); + exit; +} +add_action('admin_post_go_form_update_egd_data', 'go_form_update_egd_data'); + function go_form_update_entry() { go_form_admin_action('update_entry'); diff --git a/go-form-plugin/templates/settings-page.php b/go-form-plugin/templates/settings-page.php index 68ac1d6..c037b14 100644 --- a/go-form-plugin/templates/settings-page.php +++ b/go-form-plugin/templates/settings-page.php @@ -15,10 +15,14 @@ if (isset($_GET['deleted_field'])) echo '
Custom field deleted!
Entry updated!
' . esc_html($_GET['egd_updated']) . ' entries updated with EGD data!
First Name and Last Name are required!
A required custom field is missing!
Failed to update EGD data. Check error logs.