function dwqa_single_postclass($post_class) { global $post, $current_user; if (get_post_type($post) == 'dwqa-answer') { $post_class[] = 'dwqa-answer'; $post_class[] = 'dwqa-status-' . get_post_status($post->ID); if (dwqa_is_answer_flag($post->ID)) { $post_class[] = 'answer-flagged-content'; } if (user_can($post->post_author, 'edit_published_posts')) { $post_class[] = 'staff'; } $question_id = get_post_meta($post->ID, '_question', true); $best_answer_id = dwqa_get_the_best_answer($question_id); if ($best_answer_id && $best_answer_id == $post->ID) { $post_class[] = 'best-answer'; } if (!is_user_logged_in() || $current_user->ID != $post->ID || !current_user_can('edit_posts')) { $post_class[] = 'dwqa-no-click'; } } if (get_post_type($post) == 'dwqa-answer' && get_post_type($post) == 'dwqa-question') { if (in_array('hentry', $post_class)) { unset($post_class); } } return $post_class; }
function dwqa_is_the_best_answer($answer_id, $question_id = false) { if (!$question_id) { $question_id = get_the_ID(); } $best_answer = dwqa_get_the_best_answer($question_id); if ($best_answer && $best_answer == $answer_id) { return true; } return false; }
public function prepare_answers($posts, $query) { global $dwqa; $query->test = 'rambu'; if (is_main_query() && $query->is_single() && $query->query_vars['post_type'] == $dwqa->question->get_slug()) { $question = $posts[0]; $ans_cur_page = isset($_GET['ans-page']) ? intval($_GET['ans-page']) : 1; // We will include the all answers of this question here; $args = array('post_type' => 'dwqa-answer', 'posts_per_page' => get_option('posts_per_page'), 'order' => 'ASC', 'paged' => $ans_cur_page, 'meta_query' => array(array('key' => '_question', 'value' => $question->ID)), 'post_status' => array('publish', 'private', 'draft'), 'perm' => 'readable'); $query->dwqa_answers = new WP_Query($args); $query->dwqa_answers->best_answer = dwqa_get_the_best_answer($question->ID); } return $posts; }
<?php global $current_user, $post, $dwqa_options; $ans_cur_page = isset($_GET['ans-page']) ? intval($_GET['ans-page']) : 1; $question_id = $post->ID; $question = $post; $best_answer_id = dwqa_get_the_best_answer($question_id); $draft_answers = dwqa_user_get_draft($question_id); // get all answers for this posts $args = array('post_type' => 'dwqa-answer', 'posts_per_page' => 99, 'order' => 'ASC', 'page' => $ans_cur_page, 'paged' => $ans_cur_page, 'meta_query' => array(array('key' => '_question', 'value' => array($question_id), 'compare' => 'IN')), 'post_status' => array('publish', 'private', 'draft'), 'perm' => 'readable'); $answers = new WP_Query($args); if ($answers->found_posts > 0) { ?> <h3 class="dwqa-headline"> <?php printf('<span class="answer-count"><span class="digit">%d</span> %s</span>', $answers->found_posts, _n('answer', 'answers', $answers->found_posts, 'dwqa')); ?> </h3> <?php } if ($answers->found_posts > 0 || !empty($draft_answers)) { // Display answers ?> <div class="dwqa-list-answers"> <?php // Display best answer if ($best_answer_id) { $post = get_post($best_answer_id); setup_postdata($post); dwqa_load_template('content', 'answer'); }
function dwqa_vote_best_answer_button() { global $current_user; $question_id = get_post_meta(get_the_ID(), '_question', true); $question = get_post($question_id); $best_answer = dwqa_get_the_best_answer($question_id); $data = is_user_logged_in() && ($current_user->ID == $question->post_author || current_user_can('edit_posts')) ? 'data-answer="' . get_the_ID() . '" data-nonce="' . wp_create_nonce('_dwqa_vote_best_answer') . '" data-ajax="true"' : 'data-ajax="false"'; if (get_post_status(get_the_ID()) != 'publish') { return false; } if ($best_answer == get_the_ID() || is_user_logged_in() && ($current_user->ID == $question->post_author || current_user_can('edit_posts'))) { ?> <div class="entry-vote-best <?php echo $best_answer == get_the_ID() ? 'active' : ''; ?> " <?php echo $data; ?> > <a href="javascript:void(0);" title="<?php _e('Choose as best answer', 'dwqa'); ?> "> <div class="entry-vote-best-bg"></div> <i class="icon-thumbs-up"></i> </a> </div> <?php } }
<?php /** * Template use for display a single question * * @since DW Question Answer 1.0 */ global $current_user, $post; $post_id = get_the_ID(); $question = get_post($post_id); $best_answer_id = dwqa_get_the_best_answer($post_id); $status = array('publish', 'private'); $args = array('post_type' => 'dwqa-answer', 'posts_per_page' => -1, 'order' => 'ASC', 'meta_query' => array(array('key' => '_question', 'value' => array($post_id), 'compare' => 'IN')), 'post_status' => $status); $answers = new WP_Query($args); ?> <?php if (have_posts()) { ?> <?php while (have_posts()) { the_post(); ?> <?php $post_id = get_the_ID(); $post_status = get_post_status(); ?>