public function handle_question_editing() { global $wpdb; if (!wp_verify_nonce($_POST['_wpnonce'], 'qa_edit')) { wp_die(__('Nonce error: It looks like you don\'t have permission to do that.', QA_TEXTDOMAIN)); } $question_id = (int) $_POST['question_id']; $question = array('post_title' => trim(wp_strip_all_tags($_POST['question_title'])), 'post_content' => trim($_POST['question_content'])); if (empty($question['post_title']) || empty($question['post_content'])) { wp_die(__('Questions must have both a title and a body. Please use your browser\'s back button to edit your question.', QA_TEXTDOMAIN)); } // Check for duplicates if (!$question_id) { $dup_id = $wpdb->get_var($wpdb->prepare("\n\t\t\t\t\tSELECT ID\n\t\t\t\t\tFROM {$wpdb->posts}\n\t\t\t\t\tWHERE post_type = 'question'\n\t\t\t\t\tAND post_status = 'publish'\n\t\t\t\t\tAND (post_title = %s OR post_content = %s)\n\t\t\t\t\tLIMIT 1\n\t\t\t\t\t", $question['post_title'], $question['post_content'])); if ($dup_id) { wp_die(sprintf(__('It seems that this question was already asked. Click <a href="%s" target="_blank">here</a> to view it, if it is approved by the admin. If title or content of your question is the same as any of the previous questions, it is regarded as duplicate. Please use your browser\'s back button to edit your question.', QA_TEXTDOMAIN), qa_get_url('single', $dup_id))); } } $question_id = $this->_insert_post($question_id, $question, array('post_type' => 'question', 'comment_status' => 'open')); return qa_get_url('single', $question_id); }
public function handle_forms() { if (!isset($_REQUEST['_wpnonce'])) { return; } // Handle actions if (isset($_REQUEST['dln_delete']) && wp_verify_nonce($_REQUEST['_wpnonce'], 'dln_delete')) { $post = get_post($_REQUEST['dln_delete']); if ($post && current_user_can('delete_post', $post->ID)) { if ('answer' == $post->post_type) { wp_delete_post($post->ID); $url = qa_get_url('single', $post->post_parent); } elseif ('question' == $post->post_type) { wp_delete_post($post->ID); $url = add_query_arg('dln_msg', 'deleted', dln_get_url('archive')); } } } elseif (isset($_POST['dln_action'])) { $action = $_POST['dln_action']; var_dump($_POST); die; switch ($action) { case 'edit_question': $url = $this->handle_question_editing(); break; case 'edit_answer': $url = $this->handle_answer_editing(); break; } } else { return; } if (!$url) { $url = add_query_arg('dln_error', 1, dln_get_url('archive')); } wp_redirect($url); die; }
function test_rewrites() { global $pagenow; if ('index.php' != $pagenow) { return; } $archives = array(qa_get_url('archive'), qa_get_url('user')); $tag_id = (int) reset(get_terms('question_tag', array('fields' => 'ids'))); if ($tag_id) { $archives[] = qa_get_url('tag', $tag_id); } $urls = array(qa_get_url('ask')); $question_id = reset(get_posts(array('post_type' => 'question', 'fields' => 'ids'))); if ($question_id) { $urls[] = qa_get_url('single', $question_id); $urls[] = qa_get_url('edit', $question_id); } $urls = array_merge($urls, $archives); foreach ($urls as $url) { $class = '200' == wp_remote_retrieve_response_code(wp_remote_get($url)) ? 'updated' : 'error'; echo "<div class='{$class}'><p><a href='{$url}'>{$url}</a></p></div>"; } }
function get_the_answer_form() { global $wp_query, $user_ID, $wp_version, $qa_general_settings; $out = ''; if (is_qa_page('edit')) { $answer = $wp_query->posts[0]; if ($user_ID == 0 && !qa_visitor_can('edit_published_answers', $answer->ID) && !current_user_can('edit_published_answers', $answer->ID)) { return; } } else { if ($user_ID == 0 && !qa_visitor_can('publish_answers') && !current_user_can('publish_answers')) { $out .= '<p>' . __('You are not allowed to add answers!', QA_TEXTDOMAIN) . '</p>'; return; } $answer = (object) array('ID' => '', 'post_parent' => get_the_ID(), 'post_content' => ''); } $out .= '<form id="answer-form" method="post" action="' . qa_get_url('archive') . '">'; $out .= wp_nonce_field('qa_answer', "_wpnonce", true, false); $out .= '<input type="hidden" name="qa_action" value="edit_answer" /> <input type="hidden" name="question_id" value="' . esc_attr($answer->post_parent) . '" /> <input type="hidden" name="answer_id" value="' . esc_attr($answer->ID) . '" />'; $use_editor = true; if (isset($qa_general_settings["disable_editor"]) && $qa_general_settings["disable_editor"]) { $use_editor = false; } if (version_compare($wp_version, "3.3") >= 0 && $use_editor) { $wp_editor_settings = apply_filters('qa_answer_editor_settings', array(), $answer->ID); $out .= '<p>'; ob_start(); wp_editor($answer->post_content, 'answer', $wp_editor_settings); $out .= ob_get_contents(); ob_end_clean(); $out .= '</p>'; } else { $out .= '<p><textarea name="answer" class="wp32">' . esc_textarea($answer->post_content) . '</textarea></p>'; } $out .= get_the_qa_submit_button(); $out .= '</form>'; return $out; }
function registration_redirect($redirect_to) { $post_id = $this->_get_post_to_claim(); if (!$post_id) { return; } // Check if post is published $post = get_post($post_id); if ('publish' == $post->post_status) { $url = qa_get_url('single', $post_id); } else { if (!($url = get_permalink($this->g_settings['thank_you']))) { $url = site_url(); } } return $url; }
function handle_answer_editing() { global $wpdb; if (!wp_verify_nonce($_POST['_wpnonce'], 'qa_answer')) { wp_die(__('Are you sure you want to do that?', QA_TEXTDOMAIN)); } $question_id = (int) $_POST['question_id']; $answer_id = (int) $_POST['answer_id']; if (!$answer_id && !current_user_can('publish_answers')) { wp_die(__('You are not allowed to post answers', QA_TEXTDOMAIN)); } $answer = array('post_parent' => absint($question_id), 'post_content' => trim($_POST['answer']), 'post_type' => 'answer', 'post_status' => 'publish'); if (empty($answer['post_parent'])) { wp_die(__('Answer must be associated to a question.', QA_TEXTDOMAIN)); } if (empty($answer['post_content'])) { wp_die(__('You have to actually write something.', QA_TEXTDOMAIN)); } // Check for duplicates $dup_id = $wpdb->get_var($wpdb->prepare("\n\t\t\tSELECT ID\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE post_type = 'answer'\n\t\t\tAND post_status = 'publish'\n\t\t\tAND post_parent = %d\n\t\t\tAND post_content = %s\n\t\t\tLIMIT 1\n\t\t", $answer['post_parent'], $answer['post_content'])); if ($dup_id) { wp_die(sprintf(__('It seems that answer was <a href="%s">already given</a>.', QA_TEXTDOMAIN), qa_get_url('single', $dup_id))); } $answer_id = $this->_insert_post($answer_id, $answer, array('post_type' => 'answer', 'comment_status' => 'open')); return qa_get_url('single', $answer_id); }
function the_answer_form() { global $wp_query, $wp_version; if (is_qa_page('edit')) { $answer = $wp_query->posts[0]; if (!current_user_can('edit_post', $answer->ID)) { return; } } elseif (!current_user_can('publish_answers')) { echo _qa_html('p', sprintf(__('Please <a href="%s">login</a> to post questions.', QA_TEXTDOMAIN), wp_login_url(qa_get_url('single', get_queried_object_id())))); return; } else { $answer = (object) array('ID' => '', 'post_parent' => get_the_ID(), 'post_content' => ''); } ?> <form id="answer-form" method="post" action="<?php echo qa_get_url('archive'); ?> "> <?php wp_nonce_field('qa_answer'); ?> <input type="hidden" name="qa_action" value="edit_answer" /> <input type="hidden" name="question_id" value="<?php echo esc_attr($answer->post_parent); ?> " /> <input type="hidden" name="answer_id" value="<?php echo esc_attr($answer->ID); ?> " /> <?php if (version_compare($wp_version, "3.3") >= 0) { ?> <p><?php wp_editor($answer->post_content, 'answer', array('media_buttons' => false)); ?> </p> <?php } else { ?> <p><textarea name="answer" class="wp32"><?php echo esc_textarea($answer->post_content); ?> </textarea></p> <?php } ?> <?php the_qa_submit_button(); ?> </form> <?php }
function notify($new_status, $old_status, $post) { global $current_site; if ('answer' != $post->post_type || 'publish' != $new_status || $new_status == $old_status) { return; } $author = get_userdata($post->post_author); $question_id = $post->post_parent; $question = get_post($question_id); $subscribers = get_post_meta($question_id, '_sub'); if (!in_array($question->post_author, $subscribers)) { $subscribers[] = $question->post_author; } // Notify question author too $subject = sprintf(__('[%s] New answer on "%s"'), get_option('blogname'), $question->post_title); $content = sprintf(__('%s added a new answer to %s:', QA_TEXTDOMAIN), _qa_html('a', array('href' => qa_get_url('user', $post->post_author)), $author->user_nicename), _qa_html('a', array('href' => qa_get_url('single', $question_id)), get_post_field('post_title', $question_id))); $content .= "<br/><br/>" . $post->post_content . "<br/><br/>"; cache_users($subscribers); $admin_email = get_site_option('admin_email'); if ($admin_email == '') { $admin_email = 'admin@' . $current_site->domain; } $from_email = $admin_email; $message_headers = "MIME-Version: 1.0\n" . "From: " . $current_site->site_name . " <{$from_email}>\n" . "Content-Type: text/html; charset=\"" . get_option('blog_charset') . "\"\n"; foreach ($subscribers as $subscriber_id) { // Don't notify the author of the answer if ($post->post_author != $subscriber_id) { $msg = $content . sprintf(__('To manage your subscription, visit <a href="%s">the question</a>.', QA_TEXTDOMAIN), qa_get_url('single', $post->ID)); } else { $msg = $content; } wp_mail(get_user_option('user_email', $subscriber_id), $subject, $msg, $message_headers); } }