0,
),
$atts,
'ahtpoma_poll'
);
$poll_id = absint($atts['id']);
$polls = get_option('ahtpoma_polls', array());
if (! isset($polls[$poll_id]) || empty($polls[$poll_id]['status'])) {
return '';
}
$poll = $polls[$poll_id];
$total_votes = array_sum($poll['votes']);
// Calculate percentages
$poll['votes_percent'] = array();
if ($total_votes > 0) {
foreach ($poll['votes'] as $vote) {
$poll['votes_percent'][] = round(($vote / $total_votes) * 100, 2);
}
} else {
$poll['votes_percent'] = array_fill(0, count($poll['votes']), 0);
}
ob_start();
?>
'Invalid data.'));
}
// Verify nonce
$nonce = isset($_POST['ahtpoma_vote_nonce'])
? sanitize_text_field(wp_unslash($_POST['ahtpoma_vote_nonce']))
: '';
if (empty($nonce) || ! wp_verify_nonce($nonce, 'ahtpoma_vote_nonce')) {
wp_send_json_error(array('message' => 'Invalid nonce.'));
}
$poll_id = absint($_POST['poll_id']);
$option_index = absint($_POST['option_index']);
$polls = get_option('ahtpoma_polls', array());
if (! isset($polls[$poll_id]) || ! isset($polls[$poll_id]['options'][$option_index])) {
wp_send_json_error(array('message' => 'Poll or option not found.'));
}
// Ensure votes arrays exist
if (! isset($polls[$poll_id]['votes'])) {
$polls[$poll_id]['votes'] = array_fill(0, count($polls[$poll_id]['options']), 0);
}
$user_id = get_current_user_id();
if ($user_id) {
// Logged-in user
$user_votes = get_user_meta($user_id, 'ahtpoma_votes', true);
if (! is_array($user_votes)) {
$user_votes = array();
}
if (isset($user_votes[$poll_id])) {
$previously_selected = absint($user_votes[$poll_id]);
if ($previously_selected !== $option_index) {
if ($polls[$poll_id]['votes'][$previously_selected] > 0) {
$polls[$poll_id]['votes'][$previously_selected]--;
}
$polls[$poll_id]['votes'][$option_index]++;
$user_votes[$poll_id] = $option_index;
update_user_meta($user_id, 'ahtpoma_votes', $user_votes);
}
} else {
$user_votes[$poll_id] = $option_index;
update_user_meta($user_id, 'ahtpoma_votes', $user_votes);
$polls[$poll_id]['votes'][$option_index]++;
}
} else {
// Guest user (cookie based)
$cookie_name = 'ahtpoma_voted_' . $poll_id;
if (isset($_COOKIE[$cookie_name])) {
$previously_selected = absint($_COOKIE[$cookie_name]);
if ($previously_selected !== $option_index) {
if ($polls[$poll_id]['votes'][$previously_selected] > 0) {
$polls[$poll_id]['votes'][$previously_selected]--;
}
$polls[$poll_id]['votes'][$option_index]++;
setcookie($cookie_name, $option_index, time() + (30 * DAY_IN_SECONDS), COOKIEPATH, COOKIE_DOMAIN);
}
} else {
setcookie($cookie_name, $option_index, time() + (30 * DAY_IN_SECONDS), COOKIEPATH, COOKIE_DOMAIN);
$polls[$poll_id]['votes'][$option_index]++;
}
}
// Recalculate percentages
$total_votes = array_sum($polls[$poll_id]['votes']);
$polls[$poll_id]['votes_percent'] = array();
foreach ($polls[$poll_id]['votes'] as $vote) {
$polls[$poll_id]['votes_percent'][] = $total_votes > 0
? round(($vote / $total_votes) * 100, 2)
: 0;
}
update_option('ahtpoma_polls', $polls);
wp_send_json_success(array(
'new_percent' => $polls[$poll_id]['votes_percent'][$option_index],
'new_vote_count' => $polls[$poll_id]['votes'][$option_index],
'total_votes' => $total_votes,
'votes' => $polls[$poll_id]['votes'],
'votes_percent' => $polls[$poll_id]['votes_percent'],
));
}
}
new AHTPOMA_Frontend();