Beispiel #1
0
 /**
  * Additional methods in theme
  */
 public static function insert_answer($question_id, $content, $author = false, $answer_id = 0)
 {
     $instance = self::get_instance();
     global $current_user;
     if (!$current_user->ID) {
         return new WP_Error('logged_in_required', __('Login Required', ET_DOMAIN));
     }
     if ($author == false) {
         $author = $current_user->ID;
     }
     $question = get_post($question_id);
     $content = preg_replace('/\\[quote\\].*(<br\\s*\\/?>\\s*).*\\[\\/quote\\]/', '', $content);
     $data = array('post_title' => 'RE: ' . $question->post_title, 'post_content' => $content, 'post_parent' => $question_id, 'author' => $author, 'post_type' => 'answer', 'post_status' => ae_get_option('pending_answers') && !(current_user_can('manage_options') || qa_user_can('approve_answer')) ? 'pending' : 'publish', 'et_answer_parent' => $answer_id);
     $result = $instance->_insert($data);
     // if item is inserted successfully, update statistic
     if ($result) {
         //update user answers count
         $count = et_count_user_posts($current_user->ID, 'answer');
         update_user_meta($current_user->ID, 'et_answer_count', $count);
         //update user following questions
         $follow_questions = (array) get_user_meta($current_user->ID, 'qa_following_questions', true);
         if (!in_array($question_id, $follow_questions)) {
             array_push($follow_questions, $question_id);
         }
         $follow_questions = array_unique(array_filter($follow_questions));
         update_user_meta($current_user->ID, 'qa_following_questions', $follow_questions);
         // update question's update date
         update_post_meta($question_id, 'et_updated_date', current_time('mysql'));
         // update last update author
         update_post_meta($question_id, 'et_last_author', $author);
         // update answer_authors
         $answer_authors = get_post_meta($question_id, 'et_answer_authors', true);
         $answer_authors = is_array($answer_authors) ? $answer_authors : array();
         if (!in_array($author, $answer_authors)) {
             $answer_authors[] = $author;
             update_post_meta($question_id, 'et_answer_authors', $answer_authors);
         }
         // update answer author for answer
         if ($answer_id) {
             $answer_authors = get_post_meta($answer_id, 'et_answer_authors', true);
             $answer_authors = is_array($answer_authors) ? $answer_authors : array();
             if (!in_array($author, $answer_authors)) {
                 $answer_authors[] = $author;
                 update_post_meta($answer_id, 'et_answer_authors', $answer_authors);
             }
         }
         if ($answer_id == 0) {
             QA_Questions::count_comments($question->ID);
         } else {
             QA_Answers::count_comments($answer_id);
         }
     }
     return $result;
 }