Example #1
0
 /**
  * Show similar questions when asking a question.
  *
  * @since 2.0.1
  */
 public function suggest_similar_questions()
 {
     if (empty($_POST['value']) || !ap_verify_default_nonce() && !current_user_can('manage_options')) {
         wp_die('false');
     }
     $keyword = sanitize_text_field(wp_unslash($_POST['value']));
     $is_admin = (bool) $_POST['is_admin'];
     $questions = get_posts(array('post_type' => 'question', 'showposts' => 10, 's' => $keyword));
     if ($questions) {
         $items = '<div class="ap-similar-questions-head">';
         $items .= '<h3>' . ap_icon('check', true) . sprintf(__('%d similar questions found', 'anspress-question-answer'), count($questions)) . '</h3>';
         $items .= '<p>' . __('We\'ve found similar questions that have already been asked, click to read them.', 'anspress-question-answer') . '</p>';
         $items .= '</div>';
         $items .= '<div class="ap-similar-questions">';
         foreach ($questions as $p) {
             $count = ap_count_answer_meta($p->ID);
             $p->post_title = ap_highlight_words($p->post_title, $keyword);
             if ($is_admin) {
                 $items .= '<div class="ap-q-suggestion-item clearfix"><a class="select-question-button button button-primary button-small" href="' . add_query_arg(array('post_type' => 'answer', 'post_parent' => $p->ID), admin_url('post-new.php')) . '">' . __('Select', 'anspress-question-answer') . '</a><span class="question-title">' . $p->post_title . '</span><span class="acount">' . sprintf(_n('1 Answer', '%d Answers', $count, 'anspress-question-answer'), $count) . '</span></div>';
             } else {
                 $items .= '<a class="ap-sqitem clearfix" target="_blank" href="' . get_permalink($p->ID) . '"><span class="acount">' . sprintf(_n('1 Answer', '%d Answers', $count, 'anspress-question-answer'), $count) . '</span><span class="ap-title">' . $p->post_title . '</span></a>';
             }
         }
         $items .= '</div>';
         $result = array('status' => true, 'html' => $items);
     } else {
         $result = array('status' => false, 'message' => __('No related questions found', 'anspress-question-answer'));
     }
     $this->send($result);
 }
Example #2
0
 /**
  * Show similar questions when asking a question
  * @return void
  * @since 2.0.1
  */
 public function suggest_similar_questions()
 {
     if (empty($_POST['value'])) {
         return;
     }
     $keyword = sanitize_text_field($_POST['value']);
     $questions = get_posts(array('post_type' => 'question', 'showposts' => 10, 's' => $keyword));
     if ($questions) {
         $items = '<div class="ap-similar-questions-head">';
         $items .= '<h3>' . ap_icon('check', true) . sprintf(__('%d similar questions found', 'ap'), count($questions)) . '</h3>';
         $items .= '<p>' . __('We found similar questions that have already been asked, click to read them. Avoid creating duplicate questions, it will be deleted.') . '</p>';
         $items .= '</div>';
         $items .= '<div class="ap-similar-questions">';
         foreach ($questions as $p) {
             $count = ap_count_answer_meta($p->ID);
             $p->post_title = ap_highlight_words($p->post_title, $keyword);
             if (!isset($_POST['is_admin'])) {
                 $items .= '<a class="ap-sqitem clearfix" href="' . get_permalink($p->ID) . '"><span class="acount">' . sprintf(_n('1 Answer', '%d Answers', $count, 'ap'), $count) . '</span><span class="ap-title">' . $p->post_title . '</span></a>';
             } else {
                 $items .= '<div class="ap-q-suggestion-item clearfix"><a class="select-question-button button button-primary button-small" href="' . add_query_arg(array('post_type' => 'answer', 'post_parent' => $p->ID), admin_url('post-new.php')) . '">' . __('Select', 'ap') . '</a><span class="question-title">' . $p->post_title . '</span><span class="acount">' . sprintf(_n('1 Answer', '%d Answers', $count, 'ap'), $count) . '</span></div>';
             }
         }
         $items .= '</div>';
         $result = array('status' => true, 'html' => $items);
     } else {
         $result = array('status' => false, 'message' => __('No related questions found', 'ap'));
     }
     ap_send_json($result);
 }