Untitled
unknown
plain_text
10 months ago
8.2 kB
12
Indexable
<?php
/*
Plugin Name: Simple Raffle / Tirage au sort
Description: Shortcode [raffle_registration] pour inscrire les participants et page d'admin pour exporter et tirer un gagnant.
Version: 1.0
Author: ChatGPT (pour toi)
*/
if (!defined('ABSPATH')) exit;
global $raffle_db_version;
$raffle_db_version = '1.0';
// Activation : créer la table
function raffle_install() {
global $wpdb, $raffle_db_version;
$table_name = $wpdb->prefix . 'raffle_entries';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id BIGINT(20) NOT NULL AUTO_INCREMENT,
name VARCHAR(191) NOT NULL,
email VARCHAR(191) NOT NULL,
phone VARCHAR(50) DEFAULT '',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY email (email)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option('raffle_db_version', $raffle_db_version);
}
register_activation_hook(__FILE__, 'raffle_install');
// Shortcode : form d'inscription
function raffle_registration_shortcode($atts) {
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['raffle_submit'])) {
return raffle_handle_submission();
}
ob_start();
?>
<form method="post" class="raffle-form">
<?php wp_nonce_field('raffle_submit_action', 'raffle_nonce'); ?>
<p>
<label for="raffle_name">Nom</label><br>
<input type="text" id="raffle_name" name="raffle_name" required>
</p>
<p>
<label for="raffle_email">Email</label><br>
<input type="email" id="raffle_email" name="raffle_email" required>
</p>
<p>
<label for="raffle_phone">Téléphone (optionnel)</label><br>
<input type="text" id="raffle_phone" name="raffle_phone">
</p>
<p>
<button type="submit" name="raffle_submit">S'inscrire au tirage</button>
</p>
</form>
<?php
return ob_get_clean();
}
add_shortcode('raffle_registration', 'raffle_registration_shortcode');
// Gestion de la soumission
function raffle_handle_submission() {
if (!isset($_POST['raffle_nonce']) || !wp_verify_nonce($_POST['raffle_nonce'], 'raffle_submit_action')) {
return '<p>Erreur de sécurité. Réessayez.</p>';
}
$name = sanitize_text_field($_POST['raffle_name']);
$email = sanitize_email($_POST['raffle_email']);
$phone = sanitize_text_field($_POST['raffle_phone']);
if (!is_email($email)) {
return '<p>Email invalide.</p>' . raffle_registration_shortcode([]);
}
global $wpdb;
$table = $wpdb->prefix . 'raffle_entries';
// Empêcher doublons par email
$exists = $wpdb->get_var($wpdb->prepare("SELECT id FROM $table WHERE email = %s", $email));
if ($exists) {
return '<p>Vous êtes déjà inscrit avec cet email. Merci !</p>';
}
$inserted = $wpdb->insert(
$table,
[
'name' => $name,
'email' => $email,
'phone' => $phone,
'created_at' => current_time('mysql', 1)
],
['%s','%s','%s','%s']
);
if ($inserted) {
// tu peux ajouter ici un envoi d'email de confirmation si tu veux
return '<p>Inscription confirmée ! Bonne chance 🍀</p>';
} else {
return '<p>Erreur lors de l\'enregistrement. Réessayez plus tard.</p>';
}
}
// Ajout menu admin
function raffle_admin_menu() {
add_menu_page('Raffle Entries', 'Raffle Entries', 'manage_options', 'raffle-entries', 'raffle_admin_page', 'dashicons-tickets', 26);
}
add_action('admin_menu', 'raffle_admin_menu');
// Admin page : lister, export csv, tirer au sort
function raffle_admin_page() {
if (!current_user_can('manage_options')) {
wp_die('Accès refusé');
}
global $wpdb;
$table = $wpdb->prefix . 'raffle_entries';
// Actions : export CSV
if (isset($_POST['raffle_export_csv']) && check_admin_referer('raffle_export_action', 'raffle_export_nonce')) {
$rows = $wpdb->get_results("SELECT * FROM $table ORDER BY created_at DESC", ARRAY_A);
if ($rows) {
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=raffle_entries_' . date('Ymd_His') . '.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array_keys($rows[0]));
foreach ($rows as $r) fputcsv($output, $r);
exit;
} else {
echo '<div class="notice notice-warning"><p>Aucune entrée à exporter.</p></div>';
}
}
// Action : pick winner
$winner = null;
if (isset($_POST['raffle_pick_winner']) && check_admin_referer('raffle_pick_action', 'raffle_pick_nonce')) {
$count = $wpdb->get_var("SELECT COUNT(*) FROM $table");
if ($count == 0) {
echo '<div class="notice notice-error"><p>Pas d\'inscrit pour tirer au sort.</p></div>';
} else {
// tirer au sort aléatoirement
// Méthode: sélectionner un ID aléatoire dans la plage existante pour éviter ORDER BY RAND sur large table
$ids = $wpdb->get_col("SELECT id FROM $table");
if ($ids && is_array($ids)) {
$random_key = array_rand($ids);
$random_id = intval($ids[$random_key]);
$winner = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table WHERE id = %d", $random_id), ARRAY_A);
}
}
}
// Récupérer dernières entrées pour affichage
$entries = $wpdb->get_results("SELECT * FROM $table ORDER BY created_at DESC LIMIT 200", ARRAY_A);
?>
<div class="wrap">
<h1>Raffle Entries</h1>
<form method="post" style="display:inline-block; margin-right:10px;">
<?php wp_nonce_field('raffle_export_action', 'raffle_export_nonce'); ?>
<input type="submit" name="raffle_export_csv" class="button" value="Exporter CSV">
</form>
<form method="post" style="display:inline-block;">
<?php wp_nonce_field('raffle_pick_action', 'raffle_pick_nonce'); ?>
<input type="submit" name="raffle_pick_winner" class="button button-primary" value="Tirer au sort un gagnant">
</form>
<?php if ($winner): ?>
<h2 style="margin-top:20px;">🎉 Gagnant :</h2>
<table class="widefat fixed" style="max-width:800px;">
<thead><tr><th>ID</th><th>Nom</th><th>Email</th><th>Téléphone</th><th>Inscrit le</th></tr></thead>
<tbody>
<tr>
<td><?php echo esc_html($winner['id']); ?></td>
<td><?php echo esc_html($winner['name']); ?></td>
<td><?php echo esc_html($winner['email']); ?></td>
<td><?php echo esc_html($winner['phone']); ?></td>
<td><?php echo esc_html($winner['created_at']); ?></td>
</tr>
</tbody>
</table>
<?php endif; ?>
<h2 style="margin-top:20px;">Dernières inscriptions (max 200)</h2>
<table class="widefat fixed">
<thead><tr><th>ID</th><th>Nom</th><th>Email</th><th>Téléphone</th><th>Inscrit le</th></tr></thead>
<tbody>
<?php if ($entries): foreach ($entries as $e): ?>
<tr>
<td><?php echo esc_html($e['id']); ?></td>
<td><?php echo esc_html($e['name']); ?></td>
<td><?php echo esc_html($e['email']); ?></td>
<td><?php echo esc_html($e['phone']); ?></td>
<td><?php echo esc_html($e['created_at']); ?></td>
</tr>
<?php endforeach; else: ?>
<tr><td colspan="5">Aucune inscription.</td></tr>
<?php endif; ?>
</tbody>
</table>
</div>
<?php
}
// Désactivation : optionnel, ne pas supprimer la table pour garder les données
// add deactivation hook si tu veux supprimer la table à la désactivation : register_uninstall_hook etc.
Editor is loading...
Leave a Comment