array($this, 'sanitize_polls'), ) ); } /** * Sanitize Poll Data */ public function sanitize_polls($input) { if (! is_array($input)) { return array(); } $sanitized = array(); foreach ($input as $key => $poll) { $sanitized[$key] = array( 'question' => isset($poll['question']) ? sanitize_text_field($poll['question']) : '', 'options' => isset($poll['options']) && is_array($poll['options']) ? array_map('sanitize_text_field', $poll['options']) : array(), 'votes' => isset($poll['votes']) && is_array($poll['votes']) ? array_map('absint', $poll['votes']) : array(), 'bgcolor' => isset($poll['bgcolor']) ? sanitize_hex_color($poll['bgcolor']) : '', 'status' => isset($poll['status']) ? absint($poll['status']) : 0, ); } return $sanitized; } /** * Poll List Page */ public function polls_list_page() { include plugin_dir_path(__FILE__) . 'polls-list.php'; } /** * Handle Actions (Delete / Toggle Status) */ public function handle_poll_action() { if (! is_admin()) { return; } if (! isset($_GET['page']) || 'ahtpoma_polls' !== $_GET['page']) { return; } if (! isset($_GET['action'], $_GET['poll_id'])) { return; } $poll_id = absint($_GET['poll_id']); $action = sanitize_text_field(wp_unslash($_GET['action'])); if ( ! isset($_GET['_wpnonce']) || ! wp_verify_nonce( sanitize_text_field(wp_unslash($_GET['_wpnonce'])), 'ahtpoma_poll_action' ) ) { wp_die(esc_html__('Security check failed.', 'aht-poll-master')); } switch ($action) { case 'delete': $this->delete_poll($poll_id); break; case 'toggle_status': $this->toggle_poll_status($poll_id); break; } } /** * Toggle Status */ /** * Toggle Status */ private function toggle_poll_status($poll_id) { $polls = get_option('ahtpoma_polls', array()); if (isset($polls[$poll_id])) { $polls[$poll_id]['status'] = ! empty($polls[$poll_id]['status']) ? 0 : 1; update_option('ahtpoma_polls', $polls); $status_text = $polls[$poll_id]['status'] ? 'activated' : 'deactivated'; wp_safe_redirect( add_query_arg( array( 'page' => 'ahtpoma_polls', 'message' => 'status_updated', 'status' => $status_text, ), admin_url('admin.php') ) ); exit; } wp_safe_redirect( add_query_arg( array( 'page' => 'ahtpoma_polls', 'message' => 'error', ), admin_url('admin.php') ) ); exit; } /** * Delete Poll */ private function delete_poll($poll_id) { $polls = get_option('ahtpoma_polls', array()); if (isset($polls[$poll_id])) { unset($polls[$poll_id]); update_option('ahtpoma_polls', $polls); wp_safe_redirect( add_query_arg( array( 'page' => 'ahtpoma_polls', 'message' => 'deleted', ), admin_url('admin.php') ) ); exit; } wp_safe_redirect( add_query_arg( array( 'page' => 'ahtpoma_polls', 'message' => 'error', ), admin_url('admin.php') ) ); exit; } /** * View Poll Page */ public function view_poll_page() { if (! isset($_GET['poll_id'], $_GET['_wpnonce'])) { return; } $nonce = sanitize_text_field(wp_unslash($_GET['_wpnonce'])); if (! wp_verify_nonce($nonce, 'ahtpoma_view_poll_action')) { return; } $poll_id = absint($_GET['poll_id']); $this->view_poll($poll_id); } /** * Edit Poll Page */ public function edit_poll_page() { if (! isset($_GET['poll_id'], $_GET['_wpnonce'])) { return; } $nonce = sanitize_text_field(wp_unslash($_GET['_wpnonce'])); if (! wp_verify_nonce($nonce, 'ahtpoma_edit_poll_action')) { return; } $poll_id = absint(wp_unslash($_GET['poll_id'])); $this->edit_poll($poll_id); } /** * View Poll */ private function view_poll($poll_id) { include plugin_dir_path(__FILE__) . 'view-poll.php'; } /** * View All Polls */ public function view_all_poll() { include plugin_dir_path(__FILE__) . 'view-all-polls.php'; } /** * Handle Edit Poll Submit */ public function handle_edit_poll() { if (! is_admin()) { return; } if ( ! isset($_GET['page']) || 'ahtpoma_edit_poll' !== sanitize_text_field(wp_unslash($_GET['page'])) ) { return; } if ( ! isset($_SERVER['REQUEST_METHOD']) || 'POST' !== sanitize_text_field(wp_unslash($_SERVER['REQUEST_METHOD'])) ) { return; } if (! isset($_POST['edit_poll'])) { return; } if (! isset($_GET['poll_id'])) { return; } $poll_id = absint($_GET['poll_id']); // Verify nonce if ( ! isset($_POST['edit_poll_nonce']) || ! wp_verify_nonce( sanitize_text_field(wp_unslash($_POST['edit_poll_nonce'])), 'ahtpoma_edit_poll_action' ) ) { wp_die(esc_html__('Security check failed.', 'aht-poll-master')); } $polls = get_option('ahtpoma_polls', array()); if (! isset($polls[$poll_id])) { return; } $question = isset($_POST['question']) ? sanitize_text_field(wp_unslash($_POST['question'])) : ''; $options = isset($_POST['options']) && is_array($_POST['options']) ? array_map('sanitize_text_field', wp_unslash($_POST['options'])) : array(); $status = isset($_POST['status']) ? 1 : 0; $polls[$poll_id] = array( 'question' => $question, 'options' => $options, 'votes' => isset($polls[$poll_id]['votes']) ? $polls[$poll_id]['votes'] : array_fill(0, count($options), 0), 'bgcolor' => isset($polls[$poll_id]['bgcolor']) ? $polls[$poll_id]['bgcolor'] : '', 'status' => $status, ); update_option('ahtpoma_polls', $polls); wp_safe_redirect( add_query_arg( array( 'page' => 'ahtpoma_polls', 'message' => 'updated', ), admin_url('admin.php') ) ); exit; } /** * Edit Poll Form */ private function edit_poll($poll_id) { $polls = get_option('ahtpoma_polls', array()); if (! isset($polls[$poll_id]) || ! is_array($polls[$poll_id])) { echo '
' . esc_html__('Poll not found.', 'aht-poll-master') . '