get_charset_collate();
$votes = $wpdb->prefix . ‘as45_votes’;
$rounds = $wpdb->prefix . ‘as45_rounds’;
require_once(ABSPATH . ‘wp-admin/includes/upgrade.php’);
dbDelta(“CREATE TABLE $rounds (
id INT NOT NULL AUTO_INCREMENT,
round_name VARCHAR(255),
close_time DATETIME,
status VARCHAR(20) DEFAULT ‘open’,
PRIMARY KEY (id)
) $charset;”);
dbDelta(“CREATE TABLE $votes (
id INT NOT NULL AUTO_INCREMENT,
round_id INT,
email VARCHAR(255),
vote_3 VARCHAR(100),
vote_2 VARCHAR(100),
vote_1 VARCHAR(100),
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) $charset;”);
}
/* —————————–
SQUAD LIST
——————————*/
function as45mom_players() {
return [
“Sascha”,”Alan”,”Jiggsy”,”Andrew P”,”Paul”,”Michael”,
“Bjorn”,”Luciano”,”Arnie”,”Richie”,”Coops”,”Slippers”,
“David Lee”,”Toby”,”Daniel”,”Damien”,”Ferris”,”I-Van”
];
}
/* —————————–
SHORTCODE FRONTEND FORM
Use [motm_vote]
——————————*/
add_shortcode(‘motm_vote’, ‘as45mom_vote_form’);
function as45mom_vote_form() {
global $wpdb;
$rounds = $wpdb->prefix . ‘as45_rounds’;
$votes = $wpdb->prefix . ‘as45_votes’;
$round = $wpdb->get_row(“SELECT * FROM $rounds WHERE status=’open’ ORDER BY id DESC LIMIT 1”);
if (!$round) return “
No active round currently open.
“;
if (strtotime($round->close_time) < current_time('timestamp')) { return "
Voting closed.
“;
}
if ($_SERVER[‘REQUEST_METHOD’] === ‘POST’ && isset($_POST[‘as45_submit’])) {
$email = sanitize_email($_POST[’email’]);
$v3 = sanitize_text_field($_POST[‘vote_3’]);
$v2 = sanitize_text_field($_POST[‘vote_2’]);
$v1 = sanitize_text_field($_POST[‘vote_1’]);
if ($v3 == $v2 || $v3 == $v1 || $v2 == $v1) {
return “
Please choose 3 different players.
“;
}
$exists = $wpdb->get_var($wpdb->prepare(
“SELECT COUNT(*) FROM $votes WHERE round_id=%d AND email=%s”,
$round->id, $email
));
if ($exists) {
return “
You have already voted this round.
“;
}
$wpdb->insert($votes, [
’round_id’ => $round->id,
’email’ => $email,
‘vote_3’ => $v3,
‘vote_2’ => $v2,
‘vote_1’ => $v1
]);
return “
Vote submitted successfully.
“;
}
$players = as45mom_players();
ob_start(); ?>
round_name); ?>
Voting closes: close_time); ?>
prefix . ‘as45_rounds’;
if (isset($_POST[‘create_round’])) {
$wpdb->insert($rounds, [
’round_name’ => sanitize_text_field($_POST[’round_name’]),
‘close_time’ => sanitize_text_field($_POST[‘close_time’]),
‘status’ => ‘open’
]);
echo “
Round created.
“;
}
?>
Create Round
prefix . ‘as45_votes’;
$rounds = $wpdb->prefix . ‘as45_rounds’;
$round = $wpdb->get_row(“SELECT * FROM $rounds ORDER BY id DESC LIMIT 1”);
if (!$round) return “No results yet.”;
if (strtotime($round->close_time) > current_time(‘timestamp’)) {
return “Results available after voting closes.”;
}
$rows = $wpdb->get_results($wpdb->prepare(
“SELECT * FROM $votes WHERE round_id=%d”,
$round->id
));
$scores = [];
foreach($rows as $r) {
$scores[$r->vote_3] = ($scores[$r->vote_3] ?? 0) + 3;
$scores[$r->vote_2] = ($scores[$r->vote_2] ?? 0) + 2;
$scores[$r->vote_1] = ($scores[$r->vote_1] ?? 0) + 1;
}
arsort($scores);
$html = “
{$round->round_name} Results
| Player | Points |
|---|---|
| $player | $pts |
“;
return $html;
}