diff --git a/go-form-plugin/go-form-plugin.php b/go-form-plugin/go-form-plugin.php index 359d151..8714b6f 100644 --- a/go-form-plugin/go-form-plugin.php +++ b/go-form-plugin/go-form-plugin.php @@ -6,10 +6,12 @@ * Author: Nikola Petrov */ -if (!defined('ABSPATH')) exit; +if (!defined('ABSPATH')) + exit; // ========== Database ========== -function go_form_activate() { +function go_form_activate() +{ global $wpdb; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); $charset = $wpdb->get_charset_collate(); @@ -46,7 +48,8 @@ function go_form_activate() { } register_activation_hook(__FILE__, 'go_form_activate'); -function go_form_uninstall() { +function go_form_uninstall() +{ global $wpdb; $wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}go_form_entries"); $wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}go_form_forms"); @@ -65,14 +68,16 @@ $ranks = [ 35 => '6d', 36 => '7d', 37 => '8d', 38 => '9d', 39 => '1p', 40 => '2p', 41 => '3p', 42 => '4p', 43 => '5p', 44 => '6p', 45 => '7p', 46 => '8p', 47 => '9p' - ]; +]; -function go_form_get_forms() { +function go_form_get_forms() +{ global $wpdb; return $wpdb->get_results("SELECT * FROM {$wpdb->prefix}go_form_forms ORDER BY name ASC"); } -function go_form_get_entries($form_id) { +function go_form_get_entries($form_id) +{ global $wpdb; return $wpdb->get_results($wpdb->prepare( "SELECT * FROM {$wpdb->prefix}go_form_entries WHERE form_id = %d ORDER BY rank DESC", @@ -80,50 +85,16 @@ function go_form_get_entries($form_id) { )); } -function go_form_render_entries_table($entries, $show_delete = false) { +function go_form_render_entries_table($entries, $show_admin = false) +{ global $ranks; - if (empty($entries)) { - echo '

No entries yet.

'; - return; - } - - $headers = ['First Name', 'Last Name', 'Country', 'Club', 'Rank', 'EGD Number']; - if ($show_delete) { - $headers[] = 'Email'; - $headers[] = 'Date Added'; - $headers[] = 'Action'; - } - - echo ' - '; - - foreach ($entries as $e) { - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - - if ($show_delete) { - echo ''; - echo ''; - echo ''; - } - echo ''; - } - echo '
'.implode('', $headers).'
'.esc_html($e->first_name).''.esc_html($e->last_name).''.esc_html($e->country).''.esc_html($e->club).''.esc_html($ranks[$e->rank]).''.esc_html($e->egd_number).''.esc_html($e->email).''.esc_html($e->created_at).'
- '.wp_nonce_field('go_form_delete_action', 'go_form_delete_nonce', true, false).' - - - -
'; + include "templates/table.php"; } // ========== Form Handling ========== -function go_form_handle_submission() { +function go_form_handle_submission() +{ if (!isset($_POST['go_form_nonce']) || !wp_verify_nonce($_POST['go_form_nonce'], 'go_form_action')) wp_die('Security check failed'); @@ -140,48 +111,44 @@ function go_form_handle_submission() { ]; if (empty($data['first_name']) || empty($data['last_name'])) { - wp_redirect($_SERVER['HTTP_REFERER'] . '?form_error=1#form'); exit; + wp_redirect($_SERVER['HTTP_REFERER']); + exit; } if ($data['rank'] < 0 || $data['rank'] > 47) { - wp_redirect($_SERVER['HTTP_REFERER'] . '?form_error=rank#form'); exit; + wp_redirect($_SERVER['HTTP_REFERER']); + exit; } $wpdb->insert("{$wpdb->prefix}go_form_entries", $data); - wp_redirect($_SERVER['HTTP_REFERER'] . '?form_success=1#form'); + wp_redirect($_SERVER['HTTP_REFERER']); exit; } add_action('admin_post_go_form_submit', 'go_form_handle_submission'); add_action('admin_post_nopriv_go_form_submit', 'go_form_handle_submission'); // ========== Shortcode ========== -function go_form_shortcode($atts) { +function go_form_shortcode($atts) +{ global $ranks; $form_id = intval(shortcode_atts(['id' => 1], $atts)['id']); - $msg = ''; - if (isset($_GET['form_success']) && $_GET['form_success'] == 1) - $msg = '
Entry added successfully!
'; - elseif (isset($_GET['form_error'])) { - $errors = ['1' => 'Please fill in all required fields.', 'rank' => 'Select valid rank']; - $msg = '
'.esc_html($errors[$_GET['form_error']] ?? $errors['1']).'
'; - } - ob_start(); - echo $msg; - include plugin_dir_path(__FILE__) . 'templates/form-shortcode.php'; + include 'templates/form-shortcode.php'; return ob_get_clean(); } add_shortcode('go_form', 'go_form_shortcode'); // ========== Admin Actions ========== -function go_form_admin_action($type) { +function go_form_admin_action($type) +{ if (!isset($_POST["go_form_{$type}_nonce"]) || !wp_verify_nonce($_POST["go_form_{$type}_nonce"], "go_form_{$type}_action")) wp_die('Security check failed'); if (!current_user_can('manage_options')) wp_die('Insufficient permissions.'); } -function go_form_delete_entry() { +function go_form_delete_entry() +{ go_form_admin_action('delete'); if (isset($_POST['entry_id'])) { global $wpdb; @@ -192,7 +159,8 @@ function go_form_delete_entry() { } add_action('admin_post_go_form_delete_entry', 'go_form_delete_entry'); -function go_form_create_form() { +function go_form_create_form() +{ go_form_admin_action('create'); if (isset($_POST['form_name']) && !empty($_POST['form_name'])) { global $wpdb; @@ -203,7 +171,8 @@ function go_form_create_form() { } add_action('admin_post_go_form_create_form', 'go_form_create_form'); -function go_form_delete_form() { +function go_form_delete_form() +{ go_form_admin_action('delete_form'); if (isset($_POST['form_id'])) { global $wpdb; @@ -217,26 +186,32 @@ function go_form_delete_form() { add_action('admin_post_go_form_delete_form', 'go_form_delete_form'); // ========== Admin Page ========== -function go_form_admin_menu() { +function go_form_admin_menu() +{ add_menu_page('Go Form Settings', 'Go Form', 'manage_options', 'go-form-settings', 'go_form_settings_page', 'dashicons-admin-generic'); } add_action('admin_menu', 'go_form_admin_menu'); -function go_form_settings_page() { - if (!current_user_can('manage_options')) wp_die('No access.'); +function go_form_settings_page() +{ + if (!current_user_can('manage_options')) + wp_die('No access.'); $forms = go_form_get_forms(); $entries_by_form = []; - foreach ($forms as $f) $entries_by_form[$f->id] = go_form_get_entries($f->id); + foreach ($forms as $f) + $entries_by_form[$f->id] = go_form_get_entries($f->id); - if (isset($_GET['deleted'])) echo '

Deleted!

'; - if (isset($_GET['created'])) echo '

Form created!

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

Deleted!

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

Form created!

'; echo '

Go Form Settings

Create New Form

-
- '.wp_nonce_field('go_form_create_action', 'go_form_create_nonce', true, false).' + + ' . wp_nonce_field('go_form_create_action', 'go_form_create_nonce', true, false) . ' @@ -245,18 +220,21 @@ function go_form_settings_page() {

Manage Forms & Entries

'; - if (empty($forms)) { echo '

No forms yet.

'; return; } + if (empty($forms)) { + echo '

No forms yet.

'; + return; + } foreach ($forms as $f) { $entries = $entries_by_form[$f->id] ?? []; echo '
-

'.esc_html($f->name).' (ID: '.esc_html($f->id).')

-

Shortcode: [go_form id='.esc_html($f->id).']

+

' . esc_html($f->name) . ' (ID: ' . esc_html($f->id) . ')

+

Shortcode: [go_form id=' . esc_html($f->id) . ']

-
- '.wp_nonce_field('go_form_delete_form_action', 'go_form_delete_form_nonce', true, false).' + + ' . wp_nonce_field('go_form_delete_form_action', 'go_form_delete_form_nonce', true, false) . ' - + '; diff --git a/go-form-plugin/templates/form-shortcode.php b/go-form-plugin/templates/form-shortcode.php index d6170be..8f259a9 100644 --- a/go-form-plugin/templates/form-shortcode.php +++ b/go-form-plugin/templates/form-shortcode.php @@ -1,48 +1,53 @@
@@ -51,23 +56,23 @@ - - - - - - - + + + + + + + - + $label) { + echo "\n"; + } + ?> + + + +
@@ -79,74 +84,74 @@

Players

- +
+ function selectPlayer(player) { + document.getElementById('first_name').value = player.Name || player.Real_Name || ''; + document.getElementById('last_name').value = player.Last_Name || player.Real_Last_Name || ''; + document.getElementById('country').value = player.Country_Code || ''; + document.getElementById('club').value = player.Club || ''; + document.getElementById('rank').value = player.Grade_n || 0; + document.getElementById('egd_number').value = player.Pin_Player || ''; + closePopup(); + } + + function closePopup() { + egdOverlay.style.display = 'none'; + } + + document.getElementById('egd-search').addEventListener('click', fetchPlayers); + document.getElementById('egd-popup-close').addEventListener('click', closePopup); + }); + \ No newline at end of file diff --git a/go-form-plugin/templates/table.php b/go-form-plugin/templates/table.php new file mode 100644 index 0000000..544b3e0 --- /dev/null +++ b/go-form-plugin/templates/table.php @@ -0,0 +1,48 @@ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
First NameLast NameCountryClubRankEGD NumberEmailDate AddedAction
first_name) ?> last_name) ?> country) ?> club) ?> rank]) ?> egd_number) ?> email) ?> created_at) ?> +
+ + + + +
+
\ No newline at end of file