admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('tpoll_vote'), 'strings' => array( 'error' => __('An error occurred. Please try again.', '3task-polls'), 'voted' => __('Thank you for voting!', '3task-polls'), 'alreadyVoted' => __('You have already voted.', '3task-polls'), 'selectAnswer' => __('Please select an answer.', '3task-polls'), 'clickToRate' => __('Click to rate', '3task-polls'), /* translators: %s: rating value */ 'yourRating' => __('Your rating: %s', '3task-polls') ) )); } /** * AJAX: Cast vote */ public function ajax_vote() { // Verify nonce if (!check_ajax_referer('tpoll_vote', 'nonce', false)) { wp_send_json_error(array('message' => __('Security check failed.', '3task-polls'))); } $poll_id = isset($_POST['poll_id']) ? absint($_POST['poll_id']) : 0; $answers = isset($_POST['answers']) ? array_map('absint', (array) $_POST['answers']) : array(); $value = isset($_POST['value']) ? intval($_POST['value']) : 1; if (!$poll_id) { wp_send_json_error(array('message' => __('Invalid poll.', '3task-polls'))); } // Handle single answer if (isset($_POST['answer']) && !empty($_POST['answer'])) { $answers = array(absint($_POST['answer'])); } if (empty($answers)) { wp_send_json_error(array('message' => __('Please select an answer.', '3task-polls'))); } // Cast vote $result = TPoll_Vote::cast($poll_id, $answers, $value); if (is_wp_error($result)) { wp_send_json_error(array('message' => $result->get_error_message())); } // Get updated results $results = TPoll_Vote::get_results($poll_id); wp_send_json_success(array( 'message' => __('Thank you for voting!', '3task-polls'), 'results' => $results, 'html' => $this->render_results_html($poll_id, $results) )); } /** * AJAX: Get results */ public function ajax_get_results() { if (!check_ajax_referer('tpoll_vote', 'nonce', false)) { wp_send_json_error(array('message' => __('Security check failed.', '3task-polls'))); } $poll_id = isset($_GET['poll_id']) ? absint($_GET['poll_id']) : 0; if (!$poll_id) { wp_send_json_error(array('message' => __('Invalid poll.', '3task-polls'))); } $results = TPoll_Vote::get_results($poll_id); if (!$results) { wp_send_json_error(array('message' => __('Poll not found.', '3task-polls'))); } wp_send_json_success(array( 'results' => $results, 'html' => $this->render_results_html($poll_id, $results) )); } /** * Render results HTML */ private function render_results_html($poll_id, $results) { $poll = TPoll_Poll::get($poll_id); $settings = get_option('tpoll_settings', array()); $show_voters = $poll ? $poll->get_setting('show_voters_count', $settings['show_voters_count'] ?? true) : true; ob_start(); ?>
%
type === 'rating' && isset($results['average_rating'])) : ?>
' . esc_html($results['average_rating']) . '' ); ?>