Format indent
This commit is contained in:
@@ -7,25 +7,25 @@
|
||||
*/
|
||||
|
||||
if (!defined('ABSPATH'))
|
||||
exit;
|
||||
exit;
|
||||
|
||||
// ========== Database ==========
|
||||
function go_form_activate()
|
||||
{
|
||||
global $wpdb;
|
||||
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
|
||||
$charset = $wpdb->get_charset_collate();
|
||||
$forms = $wpdb->prefix . 'go_form_forms';
|
||||
$entries = $wpdb->prefix . 'go_form_entries';
|
||||
global $wpdb;
|
||||
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
|
||||
$charset = $wpdb->get_charset_collate();
|
||||
$forms = $wpdb->prefix . 'go_form_forms';
|
||||
$entries = $wpdb->prefix . 'go_form_entries';
|
||||
|
||||
dbDelta("CREATE TABLE $forms (
|
||||
dbDelta("CREATE TABLE $forms (
|
||||
id mediumint(9) NOT NULL AUTO_INCREMENT,
|
||||
name varchar(255) NOT NULL,
|
||||
created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
PRIMARY KEY (id)
|
||||
) $charset;");
|
||||
|
||||
dbDelta("CREATE TABLE $entries (
|
||||
dbDelta("CREATE TABLE $entries (
|
||||
id mediumint(9) NOT NULL AUTO_INCREMENT,
|
||||
form_id mediumint(9) DEFAULT 1,
|
||||
first_name varchar(100) NOT NULL,
|
||||
@@ -41,19 +41,19 @@ function go_form_activate()
|
||||
KEY form_id (form_id)
|
||||
) $charset;");
|
||||
|
||||
// Add default form if none exist
|
||||
$existing_forms = $wpdb->get_var("SELECT COUNT(*) FROM $forms");
|
||||
if ($existing_forms == 0) {
|
||||
$wpdb->insert($forms, ['name' => 'Default Form']);
|
||||
}
|
||||
// Add default form if none exist
|
||||
$existing_forms = $wpdb->get_var("SELECT COUNT(*) FROM $forms");
|
||||
if ($existing_forms == 0) {
|
||||
$wpdb->insert($forms, ['name' => 'Default Form']);
|
||||
}
|
||||
}
|
||||
register_activation_hook(__FILE__, 'go_form_activate');
|
||||
|
||||
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");
|
||||
global $wpdb;
|
||||
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}go_form_entries");
|
||||
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}go_form_forms");
|
||||
}
|
||||
register_uninstall_hook(__FILE__, 'go_form_uninstall');
|
||||
|
||||
@@ -73,57 +73,57 @@ $ranks = [
|
||||
|
||||
function go_form_get_forms()
|
||||
{
|
||||
global $wpdb;
|
||||
return $wpdb->get_results("SELECT * FROM {$wpdb->prefix}go_form_forms ORDER BY name ASC");
|
||||
global $wpdb;
|
||||
return $wpdb->get_results("SELECT * FROM {$wpdb->prefix}go_form_forms ORDER BY name ASC");
|
||||
}
|
||||
|
||||
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",
|
||||
$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",
|
||||
$form_id
|
||||
));
|
||||
}
|
||||
|
||||
function go_form_render_entries_table($entries, $show_admin = false)
|
||||
{
|
||||
global $ranks;
|
||||
global $ranks;
|
||||
|
||||
include "templates/table.php";
|
||||
include "templates/table.php";
|
||||
}
|
||||
|
||||
// ========== Form Handling ==========
|
||||
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');
|
||||
if (!isset($_POST['go_form_nonce']) || !wp_verify_nonce($_POST['go_form_nonce'], 'go_form_action'))
|
||||
wp_die('Security check failed');
|
||||
|
||||
global $wpdb;
|
||||
$data = [
|
||||
'form_id' => isset($_POST['form_id']) ? intval($_POST['form_id']) : 1,
|
||||
'first_name' => sanitize_text_field($_POST['first_name']),
|
||||
'last_name' => sanitize_text_field($_POST['last_name']),
|
||||
'country' => sanitize_text_field($_POST['country'] ?? ''),
|
||||
'club' => sanitize_text_field($_POST['club'] ?? ''),
|
||||
'rank' => intval($_POST['rank']),
|
||||
'email' => sanitize_email($_POST['email'] ?? ''),
|
||||
'egd_number' => sanitize_text_field($_POST['egd_number'] ?? ''),
|
||||
'comment' => sanitize_textarea_field($_POST['comment'] ?? '')
|
||||
];
|
||||
global $wpdb;
|
||||
$data = [
|
||||
'form_id' => isset($_POST['form_id']) ? intval($_POST['form_id']) : 1,
|
||||
'first_name' => sanitize_text_field($_POST['first_name']),
|
||||
'last_name' => sanitize_text_field($_POST['last_name']),
|
||||
'country' => sanitize_text_field($_POST['country'] ?? ''),
|
||||
'club' => sanitize_text_field($_POST['club'] ?? ''),
|
||||
'rank' => intval($_POST['rank']),
|
||||
'email' => sanitize_email($_POST['email'] ?? ''),
|
||||
'egd_number' => sanitize_text_field($_POST['egd_number'] ?? ''),
|
||||
'comment' => sanitize_textarea_field($_POST['comment'] ?? '')
|
||||
];
|
||||
|
||||
if (empty($data['first_name']) || empty($data['last_name'])) {
|
||||
wp_redirect($_SERVER['HTTP_REFERER']);
|
||||
exit;
|
||||
}
|
||||
if ($data['rank'] < 0 || $data['rank'] > 47) {
|
||||
wp_redirect($_SERVER['HTTP_REFERER']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$wpdb->insert("{$wpdb->prefix}go_form_entries", $data);
|
||||
if (empty($data['first_name']) || empty($data['last_name'])) {
|
||||
wp_redirect($_SERVER['HTTP_REFERER']);
|
||||
exit;
|
||||
}
|
||||
if ($data['rank'] < 0 || $data['rank'] > 47) {
|
||||
wp_redirect($_SERVER['HTTP_REFERER']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$wpdb->insert("{$wpdb->prefix}go_form_entries", $data);
|
||||
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');
|
||||
@@ -131,85 +131,85 @@ add_action('admin_post_nopriv_go_form_submit', 'go_form_handle_submission');
|
||||
// ========== Shortcode ==========
|
||||
function go_form_shortcode($atts)
|
||||
{
|
||||
global $ranks;
|
||||
$form_id = intval(shortcode_atts(['id' => 1], $atts)['id']);
|
||||
global $ranks;
|
||||
$form_id = intval(shortcode_atts(['id' => 1], $atts)['id']);
|
||||
|
||||
ob_start();
|
||||
include 'templates/form-shortcode.php';
|
||||
return ob_get_clean();
|
||||
ob_start();
|
||||
include 'templates/form-shortcode.php';
|
||||
return ob_get_clean();
|
||||
}
|
||||
add_shortcode('go_form', 'go_form_shortcode');
|
||||
|
||||
// ========== Admin Actions ==========
|
||||
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.');
|
||||
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()
|
||||
{
|
||||
go_form_admin_action('delete');
|
||||
if (isset($_POST['entry_id'])) {
|
||||
global $wpdb;
|
||||
$wpdb->delete("{$wpdb->prefix}go_form_entries", ['id' => intval($_POST['entry_id'])]);
|
||||
}
|
||||
wp_redirect(admin_url('admin.php?page=go-form-settings&deleted=1'));
|
||||
exit;
|
||||
go_form_admin_action('delete');
|
||||
if (isset($_POST['entry_id'])) {
|
||||
global $wpdb;
|
||||
$wpdb->delete("{$wpdb->prefix}go_form_entries", ['id' => intval($_POST['entry_id'])]);
|
||||
}
|
||||
wp_redirect(admin_url('admin.php?page=go-form-settings&deleted=1'));
|
||||
exit;
|
||||
}
|
||||
add_action('admin_post_go_form_delete_entry', 'go_form_delete_entry');
|
||||
|
||||
function go_form_create_form()
|
||||
{
|
||||
go_form_admin_action('create');
|
||||
if (isset($_POST['form_name']) && !empty($_POST['form_name'])) {
|
||||
global $wpdb;
|
||||
$wpdb->insert("{$wpdb->prefix}go_form_forms", ['name' => sanitize_text_field($_POST['form_name'])]);
|
||||
}
|
||||
wp_redirect(admin_url('admin.php?page=go-form-settings&created=1'));
|
||||
exit;
|
||||
go_form_admin_action('create');
|
||||
if (isset($_POST['form_name']) && !empty($_POST['form_name'])) {
|
||||
global $wpdb;
|
||||
$wpdb->insert("{$wpdb->prefix}go_form_forms", ['name' => sanitize_text_field($_POST['form_name'])]);
|
||||
}
|
||||
wp_redirect(admin_url('admin.php?page=go-form-settings&created=1'));
|
||||
exit;
|
||||
}
|
||||
add_action('admin_post_go_form_create_form', 'go_form_create_form');
|
||||
|
||||
function go_form_delete_form()
|
||||
{
|
||||
go_form_admin_action('delete_form');
|
||||
if (isset($_POST['form_id'])) {
|
||||
global $wpdb;
|
||||
$id = intval($_POST['form_id']);
|
||||
$wpdb->delete("{$wpdb->prefix}go_form_entries", ['form_id' => $id]);
|
||||
$wpdb->delete("{$wpdb->prefix}go_form_forms", ['id' => $id]);
|
||||
}
|
||||
wp_redirect(admin_url('admin.php?page=go-form-settings&deleted=1'));
|
||||
exit;
|
||||
go_form_admin_action('delete_form');
|
||||
if (isset($_POST['form_id'])) {
|
||||
global $wpdb;
|
||||
$id = intval($_POST['form_id']);
|
||||
$wpdb->delete("{$wpdb->prefix}go_form_entries", ['form_id' => $id]);
|
||||
$wpdb->delete("{$wpdb->prefix}go_form_forms", ['id' => $id]);
|
||||
}
|
||||
wp_redirect(admin_url('admin.php?page=go-form-settings&deleted=1'));
|
||||
exit;
|
||||
}
|
||||
add_action('admin_post_go_form_delete_form', 'go_form_delete_form');
|
||||
|
||||
// ========== Admin Page ==========
|
||||
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_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.');
|
||||
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);
|
||||
$forms = go_form_get_forms();
|
||||
$entries_by_form = [];
|
||||
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>
|
||||
echo '<div class="wrap"><h1>Go Form Settings</h1>
|
||||
|
||||
<h2>Create New Form</h2>
|
||||
<form method="post" action="' . admin_url('admin-post.php') . '">
|
||||
@@ -222,14 +222,14 @@ 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">
|
||||
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>
|
||||
|
||||
@@ -240,8 +240,8 @@ function go_form_settings_page()
|
||||
<input type="submit" value="Delete Form" class="button delete" onclick="return confirm(\'Delete this form and ALL entries?\')">
|
||||
</form>';
|
||||
|
||||
go_form_render_entries_table($entries, true);
|
||||
echo '</div>';
|
||||
}
|
||||
go_form_render_entries_table($entries, true);
|
||||
echo '</div>';
|
||||
}
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user