table = $wpdb->prefix . ‘Legal_Eagle_Weekly_picks’;
register_activation_hook(__FILE__, array($this, ‘activate’));
add_shortcode(‘Legal_Eagle_Weekly_picks’, array($this, ‘shortcode’));
add_action(‘init’, array($this, ‘handle_submission’));
add_action(‘admin_menu’, array($this, ‘admin_menu’));
}
public function activate(){
global $wpdb; $charset_collate = $wpdb->get_charset_collate();
$sql = “CREATE TABLE {$this->table} (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id BIGINT UNSIGNED NOT NULL,
week VARCHAR(20) NOT NULL,
pick TEXT NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY unique_user_week (user_id, week),
KEY week_idx (week)
) $charset_collate;”;
require_once ABSPATH . ‘wp-admin/includes/upgrade.php’;
dbDelta($sql);
}
private function current_user_allowed(){
if (!is_user_logged_in()) return false;
if (empty($this->allowed_identifiers)) return true; // if not configured yet, let anyone (for dev)
$user = wp_get_current_user();
$set = array_map(‘strtolower’, $this->allowed_identifiers);
return in_array(strtolower($user->user_login), $set, true) || in_array(strtolower($user->user_email), $set, true);
}
public function handle_submission(){
if (!isset($_POST[‘swp_nonce’]) || !wp_verify_nonce($_POST[‘swp_nonce’], ‘swp_submit’)) return;
if (!is_user_logged_in()) return;
if (!$this->current_user_allowed()) return;
if (!isset($_POST[‘swp_action’]) || $_POST[‘swp_action’] !== ‘submit_pick’) return;
$week = isset($_POST[‘swp_week’]) ? sanitize_text_field(wp_unslash($_POST[‘swp_week’])) : ”;
$pick = isset($_POST[‘swp_pick’]) ? wp_kses_post(wp_unslash($_POST[‘swp_pick’])) : ”;
if ($week === ” || $pick === ”){
wp_redirect(add_query_arg(‘swp_msg’, ‘missing’, wp_get_referer()));
exit;
}
global $wpdb; $user_id = get_current_user_id();
$data = array(‘user_id’ => $user_id, ‘week’ => $week, ‘pick’ => $pick);
// insert or update (unique on user_id+week)
$existing = $wpdb->get_var($wpdb->prepare(“SELECT id FROM {$this->table} WHERE user_id=%d AND week=%s”, $user_id, $week));
if ($existing){
$wpdb->update($this->table, $data, array(‘id’ => $existing));
$status = ‘updated’;
} else {
$wpdb->insert($this->table, $data);
$status = ‘saved’;
}
wp_redirect(add_query_arg(‘swp_msg’, $status, remove_query_arg(array(‘swp_msg’))));
exit;
}
public function shortcode($atts){
$a = shortcode_atts(array(
‘week’ => ”, // e.g., “2025-W01” or “Week 8”
), $atts, ‘Legal_Eagle_Weekly_picks’);
ob_start();
echo ‘
echo ‘
Surge Weekly Picks
‘;
if (!is_user_logged_in()){
echo ‘
You must be logged in to submit a pick.
‘;
echo ‘
‘;
return ob_get_clean();
}
if (!$this->current_user_allowed()){
echo ‘
Sorry, you are not on the list of participants for this contest.
‘;
echo ‘