예제 #1
0
/**
 * Save question submitted
 * @return void
 */
function dwqa_submit_question()
{
    global $post_submit_filter, $dwqa_options;
    if (isset($_POST['dwqa-action']) && 'dwqa-submit-question' == $_POST['dwqa-action']) {
        global $dwqa_current_error;
        $valid_captcha = dwqa_valid_captcha('question');
        $dwqa_submit_question_errors = new WP_Error();
        if (isset($_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'dwqa-submit-question-nonce-#!')) {
            if ($valid_captcha) {
                if (empty($_POST['question-title'])) {
                    $dwqa_submit_question_errors->add('submit_question', 'You must enter a valid question title');
                    return false;
                }
                $title = esc_html($_POST['question-title']);
                $category = isset($_POST['question-category']) ? (int) $_POST['question-category'] : 0;
                if (!term_exists($category, 'dwqa-question_category')) {
                    $category = 0;
                }
                $tags = isset($_POST['question-tag']) ? esc_html($_POST['question-tag']) : '';
                $content = isset($_POST['question-content']) ? $_POST['question-content'] : '';
                $content = wp_kses(dwqa_pre_content_filter($content), $post_submit_filter);
                $user_id = 0;
                $is_anonymous = false;
                if (is_user_logged_in()) {
                    $user_id = get_current_user_id();
                } else {
                    //$post_author_email = $_POST['user-email'];
                    if (isset($_POST['login-type']) && $_POST['login-type'] == 'sign-in') {
                        $user = wp_signon(array('user_login' => $_POST['user-name'], 'user_password' => $_POST['user-password']), false);
                        if (!is_wp_error($user)) {
                            global $current_user;
                            $current_user = $user;
                            get_currentuserinfo();
                            $user_id = $user->data->ID;
                        } else {
                            $dwqa_current_error = $user;
                            return false;
                        }
                    } else {
                        //Create new user
                        $users_can_register = get_option('users_can_register');
                        if (isset($_POST['user-email']) && isset($_POST['user-name-signup']) && $users_can_register && !email_exists($_POST['user-email']) && !username_exists($_POST['user-name-signup'])) {
                            if (isset($_POST['password-signup'])) {
                                $password = $_POST['password-signup'];
                            } else {
                                $password = wp_generate_password(12, false);
                            }
                            $user_id = wp_create_user($_POST['user-name-signup'], $password, $_POST['user-email']);
                            if (is_wp_error($user_id)) {
                                $dwqa_current_error = $user_id;
                                return false;
                            }
                            wp_new_user_notification($user_id, $password);
                            $user = wp_signon(array('user_login' => $_POST['user-name-signup'], 'user_password' => $password), false);
                            if (!is_wp_error($user)) {
                                global $current_user;
                                $current_user = $user;
                                get_currentuserinfo();
                                $user_id = $user->data->ID;
                            } else {
                                $dwqa_current_error = $user;
                                return false;
                            }
                        } else {
                            $message = '';
                            if (!$users_can_register) {
                                $message .= __('User Registration was disabled.', 'dwqa') . '<br>';
                            }
                            if (email_exists($_POST['user-email'])) {
                                $message .= __('This email is already registered, please choose another one.', 'dwqa') . '<br>';
                            }
                            if (username_exists($_POST['user-name'])) {
                                $message .= __('This username is already registered. Please choose another one.', 'dwqa') . '<br>';
                            }
                            $dwqa_current_error = new WP_Error('submit_question', $message);
                            return false;
                        }
                    }
                }
                $post_status = isset($_POST['private-message']) && $_POST['private-message'] ? 'private' : 'publish';
                $postarr = array('comment_status' => 'open', 'post_author' => $user_id, 'post_content' => $content, 'post_status' => $post_status, 'post_title' => $title, 'post_type' => 'dwqa-question', 'tax_input' => array('dwqa-question_category' => array($category), 'dwqa-question_tag' => explode(',', $tags)));
                if (dwqa_current_user_can('post_question')) {
                    $new_question = dwqa_insert_question($postarr);
                } else {
                    $new_question = new WP_Error('permission', __("You do not have permission to submit question.", 'dwqa'));
                }
                if (!is_wp_error($new_question)) {
                    exit(wp_safe_redirect(get_permalink($new_question)));
                } else {
                    $dwqa_current_error = $new_question;
                }
            } else {
                $dwqa_submit_question_errors->add('submit_question', __('Captcha is not correct', 'dwqa'));
            }
        } else {
            $dwqa_submit_question_errors->add('submit_question', __('Are you cheating huh?', 'dwqa'));
        }
        $dwqa_current_error = $dwqa_submit_question_errors;
    }
}
예제 #2
0
 public function submit_question()
 {
     global $dwqa_options;
     if (isset($_POST['dwqa-question-submit'])) {
         global $dwqa_current_error;
         $valid_captcha = dwqa_valid_captcha('question');
         $dwqa_submit_question_errors = new WP_Error();
         if (isset($_POST['_wpnonce']) && wp_verify_nonce(esc_html($_POST['_wpnonce']), '_dwqa_submit_question')) {
             if ($valid_captcha) {
                 if (empty($_POST['question-title'])) {
                     // $dwqa_submit_question_errors->add( 'submit_question', 'You must enter a valid question title' );
                     dwqa_add_notice(__('You must enter a valid question title.', 'error'));
                     return false;
                 }
                 $title = esc_html($_POST['question-title']);
                 $category = isset($_POST['question-category']) ? intval($_POST['question-category']) : 0;
                 if (!term_exists($category, 'dwqa-question_category')) {
                     $category = 0;
                 }
                 $tags = isset($_POST['question-tag']) ? esc_html($_POST['question-tag']) : '';
                 $content = isset($_POST['question-content']) ? $_POST['question-content'] : '';
                 $content = apply_filters('dwqa_prepare_question_content', $content);
                 $user_id = 0;
                 $is_anonymous = false;
                 if (is_user_logged_in()) {
                     $user_id = get_current_user_id();
                 } else {
                     //$post_author_email = $_POST['user-email'];
                     if (isset($_POST['login-type']) && $_POST['login-type'] == 'sign-in') {
                         $user = wp_signon(array('user_login' => isset($_POST['user-name']) ? esc_html($_POST['user-name']) : '', 'user_password' => isset($_POST['user-password']) ? esc_html($_POST['user-password']) : ''), false);
                         if (!is_wp_error($user)) {
                             global $current_user;
                             $current_user = $user;
                             get_currentuserinfo();
                             $user_id = $user->data->ID;
                         } else {
                             $dwqa_current_error = $user;
                             return false;
                         }
                     } elseif (isset($_POST['login-type']) && $_POST['login-type'] == 'sign-up') {
                         //Create new user
                         $users_can_register = get_option('users_can_register');
                         if (isset($_POST['user-email']) && isset($_POST['user-name-signup']) && $users_can_register && !email_exists($_POST['user-email']) && !username_exists($_POST['user-name-signup'])) {
                             if (isset($_POST['password-signup'])) {
                                 $password = esc_html($_POST['password-signup']);
                             } else {
                                 $password = wp_generate_password(12, false);
                             }
                             $user_id = wp_create_user(esc_html($_POST['user-name-signup']), $password, sanitize_email($_POST['user-email']));
                             if (is_wp_error($user_id)) {
                                 $dwqa_current_error = $user_id;
                                 return false;
                             }
                             wp_new_user_notification($user_id, $password);
                             $user = wp_signon(array('user_login' => esc_html($_POST['user-name-signup']), 'user_password' => $password), false);
                             if (!is_wp_error($user)) {
                                 global $current_user;
                                 $current_user = $user;
                                 get_currentuserinfo();
                                 $user_id = $user->data->ID;
                             } else {
                                 $dwqa_current_error = $user;
                                 return false;
                             }
                         } else {
                             $message = '';
                             if (!$users_can_register) {
                                 $message .= __('User Registration was disabled.', 'dwqa') . '<br>';
                             }
                             if (isset($_POST['user-name']) && email_exists(sanitize_email($_POST['user-email']))) {
                                 $message .= __('This email is already registered, please choose another one.', 'dwqa') . '<br>';
                             }
                             if (isset($_POST['user-name']) && username_exists(esc_html($_POST['user-name']))) {
                                 $message .= __('This username is already registered. Please use another one.', 'dwqa') . '<br>';
                             }
                             // $dwqa_current_error = new WP_Error( 'submit_question', $message );
                             dwqa_add_notice($message, 'error');
                             return false;
                         }
                     } else {
                         $is_anonymous = true;
                         $question_author_email = isset($_POST['_dwqa_anonymous_email']) && is_email($_POST['_dwqa_anonymous_email']) ? sanitize_email($_POST['_dwqa_anonymous_email']) : false;
                         $user_id = 0;
                     }
                 }
                 $post_status = isset($_POST['question-status']) && esc_html($_POST['question-status']) ? $_POST['question-status'] : 'publish';
                 //Enable review mode
                 global $dwqa_general_settings;
                 if (isset($dwqa_general_settings['enable-review-question']) && $dwqa_general_settings['enable-review-question'] && $post_status != 'private' && !current_user_can('manage_options')) {
                     $post_status = 'pending';
                 }
                 $postarr = array('comment_status' => 'open', 'post_author' => $user_id, 'post_content' => $content, 'post_status' => $post_status, 'post_title' => $title, 'post_type' => 'dwqa-question', 'tax_input' => array('dwqa-question_category' => array($category), 'dwqa-question_tag' => explode(',', $tags)));
                 if (apply_filters('dwqa-current-user-can-add-question', dwqa_current_user_can('post_question'), $postarr)) {
                     $new_question = $this->insert_question($postarr);
                 } else {
                     //$dwqa_submit_question_errors->add( 'submit_question',  __( 'You do not have permission to submit question.', 'dwqa' ) );
                     dwqa_add_notice(__('You do not have permission to submit question.', 'dwqa'), 'error');
                     $new_question = $dwqa_submit_question_errors;
                 }
                 if (dwqa_count_notices('error') == 0) {
                     if ($is_anonymous) {
                         update_post_meta($new_question, '_dwqa_anonymous_email', $question_author_email);
                         update_post_meta($new_question, '_dwqa_is_anonymous', true);
                     }
                     if (isset($dwqa_options['enable-review-question']) && $dwqa_options['enable-review-question'] && !current_user_can('manage_options') && $post_status != 'private') {
                         dwqa_add_notice(__('Your question is waiting moderator.', 'dwqa'), 'success');
                     } else {
                         exit(wp_safe_redirect(get_permalink($new_question)));
                     }
                 }
             } else {
                 // $dwqa_submit_question_errors->add( 'submit_question', __( 'Captcha is not correct','dwqa' ) );
                 dwqa_add_notice(__('Captcha is not correct', 'dwqa'), 'error');
             }
         } else {
             // $dwqa_submit_question_errors->add( 'submit_question', __( 'Are you cheating huh?','dwqa' ) );
             dwqa_add_notice(__('Are you cheating huh?', 'dwqa'), 'error');
         }
         //$dwqa_current_error = $dwqa_submit_question_errors;
     }
 }
예제 #3
0
파일: Answer.php 프로젝트: Trideon/gigolo
 public function insert()
 {
     global $dwqa_options;
     if (!isset($_POST['dwqa-action']) || !isset($_POST['submit-answer'])) {
         return false;
     }
     $dwqa_add_answer_errors = new WP_Error();
     if (!isset($_POST['_wpnonce']) || !wp_verify_nonce(esc_html($_POST['_wpnonce']), '_dwqa_add_new_answer')) {
         $dwqa_add_answer_errors->add('answer_question', '"Helllo", Are you cheating huh?.');
     }
     if ($_POST['submit-answer'] == __('Delete draft', 'dwqa')) {
         $draft = isset($_POST['answer-id']) ? intval($_POST['answer-id']) : 0;
         if ($draft) {
             wp_delete_post($draft);
         }
         return false;
     }
     if (empty($_POST['answer-content']) || empty($_POST['question'])) {
         if (empty($_POST['answer-content'])) {
             $dwqa_add_answer_errors->add('answer_question', 'answer content is empty');
         }
         if (empty($_POST['question'])) {
             $dwqa_add_answer_errors->add('answer_question', 'question is empty');
         }
     } else {
         $user_id = 0;
         $is_anonymous = false;
         if (is_user_logged_in()) {
             $user_id = get_current_user_id();
         } else {
             $is_anonymous = true;
             if (isset($_POST['user-email']) && is_email($_POST['user-email'])) {
                 $post_author_email = sanitize_email($_POST['user-email']);
             }
         }
         $question_id = intval($_POST['question']);
         $question = get_post($question_id);
         $answer_title = __('Answer for ', 'dwqa') . $question->post_title;
         $answ_content = apply_filters('dwqa_prepare_answer_content', $_POST['answer-content']);
         $post_status = isset($_POST['private-message']) && esc_html($_POST['private-message']) ? 'private' : 'publish';
         $answers = array('comment_status' => 'open', 'post_author' => $user_id, 'post_content' => $answ_content, 'post_status' => $post_status, 'post_title' => $answer_title, 'post_type' => $this->get_slug());
         if ($_POST['submit-answer'] == __('Save draft', 'dwqa')) {
             $answers['post_status'] = 'draft';
         } else {
             if (isset($_POST['privacy']) && 'private' == $_POST['privacy']) {
                 $answers['post_status'] = 'private';
             }
         }
         switch ($_POST['dwqa-action']) {
             case 'add-answer':
                 $valid_captcha = dwqa_valid_captcha('single-question');
                 if ($valid_captcha) {
                     if (dwqa_current_user_can('post_answer')) {
                         $answer_id = wp_insert_post($answers, true);
                     } else {
                         $answer_id = new WP_Error('permission', __('You do not have permission to submit question.', 'dwqa'));
                     }
                     if (!is_wp_error($answer_id)) {
                         //Send email alert for author of question about this answer
                         $question_author = $question->post_author;
                         if (user_can($answers['post_author'], 'edit_posts') && $answers['post_status'] != 'draft') {
                             update_post_meta($question_id, '_dwqa_status', 'answered');
                             update_post_meta($question_id, '_dwqa_answered_time', time());
                         }
                         update_post_meta($answer_id, '_question', $question_id);
                         if ($is_anonymous) {
                             update_post_meta($answer_id, '_dwqa_is_anonymous', true);
                             if (isset($post_author_email) && is_email($post_author_email)) {
                                 update_post_meta($answer_id, '_dwqa_anonymous_email', $post_author_email);
                             }
                         }
                         do_action('dwqa_add_answer', $answer_id, $question_id);
                         // wp_redirect( get_permalink( $question_id ) );
                         // wp_send_json_success( array( 'url' => get_permalink( $question_id ) ) );
                         // return true;
                     } else {
                         $dwqa_add_answer_errors = $answer_id;
                     }
                 } else {
                     $dwqa_add_answer_errors->add('in_valid_captcha', __('Captcha is not correct', 'dwqa'));
                 }
                 break;
             case 'update-answer':
                 if (!isset($_POST['answer-id'])) {
                     $dwqa_add_answer_errors->add('missing-content', __('Answer is missing', 'dwqa'));
                     break;
                 }
                 $answer_id = intval($_POST['answer-id']);
                 $answer_author = get_post_field('post_author', $answer_id);
                 global $current_user;
                 if (!(dwqa_current_user_can('edit_answer') || is_user_logged_in() && $answer_author == $current_user->ID)) {
                     $dwqa_add_answer_errors->add('permission-denided', __('You do not have permission to edit this post', 'dwqa'));
                     break;
                 }
                 if (get_post_type($answer_id) != 'dwqa-answer') {
                     $dwqa_add_answer_errors->add('posttype-error', __('This post is not an answer', 'dwqa'));
                     break;
                 }
                 $answer_update = array('ID' => $answer_id, 'post_content' => $answ_content);
                 $post_status = get_post_status($answer_id);
                 if ($post_status == 'draft' && strtolower($_POST['submit-answer']) == 'publish' || $post_status != 'draft' && strtolower($_POST['submit-answer']) == 'update') {
                     $answer_update['post_status'] = isset($_POST['privacy']) && 'private' == esc_html($_POST['privacy']) ? 'private' : 'publish';
                     update_post_meta($question_id, '_dwqa_status', 're-open');
                 }
                 $old_post = get_post($answer_id);
                 $answer_id = wp_update_post($answer_update);
                 $new_post = get_post($answer_id);
                 do_action('dwqa_update_answer', $answer_id, $old_post, $new_post);
                 if ($answer_id) {
                     wp_safe_redirect(get_permalink($question_id));
                     exit(0);
                 }
                 break;
         }
     }
     $url = get_permalink($question_id);
     $error_messages = $dwqa_add_answer_errors->get_error_messages();
     foreach ($error_messages as $value) {
         $url = esc_url_raw(add_query_arg('errors', urlencode($value), $url));
     }
     wp_safe_redirect($url);
     exit(0);
 }