From cdda18f6de0e4fc031ce8c20ee4da89f5c8ffdb3 Mon Sep 17 00:00:00 2001 From: Nikola Petrov Date: Tue, 12 May 2026 13:25:42 +0200 Subject: [PATCH] Add update egd entries --- go-form-plugin/go-form-plugin.php | 60 ++++++++++++++++++++++ go-form-plugin/templates/settings-page.php | 5 ++ 2 files changed, 65 insertions(+) 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!

'; if (isset($_GET['updated_entry'])) echo '

Entry updated!

'; +if (isset($_GET['egd_updated'])) + echo '

' . esc_html($_GET['egd_updated']) . ' entries updated with EGD data!

'; if (isset($_GET['error']) && $_GET['error'] == 'missing_required') echo '

First Name and Last Name are required!

'; if (isset($_GET['error']) && $_GET['error'] == 'missing_required_field') echo '

A required custom field is missing!

'; +if (isset($_GET['error']) && $_GET['error'] == 'egd_update_failed') + echo '

Failed to update EGD data. Check error logs.

'; ?>
@@ -41,6 +45,7 @@ if (isset($_GET['error']) && $_GET['error'] == 'missing_required_field') action_button('go_form_export_pairgoth', 'Export to Pairgoth', 'Confrm Export', $selected_form_id); action_button('go_form_export_opengotha', 'Export to Opengoth', 'Confrm Export', $selected_form_id); action_button('go_form_export_mcmahon', 'Export to McMahon', 'Confrm Export', $selected_form_id); + action_button('go_form_update_egd_data', 'Update EGD Data', 'Update rank and rating from EGD for all entries with EGD numbers?', $selected_form_id); echo '
';