function ap_user_can_answer($question_id) { if (is_super_admin()) { return true; } if (ap_opt('close_after_selecting') && ap_is_answer_selected($question_id)) { return false; } if (current_user_can('ap_new_answer') || ap_show_form_to_guest()) { if (!ap_opt('multiple_answers') && ap_is_user_answered($question_id, get_current_user_id()) && get_current_user_id() != '0') { return false; } else { return true; } } return false; }
public function ap_set_best_answer() { $args = explode('-', sanitize_text_field($_POST['args'])); if (wp_verify_nonce($args[1], 'answer-' . $args[0])) { $post = get_post($args[0]); $user_id = get_current_user_id(); if (ap_is_answer_selected($post->post_parent)) { ap_do_event('unselect_answer', $user_id, $post->post_parent, $post->ID); update_post_meta($post->ID, ANSPRESS_BEST_META, 0); update_post_meta($post->post_parent, ANSPRESS_SELECTED_META, false); $html = ap_select_answer_btn_html($args[0]); $result = array('action' => 'unselected', 'message' => __('Uselected the answer', 'ap'), 'html' => $html); } else { ap_do_event('select_answer', $user_id, $post->post_parent, $post->ID); update_post_meta($post->ID, ANSPRESS_BEST_META, 1); update_post_meta($post->post_parent, ANSPRESS_SELECTED_META, $post->ID); $html = ap_select_answer_btn_html($args[0]); $result = array('action' => 'selected', 'message' => __('Thank you for awarding best answer', 'ap'), 'html' => $html); } } else { $result = array('action' => false, 'message' => __('Please try again', 'ap')); } die(json_encode($result)); }
function ap_select_answer_btn_html($post_id) { if (!ap_user_can_select_answer($post_id)) { return; } $ans = get_post($post_id); $action = 'answer-' . $post_id; $nonce = wp_create_nonce($action); if (!ap_is_answer_selected($ans->post_parent)) { return '<a href="#" class="ap-btn-select ap-sicon ap-icon-checkmark ap-tip" data-button="ap-select-answer" data-args="' . $post_id . '-' . $nonce . '" title="' . __('Select this answer as best', 'ap') . '"></a>'; } elseif (ap_is_answer_selected($ans->post_parent) && ap_is_best_answer($ans->ID)) { return '<a href="#" class="ap-btn-select ap-sicon ap-icon-checkmark selected ap-tip" data-button="ap-select-answer" data-args="' . $post_id . '-' . $nonce . '" title="' . __('Unselect this answer', 'ap') . '"></a>'; } }