示例#1
0
 /**
  * Delete a answer and child answers
  * @param int $id
  * @param bool $force_delete
  * @return bool $success
  */
 public static function delete($id, $force_delete = false)
 {
     $instance = self::get_instance();
     $answer = get_post($id);
     $question = get_post($answer->post_parent);
     $answer = QA_Answers::convert($answer);
     $question = QA_Questions::convert($question);
     /* also delete question likes */
     $comments = get_comments(array('post_id' => $id, 'parent' => 0, 'status' => 'approve', 'post_status' => 'publish'));
     if (is_array($comments) && count($comments) > 0) {
         foreach ($comments as $comment) {
             wp_delete_comment($comment->comment_ID, $force_delete);
         }
     }
     $success = $instance->_delete($id, $force_delete);
     if ($success) {
         //update answer count
         $count = et_count_user_posts($answer->post_author, 'answer');
         update_user_meta($answer->post_author, 'et_answer_count', $count);
         //update status answered for question:
         $is_best_answer = get_post_meta($id, 'et_is_best_answer', true);
         if ($is_best_answer) {
             delete_post_meta($question->ID, 'et_best_answer');
         }
     }
     return $success;
 }
<?php

global $post;
$answer = QA_Answers::convert($post);
$question = QA_Questions::convert(get_post($answer->post_parent));
$et_post_date = et_the_time(strtotime($answer->post_date));
$badge_points = qa_get_badge_point();
$category = !empty($question->question_category[0]) ? $question->question_category[0]->name : __('No Category', ET_DOMAIN);
$category_link = !empty($question->question_category[0]) ? get_term_link($question->question_category[0]->term_id, 'question_category') : '#';
?>
<li <?php 
post_class('answer-item question-item pending-question');
?>
 data-id="<?php 
echo $post->ID;
?>
">
    <div class="col-md-8 q-left-content">
        <div class="q-ltop-content title-answer-style">
            <a href="<?php 
echo get_permalink($question->ID);
?>
" class="question-title">
                <?php 
the_title();
?>
            </a>
        </div>
        <div class="q-lbtm-content">
            <div class="question-cat">
                <span class="author-avatar">
if (is_user_logged_in() && current_user_can('manage_options')) {
    $reply_args['post_status'] = array('publish', 'pending');
}
if (isset($_GET['sort']) && $_GET['sort'] == "oldest") {
    $reply_args['order'] = 'ASC';
} else {
    add_filter("posts_join", array("QA_Front", "_post_vote_join"));
    add_filter("posts_orderby", array("QA_Front", "_post_vote_orderby"));
}
$replyQuery = new WP_Query($reply_args);
$answersData = array();
global $post;
if ($replyQuery->have_posts()) {
    while ($replyQuery->have_posts()) {
        $replyQuery->the_post();
        $answersData[] = QA_Answers::convert($post);
        get_template_part('mobile/template/item', 'answer');
    }
}
wp_reset_query();
?>
    </div>
    <div class="clearfix" style="height:20px;"></div>
    <!-- CONTENT ANSWERS LOOP / END -->
    <!-- PAGINATIONS ANSWER -->
    <section class="list-pagination-wrapper">
        <?php 
echo paginate_links(array('base' => get_permalink($question->ID) . '%#%', 'format' => $wp_rewrite->using_permalinks() ? 'page/%#%' : '?paged=%#%', 'current' => max(1, $paged), 'mid_size' => 1, 'total' => $replyQuery->max_num_pages, 'prev_text' => '<', 'next_text' => '>', 'type' => 'list'));
?>
         
    </section>
示例#4
0
/**
*
* TEMPLATE LOOP FOR ANSWERS
* @param array $answers
* @author ThaiNT
* @since 1.0
*
**/
function qa_answers_loop()
{
    global $post, $wp_rewrite, $current_user, $qa_question;
    $question_ID = $post->ID;
    $answersData = array();
    $commentsData = array();
    $question = QA_Questions::convert(get_post($question_ID));
    $qa_question = $question;
    $paged = get_query_var('page') ? get_query_var('page') : 1;
    $reply_args = array('post_type' => 'answer', 'post_parent' => $post->ID, 'paged' => $paged);
    //show pending answer if current user is admin
    if (is_user_logged_in() && (qa_user_can('approve_answer') || current_user_can('manage_options'))) {
        $reply_args['post_status'] = array('publish', 'pending');
    }
    if (isset($_GET['sort']) && $_GET['sort'] == "oldest") {
        $reply_args['order'] = 'ASC';
    } else {
        add_filter("posts_join", array("QA_Front", "_post_vote_join"));
        add_filter("posts_orderby", array("QA_Front", "_post_vote_orderby"));
    }
    $replyQuery = new WP_Query($reply_args);
    ?>
	<!-- ANSWERS LOOP -->
	<div id="answers_main_list">
		<?php 
    if ($replyQuery->have_posts()) {
        while ($replyQuery->have_posts()) {
            $replyQuery->the_post();
            global $post, $qa_answer, $qa_answer_comments;
            $qa_answer = QA_Answers::convert($post);
            $answersData[] = $qa_answer;
            $qa_answer_comments = get_comments(array('post_id' => $qa_answer->ID, 'parent' => 0, 'status' => 'approve', 'post_status' => 'publish', 'order' => 'ASC', 'type' => 'answer'));
            $commentsData = array_merge($commentsData, $qa_answer_comments);
            ?>
			<div class="row question-main-content question-item answer-item" id="<?php 
            echo $qa_answer->ID;
            ?>
">
			    <?php 
            get_template_part('template/item', 'answer');
            ?>
			</div><!-- END REPLY-ITEM -->
			<?php 
        }
    }
    wp_reset_query();
    ?>
	</div>
	<!-- ANSWERS LOOP -->
	<div class="row paginations <?php 
    echo $replyQuery->max_num_pages > 1 ? '' : 'collapse';
    ?>
">
	    <div class="col-md-12">
	        <?php 
    echo paginate_links(array('base' => get_permalink($question_ID) . '%#%', 'format' => $wp_rewrite->using_permalinks() ? 'page/%#%' : '?paged=%#%', 'current' => max(1, $paged), 'total' => $replyQuery->max_num_pages, 'mid_size' => 1, 'prev_text' => '<', 'next_text' => '>', 'type' => 'list'));
    ?>
	    </div>
	</div><!-- END PAGINATIONS -->
	<script type="text/javascript">
		<?php 
    $parent_comments = get_comments(array('post_id' => $question_ID, 'parent' => 0, 'status' => 'approve', 'post_status' => 'publish', 'order' => 'ASC', 'type' => 'question'));
    $commentsData = !empty($commentsData) ? $commentsData : array();
    ?>
		var answersData  = <?php 
    echo defined('JSON_HEX_QUOT') ? json_encode($answersData, JSON_HEX_QUOT) : json_encode($answersData);
    ?>
;
		var commentsData = <?php 
    echo defined('JSON_HEX_QUOT') ? json_encode(array_merge($parent_comments, $commentsData), JSON_HEX_QUOT) : json_encode(array_merge($parent_comments, $commentsData));
    ?>
;
	</script>
<?php 
}
示例#5
0
 public function update_post()
 {
     try {
         global $current_user;
         if (!isset($_POST['do_action'])) {
             throw new Exception(__("Invalid action", ET_DOMAIN));
         }
         if (isset($_POST['content']['post_title']) && $_POST['content']['post_title'] != strip_tags($_POST['content']['post_title'])) {
             throw new Exception(__("Post title should not contain any HTML Tag.", ET_DOMAIN));
         }
         $action = $_POST['do_action'];
         switch ($action) {
             case 'vote_down':
                 if (!qa_user_can('vote_down')) {
                     throw new Exception(__("You don't have enough point to vote up.", ET_DOMAIN));
                 }
             case 'vote_up':
                 if (!qa_user_can('vote_up')) {
                     throw new Exception(__("You don't have enough point to vote down.", ET_DOMAIN));
                 }
                 QA_Questions::vote($_POST['ID'], $action);
                 $post = QA_Questions::convert(get_post($_POST['ID']));
                 $resp = array('success' => true, 'data' => $post);
                 break;
             case 'accept-answer':
             case 'un-accept-answer':
                 $question = get_post($_POST['post_parent']);
                 $answerID = $action == "accept-answer" ? $_POST['ID'] : 0;
                 if ($current_user->ID != $question->post_author) {
                     throw new Exception(__("Only question author can mark answered.", ET_DOMAIN));
                     return false;
                 }
                 QA_Questions::mark_answer($_POST['post_parent'], $answerID);
                 do_action('qa_accept_answer', $answerID, $action);
                 $resp = array('success' => true);
                 break;
             case 'saveComment':
                 $data = array();
                 $data['comment_ID'] = $_POST['comment_ID'];
                 $data['comment_content'] = $_POST['comment_content'];
                 $success = QA_Comments::update($data);
                 $comment = QA_Comments::convert(get_comment($_POST['comment_ID']));
                 $resp = array('success' => true, 'data' => $comment);
                 break;
             case 'savePost':
                 $data = array();
                 $data['ID'] = $_POST['ID'];
                 $data['post_content'] = $_POST['post_content'];
                 $data['post_author'] = $_POST['post_author'];
                 $success = QA_Answers::update($data);
                 $post = QA_Answers::convert(get_post($_POST['ID']));
                 if ($success && !is_wp_error($success)) {
                     $resp = array('success' => true, 'data' => $post);
                 } else {
                     $resp = array('success' => false, 'data' => $post, 'msg' => $success->get_error_message());
                 }
                 break;
             case 'saveQuestion':
                 $data = $_POST['content'];
                 $data['ID'] = $_POST['ID'];
                 $data['qa_tag'] = isset($data['tags']) ? $data['tags'] : array();
                 $data['post_author'] = $_POST['post_author'];
                 unset($data['tags']);
                 $success = QA_Questions::update($data);
                 $post = QA_Questions::convert(get_post($_POST['ID']));
                 if ($success && !is_wp_error($success)) {
                     $resp = array('success' => true, 'data' => $post, 'msg' => __('Question has been saved successfully.', ET_DOMAIN), 'redirect' => get_permalink($_POST['ID']));
                 } else {
                     $resp = array('success' => false, 'data' => $post, 'msg' => $success->get_error_message());
                 }
                 break;
             case 'approve':
                 $id = $_POST['ID'];
                 $success = QA_Questions::change_status($id, "publish");
                 $post = QA_Questions::convert(get_post($id));
                 $point = qa_get_badge_point();
                 //store question id to data for send mail
                 QA_Engine::qa_questions_new_answer($id);
                 if ($success && !is_wp_error($success)) {
                     if ($post->post_type == "question") {
                         //add points to user
                         if (!empty($point->create_question)) {
                             qa_update_user_point($post->post_author, $point->create_question);
                         }
                         // set transient for new question
                         set_transient('qa_notify_' . mt_rand(100000, 999999), array('title' => __('New Question', ET_DOMAIN), 'content' => __("There's a new post, why don't you give a look at", ET_DOMAIN) . ' <a href ="' . get_permalink($id) . '">' . get_the_title($id) . '</a>', 'type' => 'update', 'user' => md5($current_user->user_login)), 20);
                         $resp = array('success' => true, 'data' => $post, 'msg' => __("You've just successfully approved a question.", ET_DOMAIN), 'redirect' => get_permalink($id));
                     } else {
                         if ($post->post_type == "answer") {
                             //add point to user
                             if (!empty($point->post_answer)) {
                                 qa_update_user_point($post->post_author, $point->post_answer);
                             }
                             $resp = array('success' => true, 'data' => $post, 'msg' => __("You've just successfully approved an answer.", ET_DOMAIN), 'redirect' => get_permalink($id));
                         }
                     }
                 } else {
                     $resp = array('success' => false, 'data' => $post, 'msg' => $success->get_error_message());
                 }
                 break;
             case 'follow':
             case 'unfollow':
                 if (!$current_user->ID) {
                     throw new Exception(__('Login required', ET_DOMAIN));
                 }
                 $result = QA_Questions::toggle_follow($_POST['ID'], $current_user->ID);
                 if (!is_array($result)) {
                     throw new Exception(__('Error occurred', ET_DOMAIN));
                 }
                 if (in_array($current_user->ID, $result)) {
                     $msg = __('You have started following this question.', ET_DOMAIN);
                 } else {
                     $msg = __('You have stopped following this question.', ET_DOMAIN);
                 }
                 $resp = array('success' => true, 'msg' => $msg, 'data' => array('isFollow' => in_array($current_user->ID, $result), 'following' => $result));
                 break;
             case 'report':
                 $id = $_POST['ID'];
                 if (!isset($_POST) || !$id) {
                     throw new Exception(__("Invalid post", ET_DOMAIN));
                 } else {
                     $fl = $this->report($id, $_POST['data']['message']);
                     if ($fl) {
                         $resp = array('success' => true, 'msg' => __("You have reported successfully", ET_DOMAIN));
                     } else {
                         $resp = array('success' => false, 'msg' => __("Error when reported!", ET_DOMAIN));
                     }
                 }
                 break;
             default:
                 throw new Exception(__("Invalid action", ET_DOMAIN));
                 break;
         }
     } catch (Exception $e) {
         $resp = array('success' => false, 'msg' => $e->getMessage());
     }
     return $resp;
 }