* @license GPL-2.0+ * @link http://tovolt.com * @copyright 2014 ToVolt */ class anspress_form { /** * Instance of this class. */ protected static $instance = null; /** * Return an instance of this class. * @return object A single instance of this class. */ public static function get_instance() { // If the single instance hasn't been set, set it now. if (null == self::$instance) { self::$instance = new self; } return self::$instance; } /** * Initialize the plugin by setting localization and loading public scripts * and styles. */ public function __construct() { add_action( 'init', array($this, 'process_ask_form') ); add_action( 'init', array($this, 'process_answer_form') ); add_action( 'init', array($this, 'process_edit_question_form') ); add_action( 'init', array($this, 'process_edit_answer_form') ); add_action( 'wp_ajax_ap_load_comment_form', array($this, 'load_ajax_commentform') ); add_action( 'wp_ajax_nopriv_ap_load_comment_form', array($this, 'load_ajax_commentform') ); add_action( 'wp_ajax_nopriv_ap_not_logged_in_messgae', array($this, 'ap_not_logged_in_messgae') ); } public function process_ask_form(){ if(isset($_POST['is_question']) && isset($_POST['submitted']) && isset($_POST['ask_form']) && wp_verify_nonce($_POST['ask_form'], 'post_nonce')) { if ( !is_user_logged_in() ) return; if(!ap_user_can_ask()) return; $validate = ap_validate_form(); if($validate['has_error']) return; do_action('process_ask_form'); global $current_user; $user_id = $current_user->ID; $question_array = array( 'post_title' => sanitize_text_field($_POST['post_title']), 'post_author' => $user_id, 'post_content' => wp_kses($_POST['post_content'], ap_form_allowed_tags()), 'post_type' => 'question', 'post_status' => 'publish' ); $post_id = wp_insert_post($question_array); if($post_id){ // Update Custom Meta wp_set_post_terms( $post_id, sanitize_text_field($_POST['category']), 'question_category' ); wp_set_post_terms( $post_id, sanitize_text_field($_POST['tags']), 'question_tags' ); // Redirect wp_redirect( get_permalink($post_id) ); exit; } } } public function process_answer_form(){ if(isset($_POST['is_answer']) && isset($_POST['submitted']) && isset($_POST['answer_form']) && wp_verify_nonce($_POST['answer_form'], 'post_nonce')) { if ( !is_user_logged_in() ) return; $validate = ap_validate_form(); if($validate['has_error']) return; if(!isset($_POST['form_question_id']) && (!is_int($_POST['form_question_id'])) && ('question' !== get_post_type( $_POST['form_question_id'] ))) return; $post = get_post($_POST['form_question_id']); global $current_user; $user_id = $current_user->ID; if(!ap_user_can_answer($post->ID) ) return; do_action('process_answer_form'); $ans_array = array( 'post_author' => $user_id, 'post_content' => wp_kses($_POST['post_content'], ap_form_allowed_tags()), 'post_type' => 'answer', 'post_status' => 'publish', 'post_parent' => sanitize_text_field($_POST['form_question_id']) ); $post_id = wp_insert_post($ans_array); } } public function process_edit_question_form(){ if(isset($_POST['is_question']) && isset($_POST['submitted']) && isset($_POST['edited']) && wp_verify_nonce($_POST['edit_question'], 'post_nonce-'.$_POST['question_id'])) { $post_id = $_POST['question_id']; $post = get_post($post_id); if( !ap_user_can_edit($post->ID)) return; if(!ap_user_can_ask()) return; $validate = ap_validate_form(); if($validate['has_error']) return; do_action('process_ask_form'); global $current_user; $user_id = $current_user->ID; $question_array = array( 'ID' => $post_id, 'post_title' => sanitize_text_field($_POST['post_title']), //'post_author' => $user_id, 'post_content' => wp_kses($_POST['post_content'], ap_form_allowed_tags()), 'post_status' => 'publish' ); $post_id = wp_update_post($question_array); if($post_id){ // Update Custom Meta wp_set_post_terms( $post_id, sanitize_text_field($_POST['category']), 'question_category' ); wp_set_post_terms( $post_id, sanitize_text_field($_POST['tags']), 'question_tags' ); // Redirect wp_redirect( get_permalink($post_id) ); exit; } } } public function process_edit_answer_form(){ if(isset($_POST['is_answer']) && isset($_POST['submitted']) && isset($_POST['edited']) && wp_verify_nonce($_POST['edit_answer'], 'post_nonce-'.$_POST['answer_id'])) { $post_id = $_POST['answer_id']; $post = get_post($post_id); if( !ap_user_can_edit($post->ID)) return; $validate = ap_validate_form(); if($validate['has_error']) return; global $current_user; $user_id = $current_user->ID; $answer_array = array( 'ID' => $post_id, //'post_author' => $user_id, 'post_content' => wp_kses($_POST['post_content'], ap_form_allowed_tags()), 'post_status' => 'publish' ); $post_id = wp_update_post($answer_array); if($post_id){ // Update Custom Meta wp_set_post_terms( $post_id, sanitize_text_field($_POST['category']), 'question_category' ); wp_set_post_terms( $post_id, sanitize_text_field($_POST['tags']), 'question_tags' ); $cur_post = get_post($post_id); // Redirect wp_redirect( get_permalink($cur_post->post_parent) ); exit; } } } public function load_ajax_commentform(){ if(!is_user_logged_in()){ echo 'not_logged_in'; die(); } $args = explode('-', sanitize_text_field($_REQUEST['args'])); $action = get_post_type($args[0]).'-'.$args[0]; if(wp_verify_nonce( $args[1], $action )){ $comment_args = array( 'title_reply' => '', 'logged_in_as' => '', 'comment_field' => '', 'comment_notes_after' => '' ); comment_form($comment_args, $args[0] ); } die(); } public function ap_not_logged_in_messgae(){ ap_please_login(); die(); } } function ap_form_allowed_tags(){ $allowed_tags = array( 'a' => array( 'href' => array(), 'title' => array() ), 'p' => array(), 'br' => array(), 'em' => array(), 'strong' => array(), 'pre' => array(), ); return apply_filters( 'ap_allowed_tags', $allowed_tags); } function ap_ask_form(){ global $post; global $current_user; $validate = ap_validate_form(); if(isset($validate['has_error']) && $validate['has_error']){ echo '
'.__('You don\'t have permission to access this page.', 'ap').'
'; return; } $action = get_post_type($post_id).'-'.$post_id; if(!isset($_REQUEST['ap_nonce']) || !wp_verify_nonce($_REQUEST['ap_nonce'], $action)){ echo ''.__('Trying to cheat? huh!.', 'ap').'
'; return; } global $current_user; $post = get_post($post_id); $cats_t = get_the_terms( $post_id, 'question_category' ); foreach($cats_t as $c) $category = $c->term_id; $tags_t = get_the_terms( $post_id, 'question_tags' ); $tags =''; foreach($tags_t as $t) $tags .= $t->name.', '; $validate = ap_validate_form(); if(isset($validate['has_error']) && $validate['has_error']){ echo ''.__('You don\'t have permission to access this page.', 'ap').'
'; return; } $action = get_post_type($post_id).'-'.$post_id; if(!isset($_REQUEST['ap_nonce']) || !wp_verify_nonce($_REQUEST['ap_nonce'], $action)){ echo ''.__('Trying to cheat? huh!.', 'ap').'
'; return; } global $current_user; $post = get_post($post_id); $validate = ap_validate_form(); if(isset($validate['has_error']) && $validate['has_error']){ echo '