Move table to template

This commit is contained in:
2026-05-06 12:13:18 +02:00
parent 6af0d59ed9
commit 6884ce93a4
3 changed files with 232 additions and 201 deletions

View File

@@ -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 '<p>No entries yet.</p>';
return;
}
$headers = ['First Name', 'Last Name', 'Country', 'Club', 'Rank', 'EGD Number'];
if ($show_delete) {
$headers[] = 'Email';
$headers[] = 'Date Added';
$headers[] = 'Action';
}
echo '<table class="'.($show_delete ? 'wp-list-table widefat fixed striped' : 'go-entries-table').'">
<thead><tr><th>'.implode('</th><th>', $headers).'</th></tr></thead><tbody>';
foreach ($entries as $e) {
echo '<tr>';
echo '<td>'.esc_html($e->first_name).'</td>';
echo '<td>'.esc_html($e->last_name).'</td>';
echo '<td>'.esc_html($e->country).'</td>';
echo '<td>'.esc_html($e->club).'</td>';
echo '<td>'.esc_html($ranks[$e->rank]).'</td>';
echo '<td>'.esc_html($e->egd_number).'</td>';
if ($show_delete) {
echo '<td>'.esc_html($e->email).'</td>';
echo '<td>'.esc_html($e->created_at).'</td>';
echo '<td><form method="post" action="'.admin_url('admin-post.php').'">
'.wp_nonce_field('go_form_delete_action', 'go_form_delete_nonce', true, false).'
<input type="hidden" name="action" value="go_form_delete_entry">
<input type="hidden" name="entry_id" value="'.esc_attr($e->id).'">
<input type="submit" value="Delete" class="button delete" onclick="return confirm(\'Delete this entry?\')">
</form></td>';
}
echo '</tr>';
}
echo '</tbody></table>';
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 = '<div>Entry added successfully!</div>';
elseif (isset($_GET['form_error'])) {
$errors = ['1' => 'Please fill in all required fields.', 'rank' => 'Select valid rank'];
$msg = '<div>'.esc_html($errors[$_GET['form_error']] ?? $errors['1']).'</div>';
}
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 '<div class="notice notice-success"><p>Deleted!</p></div>';
if (isset($_GET['created'])) echo '<div class="notice notice-success"><p>Form created!</p></div>';
if (isset($_GET['deleted']))
echo '<div class="notice notice-success"><p>Deleted!</p></div>';
if (isset($_GET['created']))
echo '<div class="notice notice-success"><p>Form created!</p></div>';
echo '<div class="wrap"><h1>Go Form Settings</h1>
<h2>Create New Form</h2>
<form method="post" action="'.admin_url('admin-post.php').'">
'.wp_nonce_field('go_form_create_action', 'go_form_create_nonce', true, false).'
<form method="post" action="' . admin_url('admin-post.php') . '">
' . wp_nonce_field('go_form_create_action', 'go_form_create_nonce', true, false) . '
<input type="hidden" name="action" value="go_form_create_form">
<table class="form-table">
<tr><th><label for="form_name">Form Name:</label></th><td><input type="text" name="form_name" id="form_name" required></td></tr>
@@ -245,18 +220,21 @@ function go_form_settings_page() {
</form>
<h2>Manage Forms & Entries</h2>';
if (empty($forms)) { echo '<p>No forms yet.</p>'; return; }
if (empty($forms)) {
echo '<p>No forms yet.</p>';
return;
}
foreach ($forms as $f) {
$entries = $entries_by_form[$f->id] ?? [];
echo '<div style="margin-bottom:20px">
<h3>'.esc_html($f->name).' <span style="color:#888">(ID: '.esc_html($f->id).')</span></h3>
<p><small>Shortcode: <code>[go_form id='.esc_html($f->id).']</code></small></p>
<h3>' . esc_html($f->name) . ' <span style="color:#888">(ID: ' . esc_html($f->id) . ')</span></h3>
<p><small>Shortcode: <code>[go_form id=' . esc_html($f->id) . ']</code></small></p>
<form method="post" action="'.admin_url('admin-post.php').'" style="margin-bottom:15px">
'.wp_nonce_field('go_form_delete_form_action', 'go_form_delete_form_nonce', true, false).'
<form method="post" action="' . admin_url('admin-post.php') . '" style="margin-bottom:15px">
' . wp_nonce_field('go_form_delete_form_action', 'go_form_delete_form_nonce', true, false) . '
<input type="hidden" name="action" value="go_form_delete_form">
<input type="hidden" name="form_id" value="'.esc_attr($f->id).'">
<input type="hidden" name="form_id" value="' . esc_attr($f->id) . '">
<input type="submit" value="Delete Form" class="button delete" onclick="return confirm(\'Delete this form and ALL entries?\')">
</form>';

View File

@@ -1,17 +1,18 @@
<style>
#egd-popup-overlay {
#egd-popup-overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.5);
background: rgba(0, 0, 0, 0.5);
z-index: 1000;
justify-content: center;
align-items: center;
}
#egd-results {
}
#egd-results {
background: white;
max-height: 70vh;
overflow-y: auto;
@@ -20,10 +21,11 @@
border-radius: 5px;
max-width: 500px;
width: 90%;
box-shadow: 0 2px 10px rgba(0,0,0,0.2);
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
position: relative;
}
#egd-popup-close {
}
#egd-popup-close {
position: absolute;
top: 5px;
right: 10px;
@@ -31,18 +33,21 @@
border: none;
font-size: 20px;
cursor: pointer;
}
#egd-results h3 {
}
#egd-results h3 {
margin-top: 0;
}
.egd-result-item {
}
.egd-result-item {
padding: 8px 0;
border-bottom: 1px solid #eee;
cursor: pointer;
}
.egd-result-item:hover {
}
.egd-result-item:hover {
background-color: #f5f5f5;
}
}
</style>
<div id="form">
@@ -79,11 +84,11 @@
<input type="submit" name="go_form_submit" value="Submit">
</form>
<h2>Players</h2>
<?php go_form_render_entries_table(go_form_get_entries($form_id)); ?>
<?php go_form_render_entries_table(go_form_get_entries($form_id)); ?>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
document.addEventListener('DOMContentLoaded', function () {
const contentDiv = document.getElementById('egd-results-content');
const egdOverlay = document.getElementById('egd-popup-overlay');
@@ -148,5 +153,5 @@ document.addEventListener('DOMContentLoaded', function() {
document.getElementById('egd-search').addEventListener('click', fetchPlayers);
document.getElementById('egd-popup-close').addEventListener('click', closePopup);
});
});
</script>

View File

@@ -0,0 +1,48 @@
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Country</th>
<th>Club</th>
<th>Rank</th>
<th>EGD Number</th>
<?php if ($show_admin): ?>
<th>Email</th>
<th>Date Added</th>
<th>Action</th>
<?php endif; ?>
</tr>
</thead>
<tbody>
<?php foreach ($entries as $e): ?>
<tr>
<td> <?= esc_html($e->first_name) ?> </td>
<td> <?= esc_html($e->last_name) ?> </td>
<td> <?= esc_html($e->country) ?> </td>
<td> <?= esc_html($e->club) ?> </td>
<td> <?= esc_html($ranks[$e->rank]) ?> </td>
<td> <?= esc_html($e->egd_number) ?> </td>
<?php if ($show_admin): ?>
<td> <?= esc_html($e->email) ?> </td>
<td> <?= esc_html($e->created_at) ?> </td>
<td>
<form method="post" action=" <?= admin_url('admin-post.php') ?> ">
<?= wp_nonce_field('go_form_delete_action', 'go_form_delete_nonce', true, false) ?>
<input type="hidden" name="action" value="go_form_delete_entry">
<input type="hidden" name="entry_id" value=" <?= esc_attr($e->id) ?> ">
<input type="submit" value="Delete" class="button delete" onclick="return confirm('Delete this entry?')">
</form>
</td>
<?php endif; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>