Example #1
0
function dwqa_action_vote()
{
    $result = array('error_code' => 'authorization', 'error_message' => __('Are you cheating, huh?', 'dwqa'));
    $vote_for = isset($_POST['vote_for']) && sanitize_text_field($_POST['vote_for']) == 'question' ? 'question' : 'answer';
    if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field($_POST['nonce']), '_dwqa_' . $vote_for . '_vote_nonce')) {
        wp_send_json_error($result);
    }
    if (!isset($_POST[$vote_for . '_id'])) {
        $result['error_code'] = 'missing ' . $vote_for;
        $result['error_message'] = __('What ' . $vote_for . ' are you looking for?', 'dwqa');
        wp_send_json_error($result);
    }
    $post_id = sanitize_text_field($_POST[$vote_for . '_id']);
    $point = isset($_POST['type']) && sanitize_text_field($_POST['type']) == 'up' ? 1 : -1;
    //vote
    if (is_user_logged_in()) {
        global $current_user;
        if (!dwqa_is_user_voted($post_id, $point)) {
            $votes = maybe_unserialize(get_post_meta($post_id, '_dwqa_votes_log', true));
            $votes[$current_user->ID] = $point;
            //update
            do_action('dwqa_vote_' . $vote_for, $post_id, (int) $point);
            update_post_meta($post_id, '_dwqa_votes_log', serialize($votes));
            // Update vote point
            dwqa_update_vote_count($post_id);
            $point = dwqa_vote_count($post_id);
            if ($point > 0) {
                $point = '+' . $point;
            }
            wp_send_json_success(array('vote' => $point));
        } else {
            $result['error_code'] = 'voted';
            $result['error_message'] = __('You voted for this ' . $vote_for, 'dwqa');
            wp_send_json_error($result);
        }
    } elseif ('question' == $vote_for) {
        // useful of question with meta field is "_dwqa_question_useful", point of this question
        $useful = get_post_meta($post_id, '_dwqa_' . $vote_for . '_useful', true);
        $useful = $useful ? (int) $useful : 0;
        do_action('dwqa_vote_' . $vote_for, $post_id, (int) $point);
        update_post_meta($post_id, '_dwqa_' . $vote_for . '_useful', $useful + $point);
        // Number of votes by guest
        $useful_rate = get_post_meta($post_id, '_dwqa_' . $vote_for . '_useful_rate', true);
        $useful_rate = $useful_rate ? (int) $useful_rate : 0;
        update_post_meta($post_id, '_dwqa_' . $vote_for . '_useful_rate', $useful_rate + 1);
    }
}
Example #2
0
function dwqa_get_the_best_answer($question_id = false)
{
    if (!$question_id) {
        $question_id = get_the_ID();
    }
    if ('dwqa-question' != get_post_type($question_id)) {
        return false;
    }
    global $dwqa, $wpdb;
    $user_vote = get_post_meta($question_id, '_dwqa_best_answer', true);
    if ($user_vote && get_post($user_vote)) {
        return $user_vote;
    }
    $answer_id = get_transient('dwqa-best-answer-for-' . $question_id);
    if (!$answer_id) {
        $answers = get_posts(array('post_type' => $dwqa->answer->get_slug(), 'posts_per_page' => 1, 'meta_key' => '_dwqa_votes', 'meta_query' => array('relation' => 'AND', array('key' => '_question', 'value' => $question_id . '', 'compare' => '=')), 'fields' => 'ids', 'orderby' => 'meta_value_num', 'order' => 'DESC'));
        $answer_id = !empty($answers) ? $answers[0] : false;
        set_transient('dwqa-best-answer-for-' . $question_id, $answer_id, 21600);
    }
    if ($answer_id && (int) dwqa_vote_count($answer_id) > 2) {
        return $answer_id;
    }
    return false;
}
function dwqa_question_columns_content($column_name, $post_ID)
{
    switch ($column_name) {
        case 'info':
            echo ucfirst(get_post_meta($post_ID, '_dwqa_status', true)) . '<br>';
            echo '<strong>' . dwqa_question_answers_count($post_ID) . '</strong> ' . __('answered', 'dwqa') . '<br>';
            echo '<strong>' . dwqa_vote_count($post_ID) . '</strong> ' . __('voted', 'dwqa') . '<br>';
            echo '<strong>' . dwqa_question_views_count($post_ID) . '</strong> ' . __('views', 'dwqa') . '<br>';
            break;
        case 'question-category':
            $terms = wp_get_post_terms($post_ID, 'dwqa-question_category');
            $i = 0;
            foreach ($terms as $term) {
                if ($i > 0) {
                    echo ', ';
                }
                echo '<a href="' . get_term_link($term, 'dwqa-question_category') . '">' . $term->name . '</a> ';
                $i++;
            }
            break;
        case 'question-tag':
            $terms = wp_get_post_terms($post_ID, 'dwqa-question_tag');
            $i = 0;
            foreach ($terms as $term) {
                if ($i > 0) {
                    echo ', ';
                }
                echo '<a href="' . get_term_link($term, 'dwqa-question_tag') . '">' . $term->name . '</a> ';
                $i++;
            }
            break;
    }
}
function dwqa_question_meta_button($post_id = false)
{
    if (!$post_id) {
        $post_id = get_the_ID();
    }
    ?>
	<div class="dwqa-meta">
		<div class="dwqa-vote" data-type="question" data-nonce="<?php 
    echo wp_create_nonce('_dwqa_question_vote_nonce');
    ?>
" data-question="<?php 
    echo $post_id;
    ?>
" >
			<a class="dwqa-vote-dwqa-btn dwqa-vote-up" data-vote="up" href="#"  title="<?php 
    _e('Vote Up', 'dwqa');
    ?>
"><?php 
    _e('Vote Up', 'dwqa');
    ?>
</a>
			<div class="dwqa-vote-count"><?php 
    $point = dwqa_vote_count();
    echo $point > 0 ? '+' . $point : $point;
    ?>
</div>
			<a class="dwqa-vote-dwqa-btn dwqa-vote-down" data-vote="down" href="#"  title="<?php 
    _e('Vote Down', 'dwqa');
    ?>
"><?php 
    _e('Vote Down', 'dwqa');
    ?>
</a>
		</div>
		
		<?php 
    dwqa_question_status_button($post_id);
    ?>

		<?php 
    dwqa_question_privacy_button($post_id);
    ?>

		<?php 
    $categories = wp_get_post_terms($post_id, 'dwqa-question_category');
    ?>
		<?php 
    if (!empty($categories)) {
        ?>
			<?php 
        $cat = $categories[0];
        ?>
		<div class="dwqa-category">
			<a class="dwqa-category-name" href="<?php 
        echo get_term_link($cat);
        ?>
" title="<?php 
        _e('All questions from', 'dwqa');
        ?>
 <?php 
        echo $cat->name;
        ?>
"><i class="fa fa-folder-open"></i> <?php 
        echo $cat->name;
        ?>
</a>
		</div>
		<?php 
    }
    ?>
 <!-- Question Categories -->

		<?php 
    dwqa_question_action_buttons($post_id);
    ?>

		<?php 
    if (is_user_logged_in()) {
        ?>
		<span data-post="<?php 
        echo $post_id;
        ?>
" data-nonce="<?php 
        echo wp_create_nonce('_dwqa_follow_question');
        ?>
" class="dwqa-favourite <?php 
        echo dwqa_is_followed($post_id) ? 'active' : '';
        ?>
" title="<?php 
        echo dwqa_is_followed($post_id) ? __('Unfollow This Question', 'dwqa') : __('Follow This Question', 'dwqa');
        ?>
"><!-- add class 'active' -->
			<span class="dwpa_follow"><?php 
        _e('follow', 'dwqa');
        ?>
</span>
			<span class="dwpa_following"><?php 
        _e('following', 'dwqa');
        ?>
</span>
			<span class="dwpa_unfollow"><?php 
        _e('unfollow', 'dwqa');
        ?>
</span>
		</span>
		<?php 
    }
    ?>

		<?php 
    if (dwqa_current_user_can('edit_question')) {
        ?>
		<span  data-post="<?php 
        echo $post_id;
        ?>
" data-nonce="<?php 
        echo wp_create_nonce('_dwqa_stick_question');
        ?>
" class="dwqa-stick-question <?php 
        echo dwqa_is_sticky($post_id) ? 'active' : '';
        ?>
" title="<?php 
        echo dwqa_is_sticky($post_id) ? __('Unpin this Question from the top', 'dwqa') : __('Pin this Question to top', 'dwqa');
        ?>
"><i class="fa fa-bookmark"></i></span>
		<?php 
    }
    ?>
	</div>
	<?php 
}
echo get_the_term_list(get_the_ID(), 'dwqa-question_category', '<span class="dwqa-question-category">' . __('&nbsp; &bull; &nbsp;', 'dwqa'), ', ', '</span>');
?>
	</div>
	<div class="dwqa-question-stats">
		<span class="dwqa-views-count">
			<?php 
$views_count = dwqa_question_views_count();
?>
			<?php 
printf(__('<strong>%1$s</strong> views', 'dwqa'), $views_count);
?>
		</span>
		<span class="dwqa-answers-count">
			<?php 
$answers_count = dwqa_question_answers_count();
?>
			<?php 
printf(__('<strong>%1$s</strong> answers', 'dwqa'), $answers_count);
?>
		</span>
		<span class="dwqa-votes-count">
			<?php 
$vote_count = dwqa_vote_count();
?>
			<?php 
printf(__('<strong>%1$s</strong> votes', 'dwqa'), $vote_count);
?>
		</span>
	</div>
</div>
                            <span class="dwqa-vote" data-type="question" data-nonce="<?php 
        echo wp_create_nonce('_dwqa_question_vote_nonce');
        ?>
" data-question="<?php 
        echo $post_id;
        ?>
" >
                                <a class="dwqa-vote-dwqa-btn dwqa-vote-up" data-vote="up" href="#"  title="<?php 
        _e('Vote Up', 'dwqa');
        ?>
"><?php 
        _e('Vote Up', 'dwqa');
        ?>
</a>
                                <div class="dwqa-vote-count"><?php 
        $point = dwqa_vote_count();
        echo $point > 0 ? '+' . $point : $point;
        ?>
</div>
                                <a class="dwqa-vote-dwqa-btn dwqa-vote-down" data-vote="down" href="#"  title="<?php 
        _e('Vote Down', 'dwqa');
        ?>
"><?php 
        _e('Vote Down', 'dwqa');
        ?>
</a>
                            </span>

                            <?php 
        if (is_user_logged_in()) {
            ?>
function dwqa_get_the_best_answer($question_id = false)
{
    if (!$question_id) {
        $question_id = get_the_ID();
    }
    if ('dwqa-question' != get_post_type($question_id)) {
        return false;
    }
    $user_vote = get_post_meta($question_id, '_dwqa_best_answer', true);
    if ($user_vote && get_post($user_vote)) {
        return $user_vote;
    }
    global $wpdb;
    $query = "SELECT `post_id` FROM `{$wpdb->prefix}postmeta` LEFT JOIN `{$wpdb->prefix}posts` \n                ON `{$wpdb->prefix}postmeta`.post_id = `{$wpdb->prefix}posts`.ID   \n                WHERE `post_id` \n                    IN ( SELECT  `post_id` FROM `{$wpdb->prefix}postmeta` \n                            WHERE `meta_key` = '_question' AND `meta_value` = {$question_id} ) \n                    AND `meta_key` = '_dwqa_votes'\n                    ORDER BY CAST( `meta_value` as DECIMAL ) DESC LIMIT 0,1";
    $answer_id = $wpdb->get_var($query);
    if ($answer_id && (int) dwqa_vote_count($answer_id) > 2) {
        return $answer_id;
    }
    return false;
}
Example #8
0
			</div>
			<div class="dwqa-comment">
				<?php 
$answer_count = dwqa_question_answers_count();
?>
				<?php 
if ($answer_count > 0) {
    printf('<strong>%d</strong> %s', $answer_count, _n('answer', 'answers', $answer_count, 'dwqa'));
    ?>
				<?php 
} else {
    echo '<strong>0</strong> ' . __('answer', 'dwqa');
}
?>
			</div>
			<div class="dwqa-vote">
				<?php 
$answer_vote = dwqa_vote_count();
?>
				<?php 
if ($answer_vote > 0) {
    printf('<strong>%d</strong> %s', $answer_vote, _n('vote', 'votes', $answer_vote, 'dwqa'));
    ?>
				<?php 
} else {
    echo '<strong>0</strong> ' . __('vote', 'dwqa');
}
?>
			</div>
		</footer>
	</article>
 */
?>

<?php 
do_action('dwqa_before_single_question_content');
?>
<div class="dwqa-question-item">
	<div class="dwqa-question-vote" data-nonce="<?php 
echo wp_create_nonce('_dwqa_question_vote_nonce');
?>
" data-post="<?php 
the_ID();
?>
">
		<span class="dwqa-vote-count"><?php 
echo dwqa_vote_count();
?>
</span>
		<a class="dwqa-vote dwqa-vote-up" href="#"><?php 
_e('Vote Up', 'dwqa');
?>
</a>
		<a class="dwqa-vote dwqa-vote-down" href="#"><?php 
_e('Vote Down', 'dwqa');
?>
</a>
	</div>
	<div class="dwqa-question-meta">
		<?php 
$user_id = get_post_field('post_author', get_the_ID()) ? get_post_field('post_author', get_the_ID()) : false;
?>
Example #10
0
function dwqa_get_the_best_answer($question_id = false)
{
    if (!$question_id) {
        $question_id = get_the_ID();
    }
    if ('dwqa-question' != get_post_type($question_id)) {
        return false;
    }
    $user_vote = get_post_meta($question_id, '_dwqa_best_answer', true);
    if ($user_vote && get_post($user_vote)) {
        return $user_vote;
    }
    $answer_id = wp_cache_get('dwqa-best-answer-for-' . $question_id, 'dwqa');
    if (!$answer_id) {
        global $wpdb;
        $query = "SELECT `post_id` FROM `{$wpdb->postmeta}`\n\t\t\t\t\tWHERE `post_id` IN ( \n\t\t\t\t\t\t\tSELECT  `post_id` FROM `{$wpdb->postmeta}` \n\t\t\t\t\t\t\tWHERE `meta_key` = '_question' AND `meta_value` = {$question_id} \n\t\t\t\t\t) \n\t\t\t\t\tAND `meta_key` = '_dwqa_votes'\n\t\t\t\t\tORDER BY CAST( `meta_value` as DECIMAL ) DESC LIMIT 0,1";
        $answer_id = $wpdb->get_var($query);
        if (!$answer_id) {
            $answer_id = -1;
        }
        wp_cache_set('dwqa-best-answer-for-' . $question_id, $answer_id, 'dwqa', 21600);
    }
    if ($answer_id && (int) dwqa_vote_count($answer_id) > 2) {
        return $answer_id;
    }
    return false;
}
Example #11
0
        public function single_question()
        {
            global $post, $current_user;
            $post_id = get_the_ID();
            ?>
        <div class="dwqa-single-question">
            <!-- dwqa-status-private -->
            <article id="question-<?php 
            echo $post_id;
            ?>
" <?php 
            post_class('dwqa-question');
            ?>
>
                <header class="dwqa-header">
                    <h1 class="dwqa-title"><?php 
            the_title();
            ?>
</h1>
                    <div class="dwqa-meta">
                        <span class="dwqa-vote" data-type="question" data-nonce="<?php 
            echo wp_create_nonce('_dwqa_question_vote_nonce');
            ?>
" data-question="<?php 
            echo $post_id;
            ?>
" >
                            <a class="dwqa-vote-dwqa-btn dwqa-vote-up" data-vote="up" href="#"  title="<?php 
            _e('Vote Up', 'dwqa');
            ?>
"><?php 
            _e('Vote Up', 'dwqa');
            ?>
</a>
                            <div class="dwqa-vote-count"><?php 
            $point = dwqa_vote_count();
            echo $point > 0 ? '+' . $point : $point;
            ?>
</div>
                            <a class="dwqa-vote-dwqa-btn dwqa-vote-down" data-vote="down" href="#"  title="<?php 
            _e('Vote Down', 'dwqa');
            ?>
"><?php 
            _e('Vote Down', 'dwqa');
            ?>
</a>
                        </span>

                        <?php 
            if (is_user_logged_in()) {
                ?>
                        <span data-post="<?php 
                echo $post_id;
                ?>
" data-nonce="<?php 
                echo wp_create_nonce('_dwqa_follow_question');
                ?>
" class="dwqa-favourite <?php 
                echo dwqa_is_followed($post_id) ? 'active' : '';
                ?>
" title="<?php 
                echo dwqa_is_followed($post_id) ? __('Unfollow This Question', 'dwqa') : __('Follow This Question', 'dwqa');
                ?>
"><!-- add class 'active' -->
                            <i class="fa fa-star"></i>
                        </span>
                        <?php 
            }
            ?>
                    </div>
                </header>
                <div class="dwqa-content">
                    <?php 
            the_content();
            ?>
                </div>
                <?php 
            $tags = get_the_term_list($post_id, 'dwqa-question_tag', '<span class="dwqa-tag">', '</span><span class="dwqa-tag">', '</span>');
            if (!empty($tags)) {
                ?>
                <div class="dwqa-tags"><?php 
                echo $tags;
                ?>
</div>
                <?php 
            }
            ?>
  <!-- Question Tags -->

                <footer class="dwqa-footer">
                    <div class="dwqa-author">
                        <?php 
            echo get_avatar($post->post_author, 32, false);
            ?>
                        <span class="author">
                            <?php 
            printf('<a href="%1$s" title="%2$s %3$s">%3$s</a>', get_author_posts_url(get_the_author_meta('ID')), __('Posts by', 'dwqa'), get_the_author_meta('display_name'));
            ?>
                        </span><!-- Author Info -->
                        <span class="dwqa-date">
                            <?php 
            printf('<a href="%s" title="%s #%d">%s %s</a>', get_permalink(), __('Link to', 'dwqa'), $post_id, __('asked', 'dwqa'), get_the_date());
            ?>
                        </span> <!-- Question Date -->
                        
                        
                        <div data-post="<?php 
            echo $post_id;
            ?>
" data-nonce="<?php 
            echo wp_create_nonce('_dwqa_update_privacy_nonce');
            ?>
" data-type="question" class="dwqa-privacy">
                            <input type="hidden" name="privacy" value="<?php 
            get_post_status();
            ?>
">
                            <span class="dwqa-current-privacy"> <?php 
            echo 'private' == get_post_status() ? '<i class="fa fa-lock"></i> ' . __('Private', 'dwqa') : '<i class="fa fa-globe"></i> ' . __('Public', 'dwqa');
            ?>
</span>
                            <?php 
            if (dwqa_current_user_can('edit_question') || dwqa_current_user_can('edit_answer') || $post->post_author == $current_user->ID) {
                ?>
                            <span class="dwqa-change-privacy">
                                <div class="dwqa-btn-group">
                                    <button type="button" class="dropdown-toggle" ><i class="fa fa-caret-down"></i></button>
                                    <div class="dwqa-dropdown-menu">
                                        <div class="dwqa-dropdown-caret">
                                            <span class="dwqa-caret-outer"></span>
                                            <span class="dwqa-caret-inner"></span>
                                        </div>
                                        <ul role="menu">
                                            <li title="<?php 
                _e('Everyone can see', 'dwqa');
                ?>
" data-privacy="publish" <?php 
                echo 'publish' == get_post_status() ? 'class="current"' : '';
                ?>
><a href="#"><i class="fa fa-globe"></i> <?php 
                _e('Public', 'dwqa');
                ?>
</a></li>
                                            <li title="<?php 
                _e('Only Author and Administrator can see', 'dwqa');
                ?>
" data-privacy="private" <?php 
                echo 'private' == get_post_status() ? 'class="current"' : '';
                ?>
><a href="#" ><i class="fa fa-lock"></i> <?php 
                _e('Private', 'dwqa');
                ?>
</a></li>
                                        </ul>
                                    </div>
                                </div>
                            </span>
                            <?php 
            }
            ?>
                        </div><!-- post status -->
                    </div>
                    <?php 
            $categories = wp_get_post_terms($post_id, 'dwqa-question_category');
            if (!empty($categories)) {
                $cat = $categories[0];
                ?>
                    <div class="dwqa-category">
                        <span class="dwqa-category-title"><?php 
                _e('Category', 'dwqa');
                ?>
</span>
                        <a class="dwqa-category-name" href="<?php 
                echo get_term_link($cat);
                ?>
" title="<?php 
                _e('All questions from', 'dwqa');
                ?>
 <?php 
                echo $cat->name;
                ?>
"><?php 
                echo $cat->name;
                ?>
</a>
                    </div>
                    <?php 
            }
            ?>
 <!-- Question Categories -->

                    <?php 
            $meta = get_post_meta($post_id, '_dwqa_status', true);
            if (!$meta) {
                $meta = 'open';
            }
            ?>
                    <div class="dwqa-current-status">
                        <span class="dwqa-status-title"><?php 
            _e('Status', 'dwqa');
            ?>
</span>
                        <span class="dwqa-status-name"><?php 
            echo $meta;
            ?>
</span>
                        <?php 
            if (dwqa_current_user_can('edit_question') || dwqa_current_user_can('edit_answer') || $current_user->ID == $post->post_author) {
                ?>
                        <span class="dwqa-change-status">
                            <div class="dwqa-btn-group">
                                <button type="button" class="dropdown-toggle" ><i class="fa fa-caret-down"></i></button>
                                <div class="dwqa-dropdown-menu" data-nonce="<?php 
                echo wp_create_nonce('_dwqa_update_question_status_nonce');
                ?>
" data-question="<?php 
                the_ID();
                ?>
" >
                                    <div class="dwqa-dropdown-caret">
                                        <span class="dwqa-caret-outer"></span>
                                        <span class="dwqa-caret-inner"></span>
                                    </div>
                                    <ul role="menu" data-nonce="<?php 
                echo wp_create_nonce('_dwqa_update_question_status_nonce');
                ?>
" data-question="<?php 
                the_ID();
                ?>
">
                                        <?php 
                if ('resolved' == $meta || 'pending' == $meta || 'closed' == $meta) {
                    ?>
                                            <li class="dwqa-re-open" data-status="re-open">
                                                <a href="#"><i class="fa fa-reply"></i> <?php 
                    _e('Re-Open', 'dwqa');
                    ?>
</a>
                                            </li>
                                        <?php 
                }
                ?>
                                        <?php 
                if ('closed' != $meta) {
                    ?>
                                            <li class="dwqa-closed" data-status="closed">
                                                <a href="#"><i class="fa fa-lock"></i> <?php 
                    _e('Closed', 'dwqa');
                    ?>
</a>
                                            </li>
                                        <?php 
                }
                ?>
                                        <?php 
                if ('pending' != $meta && 'closed' != $meta && current_user_can('edit_posts', $post_id)) {
                    ?>
                                            <li class="dwqa-pending"  data-status="pending">
                                                <a href="#"><i class="fa fa-question-circle"></i> <?php 
                    _e('Pending', 'dwqa');
                    ?>
</a>
                                            </li>
                                        <?php 
                }
                ?>
                                        <?php 
                if ('resolved' != $meta && 'closed' != $meta) {
                    ?>
                                            <li class="dwqa-resolved" data-status="resolved">
                                                <a href="#"><i class="fa fa-check-circle-o"></i> <?php 
                    _e('Resolved', 'dwqa');
                    ?>
</a>
                                            </li>
                                        <?php 
                }
                ?>
                                    </ul>
                                </div>
                            </div>
                        </span>
                        <?php 
            }
            ?>
 <!-- Change Question Status -->
                    </div>
                </footer>
                <div class="dwqa-comments">
                    <?php 
            comments_template();
            ?>
                </div>
            </article><!-- end question -->

            <div id="dwqa-answers">
                <?php 
            dwqa_load_template('answers');
            ?>
            </div><!-- end dwqa-add-answers -->
        </div><!-- end dwqa-single-question -->
    <?php 
        }