Example #1
0
 public function update_question()
 {
     if (isset($_POST['dwqa-edit-question-submit'])) {
         if (isset($_POST['_wpnonce']) && wp_verify_nonce(esc_html($_POST['_wpnonce']), '_dwqa_edit_question')) {
             if (!dwqa_current_user_can('edit_question')) {
                 dwqa_add_notice(__("You do not have permission to edit question", 'dwqa'), 'error');
             }
             $question_title = apply_filters('dwqa_prepare_edit_question_title', $_POST['question_title']);
             if (empty($question_title)) {
                 dwqa_add_notice(__('You must enter a valid question title.', 'dwqa'), 'error');
             }
             $question_id = isset($_POST['question_id']) ? $_POST['question_id'] : false;
             if (!$question_id) {
                 dwqa_add_notice(__('Question is missing.', 'dwqa'), 'error');
             }
             if ('dwqa-question' !== get_post_type($question_id)) {
                 dwqa_add_notice(__('This post is not question.', 'dwqa'), 'error');
             }
             $question_content = apply_filters('dwqa_prepare_edit_question_content', $_POST['question_content']);
             $tags = isset($_POST['question-tag']) ? esc_html($_POST['question-tag']) : '';
             $category = isset($_POST['question-category']) ? intval($_POST['question-category']) : 0;
             if (!term_exists($category, 'dwqa-question_category')) {
                 $category = 0;
             }
             do_action('dwqa_prepare_update_question', $question_id);
             if (dwqa_count_notices('error') > 0) {
                 return false;
             }
             $args = array('ID' => $question_id, 'post_content' => $question_content, 'post_title' => $question_title, 'tax_input' => array('dwqa-question_category' => array($category), 'dwqa-question_tag' => explode(',', $tags)));
             $new_question_id = wp_update_post($args);
             if (!is_wp_error($new_question_id)) {
                 $old_post = get_post($question_id);
                 $new_post = get_post($new_question_id);
                 do_action('dwqa_update_question', $new_question_id, $old_post, $new_post);
                 wp_safe_redirect(get_permalink($new_question_id));
             } else {
                 dwqa_add_wp_error_message($new_question_id);
                 return false;
             }
         } else {
             dwqa_add_notice(__('Hello, Are you cheating huh?', 'dwqa'), 'error');
             return false;
         }
         exit(0);
     }
 }
function dwqa_add_wp_error_message($errors, $comment = false)
{
    if (is_wp_error($errors)) {
        dwqa_add_notice($errors->get_error_message(), 'error', $comment);
    }
}