示例#1
0
 public function create()
 {
     try {
         $args = $_POST['content'];
         global $current_user;
         if (!is_user_logged_in()) {
             throw new Exception(__("You must log in to post question.", ET_DOMAIN));
         }
         if (isset($args['post_title']) && $args['post_title'] != strip_tags($args['post_title'])) {
             throw new Exception(__("Post title should not contain any HTML Tag.", ET_DOMAIN));
         }
         if (isset($args['qa_nonce']) && wp_verify_nonce($args['qa_nonce'], 'insert_comment')) {
             if (!qa_user_can('add_comment')) {
                 throw new Exception(__("You don't have enough point to add a comment.", ET_DOMAIN));
             }
             $args['comment_content'] = $args['post_content'];
             $args['comment_author'] = $current_user->user_login;
             $args['comment_author_email'] = $current_user->user_email;
             $result = QA_Comments::insert($args);
             $comment = QA_Comments::convert(get_comment($result));
             if (is_wp_error($result)) {
                 $resp = array('success' => false, 'msg' => __('An error occur when created comment.', ET_DOMAIN));
             } else {
                 $resp = array('success' => true, 'msg' => __('Comment has been created successfully.', ET_DOMAIN), 'data' => $comment);
             }
         } elseif (isset($args['qa_nonce']) && wp_verify_nonce($args['qa_nonce'], 'insert_answer')) {
             $result = QA_Answers::insert_answer($args['post_parent'], $args['post_content']);
             QA_Answers::update_field($result, "et_vote_count", 0);
             $answer = QA_Answers::convert(get_post($result));
             if (is_wp_error($result)) {
                 $resp = array('success' => false, 'msg' => __('An error occur when created answer.', ET_DOMAIN));
             } else {
                 $msg = ae_get_option('pending_answers') && !(current_user_can('manage_options') || qa_user_can('approve_answer')) ? __('Your answer has been created successfully and need to be approved by Admin before displayed!', ET_DOMAIN) : __('Answer has been created successfully.', ET_DOMAIN);
                 $resp = array('success' => true, 'redirect' => get_permalink($answer->post_parent), 'msg' => $msg, 'data' => $answer);
             }
         } elseif (isset($args['qa_nonce']) && wp_verify_nonce($args['qa_nonce'], 'insert_question')) {
             //if option for captcha is enable
             if (ae_get_option('gg_question_captcha') && !et_load_mobile()) {
                 $captcha = isset($args['captcha']) ? $args['captcha'] : '';
                 //verify captcha
                 ae_verify_captcha($captcha, __('Please enter a valid captcha!', ET_DOMAIN));
             }
             $cats = array('qa_tag' => isset($args['tags']) ? $args['tags'] : array(), 'question_category' => $args['question_category']);
             $status = ae_get_option("pending_questions") && !current_user_can('manage_options') ? "pending" : "publish";
             $result = QA_Questions::insert_question($args['post_title'], $args['post_content'], $cats, $status);
             QA_Questions::update_field($result, "et_vote_count", 0);
             QA_Questions::update_field($result, "et_answers_count", 0);
             $post = QA_Questions::convert(get_post($result));
             $msg = ae_get_option("pending_questions") && !current_user_can('manage_options') ? __('Your question has been created successfully. It\'ll appear right after being approved by admin.', ET_DOMAIN) : __('Question has been created successfully.', ET_DOMAIN);
             $redirect = ae_get_option("pending_questions") && !current_user_can('manage_options') ? home_url() : get_permalink($result);
             if (is_wp_error($result)) {
                 $resp = array('success' => false, 'msg' => __('An error occur when created question.', ET_DOMAIN));
             } else {
                 $resp = array('success' => true, 'redirect' => $redirect, 'msg' => $msg, 'data' => $post);
             }
         } else {
             throw new Exception("Error Processing Request", 1);
         }
     } catch (Exception $e) {
         $resp = array('success' => false, 'msg' => $e->getMessage());
     }
     return $resp;
 }
示例#2
0
 public function register()
 {
     $param = $_REQUEST['content'];
     $args = array('user_email' => $param['email'], 'user_pass' => $param['password'], 'user_login' => $param['username'], 'display_name' => isset($param['display_name']) ? $param['display_name'] : $param['username']);
     $captcha = isset($_REQUEST['captcha']) ? $_REQUEST['captcha'] : '';
     // validate here, later
     try {
         //verify captcha
         ae_verify_captcha($captcha, __('Please enter a valid captcha!', ET_DOMAIN));
         $role = apply_filters('qa_custom_role', 'author');
         do_action('je_before_user_register', $args);
         // apply register & log the user in
         $auto_sign = ae_get_option('user_confirm') ? false : true;
         $user_id = et_register($args, $role, $auto_sign);
         if (is_wp_error($user_id)) {
             throw new Exception($user_id->get_error_message(), 401);
         }
         $data = get_userdata($user_id);
         $userdata = QA_Member::convert($data);
         // generate new nonces
         $msg = ae_get_option('user_confirm') ? __('You have registered an account successfully but are not able to join the discussions yet. Please confirm your email address first.', ET_DOMAIN) : __('You are registered and logged in successfully.', ET_DOMAIN);
         $response = array('success' => true, 'code' => 200, 'msg' => $msg, 'data' => $userdata, 'redirect' => apply_filters('qa_filter_redirect_link_after_register', home_url()));
     } catch (Exception $e) {
         $response = array('success' => false, 'code' => $e->getCode(), 'msg' => $e->getMessage());
     }
     wp_send_json($response);
 }