Пример #1
0
 public static function newThread($data = array())
 {
     $userId = CMA::getPostingUserId();
     $user = get_userdata($userId);
     if (empty($userId) or empty($user)) {
         throw new Exception(CMA::__('Invalid user.'));
     }
     $title = self::titleFilter($data['title']);
     $content = self::contentFilter($data['content'], $userId);
     self::validateTitle($title, $editId = false, $errors);
     if (!CMA_Settings::getOption(CMA_Settings::OPTION_QUESTION_DESCRIPTION_OPTIONAL) && empty($content)) {
         $errors[] = __('Content cannot be empty', 'cm-answers-pro');
     }
     if (($badWord = CMA_BadWords::filterIfEnabled($content)) !== false) {
         $errors[] = sprintf(CMA_Labels::getLocalized('msg_content_includes_bad_word'), $badWord);
     }
     if (!empty($_FILES) and !self::areQuestionAttachmentsAllowed()) {
         $errors[] = __('Upload is not allowed.', 'cm-answers-pro');
     } elseif (!self::validateUploadSize()) {
         $errors[] = __('The file you uploaded is too big', 'cm-answers-pro');
     } elseif (!self::validateUploadNames()) {
         $errors[] = __('The file you uploaded is not allowed', 'cm-answers-pro');
     }
     if (!empty($data['category']) && $data['category'] > 0) {
         if ($category = CMA_Category::getInstance($data['category'])) {
             if (!$category->isVisible()) {
                 $errors[] = CMA::__('You have no permission to post this question.');
             }
         } else {
             $errors[] = CMA::__('Choose a valid category.');
         }
     } else {
         if (CMA_Settings::getOption(CMA_Settings::OPTION_QUESTION_REQUIRE_CATEGORY)) {
             $errors[] = CMA::__('Choose a category.');
         }
     }
     if (!empty($errors)) {
         throw new Exception(serialize($errors));
     }
     if (CMA_Settings::getOption(CMA_Settings::OPTION_QUESTION_AUTO_APPROVE) || self::isAuthorAutoApproved($userId)) {
         $status = 'publish';
     } else {
         $status = 'draft';
         if (self::getSpamFilter() || CMA_Settings::getOption(CMA_Settings::OPTION_SIMULATE_COMMENT)) {
             /** Hack, simulate comment adding to trigger spam filters * */
             $commentdata = array('comment_post_ID' => 0, 'comment_author' => $user->first_name, 'comment_author_email' => $user->user_email, 'comment_author_url' => '', 'comment_content' => $title . ' ' . $content, 'comment_type' => self::POST_TYPE, 'user_ID' => $userId, 'comment_parent' => 0, 'comment_author_IP' => preg_replace('/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR']), 'comment_date' => current_time('mysql'), 'comment_date_gmt' => current_time('mysql', 1), 'comment_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? substr($_SERVER['HTTP_USER_AGENT'], 0, 254) : '');
             if (CMA_Settings::getOption(CMA_Settings::OPTION_SIMULATE_COMMENT)) {
                 // Simulate comment to detect flood and so on.
                 if (wp_allow_comment($commentdata) == 'spam') {
                     $status = 'draft';
                 }
             }
         }
     }
     $postData = array('post_status' => $status, 'post_type' => self::POST_TYPE, 'post_title' => $title, 'post_content' => $content, 'post_name' => urldecode(sanitize_title_with_dashes(remove_accents($title))), 'post_author' => $userId);
     do_action('cma_question_post_before', $postData);
     $id = wp_insert_post($postData);
     if ($id instanceof WP_Error) {
         return $id->get_error_message();
     } else {
         $instance = self::getInstance($id);
         $instance->setUpdated()->setResolved(false)->setAuthorIP()->checkGeolocation();
         if (!empty($data['notify']) and $data['notify'] == 1) {
             $instance->getFollowersEngine()->addFollower();
         }
         $instance->savePostMeta(array(self::$_meta['views'] => 0));
         $instance->savePostMeta(array(self::$_meta['votes_answers'] => 0));
         $instance->savePostMeta(array(self::$_meta['votes_question'] => 0));
         $instance->savePostMeta(array(self::$_meta['votes_question_answers'] => 0));
         $instance->savePostMeta(array(self::$_meta['highestRatedAnswer'] => 0));
         $instance->savePostMeta(array(self::$_meta['stickyPost'] => 0));
         if (!empty($data['category'])) {
             $r = wp_set_post_terms($id, array($data['category']), CMA_Category::TAXONOMY, true);
         }
         if (isset($data['tags'])) {
             $r = wp_set_post_tags($id, $data["tags"], true);
         }
         if (CMA_Settings::getOption(CMA_Settings::OPTION_USER_RELATED_QUESTIONS_ENABLE) and !empty($data['userRelatedQuestions'])) {
             $instance->setUserRelatedQuestions(CMA_UserRelatedQuestions::getIdsFromRaw($data['userRelatedQuestions']));
         }
         $instance->savePost();
         $attachmentsIds = CMA_QuestionAttachment::handleUpload($instance->getId());
         if (!empty($_POST['attached']) && is_array($_POST['attached'])) {
             $attachmentsIds = array_merge($attachmentsIds, $_POST['attached']);
         }
         foreach ($attachmentsIds as $attachmentId) {
             if (!empty($attachmentId)) {
                 $instance->addAttachment($attachmentId);
             }
         }
         if (CMA_Settings::getOption(CMA_Settings::OPTION_NEW_QUESTION_EVERYBODY_FOLLOW_ENABLED)) {
             $instance->makeEverybodyFollowers();
         }
         if ($status == 'draft') {
             $instance->notifyModerator();
         } else {
             self::updateQA($userId);
             $instance->notifyAboutNewQuestion();
         }
         if (CMA_Settings::getOption(CMA_Settings::OPTION_LOGS_ENABLED)) {
             CMA_QuestionPostLog::instance()->log($id);
         }
         do_action('cma_question_post_after', $instance, $data);
         return $instance;
     }
 }
<?php

CMA_UserRelatedQuestions::bootstrap();
class CMA_UserRelatedQuestions
{
    static function bootstrap()
    {
        add_action('add_meta_boxes', array(__CLASS__, 'add_meta_boxes'));
        add_action('save_post', array(__CLASS__, 'save_post'), 10, 1);
    }
    static function add_meta_boxes()
    {
        if (CMA_Settings::getOption(CMA_Settings::OPTION_USER_RELATED_QUESTIONS_ENABLE)) {
            add_meta_box('cma-user-related-questions', 'User Related Questions', array(__CLASS__, 'render_meta_box'), CMA_Thread::POST_TYPE, 'normal', 'high');
        }
    }
    static function render_meta_box($post)
    {
        wp_enqueue_style('cma-backend', CMA_RESOURCE_URL . 'backend.css');
        wp_enqueue_script('cma-admin-script', CMA_RESOURCE_URL . 'admin_script.js');
        if ($thread = CMA_Thread::getInstance($post)) {
            $questions = $thread->getUserRelatedQuestions(false);
        } else {
            $questions = array();
        }
        $nonce = wp_create_nonce(__CLASS__);
        include CMA_PATH . '/views/backend/hooks/user_related_questions_metabox.phtml';
    }
    static function save_post($postId)
    {
        if ($thread = CMA_Thread::getInstance($postId) and self::save_post_verify_nonce()) {